-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
92 lines (65 loc) · 2.87 KB
/
api.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse,Response
from pydantic import BaseModel
import io
from io import BytesIO
import numpy as np
from PIL import Image
import shutil
import os
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
app = FastAPI()
detect_fn = tf.saved_model.load('saved_model')
category_index = label_map_util.create_category_index_from_labelmap("label_map.pbtxt",use_display_name=True)
def load_image_into_numpy_array(data):
"""Load an image from file into a numpy array.
Puts image into numpy array to feed into tensorflow graph.
Note that by convention we put it into a numpy array with shape
(height, width, channels), where channels=3 for RGB.
Args:
path: the file path to the image
Returns:
uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(io.BytesIO(data)))
def predict(image):
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
image_np = load_image_into_numpy_array(image)
input_tensor = tf.convert_to_tensor(image_np)
#print(input_tensor.shape)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis, ...]
# input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.50,
agnostic_mode=False)
#print(detections)
return Image.fromarray(image_np_with_detections)
@app.post("/uploadfile/")
async def create_upload_file(image: UploadFile = File(...)):
img_data = await image.read()
predicted_image = predict(img_data)
output = BytesIO()
predicted_image.save(output, 'png')
return Response(output.getvalue(), media_type='image/png')