TheDeepDas commited on
Commit
8e1f75a
·
1 Parent(s): 4569bb2

Fix model.info() TypeError - handle tuple return in newer Ultralytics versions

Browse files
Files changed (1) hide show
  1. app/services/image_processing.py +36 -10
app/services/image_processing.py CHANGED
@@ -951,12 +951,16 @@ async def detect_objects_in_image(image_url: str) -> Optional[Dict]:
951
 
952
  # Use even lower confidence threshold for bigger models
953
  # Larger models are more accurate so we can trust lower confidence predictions
954
- if yolo_model.__class__.__name__ == "YOLO":
955
- model_size = yolo_model.info()['model_type']
956
- if 'x' in model_size: # YOLOv8x (extra large)
957
- min_confidence = 0.003 # Accept even lower confidence detections
958
- elif 'l' in model_size: # YOLOv8l (large)
959
- min_confidence = 0.004
 
 
 
 
960
 
961
  # Skip only extremely low confidence detections
962
  if confidence < min_confidence:
@@ -1211,15 +1215,37 @@ async def detect_objects_in_image(image_url: str) -> Optional[Dict]:
1211
  model_info = {}
1212
  if yolo_model is not None:
1213
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1214
  model_info = {
1215
- "model_type": yolo_model.info().get('model_type', 'unknown'),
1216
- "model_name": yolo_model.__class__.__name__,
1217
  "framework": "YOLOv8",
1218
  }
1219
- logger.info(f"Using {model_info['model_type']} model for detection")
1220
  except Exception as e:
1221
  logger.warning(f"Could not get model info: {e}")
1222
- model_info = {"model_type": "unknown", "model_name": "YOLO"}
1223
 
1224
  # Return the results with model information
1225
  return {
 
951
 
952
  # Use even lower confidence threshold for bigger models
953
  # Larger models are more accurate so we can trust lower confidence predictions
954
+ try:
955
+ if yolo_model is not None and hasattr(yolo_model, 'model') and hasattr(yolo_model.model, 'yaml'):
956
+ # Try to get model size from the model name
957
+ model_name = str(yolo_model.model.yaml.get('yaml_file', ''))
958
+ if 'yolov8x' in model_name.lower():
959
+ min_confidence = 0.003 # Accept even lower confidence detections
960
+ elif 'yolov8l' in model_name.lower():
961
+ min_confidence = 0.004
962
+ except Exception:
963
+ pass # Use default min_confidence if we can't determine model size
964
 
965
  # Skip only extremely low confidence detections
966
  if confidence < min_confidence:
 
1215
  model_info = {}
1216
  if yolo_model is not None:
1217
  try:
1218
+ # Handle different return types from model.info()
1219
+ info_result = yolo_model.info() if hasattr(yolo_model, 'info') else None
1220
+
1221
+ # Determine model type
1222
+ model_type = "unknown"
1223
+ if isinstance(info_result, dict):
1224
+ model_type = info_result.get('model_type', 'unknown')
1225
+ elif isinstance(info_result, tuple) and len(info_result) > 0:
1226
+ # New versions return tuple: try to extract model info from tuple
1227
+ model_type = str(info_result[0]) if info_result else 'unknown'
1228
+
1229
+ # Try to get model name from file path or model itself
1230
+ model_name = "YOLOv8"
1231
+ if hasattr(yolo_model, 'model') and hasattr(yolo_model.model, 'yaml'):
1232
+ yaml_file = yolo_model.model.yaml.get('yaml_file', '')
1233
+ if 'yolov8x' in str(yaml_file).lower():
1234
+ model_name = "YOLOv8x"
1235
+ elif 'yolov8l' in str(yaml_file).lower():
1236
+ model_name = "YOLOv8l"
1237
+ elif 'yolov8m' in str(yaml_file).lower():
1238
+ model_name = "YOLOv8m"
1239
+
1240
  model_info = {
1241
+ "model_type": model_type,
1242
+ "model_name": model_name,
1243
  "framework": "YOLOv8",
1244
  }
1245
+ logger.info(f"Using {model_name} model for detection")
1246
  except Exception as e:
1247
  logger.warning(f"Could not get model info: {e}")
1248
+ model_info = {"model_type": "unknown", "model_name": "YOLO", "framework": "YOLOv8"}
1249
 
1250
  # Return the results with model information
1251
  return {