Spaces:
Runtime error
Runtime error
mohamedrekik
commited on
Commit
·
48cf7a9
1
Parent(s):
6310d30
Add application file and labels
Browse files- app.py +122 -0
- coco_names.txt +80 -0
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import onnxruntime as rt
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
def image_preprocess(image):
|
7 |
+
|
8 |
+
img_height, img_width = image.shape[0:2]
|
9 |
+
image_converted = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
10 |
+
|
11 |
+
|
12 |
+
ih, iw = [input_size, input_size] # [input_size, input_size] = [640, 640]
|
13 |
+
h, w, _ = image.shape # [1944, 2592]
|
14 |
+
|
15 |
+
scale = min(iw/w, ih/h) # min(0.2469, 0.3292) = 0.2469
|
16 |
+
nw, nh = int(scale * w), int(scale * h) # [640, 480]
|
17 |
+
image_resized = cv2.resize(image_converted, (nw, nh))
|
18 |
+
|
19 |
+
image_padded = np.full(shape=[ih, iw, 3], fill_value=128.0)
|
20 |
+
dw, dh = (iw - nw) // 2, (ih-nh) // 2 # [0, 80]
|
21 |
+
image_padded[dh:nh+dh, dw:nw+dw, :] = image_resized # image_padded[80:256, 32:224]
|
22 |
+
image_padded = image_padded / 255.
|
23 |
+
# image_resized = image_resized / 255.
|
24 |
+
image_padded = image_padded[np.newaxis, ...].astype(np.float32)
|
25 |
+
image_padded = np.moveaxis(image_padded, -1, 1)
|
26 |
+
|
27 |
+
|
28 |
+
return image_padded, img_width, img_height, image
|
29 |
+
|
30 |
+
|
31 |
+
def inference(image_data):
|
32 |
+
detections = sess.run(output_names, {input_name: image_data})
|
33 |
+
return detections
|
34 |
+
|
35 |
+
def draw_detections(img, box, score, class_id):
|
36 |
+
|
37 |
+
# Extract the coordinates of the bounding box
|
38 |
+
x1, y1, x2, y2 = box
|
39 |
+
|
40 |
+
# Retrieve the color for the class ID
|
41 |
+
color = color_palette_pred[class_id]
|
42 |
+
|
43 |
+
# Draw the bounding box on the image
|
44 |
+
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), color, 3)
|
45 |
+
|
46 |
+
# Create the label text with class name and score
|
47 |
+
label = f'{classes[class_id]}: {score:.2f}'
|
48 |
+
|
49 |
+
# Calculate the dimensions of the label text
|
50 |
+
(label_width, label_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 2, 2)
|
51 |
+
|
52 |
+
# Calculate the position of the label text
|
53 |
+
label_x = x1
|
54 |
+
label_y = y1 - 10 if y1 - 10 > label_height else y1 + 10
|
55 |
+
|
56 |
+
# Draw a filled rectangle as the background for the label text
|
57 |
+
label_x = round(label_x)
|
58 |
+
label_y = round(label_y)
|
59 |
+
cv2.rectangle(img, (label_x, label_y - label_height), (label_x + label_width, label_y + label_height), color, cv2.FILLED)
|
60 |
+
|
61 |
+
# Draw the label text on the image
|
62 |
+
cv2.putText(img, label, (label_x, label_y), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 1, cv2.LINE_AA)
|
63 |
+
|
64 |
+
|
65 |
+
def postprocess(detections, img_width, img_height, input_size, original_image):
|
66 |
+
boxes = detections[1][0]
|
67 |
+
scores = detections[2][0]
|
68 |
+
class_ids = detections[3][0]
|
69 |
+
# width_scale = img_width / input_size
|
70 |
+
# height_scale = img_height / input_size
|
71 |
+
|
72 |
+
ih, iw = [input_size, input_size] # [input_size, input_size] = [640, 640]
|
73 |
+
h, w = img_height, img_width # [1944, 2592]
|
74 |
+
scale = min(iw/w, ih/h) # min(0.2469, 0.3292) = 0.2469
|
75 |
+
nw, nh = int(scale * w), int(scale * h) # [640, 480]
|
76 |
+
dw, dh = (iw - nw) // 2, (ih-nh) // 2 # [0, 80]
|
77 |
+
# dh, dw = 0, 0
|
78 |
+
# scale = 0.3292
|
79 |
+
new_boxes = []
|
80 |
+
for box, score, class_id in zip(boxes, scores, class_ids):
|
81 |
+
|
82 |
+
x1, y1, x2, y2 = box
|
83 |
+
x1 = (x1 - dw) / scale
|
84 |
+
y1 = (y1 - dh) / scale
|
85 |
+
x2 = (x2 - dw) / scale
|
86 |
+
y2 = (y2 - dh) / scale
|
87 |
+
box = [x1, y1, x2, y2]
|
88 |
+
draw_detections(original_image, box, score, class_id)
|
89 |
+
new_boxes.append(box)
|
90 |
+
return [class_ids, scores, new_boxes]
|
91 |
+
|
92 |
+
|
93 |
+
# fix all the variables
|
94 |
+
with open("coco_names.txt", "r") as f:
|
95 |
+
content = f.readlines()
|
96 |
+
|
97 |
+
content = "".join(content)
|
98 |
+
classes = content.split("\n")
|
99 |
+
color_palette_pred = np.random.uniform(0, 255, size=(len(classes), 3))
|
100 |
+
|
101 |
+
sess = rt.InferenceSession("yolov8n_coco_640.nms.onnx")
|
102 |
+
|
103 |
+
outputs = sess.get_outputs()
|
104 |
+
output_names = list(map(lambda output: output.name, outputs))
|
105 |
+
input_name = sess.get_inputs()[0].name
|
106 |
+
input_size = sess.get_inputs()[0].shape[2]
|
107 |
+
|
108 |
+
|
109 |
+
def run(img_path):
|
110 |
+
image_data, img_width, img_height, original_image = image_preprocess(img_path)
|
111 |
+
detections = inference(image_data)
|
112 |
+
l = postprocess(detections, img_width, img_height, input_size, original_image)
|
113 |
+
return original_image
|
114 |
+
|
115 |
+
demo = gr.Interface(
|
116 |
+
fn=run,
|
117 |
+
inputs=["image"],
|
118 |
+
outputs=["image"],
|
119 |
+
)
|
120 |
+
|
121 |
+
demo.launch()
|
122 |
+
|
coco_names.txt
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
person
|
2 |
+
bicycle
|
3 |
+
car
|
4 |
+
motorcycle
|
5 |
+
airplane
|
6 |
+
bus
|
7 |
+
train
|
8 |
+
truck
|
9 |
+
boat
|
10 |
+
traffic light
|
11 |
+
fire hydrant
|
12 |
+
stop sign
|
13 |
+
parking meter
|
14 |
+
bench
|
15 |
+
bird
|
16 |
+
cat
|
17 |
+
dog
|
18 |
+
horse
|
19 |
+
sheep
|
20 |
+
cow
|
21 |
+
elephant
|
22 |
+
bear
|
23 |
+
zebra
|
24 |
+
giraffe
|
25 |
+
backpack
|
26 |
+
umbrella
|
27 |
+
handbag
|
28 |
+
tie
|
29 |
+
suitcase
|
30 |
+
frisbee
|
31 |
+
skis
|
32 |
+
snowboard
|
33 |
+
sports ball
|
34 |
+
kite
|
35 |
+
baseball bat
|
36 |
+
baseball glove
|
37 |
+
skateboard
|
38 |
+
surfboard
|
39 |
+
tennis racket
|
40 |
+
bottle
|
41 |
+
wine glass
|
42 |
+
cup
|
43 |
+
fork
|
44 |
+
knife
|
45 |
+
spoon
|
46 |
+
bowl
|
47 |
+
banana
|
48 |
+
apple
|
49 |
+
sandwich
|
50 |
+
orange
|
51 |
+
broccoli
|
52 |
+
carrot
|
53 |
+
hot dog
|
54 |
+
pizza
|
55 |
+
donut
|
56 |
+
cake
|
57 |
+
chair
|
58 |
+
couch
|
59 |
+
potted plant
|
60 |
+
bed
|
61 |
+
dining table
|
62 |
+
toilet
|
63 |
+
tv
|
64 |
+
laptop
|
65 |
+
mouse
|
66 |
+
remote
|
67 |
+
keyboard
|
68 |
+
cell phone
|
69 |
+
microwave
|
70 |
+
oven
|
71 |
+
toaster
|
72 |
+
sink
|
73 |
+
refrigerator
|
74 |
+
book
|
75 |
+
clock
|
76 |
+
vase
|
77 |
+
scissors
|
78 |
+
teddy bear
|
79 |
+
hair drier
|
80 |
+
toothbrush
|