-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHuman_tracker_arduino.py
195 lines (160 loc) · 5.03 KB
/
Human_tracker_arduino.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Created during Winter Semester 2015
OpenCV Human face tracker combined with arduino powered bot to
follow humans.
@authors:
Yash Chandak Ankit Dhall
TODO:
convert frame specific values to percentages
-------------------------------------------------------------------------------
"""
import numpy as np
import sys
import time
"""
PySerial library required for arduino connection
OpenCV library requierd for face tracking
"""
import serial
import cv2
"""
Arduino connected at port No. COM28,
Confirm and change this value accordingly from control panel
Baud Rate = 9600
"""
arduino = serial.Serial('COM28', 9600)
time.sleep(2) # waiting the initialization...
print("initialised")
#gets the direction for Arduino serial
def direction(bound, initArea=40000):
"""
Direction control Index:
'<' , '>' are the frame check bits for serial communication
Numbers represent the direction to be moved as per their position on numpad
1: Back Left
2: Back
3: Back right
4: Left
5: Stay still
6: Right
7: Front Left
8: Forward
9: Forward right
"""
#anchor the centre position of the image
center=(320, 240)
#current rectangle center
curr = (bound[0] + bound[2]/2, bound[1]+bound[3]/2)
out=0
flag=0
fb = 0 #0-stay 1-fwd 2-bwd
lr = 0 #0-stay 1-left 2-right
#if the object is coming closer i.e. it's size is increasing then move bwd
if bound[2]*bound[3] > (initArea+5000) or bound[1]<50 :
fb = 2
#if the object os moving away i.e. it's size is decreasing then move towards it
elif bound[2]*bound[3] < (initArea-5000) or (bound[1]+bound[3])>430 :
fb = 1
else :
fb = 0
#move right
if curr[0] > (center[0] + 100):
lr = 2
#move left
elif curr[0] < (center[0] - 100):
lr = 1
else:
lr = 0
if lr == 0 and fb == 0:
out = 5
print "stay"
elif lr == 0 and fb == 1:
out =8
print "fwd"
elif lr == 0 and fb == 2:
out = 2
print "back"
elif lr == 1 and fb == 0:
out = 4
print "left"
elif lr == 1 and fb == 1:
out = 7
print "fwd left"
elif lr == 1 and fb == 2:
out = 1
print "left back"
elif lr == 2 and fb == 0:
out = 6
print "right"
elif lr == 2 and fb == 1:
out = 9
print "fwd right"
elif lr == 2 and fb == 2:
out = 3
print "bwd right"
else :
out = 5
print "Stay Still"
#Write the encoded direction value on the serial communication line
print out
arduino.write('<')
arduino.write(str(out))
arduino.write('>')
def detectAndDisplay(frame):
#use OpenCV HAAR face detetcion algorithm to detect faces
faces = cascade.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=3,
minSize=(30, 30),maxSize=(500,500),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
#if any face is detected then process else continue searching
if(len(faces)!=0):
#If number of faces in the image is more than 1
#Then choose the one with maximum size
max_area=-1
i=0
for (x,y,w,h) in faces:
if w*h > max_area:
max_area=w*h
pos=i
i=i+1
RECT=faces[pos]
#Mark the face being tracked on the image display
cv2.rectangle(frame, (RECT[0], RECT[1]), (RECT[0]+RECT[2], RECT[1]+RECT[3]), (0, 255, 0), 2)
#draw_str(frame, (RECT[0], RECT[3]+16), 'x: %.2f y: %.2f size: %.2f' % (RECT[2]-RECT[0])/2 % (RECT[3]-RECT[1])/2 % RECT[2]*RECT[3])
#Put the text details about the ROI on imdisplay
cv2.putText(frame, `RECT[0] + RECT[2]/2`+' '+`RECT[1]+RECT[3]/2`+' '+`RECT[2]*RECT[3]`, (RECT[0],RECT[1]+RECT[3]), cv2.FONT_HERSHEY_SIMPLEX , 1, (0,0,255));
#compute direction for the arduino bot to be moved.
direction(RECT)
else:
print 'Search...'
arduino.write('<')
arduino.write(str(5))
arduino.write('>')
cv2.imshow('frame',frame)
cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
cap = cv2.VideoCapture(1)
cap.grab()
ret, frame = cap.retrieve()
cv2.namedWindow('frame')
#Run the tracker in infinite loop
while(1):
#grab the frames from web camera
ret, frame = cap.retrieve()
if ret ==0:
print "frame not loaded"
if ret==True:
#Resize the frame for faster computation
#cv2.resize(frame,(240,320))
#Process the frame and pass data to arduino
detectAndDisplay(frame)
#cv2.imshow('input',frame)
#press ESC to exit program
ch = cv2.waitKey(1)
if ch==27:
break
#Free up memory on exit
cap.release()
cv2.destroyAllWindows()
arduino.close()