nagasurendra commited on
Commit
bbc2907
·
verified ·
1 Parent(s): cb86f50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -38
app.py CHANGED
@@ -8,63 +8,74 @@ def safe_load_yolo_model(path):
8
  torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
9
  return YOLO(path)
10
 
11
- # Load the models
12
- model_yolo11 = safe_load_yolo_model('./data/yolo11n.pt')
13
- model_best = safe_load_yolo_model('./data/best2.pt')
 
 
 
 
 
14
 
15
- def process_video(video):
16
- # Open the video using OpenCV
 
 
 
 
 
 
 
 
 
 
 
17
  cap = cv2.VideoCapture(video)
18
  fps = cap.get(cv2.CAP_PROP_FPS)
19
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
20
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
21
 
22
- # Create VideoWriter to save output video
23
- fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
24
- out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
25
 
26
  while cap.isOpened():
27
  ret, frame = cap.read()
28
  if not ret:
29
  break
30
-
31
- # Detect with YOLOv11 (general object detection model)
32
- results_yolo11 = model_yolo11(frame)
33
- # Detect with best.pt (specialized model for cracks and potholes)
34
- results_best = model_best(frame)
35
 
36
- # Draw bounding boxes and labels for YOLOv11 (General Object Detection)
37
- for result in results_yolo11:
38
- boxes = result.boxes
39
- for box in boxes:
40
- x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
41
- class_id = int(box.cls[0]) # Class index for YOLO
42
- # Use model's built-in class names
43
- label = f"{model_yolo11.names[class_id]} - {box.conf[0]:.2f}"
44
- cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
45
- cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
46
 
47
- # Draw bounding boxes and labels for best.pt (Crack and Pothole detection)
48
- for result in results_best:
49
- boxes = result.boxes
50
- for box in boxes:
51
- x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
52
- class_id = int(box.cls[0]) # Class index for best.pt
53
- # Use model's built-in class names for best.pt
54
- label = f"{model_best.names[class_id]} - {box.conf[0]:.2f}"
55
- cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
56
- cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
57
 
58
- # Write the processed frame to the output video
59
  out.write(frame)
60
 
61
  cap.release()
62
  out.release()
63
-
64
  return 'output_video.mp4'
65
 
66
- # Gradio interface
67
- iface = gr.Interface(fn=process_video, inputs=gr.Video(), outputs=gr.Video(), live=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # Launch the app
70
  iface.launch()
 
8
  torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
9
  return YOLO(path)
10
 
11
+ # Dictionary of model paths
12
+ model_paths = {
13
+ 'YOLOv11': './data/yolo11n.pt',
14
+ 'Crack & Pothole Detector': './data/best2.pt',
15
+ 'Bridge Inspector': './data/bridge.pt',
16
+ 'Road Surface Detector': './data/road.pt',
17
+ 'Pipe Detector': './data/pipe.pt'
18
+ }
19
 
20
+ # Load models into memory
21
+ models = {name: safe_load_yolo_model(path) for name, path in model_paths.items()}
22
+
23
+ # Assign colors for each model
24
+ model_colors = {
25
+ 'YOLOv11': (0, 255, 0),
26
+ 'Crack & Pothole Detector': (255, 0, 0),
27
+ 'Bridge Inspector': (0, 0, 255),
28
+ 'Road Surface Detector': (255, 255, 0),
29
+ 'Pipe Detector': (255, 0, 255)
30
+ }
31
+
32
+ def process_video(video, selected_model):
33
  cap = cv2.VideoCapture(video)
34
  fps = cap.get(cv2.CAP_PROP_FPS)
35
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
36
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
37
 
38
+ out = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
39
+
40
+ use_models = models if selected_model == 'All' else {selected_model: models[selected_model]}
41
 
42
  while cap.isOpened():
43
  ret, frame = cap.read()
44
  if not ret:
45
  break
 
 
 
 
 
46
 
47
+ for model_name, model in use_models.items():
48
+ results = model(frame)
 
 
 
 
 
 
 
 
49
 
50
+ for result in results:
51
+ for box in result.boxes:
52
+ x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
53
+ class_id = int(box.cls[0])
54
+ label = f"{model.names[class_id]} - {box.conf[0]:.2f}"
55
+ color = model_colors.get(model_name, (0, 255, 255))
56
+ cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
57
+ cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
 
 
58
 
 
59
  out.write(frame)
60
 
61
  cap.release()
62
  out.release()
 
63
  return 'output_video.mp4'
64
 
65
+ # Gradio Interface
66
+ iface = gr.Interface(
67
+ fn=process_video,
68
+ inputs=[
69
+ gr.Video(label="Upload a Video"),
70
+ gr.Dropdown(
71
+ choices=["All"] + list(model_paths.keys()),
72
+ label="Select Model(s)",
73
+ value="All"
74
+ )
75
+ ],
76
+ outputs=gr.Video(label="Processed Output"),
77
+ live=False,
78
+ title="Multi-Model YOLOv8 Video Inference"
79
+ )
80
 
 
81
  iface.launch()