forked from Anmar1985/Human_Robots_Interaction_Fall15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weebo_project
155 lines (131 loc) · 4.48 KB
/
Weebo_project
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
# GNU nano 2.2.6 File: facedetect_edit.py
#!/usr/bin/env python
import numpy as np
import cv2
import ctypes
import api
import os
import time
import sys
import struct
# local modules
from video import create_capture
from common import clock, draw_str
help_message = '''
USAGE: facedetect_edit.py [--cascade <cascade_fn>] [--nested-cascade <cascade_fn>] [<video_source>]
'''
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE)
if len(rects) == 0:
return []
rects[:,2:] += rects[:,:2]
return rects
def draw_rects(img, rects, color):
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
def Run(X,Y):
global Socket
#api.Walk(True)
print("Running...")
#packet = Socket.recv(8)
print("Packet recieved")
#command = struct.unpack('B', packet[1])[0]
command = 0
z=20
# declerating servos limmits
#headRightLim=667
#headLeftLim=364
#headUpLim=612
#headDownLim=433
headRightLim=600
headLeftLim=420
headUpLim=612
headDownLim=433
pan = api.GetMotorValue(19)
tilt= api.GetMotorValue(20)
# check when to call the walk functions
if ((pan<headRightLim) and (pan>headLeftLim)):
print "head move"
elif (pan>headRightLim):
# call walk to the left till you reach pan = 512 (need while loop)
# while (pan != 512):
print "walk to the left"
elif (pan<headLeftLim):
# call walk to the right till you reach pan = 512 (need while loop)
# while (pan != 512):
print "walk to the left"
if (X>50) and (667 > pan):
for i in range(1,z):
api.SetMotorValue(19,pan+5)
elif (X<-50) and (364 < pan):
for i in range(1,abs(z)):
api.SetMotorValue(19,pan-5)
if (Y>50) and ( 612 > tilt):
for i in range(1,z):
api.SetMotorValue(10,tilt+5)
elif (Y<-50) and (433 < tilt):
for i in range(1,abs(z)):
api.SetMotorValue(20,tilt-5)
print("pan",pan)
print("tilt",tilt)
# api.ServoShutdown()
#Run()
if __name__ == '__main__':
import sys, getopt
print help_message
try:
if api.Initialize():
print("Initalized")
else:
print("Intialization failed")
except (KeyboardInterrupt):
api.ServoShutdown()
sys.exit()
except():
api.ServoShutdown()
sys.exit()
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
try:
video_src = video_src[0]
except:
video_src = 0
args = dict(args)
cascade_fn = args.get('--cascade', "../../../../../opencv/data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../../../../opencv/data/haarcascades/haarcascade_eye.xml")
cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_fn)
cam = create_capture(video_src, fallback='synth:bg=../../../../../opencv/data/lena.jpg:noise=0.05')
try:
while True:
#break
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
t = clock()
dt = clock() - t
rects = detect(gray, cascade)
vis = img.copy()
draw_rects(vis, rects, (0, 255, 0))
if not nested.empty():
for x1, y1, x2, y2 in rects:
roi = gray[y1:y2, x1:x2]
vis_roi = vis[y1:y2, x1:x2]
subrects = detect(roi.copy(), nested)
#draw_rects(vis_roi, subrects, (255, 0, 0))
print ('time: %.1f ms' % (dt*1000))
print ('Length: %.1f cm' % (y2-y1))
x_center=(320-(x2+x1)/2)
y_center=(240-(y2+y1)/2)
Run(x_center,y_center)
print (x_center,y_center)
if 0xFF & cv2.waitKey(5) == 27:
break
cv2.destroyAllWindows()
except (KeyboardInterrupt):
print
print ("ID 20 :", api.GetMotorValue(20))
api.ServoShutdown()
sys.exit()
# this steps to read servo's value-- add break to the maim while then uncomment the next two lines
#print (api.GetMotorValue(20))
#api.ServoShutdown()