Aumkeshchy2003 commited on
Commit
d82e424
·
verified ·
1 Parent(s): a35c6a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -67
app.py CHANGED
@@ -40,79 +40,57 @@ np.random.seed(42)
40
  colors = np.random.randint(0, 255, size=(len(model.names), 3), dtype=np.uint8)
41
 
42
  def process_video(video_path):
43
- # Ensure we have a valid path
44
- if video_path is None:
45
- return None
46
 
47
- try:
48
- # For newer Gradio versions, video might be returned as a tuple
49
- if isinstance(video_path, tuple) and len(video_path) >= 1:
50
- video_path = video_path[0]
51
- # Or a dict with a 'name' key
52
- elif isinstance(video_path, dict) and 'name' in video_path:
53
- video_path = video_path['name']
54
- # Make sure it's a string
55
- video_path = str(video_path)
56
-
57
- cap = cv2.VideoCapture(video_path)
58
-
59
- if not cap.isOpened():
60
- print(f"Error: Could not open video file at {video_path}")
61
- return None
62
 
63
- frame_width = int(cap.get(3))
64
- frame_height = int(cap.get(4))
65
- fps = cap.get(cv2.CAP_PROP_FPS)
66
-
67
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
68
- output_path = "output_video.mp4"
69
- out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
70
 
71
- total_frames = 0
72
- total_time = 0
73
 
74
- while cap.isOpened():
75
- ret, frame = cap.read()
76
- if not ret:
77
- break
78
-
79
- start_time = time.time()
80
-
81
- # Convert frame for YOLOv5
82
- img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
83
- results = model(img, size=640)
84
-
85
- inference_time = time.time() - start_time
86
- total_time += inference_time
87
- total_frames += 1
88
-
89
- detections = results.pred[0].cpu().numpy()
90
 
91
- for *xyxy, conf, cls in detections:
92
- x1, y1, x2, y2 = map(int, xyxy)
93
- class_id = int(cls)
94
- color = colors[class_id].tolist()
95
- cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
96
- label = f"{model.names[class_id]} {conf:.2f}"
97
- cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
98
 
99
- # Calculate FPS
100
- avg_fps = total_frames / total_time if total_time > 0 else 0
101
- cv2.putText(frame, f"FPS: {avg_fps:.2f}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
102
 
103
- out.write(frame)
104
 
105
- cap.release()
106
- out.release()
107
-
108
- print(f"Video processed successfully, output at: {output_path}")
109
- return output_path
110
 
111
- except Exception as e:
112
- print(f"Error processing video: {str(e)}")
113
- import traceback
114
- traceback.print_exc()
115
- return None
116
 
117
  def process_image(image):
118
  img = np.array(image)
@@ -182,11 +160,9 @@ with gr.Blocks(css=css, title="Video & Image Object Detection by YOLOv5") as dem
182
  with gr.Tabs():
183
  with gr.TabItem("Video Detection", elem_classes="tab-item"):
184
  with gr.Row():
185
- # Keep using gr.Video but with source="upload" parameter
186
  video_input = gr.Video(
187
  label="Upload Video",
188
- interactive=True,
189
- source="upload", # Explicitly set upload as source
190
  elem_id="video-input"
191
  )
192
 
 
40
  colors = np.random.randint(0, 255, size=(len(model.names), 3), dtype=np.uint8)
41
 
42
  def process_video(video_path):
43
+ cap = cv2.VideoCapture(video_path)
 
 
44
 
45
+ if not cap.isOpened():
46
+ return "Error: Could not open video file."
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ frame_width = int(cap.get(3))
49
+ frame_height = int(cap.get(4))
50
+ fps = cap.get(cv2.CAP_PROP_FPS)
51
+
52
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
53
+ output_path = "output_video.mp4"
54
+ out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
55
 
56
+ total_frames = 0
57
+ total_time = 0
58
 
59
+ while cap.isOpened():
60
+ ret, frame = cap.read()
61
+ if not ret:
62
+ break
63
+
64
+ start_time = time.time()
65
+
66
+ # Convert frame for YOLOv5
67
+ img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
68
+ results = model(img, size=640)
69
+
70
+ inference_time = time.time() - start_time
71
+ total_time += inference_time
72
+ total_frames += 1
73
+
74
+ detections = results.pred[0].cpu().numpy()
75
 
76
+ for *xyxy, conf, cls in detections:
77
+ x1, y1, x2, y2 = map(int, xyxy)
78
+ class_id = int(cls)
79
+ color = colors[class_id].tolist()
80
+ cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
81
+ label = f"{model.names[class_id]} {conf:.2f}"
82
+ cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
83
 
84
+ # Calculate FPS
85
+ avg_fps = total_frames / total_time if total_time > 0 else 0
86
+ cv2.putText(frame, f"FPS: {avg_fps:.2f}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
87
 
88
+ out.write(frame)
89
 
90
+ cap.release()
91
+ out.release()
 
 
 
92
 
93
+ return output_path
 
 
 
 
94
 
95
  def process_image(image):
96
  img = np.array(image)
 
160
  with gr.Tabs():
161
  with gr.TabItem("Video Detection", elem_classes="tab-item"):
162
  with gr.Row():
 
163
  video_input = gr.Video(
164
  label="Upload Video",
165
+ interactive=True,
 
166
  elem_id="video-input"
167
  )
168