ning8429 commited on
Commit
3bf0eab
·
verified ·
1 Parent(s): b5bb20c

Update api_server.py

Browse files
Files changed (1) hide show
  1. api_server.py +21 -24
api_server.py CHANGED
@@ -70,31 +70,28 @@ def predict():
70
 
71
  # Make a prediction using YOLO
72
  results = model(image_data)
73
-
74
- # 準備返回多張圖像
75
- images_io = []
76
-
77
- # YOLOv5/YOLOv8 results have a .ims attribute that contains the rendered images
78
- for i, img in enumerate(results.ims): # Assuming `ims` contains the rendered images
79
- img_io = io.BytesIO()
80
- img = Image.fromarray(img) # Convert numpy array back to PIL image if necessary
81
- img.save(img_io, 'PNG') # Save YOLO-processed image to buffer
82
- img_io.seek(0)
83
- images_io.append((f'image_{i}.png', img_io)) # Store each image with its filename
84
-
85
- # 打包多張圖像為 ZIP 文件進行返回
86
- zip_io = io.BytesIO()
87
- with zipfile.ZipFile(zip_io, 'w') as zip_file:
88
- for filename, image in images_io:
89
- zip_file.writestr(filename, image.getvalue())
90
-
91
- zip_io.seek(0)
92
 
93
- # 返回壓縮包
94
- return send_file(zip_io, mimetype='application/zip', as_attachment=True, download_name='predictions.zip')
95
-
96
- # # Preprocess the image
97
- # processed_image = preprocess_image(image_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
 
100
 
 
70
 
71
  # Make a prediction using YOLO
72
  results = model(image_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ saved_images = []
75
+
76
+ # 儲存辨識後的圖片到指定資料夾
77
+ for result in results:
78
+ # 保存完整圖片
79
+ yolo_path = YOLO_DIR + f"results_{Path(result.path).name}"
80
+ result.save(yolo_path)
81
+ saved_images.append(yolo_path)
82
+
83
+ # 保存裁剪後的圖片
84
+ cropped_images = result.save_crop(YOLO_DIR) # save_crop usually returns list of cropped image paths
85
+ saved_images.extend(cropped_images) # 將裁剪後的圖片加入到返回列表
86
+
87
+ end_time = time.time()
88
+ inference_time = end_time - start_time
89
+
90
+ # 返回辨識結果的文件路徑以及推理時間
91
+ return jsonify({
92
+ 'saved_images': saved_images,
93
+ 'inference_time': inference_time
94
+ }), 200
95
 
96
 
97