Spaces:
Build error
Build error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
with open('coco.names', 'r') as f:
|
6 |
+
classes = f.read().splitlines()
|
7 |
+
|
8 |
+
net = cv2.dnn.readNetFromDarknet('yolov4.cfg', 'yolov4.weights')
|
9 |
+
|
10 |
+
model = cv2.dnn_DetectionModel(net)
|
11 |
+
model.setInputParams(scale=1 / 255, size=(416, 416), swapRB=True)
|
12 |
+
|
13 |
+
def detect(img):
|
14 |
+
img = Image.fromarray(img)
|
15 |
+
classIds, scores, boxes = model.detect(img, confThreshold=0.6, nmsThreshold=0.4)
|
16 |
+
|
17 |
+
for (classId, score, box) in zip(classIds, scores, boxes):
|
18 |
+
cv2.rectangle(img, (box[0], box[1]), (box[0] + box[2], box[1] + box[3]),
|
19 |
+
color=(0, 255, 0), thickness=2)
|
20 |
+
text = '%s: %.2f' % (classes[classId], score)
|
21 |
+
cv2.putText(img, text, (box[0], box[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,
|
22 |
+
color=(0, 255, 0), thickness=2)
|
23 |
+
|
24 |
+
return img
|
25 |
+
|
26 |
+
image_in = gr.components.Image()
|
27 |
+
image_out = gr.components.Image()
|
28 |
+
|
29 |
+
classes_to_show = gr.components.Textbox(placeholder="e.g. person, boat", label="Classes to use (empty means all classes)")
|
30 |
+
|
31 |
+
Iface = gr.Interface(
|
32 |
+
fn=detect,
|
33 |
+
inputs=image_in,
|
34 |
+
outputs=image_out,
|
35 |
+
|
36 |
+
title="Object Detection with YOLOS",
|
37 |
+
description=description,
|
38 |
+
).launch()
|