Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +31 -22
api_server.py
CHANGED
@@ -6,6 +6,7 @@ import torchvision.transforms as transforms
|
|
6 |
from pathlib import Path
|
7 |
from ultralytics import YOLO
|
8 |
import io
|
|
|
9 |
|
10 |
# Disable tensorflow warnings
|
11 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
@@ -47,6 +48,13 @@ elif load_type == 'remote_hub_from_pretrained':
|
|
47 |
else:
|
48 |
raise AssertionError('No load type is specified!')
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# Initialize the Flask application
|
51 |
app = Flask(__name__)
|
52 |
|
@@ -54,6 +62,9 @@ app = Flask(__name__)
|
|
54 |
# API route for prediction(YOLO)
|
55 |
@app.route('/predict', methods=['POST'])
|
56 |
def predict():
|
|
|
|
|
|
|
57 |
if 'image' not in request.files:
|
58 |
# Handle if no file is selected
|
59 |
return 'No file selected'
|
@@ -93,28 +104,26 @@ def predict():
|
|
93 |
|
94 |
# 儲存辨識後的圖片到指定資料夾
|
95 |
for result in results:
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
#
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
}), 200
|
117 |
-
|
118 |
|
119 |
|
120 |
# # dictionary is not a JSON: https://www.quora.com/What-is-the-difference-between-JSON-and-a-dictionary
|
|
|
6 |
from pathlib import Path
|
7 |
from ultralytics import YOLO
|
8 |
import io
|
9 |
+
import base64
|
10 |
|
11 |
# Disable tensorflow warnings
|
12 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
|
|
48 |
else:
|
49 |
raise AssertionError('No load type is specified!')
|
50 |
|
51 |
+
|
52 |
+
def image_to_base64(image_path):
|
53 |
+
with open(image_path, "rb") as image_file:
|
54 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
55 |
+
return encoded_string
|
56 |
+
|
57 |
+
|
58 |
# Initialize the Flask application
|
59 |
app = Flask(__name__)
|
60 |
|
|
|
62 |
# API route for prediction(YOLO)
|
63 |
@app.route('/predict', methods=['POST'])
|
64 |
def predict():
|
65 |
+
|
66 |
+
user_id = request.args.get('user_id')
|
67 |
+
|
68 |
if 'image' not in request.files:
|
69 |
# Handle if no file is selected
|
70 |
return 'No file selected'
|
|
|
104 |
|
105 |
# 儲存辨識後的圖片到指定資料夾
|
106 |
for result in results:
|
107 |
+
# 保存圖片
|
108 |
+
result.save_crop(f"{YOLO_DIR}/{user_id}")
|
109 |
+
|
110 |
+
num_detections = len(result.boxes) # Get the number of detections
|
111 |
+
labels = result.boxes.cls # Get predicted label IDs
|
112 |
+
label_names = [model.names[int(label)] for label in labels] # Convert to names
|
113 |
+
|
114 |
+
encoded_images=[]
|
115 |
+
|
116 |
+
for label_name in label_names:
|
117 |
+
output_file=f"{YOLO_DIR}/{user_id}/{label_name}/im.jpg.jpg"
|
118 |
+
# 將圖片轉換為 base64 編碼
|
119 |
+
encoded_images.append(image_to_base64(output_file))
|
120 |
+
|
121 |
+
# 建立回應資料
|
122 |
+
response_data = {
|
123 |
+
'images': encoded_images,
|
124 |
+
'description': label_names
|
125 |
+
}
|
126 |
+
return jsonify(response_data)
|
|
|
|
|
127 |
|
128 |
|
129 |
# # dictionary is not a JSON: https://www.quora.com/What-is-the-difference-between-JSON-and-a-dictionary
|