TashiKP commited on
Commit
ae0cfcc
·
verified ·
1 Parent(s): d334106

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -30
app.py CHANGED
@@ -1,38 +1,48 @@
1
  import torch
 
2
  import cv2
3
  import numpy as np
 
4
 
5
- # Load your YOLO model
6
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
7
 
8
- def detect(frame):
9
- # Perform inference on the current frame
10
- results = model(frame)
11
- return results
12
-
13
- # Open the video file (or camera)
14
- cap = cv2.VideoCapture("test_video.mp4") # replace with your video path or camera index (0 for webcam)
15
-
16
- while cap.isOpened():
17
- ret, frame = cap.read()
18
- if not ret:
19
- break
20
 
21
- # Perform inference on the current frame
22
- results = detect(frame)
23
-
24
- # Render predictions on the frame (draw bounding boxes)
25
- frame_with_boxes = np.array(results.render()[0]) # render predictions on the frame
26
-
27
- # Convert from RGB to BGR for OpenCV (since OpenCV uses BGR)
28
- frame_with_boxes = cv2.cvtColor(frame_with_boxes, cv2.COLOR_RGB2BGR)
29
-
30
- # Display the frame with results
31
- cv2.imshow("Detection", frame_with_boxes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Break loop if 'q' is pressed
34
- if cv2.waitKey(1) & 0xFF == ord('q'):
35
- break
 
 
 
 
36
 
37
- cap.release()
38
- cv2.destroyAllWindows()
 
1
  import torch
2
+ import gradio as gr
3
  import cv2
4
  import numpy as np
5
+ from datetime import datetime
6
 
7
+ # Load YOLOv5 model
8
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
9
 
10
+ def detect_video(video):
11
+ cap = cv2.VideoCapture(video.name)
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # List to hold results
14
+ detection_results = []
15
+
16
+ while cap.isOpened():
17
+ ret, frame = cap.read()
18
+ if not ret:
19
+ break
20
+
21
+ # Get timestamp
22
+ timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
23
+
24
+ # Perform detection
25
+ results = model(frame)
26
+
27
+ # Extract bounding boxes and confidence scores
28
+ for *xyxy, conf, cls in results.xywh[0]:
29
+ x1, y1, x2, y2 = map(int, xyxy) # Convert to integers
30
+ detection_results.append({
31
+ 'timestamp': timestamp,
32
+ 'coordinates': {'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2},
33
+ 'confidence': float(conf)
34
+ })
35
+
36
+ cap.release()
37
+
38
+ return detection_results
39
 
40
+ # Gradio Interface
41
+ interface = gr.Interface(fn=detect_video,
42
+ inputs=gr.inputs.Video(source="upload", type="file"),
43
+ outputs="json",
44
+ live=True,
45
+ title="YOLOv5 Video Object Detection",
46
+ description="Upload a video to detect objects and get bounding boxes with timestamps.")
47
 
48
+ interface.launch()