Spaces:
Sleeping
Sleeping
unknown
commited on
Commit
·
3aa4656
1
Parent(s):
7ed8033
Add large files
Browse files- README.md +2 -13
- app.py +217 -0
- doors.onnx +3 -0
- image.jpg +0 -0
- s.jpg +0 -0
README.md
CHANGED
@@ -1,13 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 🏃
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: indigo
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.33.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: openrail
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# YOLO-v5-Door-detection-for-visually-impaired-people
|
2 |
+
YOLO v5 model for Door detection for visually impaired people
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import time
|
4 |
+
import os
|
5 |
+
#import datetime
|
6 |
+
#from datetime import datetime
|
7 |
+
#from PIL import Image
|
8 |
+
#from io import BytesIO
|
9 |
+
#from scipy import ndimage
|
10 |
+
#from pympler.tracker import SummaryTracker
|
11 |
+
#tracker = SummaryTracker()
|
12 |
+
|
13 |
+
|
14 |
+
INPUT_WIDTH = 640
|
15 |
+
INPUT_HEIGHT = 640
|
16 |
+
SCORE_THRESHOLD = 0.45
|
17 |
+
NMS_THRESHOLD = 0.45
|
18 |
+
CONFIDENCE_THRESHOLD = 0.5
|
19 |
+
|
20 |
+
# Text parameters.
|
21 |
+
FONT_FACE = cv2.FONT_HERSHEY_SIMPLEX
|
22 |
+
FONT_SCALE = 0.7
|
23 |
+
THICKNESS = 1
|
24 |
+
|
25 |
+
# Colors.
|
26 |
+
BLACK = (0,0,0)
|
27 |
+
BLUE = (255,178,50)
|
28 |
+
YELLOW = (0,255,255)
|
29 |
+
classesFile = "coco.names"
|
30 |
+
classes = None
|
31 |
+
|
32 |
+
|
33 |
+
#frame = cv2.imread('1.jpg')
|
34 |
+
# Give the weight files to the model and load the network using them.
|
35 |
+
|
36 |
+
roi_detection_modelWeights = "doors.onnx"
|
37 |
+
roi_detection_model = cv2.dnn.readNet(roi_detection_modelWeights)
|
38 |
+
roi_detection_model.setPreferableBackend(cv2.dnn.DNN_BACKEND_DEFAULT)
|
39 |
+
roi_detection_model.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
|
40 |
+
|
41 |
+
#num_rec_modelWeights = "chan_3_32_num.onnx" #ch first
|
42 |
+
#num_rec_model = cv2.dnn.readNet(num_rec_modelWeights)
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
def draw_label(im, label, x, y):
|
48 |
+
"""Draw text onto image at location."""
|
49 |
+
# Get text size.
|
50 |
+
text_size = cv2.getTextSize(label, FONT_FACE, FONT_SCALE, THICKNESS)
|
51 |
+
dim, baseline = text_size[0], text_size[1]
|
52 |
+
# Use text size to create a BLACK rectangle.
|
53 |
+
cv2.rectangle(im, (x,y), (x + dim[0], y + dim[1] + baseline), (0,0,0), cv2.FILLED);
|
54 |
+
# Display text inside the rectangle.
|
55 |
+
cv2.putText(im, label, (x, y + dim[1]), FONT_FACE, FONT_SCALE, YELLOW, THICKNESS, cv2.LINE_AA)
|
56 |
+
def pre_process(input_image, net,w,h):
|
57 |
+
# Create a 4D blob from a frame.
|
58 |
+
#print(input_image.shape)
|
59 |
+
blob = cv2.dnn.blobFromImage(input_image, scalefactor=1/255, size=(640, 640), mean=(0, 0, 0), swapRB=True, crop=False)
|
60 |
+
# blob = cv2.dnn.blobFromImage(input_image, 1/255, (w, h), [0,0,0], 1, crop=False)
|
61 |
+
|
62 |
+
# Sets the input to the network.
|
63 |
+
net.setInput(blob)
|
64 |
+
|
65 |
+
# Run the forward pass to get output of the output layers.
|
66 |
+
|
67 |
+
outputs = net.forward(net.getUnconnectedOutLayersNames())
|
68 |
+
del (blob)
|
69 |
+
return outputs
|
70 |
+
|
71 |
+
def get_xyxy(input_image, outputs,w,h):
|
72 |
+
# Lists to hold respective values while unwrapping.
|
73 |
+
class_ids = []
|
74 |
+
confidences = []
|
75 |
+
boxes = []
|
76 |
+
output_boxes=[]
|
77 |
+
# Rows.
|
78 |
+
rows = outputs[0].shape[1]
|
79 |
+
image_height, image_width = input_image.shape[:2]
|
80 |
+
# Resizing factor.
|
81 |
+
x_factor = image_width / w
|
82 |
+
y_factor = image_height / h
|
83 |
+
# Iterate through detections.
|
84 |
+
for r in range(rows):
|
85 |
+
row = outputs[0][0][r]
|
86 |
+
confidence = row[4]
|
87 |
+
# Discard bad detections and continue.
|
88 |
+
if confidence >= CONFIDENCE_THRESHOLD:
|
89 |
+
classes_scores = row[5:]
|
90 |
+
# Get the index of max class score.
|
91 |
+
class_id = np.argmax(classes_scores)
|
92 |
+
# Continue if the class score is above threshold.
|
93 |
+
if (classes_scores[class_id] > SCORE_THRESHOLD):
|
94 |
+
confidences.append(confidence)
|
95 |
+
class_ids.append(class_id)
|
96 |
+
cx, cy, w, h = row[0], row[1], row[2], row[3]
|
97 |
+
left = int((cx - w/2) * x_factor)
|
98 |
+
top = int((cy - h/2) * y_factor)
|
99 |
+
width = int(w * x_factor)
|
100 |
+
height = int(h * y_factor)
|
101 |
+
box = np.array([left, top, width, height])
|
102 |
+
boxes.append(box)
|
103 |
+
# Perform non maximum suppression to eliminate redundant, overlapping boxes with lower confidences.
|
104 |
+
indices = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE_THRESHOLD, NMS_THRESHOLD)
|
105 |
+
for i in indices:
|
106 |
+
box = boxes[i]
|
107 |
+
left = box[0]
|
108 |
+
top = box[1]
|
109 |
+
width = box[2]
|
110 |
+
height = box[3]
|
111 |
+
# Draw bounding box.
|
112 |
+
cv2.rectangle(input_image, (left, top), (left + width, top + height), BLUE, 3*THICKNESS)
|
113 |
+
# Class label.
|
114 |
+
#label = "{}:{:.2f}".format(classes[class_ids[i]], confidences[i])
|
115 |
+
# Draw label.
|
116 |
+
draw_label(input_image, 'x', left, top)
|
117 |
+
cv2.imwrite('image.jpg',input_image)
|
118 |
+
#turn xywh into xyxy
|
119 |
+
boxes[i][2]=left + width
|
120 |
+
boxes[i][3]=top + height
|
121 |
+
#check if the height is suitable
|
122 |
+
output_boxes.append(boxes[i])
|
123 |
+
#if height >20:
|
124 |
+
# output_boxes.append(boxes[i])
|
125 |
+
#del(input_image,)
|
126 |
+
return 1,output_boxes,input_image #boxes (left,top,width,height)
|
127 |
+
|
128 |
+
def roi_detection(input_image,roi_detection_model,w,h):
|
129 |
+
detections = pre_process(input_image, roi_detection_model,w,h) #detection results
|
130 |
+
|
131 |
+
_,bounding_boxes,input_image=get_xyxy(input_image, detections,w,h) # nms and return the valid bounding boxes
|
132 |
+
#print( bounding_boxes)
|
133 |
+
#date = datetime.now().strftime("%Y_%m_%d_%I_%M_%S_%p")
|
134 |
+
#cv2.imwrite(f"lic_{date}.jpg",image_with_bounding_boxes)
|
135 |
+
#cv2.imwrite('xf.jpg',image_with_bounding_boxes)
|
136 |
+
return bounding_boxes ,input_image
|
137 |
+
|
138 |
+
|
139 |
+
# def number_detection(input_image,ch_detection_model,w,h):
|
140 |
+
# #in_image_copy=input_image.copy()
|
141 |
+
# detections = pre_process(input_image.copy(), ch_detection_model,w,h) #detection results
|
142 |
+
# image_with_bounding_boxes,bounding_boxes=get_xyxy(input_image, detections,w,h)
|
143 |
+
# #date = datetime.now().strftime("%Y_%m_%d_%I_%M_%S_%p")
|
144 |
+
# #im_name=f"ch_{date}.jpg"
|
145 |
+
# #print(im_name)
|
146 |
+
# #cv2.imwrite(im_name,image_with_bounding_boxes)
|
147 |
+
# # cv2.imwrite('x1.jpg',image_with_bounding_boxes)
|
148 |
+
# return bounding_boxes
|
149 |
+
|
150 |
+
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
def main_func(img,):
|
157 |
+
scores='door :'
|
158 |
+
img = np.array(img)
|
159 |
+
#send_im_2_tg(img)
|
160 |
+
t1=time.time()
|
161 |
+
width_height_diff=img.shape[1]-img.shape[0] #padding
|
162 |
+
#print(width_height_diff,img.shape)
|
163 |
+
if width_height_diff>0:
|
164 |
+
img = cv2.copyMakeBorder(img, 0, width_height_diff, 0, 0, cv2.BORDER_CONSTANT, (0,0,0))
|
165 |
+
if width_height_diff<0:
|
166 |
+
img = cv2.copyMakeBorder(img, 0, 0, 0, int(-1*width_height_diff), cv2.BORDER_CONSTANT, (0,0,0))
|
167 |
+
cropped_licenses_array,input_image=roi_detection(img.copy(),roi_detection_model,640,640)
|
168 |
+
|
169 |
+
if len(cropped_licenses_array)!=0:
|
170 |
+
scores=scores+"True and detection time is "+str(time.time()-t1)
|
171 |
+
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
#print('total time in sec :',time.time()-t1)
|
180 |
+
#tracker.print_diff()
|
181 |
+
del(img)
|
182 |
+
#print(scores)
|
183 |
+
#return (scores+' time_sec : '+str(time.time()-t1))
|
184 |
+
return input_image ,scores
|
185 |
+
|
186 |
+
|
187 |
+
import gradio as gr
|
188 |
+
import cv2
|
189 |
+
import os
|
190 |
+
# im = gr.Image()
|
191 |
+
def greet(im):
|
192 |
+
im=cv2.imread(im)
|
193 |
+
|
194 |
+
im,number=main_func(im)
|
195 |
+
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
196 |
+
|
197 |
+
|
198 |
+
#print(im)
|
199 |
+
|
200 |
+
#im=cv2.imread(im)
|
201 |
+
# im=os.path.join("/content/s.jpg")
|
202 |
+
|
203 |
+
return im ,number
|
204 |
+
|
205 |
+
inputs = gr.Image(type="filepath", label="Input Image")
|
206 |
+
|
207 |
+
outputs = [gr.Image(type="filepath", label="Output Image"),gr.Textbox()]
|
208 |
+
title = "YOLO-v5-Door detection for visually impaired people"
|
209 |
+
|
210 |
+
demo_app = gr.Interface(examples=["s.jpg"],
|
211 |
+
fn=greet,
|
212 |
+
inputs=inputs,
|
213 |
+
outputs=outputs,
|
214 |
+
title=title,
|
215 |
+
cache_examples=True,
|
216 |
+
)
|
217 |
+
demo_app.launch()
|
doors.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:034595b7aef074e62cddebf5555260e1660453d7cab1466fe6c4b9ba92de3568
|
3 |
+
size 28678612
|
image.jpg
ADDED
![]() |
s.jpg
ADDED
![]() |