Spaces:
Sleeping
Sleeping
Commit
·
e416d5a
1
Parent(s):
0883907
video stream
Browse files
app.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
-
# Created by yarramsettinaresh GORAKA DIGITAL PRIVATE LIMITED at
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
import time
|
5 |
from ultralytics import YOLO
|
|
|
6 |
|
7 |
-
# Load your
|
8 |
model_path = "model_- 11 october 2024 11_07.pt"
|
9 |
model = YOLO(model_path)
|
10 |
-
|
|
|
11 |
|
12 |
def ultralytics_predict(model, frame):
|
13 |
confidence_threshold = 0.2
|
@@ -32,12 +34,34 @@ def ultralytics_predict(model, frame):
|
|
32 |
class_id = int(detection.cls[0])
|
33 |
class_name = model.names[class_id]
|
34 |
if class_name not in object_count:
|
35 |
-
object_count[class_name] = dict(count=0
|
36 |
object_mapp = object_count[class_name]
|
37 |
object_mapp["count"] = object_mapp.get("count", 0) + 1
|
38 |
-
object_mapp["objects"].append(dict(conf=conf, pos=pos, text=text, color=color))
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
def ultralytics(detection, duration):
|
@@ -59,26 +83,32 @@ def ultralytics(detection, duration):
|
|
59 |
return conf, pos, text, color
|
60 |
|
61 |
|
62 |
-
def process_frame(
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Created by yarramsettinaresh GORAKA DIGITAL PRIVATE LIMITED at 01/11/24
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
import time
|
5 |
from ultralytics import YOLO
|
6 |
+
import numpy as np
|
7 |
|
8 |
+
# Load your models
|
9 |
model_path = "model_- 11 october 2024 11_07.pt"
|
10 |
model = YOLO(model_path)
|
11 |
+
# Initialize global video capture variable
|
12 |
+
cap = None
|
13 |
|
14 |
def ultralytics_predict(model, frame):
|
15 |
confidence_threshold = 0.2
|
|
|
34 |
class_id = int(detection.cls[0])
|
35 |
class_name = model.names[class_id]
|
36 |
if class_name not in object_count:
|
37 |
+
object_count[class_name] = dict(count=0)
|
38 |
object_mapp = object_count[class_name]
|
39 |
object_mapp["count"] = object_mapp.get("count", 0) + 1
|
|
|
40 |
|
41 |
+
y_offset = 150 # Initial y-offset for the text position
|
42 |
+
text_x = frame.shape[1] - 300 # X position for the text
|
43 |
+
|
44 |
+
for class_name, data in object_count.items():
|
45 |
+
count_text = f"{class_name}: {data['count']}"
|
46 |
+
|
47 |
+
# Get text size for rectangle dimensions
|
48 |
+
(text_width, text_height), _ = cv2.getTextSize(count_text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
49 |
+
rect_x1, rect_y1 = text_x - 10, y_offset - text_height - 10
|
50 |
+
rect_x2, rect_y2 = text_x + text_width + 10, y_offset + 10
|
51 |
+
|
52 |
+
# Draw semi-transparent rectangle as background
|
53 |
+
overlay = frame.copy()
|
54 |
+
cv2.rectangle(overlay, (rect_x1, rect_y1), (rect_x2, rect_y2), (0, 255, 0), -1) # Black rectangle
|
55 |
+
alpha = 0.5 # Opacity level (0 = transparent, 1 = opaque)
|
56 |
+
cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame)
|
57 |
+
|
58 |
+
# Draw red text on top of the rectangle
|
59 |
+
cv2.putText(frame, count_text, (text_x, y_offset),
|
60 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
61 |
+
|
62 |
+
y_offset += 40 # Increase y-offset for the next class count
|
63 |
+
|
64 |
+
return frame
|
65 |
|
66 |
|
67 |
def ultralytics(detection, duration):
|
|
|
83 |
return conf, pos, text, color
|
84 |
|
85 |
|
86 |
+
def process_frame():
|
87 |
+
global cap
|
88 |
+
ret, frame = cap.read()
|
89 |
+
if not ret:
|
90 |
+
cap.release() # Release the video capture if no frame is captured
|
91 |
+
return None
|
92 |
+
frame = ultralytics_predict(model, frame)
|
93 |
+
return frame # Return frame and object count
|
94 |
+
|
95 |
+
|
96 |
+
def gradio_video_stream(video_file):
|
97 |
+
print(f"gradio_video_stream init : {video_file}")
|
98 |
+
global cap
|
99 |
+
cap = cv2.VideoCapture(video_file)
|
100 |
+
while True:
|
101 |
+
frame = process_frame()
|
102 |
+
if frame is None:
|
103 |
+
break
|
104 |
+
if isinstance(frame, np.ndarray): # Check if frame is a valid numpy array
|
105 |
+
yield cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
106 |
+
else:
|
107 |
+
print("Invalid frame format")
|
108 |
+
yield cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
109 |
+
|
110 |
+
|
111 |
+
iface = gr.Interface(fn=gradio_video_stream,
|
112 |
+
inputs=gr.Video(label="Upload Video"),
|
113 |
+
outputs=gr.Image(),
|
114 |
+
).launch()
|
app1.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Created by yarramsettinaresh GORAKA DIGITAL PRIVATE LIMITED at 24/10/24
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
import time
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
# Load your YOLO model (adjust model path or type as needed)
|
8 |
+
model_path = "model_- 11 october 2024 11_07.pt"
|
9 |
+
model = YOLO(model_path)
|
10 |
+
|
11 |
+
|
12 |
+
def ultralytics_predict(model, frame):
|
13 |
+
confidence_threshold = 0.2
|
14 |
+
start_time = time.time()
|
15 |
+
results = model(frame) # Perform inference on the frame
|
16 |
+
end_time = time.time()
|
17 |
+
|
18 |
+
duration = end_time - start_time
|
19 |
+
print(f"Prediction duration: {duration:.4f} seconds")
|
20 |
+
duration_str = f"{duration:.4f} S"
|
21 |
+
|
22 |
+
object_count = {} # Dictionary to store counts of detected objects
|
23 |
+
|
24 |
+
for detection in results[0].boxes: # Iterate through detections
|
25 |
+
conf = float(detection.conf[0]) # Confidence score
|
26 |
+
if conf > confidence_threshold:
|
27 |
+
conf, pos, text, color = ultralytics(detection, duration_str)
|
28 |
+
cv2.rectangle(frame, pos[0], pos[1], color, 2)
|
29 |
+
cv2.putText(frame, text, (pos[0][0], pos[0][1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
30 |
+
|
31 |
+
# Update object count
|
32 |
+
class_id = int(detection.cls[0])
|
33 |
+
class_name = model.names[class_id]
|
34 |
+
if class_name not in object_count:
|
35 |
+
object_count[class_name] = dict(count=0, objects=[])
|
36 |
+
object_mapp = object_count[class_name]
|
37 |
+
object_mapp["count"] = object_mapp.get("count", 0) + 1
|
38 |
+
object_mapp["objects"].append(dict(conf=conf, pos=pos, text=text, color=color))
|
39 |
+
|
40 |
+
return frame # Return the count of detected objects
|
41 |
+
|
42 |
+
|
43 |
+
def ultralytics(detection, duration):
|
44 |
+
COLOUR_MAP = {
|
45 |
+
0: (0, 0, 255), # Red in BGR format
|
46 |
+
1: (0, 255, 0) # Green in BGR format
|
47 |
+
}
|
48 |
+
|
49 |
+
conf = float(detection.conf[0]) # Confidence score
|
50 |
+
class_id = int(detection.cls[0]) # Class ID
|
51 |
+
name = model.names[class_id] # Get class name
|
52 |
+
xmin, ymin, xmax, ymax = map(int, detection.xyxy[0]) # Bounding box coordinates
|
53 |
+
color = COLOUR_MAP.get(class_id, (255, 255, 255)) # Default to white if not found
|
54 |
+
|
55 |
+
# Draw bounding box and label on the frame
|
56 |
+
pos = (xmin, ymin), (xmax, ymax)
|
57 |
+
text = f"{name} {round(conf, 2)} :{duration}"
|
58 |
+
|
59 |
+
return conf, pos, text, color
|
60 |
+
|
61 |
+
|
62 |
+
def process_frame(frame):
|
63 |
+
object_count = ultralytics_predict(model, frame)
|
64 |
+
return frame, object_count # Return frame and object count
|
65 |
+
|
66 |
+
|
67 |
+
def detect_image(image):
|
68 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert to BGR format for OpenCV
|
69 |
+
result_frame, object_count = process_frame(image)
|
70 |
+
result_frame = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB) # Convert back to RGB for Gradio
|
71 |
+
return result_frame, object_count # Return both the frame and the count
|
72 |
+
|
73 |
+
|
74 |
+
# Create Gradio Interface
|
75 |
+
gr.Interface(
|
76 |
+
fn=detect_image,
|
77 |
+
inputs=gr.Image(type="numpy"), # Updated input format
|
78 |
+
outputs=[
|
79 |
+
gr.Image(type="numpy"), # Image output
|
80 |
+
gr.JSON(), # Object count output as JSON
|
81 |
+
],
|
82 |
+
title="YOLO Object Detection",
|
83 |
+
description="Upload an image to detect objects using YOLO model."
|
84 |
+
).launch()
|