minh9972t12 commited on
Commit
11f621a
·
1 Parent(s): 82e3cb0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +5 -11
main.py CHANGED
@@ -94,7 +94,6 @@ async def detect_single_image(file: UploadFile = File(...)):
94
  # Generate unique filename
95
  filename = f"detection_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}.jpg"
96
 
97
- # ✅ FIX: Use consistent relative path
98
  output_path = UPLOADS_DIR / filename
99
 
100
  # Save visualization image
@@ -110,7 +109,6 @@ async def detect_single_image(file: UploadFile = File(...)):
110
  "total_damages": len(detections['boxes']),
111
  "damage_types": list(set(detections['classes']))
112
  },
113
- # ✅ FIX: Correct paths
114
  "visualized_image_path": f"uploads/{filename}",
115
  "visualized_image_url": image_url,
116
  "filename": filename
@@ -203,7 +201,6 @@ async def compare_vehicle_damages(
203
  comparison
204
  )
205
 
206
- # ✅ FIX: Save visualization image with consistent path
207
  vis_filename = f"comparison_{timestamp_str}_{session_id}_pos{i + 1}.jpg"
208
  vis_path = UPLOADS_DIR / vis_filename # Use consistent relative path
209
 
@@ -212,7 +209,7 @@ async def compare_vehicle_damages(
212
  if not success:
213
  print(f"⚠️ Warning: Failed to save visualization for position {i + 1}")
214
  else:
215
- print(f" Saved: {vis_path}")
216
 
217
  # Generate URL for accessing the image
218
  vis_url = f"http://localhost:8000/uploads/{vis_filename}"
@@ -227,7 +224,6 @@ async def compare_vehicle_damages(
227
  "new_damages": comparison['new_damages'],
228
  "matched_damages": comparison['matched_damages'],
229
  "repaired_damages": comparison['repaired_damages'],
230
- # ✅ FIX: Correct paths
231
  "visualization_path": f"uploads/{vis_filename}",
232
  "visualization_url": vis_url,
233
  "filename": vis_filename
@@ -249,16 +245,15 @@ async def compare_vehicle_damages(
249
  grid_results = [res[f"position_{i + 1}"] for i, res in enumerate(position_results)]
250
  grid_img = visualizer.create_summary_grid(grid_results, image_pairs)
251
 
252
- # ✅ FIX: Save grid summary image with consistent path
253
  grid_filename = f"summary_grid_{timestamp_str}_{session_id}.jpg"
254
  grid_path = UPLOADS_DIR / grid_filename # Already correct in original
255
 
256
  # Save the grid image
257
  grid_success = cv2.imwrite(str(grid_path), grid_img)
258
  if not grid_success:
259
- print(f"⚠️ Warning: Failed to save grid summary")
260
  else:
261
- print(f" Saved grid: {grid_path}")
262
 
263
  grid_url = f"http://localhost:8000/uploads/{grid_filename}"
264
 
@@ -279,7 +274,6 @@ async def compare_vehicle_damages(
279
  }
280
  },
281
  "position_results": position_results,
282
- # ✅ FIX: Correct paths
283
  "summary_visualization_path": f"uploads/{grid_filename}",
284
  "summary_visualization_url": grid_url,
285
  "all_visualizations": all_visualizations, # Added for convenience
@@ -299,7 +293,7 @@ if __name__ == "__main__":
299
  uvicorn.run(
300
  "main:app",
301
  host="0.0.0.0",
302
- port=int(os.environ.get("PORT", 7860)), # Lấy port do HF set
303
- reload=False, # Production không cần reload
304
  log_level="info"
305
  )
 
94
  # Generate unique filename
95
  filename = f"detection_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}.jpg"
96
 
 
97
  output_path = UPLOADS_DIR / filename
98
 
99
  # Save visualization image
 
109
  "total_damages": len(detections['boxes']),
110
  "damage_types": list(set(detections['classes']))
111
  },
 
112
  "visualized_image_path": f"uploads/{filename}",
113
  "visualized_image_url": image_url,
114
  "filename": filename
 
201
  comparison
202
  )
203
 
 
204
  vis_filename = f"comparison_{timestamp_str}_{session_id}_pos{i + 1}.jpg"
205
  vis_path = UPLOADS_DIR / vis_filename # Use consistent relative path
206
 
 
209
  if not success:
210
  print(f"⚠️ Warning: Failed to save visualization for position {i + 1}")
211
  else:
212
+ print(f" Saved: {vis_path}")
213
 
214
  # Generate URL for accessing the image
215
  vis_url = f"http://localhost:8000/uploads/{vis_filename}"
 
224
  "new_damages": comparison['new_damages'],
225
  "matched_damages": comparison['matched_damages'],
226
  "repaired_damages": comparison['repaired_damages'],
 
227
  "visualization_path": f"uploads/{vis_filename}",
228
  "visualization_url": vis_url,
229
  "filename": vis_filename
 
245
  grid_results = [res[f"position_{i + 1}"] for i, res in enumerate(position_results)]
246
  grid_img = visualizer.create_summary_grid(grid_results, image_pairs)
247
 
 
248
  grid_filename = f"summary_grid_{timestamp_str}_{session_id}.jpg"
249
  grid_path = UPLOADS_DIR / grid_filename # Already correct in original
250
 
251
  # Save the grid image
252
  grid_success = cv2.imwrite(str(grid_path), grid_img)
253
  if not grid_success:
254
+ print(f" Warning: Failed to save grid summary")
255
  else:
256
+ print(f" Saved grid: {grid_path}")
257
 
258
  grid_url = f"http://localhost:8000/uploads/{grid_filename}"
259
 
 
274
  }
275
  },
276
  "position_results": position_results,
 
277
  "summary_visualization_path": f"uploads/{grid_filename}",
278
  "summary_visualization_url": grid_url,
279
  "all_visualizations": all_visualizations, # Added for convenience
 
293
  uvicorn.run(
294
  "main:app",
295
  host="0.0.0.0",
296
+ port=int(os.environ.get("PORT", 7860)),
297
+ reload=False,
298
  log_level="info"
299
  )