-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.py
156 lines (120 loc) · 4.19 KB
/
follow.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
#!/usr/bin/python3.5
import numpy as np
import cv2
import wiringpi2 as wpi
import time
face = cv2.CascadeClassifier('/home/odroid/Desktop/bullseye_Stage0-15.xml')
cap = cv2.VideoCapture(0)
cx = 0
wpi.wiringPiSetup() #set up
wpi.pinMode(0,1)
wpi.digitalWrite(0,0)
##cap.set(cv2.CAP_PROP_FRAME_WIDTH, 325) #resolution changes
##cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 325)
serial = wpi.serialOpen('/dev/ttySAC0', 9600) #open serial port for UART
def moveCenter(x):
if x<250:
print("Left")
wpi.serialPuts(serial, "2") #Sends TX commands
time.sleep(0.1)
return 0
elif x>375:
print("Right")
wpi.serialPuts(serial, "1")
time.sleep(0.1)
return 0
else:
print("MID")
cx=0
wpi.serialPuts(serial, "D")
time.sleep(0.1)
return 1
def moveForward(P):
if P<700:
print("FORWARD")
wpi.serialPuts(serial, "3")
time.sleep(0.1)
return 0
else:
wpi.serialPuts(serial, "D")
time.sleep(0.1)
print("reached", P)
return 1
def runImage(): #this function follows a user for a set amount of time
#This is ran with the heel command
p_prev = 0
z=0
wpi.serialPuts(serial, "D")
wpi.serialPuts(serial, "D")
time.sleep(0.1)
flag = 0
while z != 2000:
a = 0
b=0
z = z+1
ret, img = cap.read()
wpi.digitalWrite(0,0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face.detectMultiScale(gray, 1.3, 1,minSize=(110,110))
faces = face.detectMultiScale(gray, 1.1, 1)
for (x,y,w,h) in faces:
flag = 1
p = x+y+w+h
cx = x+w//2
a = moveCenter(cx);
b = moveForward((p+p_prev)//2);
cy = y+h//2
cv2.rectangle(img, (x,y), (x+w, y+h), (178,255,34),5) #draws rectange around image, for debugging purposes. Comment out during ops
wpi.digitalWrite(0,1) #turn on LED
if a != 1: #function sets a = 1 when robot is centered
a = moveCenter(cx);
if a ==1: #when a = 1 robot moves forward based on size of p
if b == 0:
b = moveForward((p+p_prev)//2);
p_prev = p
if flag == 0:
wpi.serialPuts(serial, "D")
time.sleep(0.1)
else:
flag = 0
cv2.imshow('Cam',img) #comment out during operation
if cv2.waitKey(1) & 0xFF == ord('q'):
break
wpi.serialPuts(serial, "D") #send default value to UART
time.sleep(0.1)
wpi.digitalWrite(0,0) #turn off LED
cap.release() #destroy windows
cv2.destroyAllWindows()
def approach(): #like runImage funtion but it only approaches user. Might need to improve this part
z=0
b=0
while z != 10:
a = 0
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#faces = face.detectMultiScale(gray, 1.3, 1,minSize=(110,110))
faces = face.detectMultiScale(gray, 1.1, 1)
for (x,y,w,h) in faces:
cx = x+w//2
## cy = y+h//2
cv2.rectangle(img, (x,y), (x+w, y+h), (178,255,34),5)
p = x+y+w+h
print(p)
if a != 1:
a = moveCenter(cx);
if a ==1:
#print(p)
if b >=0:
b = b+moveForward(p);
##print(b)
if b == 3:
z = z+1
b = 0
#print("a:",a, " b:",b)
cv2.imshow('Cam',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
runImage()
#approach()