Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +7 -2
api_server.py
CHANGED
|
@@ -7,6 +7,7 @@ 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'
|
|
@@ -65,6 +66,9 @@ 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'
|
|
@@ -105,7 +109,7 @@ def predict():
|
|
| 105 |
# 儲存辨識後的圖片到指定資料夾
|
| 106 |
for result in results:
|
| 107 |
# 保存圖片
|
| 108 |
-
result.save_crop(f"{YOLO_DIR}/{
|
| 109 |
|
| 110 |
num_detections = len(result.boxes) # Get the number of detections
|
| 111 |
labels = result.boxes.cls # Get predicted label IDs
|
|
@@ -114,12 +118,13 @@ def predict():
|
|
| 114 |
encoded_images=[]
|
| 115 |
|
| 116 |
for label_name in label_names:
|
| 117 |
-
output_file=f"{YOLO_DIR}/{
|
| 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 |
}
|
|
|
|
| 7 |
from ultralytics import YOLO
|
| 8 |
import io
|
| 9 |
import base64
|
| 10 |
+
import uuid
|
| 11 |
|
| 12 |
# Disable tensorflow warnings
|
| 13 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
|
|
|
| 66 |
|
| 67 |
user_id = request.args.get('user_id')
|
| 68 |
|
| 69 |
+
# 生成一個唯一的 message_id
|
| 70 |
+
message_id = str(uuid.uuid4())
|
| 71 |
+
|
| 72 |
if 'image' not in request.files:
|
| 73 |
# Handle if no file is selected
|
| 74 |
return 'No file selected'
|
|
|
|
| 109 |
# 儲存辨識後的圖片到指定資料夾
|
| 110 |
for result in results:
|
| 111 |
# 保存圖片
|
| 112 |
+
result.save_crop(f"{YOLO_DIR}/{message_id}")
|
| 113 |
|
| 114 |
num_detections = len(result.boxes) # Get the number of detections
|
| 115 |
labels = result.boxes.cls # Get predicted label IDs
|
|
|
|
| 118 |
encoded_images=[]
|
| 119 |
|
| 120 |
for label_name in label_names:
|
| 121 |
+
output_file=f"{YOLO_DIR}/{message_id}/{label_name}/im.jpg.jpg"
|
| 122 |
# 將圖片轉換為 base64 編碼
|
| 123 |
encoded_images.append(image_to_base64(output_file))
|
| 124 |
|
| 125 |
# 建立回應資料
|
| 126 |
response_data = {
|
| 127 |
+
'message_id': message_id,
|
| 128 |
'images': encoded_images,
|
| 129 |
'description': label_names
|
| 130 |
}
|