Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,79 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
33 |
-
|
34 |
-
# Save the processed image with bounding boxes
|
35 |
-
output_path = "detected_image.jpg"
|
36 |
-
image_with_boxes.save(output_path)
|
37 |
-
|
38 |
-
return jsonify({"boxes": detected_boxes, "output_image": output_path})
|
39 |
-
|
40 |
-
if __name__ == "__main__":
|
41 |
-
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import datetime
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
# Ensure these files are available in the Hugging Face Space working directory
|
7 |
+
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
|
8 |
+
layer_names = net.getLayerNames()
|
9 |
+
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
|
10 |
+
classes = []
|
11 |
+
with open("coco.names", "r") as f:
|
12 |
+
classes = [line.strip() for line in f.readlines()]
|
13 |
|
14 |
+
def detect_objects(image):
|
15 |
+
height, width, channels = image.shape
|
16 |
+
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
17 |
+
net.setInput(blob)
|
18 |
+
outs = net.forward(output_layers)
|
19 |
+
|
20 |
+
class_ids = []
|
21 |
+
confidences = []
|
22 |
+
boxes = []
|
23 |
+
|
24 |
+
for out in outs:
|
25 |
+
for detection in out:
|
26 |
+
scores = detection[5:]
|
27 |
+
class_id = np.argmax(scores)
|
28 |
+
confidence = scores[class_id]
|
29 |
+
if confidence > 0.5:
|
30 |
+
center_x = int(detection[0] * width)
|
31 |
+
center_y = int(detection[1] * height)
|
32 |
+
w = int(detection[2] * width)
|
33 |
+
h = int(detection[3] * height)
|
34 |
+
|
35 |
+
x = int(center_x - w / 2)
|
36 |
+
y = int(center_y - h / 2)
|
37 |
+
|
38 |
+
boxes.append([x, y, w, h])
|
39 |
+
confidences.append(float(confidence))
|
40 |
+
class_ids.append(class_id)
|
41 |
+
|
42 |
+
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
43 |
+
|
44 |
+
return [(boxes[i], class_ids[i], confidences[i]) for i in range(len(boxes)) if i in indexes]
|
45 |
|
46 |
+
def process_image(image):
|
47 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
48 |
+
detections = detect_objects(image)
|
49 |
+
for (box, class_id, confidence) in detections:
|
50 |
+
x, y, w, h = box
|
51 |
+
label = str(classes[class_id])
|
52 |
+
color = (0, 255, 0) if label == "person" else (0, 0, 255)
|
53 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
|
54 |
+
cv2.putText(image, f'{label} {confidence:.2f}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
|
55 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
56 |
|
57 |
+
def capture_and_process():
|
58 |
+
cap = cv2.VideoCapture(0)
|
59 |
+
while cap.isOpened():
|
60 |
+
ret, frame = cap.read()
|
61 |
+
if not ret:
|
62 |
+
break
|
63 |
+
processed_frame = process_image(frame)
|
64 |
+
yield processed_frame
|
65 |
+
cap.release()
|
66 |
|
67 |
+
# Define Gradio interface
|
68 |
+
with gr.Blocks() as iface:
|
69 |
+
gr.Markdown("# YOLO Object Detection")
|
70 |
+
gr.Markdown("## Real-time object detection using YOLO")
|
71 |
|
72 |
+
with gr.Tab("Upload Image"):
|
73 |
+
gr.Markdown("Upload an image and the YOLO model will detect objects in the image, highlighting humans.")
|
74 |
+
image_input = gr.Image(type="numpy", label="Upload an image")
|
75 |
+
image_output = gr.Image(type="numpy", label="Detected objects")
|
76 |
+
image_input.upload(process_image, inputs=image_input, outputs=image_output)
|
77 |
|
78 |
+
# Launch Gradio interface
|
79 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|