BhumikaMak commited on
Commit
2fe2b15
·
1 Parent(s): dfce026

Fix: version dependency

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -30,16 +30,17 @@ def parse_detections(results, yolo_version):
30
  boxes.append((xmin, ymin, xmax, ymax))
31
  colors.append(COLORS[category])
32
  names.append(name)
33
- else:
34
- # For YOLOv8 or other versions
35
- for box in results[0].boxes:
36
- xmin, ymin, xmax, ymax = box.xyxy.tolist()
37
- confidence = box.conf.item()
38
- class_id = int(box.cls.item())
39
- if confidence > 0.2:
40
- boxes.append((xmin, ymin, xmax, ymax))
41
- colors.append(COLORS[class_id % len(COLORS)])
42
- names.append(results[0].names[class_id])
 
43
 
44
  return boxes, colors, names
45
 
@@ -83,13 +84,15 @@ def process_image(image, yolo_versions=["yolov5"]):
83
  model = load_yolo_model(yolo_version)
84
 
85
  # Run YOLO detection
86
- results = model([rgb_img])
87
- boxes, colors, names = parse_detections(results.xyxy[0], yolo_version)
 
 
88
 
89
- detections_img = draw_detections(boxes, colors, names.copy(), rgb_img.copy())
90
 
91
  # Grad-CAM visualization
92
- target_layers = [model.model.model[-1]] # Use the last layer as target layer for Grad-CAM
93
  cam = EigenCAM(model=model.model.model[-1], target_layers=target_layers)
94
  grayscale_cam = cam(tensor)[0]
95
 
 
30
  boxes.append((xmin, ymin, xmax, ymax))
31
  colors.append(COLORS[category])
32
  names.append(name)
33
+ elif yolo_version == "yolov8":
34
+ # For YOLOv8
35
+ for result in results:
36
+ for box in result.boxes:
37
+ xmin, ymin, xmax, ymax = box.xyxy.tolist()
38
+ confidence = box.conf.item()
39
+ class_id = int(box.cls.item())
40
+ if confidence > 0.2:
41
+ boxes.append((xmin, ymin, xmax, ymax))
42
+ colors.append(COLORS[class_id % len(COLORS)])
43
+ names.append(result.names[class_id])
44
 
45
  return boxes, colors, names
46
 
 
84
  model = load_yolo_model(yolo_version)
85
 
86
  # Run YOLO detection
87
+ results = model([rgb_img]) # Ensure this is a list containing one image
88
+
89
+ # Parse detections using updated function
90
+ boxes, colors, names = parse_detections(results[0], yolo_version)
91
 
92
+ detections_img = draw_detections(boxes, colors.copy(), names.copy(), rgb_img.copy())
93
 
94
  # Grad-CAM visualization
95
+ target_layers = [model.model.model[-1]] # Use last layer as target layer for Grad-CAM
96
  cam = EigenCAM(model=model.model.model[-1], target_layers=target_layers)
97
  grayscale_cam = cam(tensor)[0]
98