-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference_pyserial.py
181 lines (151 loc) · 5.25 KB
/
inference_pyserial.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import cv2
import argparse
import numpy as np
import serial # not needed if you dont use an arduino
from time import sleep
ser = serial.Serial('/dev/ttyACM0') # open serial port, used this to communicate with arduino
# comment it out if not using with arduino
ctr=300
center_y=238
topleft=0
topright=0
bottomleft=0
bottomright=0
cap = cv2.VideoCapture(0) #getting the video feed
c=0
k=0
i=0
ap = argparse.ArgumentParser() ## getting input arguments
ap.add_argument('-i', '--image', required=True,
help = 'path to input image')
ap.add_argument('-c', '--config', required=True,
help = 'path to yolo config file')
ap.add_argument('-w', '--weights', required=True,
help = 'path to yolo pre-trained weights')
ap.add_argument('-cl', '--classes', required=True,
help = 'path to text file containing class names')
args = ap.parse_args()
def get_output_layers(net):
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
return output_layers
def draw_prediction(img, class_id, confidence, x, y, x_plus_w, y_plus_h):
#global c
#c+=1
#print(c) prints numbers
label = str(classes[class_id])
conf=round(confidence,1)
conf=str(conf)
color = (255,255,0)
if(label=="person"):
cv2.rectangle(img, (x,y), (x_plus_w,y_plus_h), color, 2)
w=x_plus_w-x
h=y_plus_h-y
pointx=x+w/2
pointy=y+h/2
xv=pointx-300
yv=pointy-238
if(xv<0 and yv<0):
print("top left")
elif(xv>0 and yv<0):
print("top right")
elif(xv>0 and yv <0):
print("bottom left")
elif(xv>0 and yv>0):
print("bottom right")
print(center_y)
# cv2.circle(img,(xv,yv),5,(255,0,0),-1)
cv2.putText(img, label, (x-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
cv2.putText(img, conf, (x+20,y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
else:
pass
while True:
ret,image=cap.read()
w = image.shape[1]
h = image.shape[0]
w=int(w/2)
h=int(h/2)
cv2.line(image,(0,h),(w*2,h),(0,0,255),5)
cv2.line(image,(w,0),(w,h*2),(0,0,255),5)
cv2.imshow("frame",image)
if ret:
Width = image.shape[1]
Height = image.shape[0]
scale = 0.00392
classes=None
with open(args.classes, 'r') as f:
classes = [line.strip() for line in f.readlines()]
COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
net = cv2.dnn.readNet(args.weights, args.config)
blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)
net.setInput(blob)
outs = net.forward(get_output_layers(net))
class_ids = []
confidences = []
boxes = []
conf_threshold = 0.5
nms_threshold = 0.4
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * Width)
center_y = int(detection[1] * Height)
w = int(detection[2] * Width)
h = int(detection[3] * Height)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
j=0
for i in indices:
j+=1
#print(j)
i = i[0]
box = boxes[i]
x = box[0]
y = box[1]
w = box[2]
h = box[3]
#print(i+1)
draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
W = image.shape[1]
w=W/2
w=int(w)
H = image.shape[0]
h=H/2
h=int(h)
pointx=(x+w/2)
pointy=(y+h/2)
xv=pointx-w
yv=pointy-h
if(xv<0 and yv<0):
topleft=1
if(xv>0 and yv<0):
topright=1
if(xv<0 and yv >0):
bottomleft=1
if(xv>0 and yv>0):
bottomright=1
i=i+1
i=str(i)
cv2.putText(image, 'Count:', (100,300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
cv2.putText(image, i, (200,300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
print('TL'+'/'+'TR'+'/'+'BL'+'/'+'BR')
print(str(topleft)+'/'+str(topright)+'/'+str(bottomleft)+'/'+str(bottomright))
s = str(topleft)+'\t'+str(topright)+'\t'+str(bottomleft)+'\t'+str(bottomright)+'\n'
ser.write(s.encode())
color=(0,0,255)
cv2.imshow("object detection", image)
topleft=0
topright=0
bottomleft=0
bottomright=0
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()