Spaces:
Runtime error
Runtime error
File size: 4,661 Bytes
41e7e8e 5b8ecd8 |
1 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import gradio as gr
import onnxruntime as rt
import cv2
import numpy as np
from PIL import Image
H, W = 224, 224
classes=['aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable',
'dog','horse','motorbike','person','pottedplant','sheep','sofa','train','tvmonitor']
providers = ['CPUExecutionProvider']
m = rt.InferenceSession("./model/yolo_efficient.onnx", providers=providers)
def nms(final_boxes, scores, IOU_threshold=0):
scores = np.array(scores)
final_boxes = np.array(final_boxes)
boxes = final_boxes[...,:-1]
boxes = [list(map(int, i)) for i in boxes]
boxes = np.array(boxes)
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = (x2 - x1)*(y2 - y1)
order = np.argsort(scores)
pick = []
while len(order) > 0:
last = len(order)-1
i = order[last]
pick.append(i)
suppress = [last]
if len(order)==0:
break
for pos in range(last):
j = order[pos]
xx1 = max(x1[i], x1[j])
yy1 = max(y1[i], y1[j])
xx2 = min(x2[i], x2[j])
yy2 = min(y2[i], y2[j])
w = max(0, xx2-xx1+1)
h = max(0, yy2-yy1+1)
overlap = float(w*h)/area[j]
if overlap > IOU_threshold:
suppress.append(pos)
order = np.delete(order, suppress)
return final_boxes[pick]
def detect_obj(input_image):
try:
image = np.array(input_image)
image = cv2.resize(image, (H, W))
img = image
image = image.astype(np.float32)
image = np.expand_dims(image, axis=0)
output = m.run(['reshape'], {"input": image})
output = np.squeeze(output, axis=0)
THRESH=.25
object_positions = np.concatenate(
[np.stack(np.where(output[..., 0]>=THRESH), axis=-1),
np.stack(np.where(output[..., 5]>=THRESH), axis=-1)], axis=0
)
selected_output = []
for indices in object_positions:
selected_output.append(output[indices[0]][indices[1]][indices[2]])
selected_output = np.array(selected_output)
final_boxes = []
final_scores = []
for i,pos in enumerate(object_positions):
for j in range(2):
if selected_output[i][j*5]>THRESH:
output_box = np.array(output[pos[0]][pos[1]][pos[2]][(j*5)+1:(j*5)+5], dtype=float)
x_centre = (np.array(pos[1], dtype=float) + output_box[0])*32
y_centre = (np.array(pos[2], dtype=float) + output_box[1])*32
x_width, y_height = abs(W*output_box[2]), abs(H*output_box[3])
x_min, y_min = int(x_centre - (x_width/2)), int(y_centre-(y_height/2))
x_max, y_max = int(x_centre+(x_width/2)), int(y_centre + (y_height/2))
if(x_min<0):x_min=0
if(y_min<0):y_min=0
if(x_max<0):x_max=0
if(y_max<0):y_max=0
final_boxes.append(
[x_min, y_min, x_max, y_max, str(classes[np.argmax(selected_output[..., 10:], axis=-1)[i]])]
)
final_scores.append(selected_output[i][j*5])
final_boxes = np.array(final_boxes)
nms_output = nms(final_boxes, final_scores, 0.3)
for i in nms_output:
cv2.rectangle(
img,
(int(i[0]), int(i[1])),
(int(i[2]), int(i[3])), (255, 0, 0)
)
cv2.putText(
img,
i[-1],
(int(i[0]), int(i[1])+15),
cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 1
)
output_pil_img = Image.fromarray(np.uint8(img)).convert('RGB')
return output_pil_img
except:
return input_image
with gr.Blocks(title="YOLOS Object Detection - ClassCat", css=".gradio-container {background:lightyellow;}") as demo:
gr.HTML('<h1>Yolo Object Detection</h1>')
gr.HTML("<h4>supported objects are [aeroplane,bicycle,bird,boat,bottle,bus,car,cat,chair,cow,diningtable,dog,horse,motorbike,person,pottedplant,sheep,sofa,train,tvmonitor]</h4>")
with gr.Row():
input_image = gr.Image(label="Input image", type="pil")
output_image = gr.Image(label="Output image", type="pil")
send_btn = gr.Button("Detect")
gr.Examples(['./samples/out_1.jpg'], inputs=input_image)
send_btn.click(fn=detect_obj, inputs=[input_image], outputs=[output_image])
demo.launch(debug=True) |