일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- zip 암호화
- analyzer test
- docker
- 파이썬
- Test
- query
- license delete
- Mac
- token filter test
- API
- plugin
- 차트
- ELASTIC
- Elasticsearch
- matplotlib
- zip 파일 암호화
- Java
- 900gle
- Python
- License
- sort
- TensorFlow
- aggregation
- high level client
- Kafka
- MySQL
- licence delete curl
- aggs
- springboot
- flask
Archives
- Today
- Total
개발잡부
[tensorflow 2]image 백터 추출 api 본문
반응형
나중에 다시 만들어야지..
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import json
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
filePath = "/Users/doo/project/opencv/web/src/main/resources/static/images/"
tempPath = "/Users/doo/project/opencv/common/temp/"
def load_img(path):
img = tf.io.read_file(path)
img = tf.io.decode_jpeg(img, channels=3)
img = tf.image.resize_with_pad(img, 224, 224)
img = tf.image.convert_image_dtype(img, tf.float32)[tf.newaxis, ...]
return img
parser = reqparse.RequestParser()
def getImageFeatureVectors(path, fileName):
module_handle = "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/4"
module = hub.load(module_handle)
img = load_img(path + fileName)
features = module(img)
feature_set = np.squeeze(features)
return feature_set
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
class Api(Resource):
def get(self, fileName):
return {'vectors': json.dumps(getImageFeatureVectors(tempPath, fileName), cls=NumpyEncoder)}
def post(self, fileName):
args = parser.parse_args()
return {'vectors': json.dumps(getImageFeatureVectors(tempPath, fileName), cls=NumpyEncoder)}
class Vector(Resource):
def get(self, fileName):
return {'vectors': json.dumps(getImageFeatureVectors(filePath, fileName), cls=NumpyEncoder)}
def post(self, fileName):
args = parser.parse_args()
return {'vectors': json.dumps(getImageFeatureVectors(filePath, fileName), cls=NumpyEncoder)}
api.add_resource(Vector, '/vectors/<string:fileName>')
api.add_resource(Api, '/api/<string:fileName>')
if __name__ == '__main__':
app.run(debug=True)
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import json
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
def load_img(path):
img = tf.io.read_file(path)
img = tf.io.decode_jpeg(img, channels=3)
img = tf.image.resize_with_pad(img, 224, 224)
img = tf.image.convert_image_dtype(img, tf.float32)[tf.newaxis, ...]
return img
parser = reqparse.RequestParser()
def getImageFeatureVectors(filePath):
module_handle = "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/4"
module = hub.load(module_handle)
img = load_img(filePath)
features = module(img)
feature_set = np.squeeze(features)
return feature_set
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
class Api(Resource):
parser = reqparse.RequestParser()
parser.add_argument('filePath', type=str, required=True, help="The 'filePath' field cannot be blank.")
def post(self):
data = Api.parser.parse_args()
return {'vectors': json.dumps(getImageFeatureVectors(data['filePath']), cls=NumpyEncoder)}
api.add_resource(Api, '/api')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
반응형
'Python > image vector' 카테고리의 다른 글
[tensorflow] feature vector (0) | 2021.12.31 |
---|
Comments