Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -44,18 +44,6 @@ colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
|
44 |
total_inference_time = 0
|
45 |
inference_count = 0
|
46 |
|
47 |
-
def preprocess_image(image):
|
48 |
-
"""Prepares image for YOLOv5 detection while maintaining aspect ratio."""
|
49 |
-
h, w, _ = image.shape
|
50 |
-
scale = 640 / max(h, w)
|
51 |
-
new_w, new_h = int(w * scale), int(h * scale)
|
52 |
-
|
53 |
-
resized_image = cv2.resize(image, (new_w, new_h))
|
54 |
-
padded_image = np.full((640, 640, 3), 114, dtype=np.uint8) # Gray padding
|
55 |
-
padded_image[:new_h, :new_w] = resized_image
|
56 |
-
|
57 |
-
return cv2.cvtColor(padded_image, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
|
58 |
-
|
59 |
def detect_objects(image):
|
60 |
global total_inference_time, inference_count
|
61 |
|
@@ -64,11 +52,14 @@ def detect_objects(image):
|
|
64 |
|
65 |
start_time = time.time()
|
66 |
|
67 |
-
#
|
68 |
-
|
|
|
|
|
|
|
69 |
|
70 |
with torch.inference_mode(): # Faster than torch.no_grad()
|
71 |
-
results = model(
|
72 |
|
73 |
inference_time = time.time() - start_time
|
74 |
total_inference_time += inference_time
|
@@ -85,17 +76,17 @@ def detect_objects(image):
|
|
85 |
color = colors[class_id].tolist()
|
86 |
|
87 |
# Keep bounding boxes within image bounds
|
88 |
-
x1, y1, x2, y2 = max(0, x1), max(0, y1), min(
|
89 |
|
90 |
# Draw bounding box
|
91 |
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
92 |
|
93 |
label = f"{model.names[class_id]} {conf:.2f}"
|
94 |
font_scale, font_thickness = 0.9, 2
|
95 |
-
(
|
96 |
|
97 |
# Label background
|
98 |
-
cv2.rectangle(output_image, (x1, y1 -
|
99 |
cv2.putText(output_image, label, (x1 + 5, y1 - 5),
|
100 |
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness, lineType=cv2.LINE_AA)
|
101 |
|
@@ -116,10 +107,10 @@ def detect_objects(image):
|
|
116 |
example_images = ["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
|
117 |
os.makedirs("examples", exist_ok=True)
|
118 |
|
119 |
-
with gr.Blocks(title="
|
120 |
gr.Markdown("""
|
121 |
-
#
|
122 |
-
Detects objects
|
123 |
""")
|
124 |
|
125 |
with gr.Row():
|
|
|
44 |
total_inference_time = 0
|
45 |
inference_count = 0
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def detect_objects(image):
|
48 |
global total_inference_time, inference_count
|
49 |
|
|
|
52 |
|
53 |
start_time = time.time()
|
54 |
|
55 |
+
# Convert image to BGR format for OpenCV
|
56 |
+
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
57 |
+
|
58 |
+
# Get image dimensions
|
59 |
+
h, w, _ = image.shape
|
60 |
|
61 |
with torch.inference_mode(): # Faster than torch.no_grad()
|
62 |
+
results = model(image_bgr)
|
63 |
|
64 |
inference_time = time.time() - start_time
|
65 |
total_inference_time += inference_time
|
|
|
76 |
color = colors[class_id].tolist()
|
77 |
|
78 |
# Keep bounding boxes within image bounds
|
79 |
+
x1, y1, x2, y2 = max(0, x1), max(0, y1), min(w, x2), min(h, y2)
|
80 |
|
81 |
# Draw bounding box
|
82 |
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
83 |
|
84 |
label = f"{model.names[class_id]} {conf:.2f}"
|
85 |
font_scale, font_thickness = 0.9, 2
|
86 |
+
(tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
|
87 |
|
88 |
# Label background
|
89 |
+
cv2.rectangle(output_image, (x1, y1 - th - 10), (x1 + tw + 10, y1), color, -1)
|
90 |
cv2.putText(output_image, label, (x1 + 5, y1 - 5),
|
91 |
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness, lineType=cv2.LINE_AA)
|
92 |
|
|
|
107 |
example_images = ["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
|
108 |
os.makedirs("examples", exist_ok=True)
|
109 |
|
110 |
+
with gr.Blocks(title="YOLOv5 Object Detection (High Quality, High FPS)") as demo:
|
111 |
gr.Markdown("""
|
112 |
+
# YOLOv5 Object Detection - High Quality & High FPS
|
113 |
+
Detects objects with full-resolution output and ultra-fast performance.
|
114 |
""")
|
115 |
|
116 |
with gr.Row():
|