ning8429 commited on
Commit
14c5f38
·
verified ·
1 Parent(s): d165af8

Update api_server.py

Browse files
Files changed (1) hide show
  1. api_server.py +16 -24
api_server.py CHANGED
@@ -74,7 +74,21 @@ def predict():
74
  # 檢查 YOLO 是否返回了有效的結果
75
  if results is None or len(results) == 0:
76
  return jsonify({'error': 'No results from YOLO model'}), 400
77
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  saved_images = []
79
 
80
  # 儲存辨識後的圖片到指定資料夾
@@ -100,31 +114,9 @@ def predict():
100
  'saved_images': saved_images,
101
  'inference_time': inference_time
102
  }), 200
 
103
 
104
 
105
-
106
- # # Process the YOLO output
107
- # detections = []
108
- # for det in results.xyxy[0]: # Assuming results are in xyxy format (xmin, ymin, xmax, ymax, confidence, class)
109
- # x_min, y_min, x_max, y_max, confidence, class_idx = det
110
- # width = x_max - x_min
111
- # height = y_max - y_min
112
- # detection = {
113
- # "label": int(class_idx),
114
- # "confidence": float(confidence),
115
- # "bbox": [float(x_min), float(y_min), float(width), float(height)]
116
- # }
117
- # detections.append(detection)
118
-
119
- # # Calculate latency in milliseconds
120
- # latency_ms = (time.time() - start_time) * 1000
121
-
122
- # # Return the detection results and latency as JSON response
123
- # response = {
124
- # 'detections': detections,
125
- # 'ml-latency-ms': round(latency_ms, 4)
126
- # }
127
-
128
  # # dictionary is not a JSON: https://www.quora.com/What-is-the-difference-between-JSON-and-a-dictionary
129
  # # flask.jsonify vs json.dumps https://sentry.io/answers/difference-between-json-dumps-and-flask-jsonify/
130
  # # The flask.jsonify() function returns a Response object with Serializable JSON and content_type=application/json.
 
74
  # 檢查 YOLO 是否返回了有效的結果
75
  if results is None or len(results) == 0:
76
  return jsonify({'error': 'No results from YOLO model'}), 400
77
+
78
+ # 渲染推理結果到圖像
79
+ img_with_boxes = results[0].plot() # 使用 results[0],假設只有一張圖像作推理
80
+
81
+ # 將 numpy array 轉換為 PIL Image
82
+ img = Image.fromarray(img_with_boxes)
83
+
84
+ # 儲存圖片到內存緩衝區
85
+ img_io = io.BytesIO()
86
+ img.save(img_io, 'PNG')
87
+ img_io.seek(0)
88
+
89
+ # 返回處理後的圖像
90
+ return send_file(img_io, mimetype='image/png')
91
+ '''
92
  saved_images = []
93
 
94
  # 儲存辨識後的圖片到指定資料夾
 
114
  'saved_images': saved_images,
115
  'inference_time': inference_time
116
  }), 200
117
+ '''
118
 
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  # # dictionary is not a JSON: https://www.quora.com/What-is-the-difference-between-JSON-and-a-dictionary
121
  # # flask.jsonify vs json.dumps https://sentry.io/answers/difference-between-json-dumps-and-flask-jsonify/
122
  # # The flask.jsonify() function returns a Response object with Serializable JSON and content_type=application/json.