-
Notifications
You must be signed in to change notification settings - Fork 0
/
_bfr1.py
111 lines (86 loc) · 2.39 KB
/
_bfr1.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
from collections import deque
import numpy as np
import argparse
import imutils
import cv2
import time
import wiringpi as wp
wp.wiringPiSetupGpio()
def Motor(a,b,pwm):
wp.pinMode(a,1)
wp.pinMode(b,1)
wp.pinMode(pwm,1)
wp.softPwmCreate(pwm,0,400)
return a,b,pwm
def dir1(motor,speed):
(a,b,pwm)=motor
wp.digitalWrite(a,1)
wp.digitalWrite(b,0)
wp.softPwmWrite(pwm,speed)
def dir2(motor,speed):
(a,b,pwm)=motor
wp.digitalWrite(a,0)
wp.digitalWrite(b,1)
wp.softPwmWrite(pwm,speed)
def forward():
motor1=Motor(20,16,21)
motor2=Motor(23,24,25)
dir1(motor1,90)
dir1(motor2,100)
def right():
motor1=Motor(20,16,21)
motor2=Motor(23,24,25)
dir1(motor2,100)
dir2(motor1,90)
def left():
motor1=Motor(20,16,21)
motor2=Motor(23,24,25)
dir1(motor1,90)
dir2(motor2,100)
def stop():
motor1=Motor(20,16,21)
motor2=Motor(23,24,25)
dir1(motor1,0)
dir2(motor2,0)
greenLower = (0, 130, 41)
greenUpper = (59, 255, 255)
camera = cv2.VideoCapture(0)
ret=camera.set(3,160);
ret=camera.set(4,120);
while True:
(grabbed, frame) = camera.read()
frame = imutils.resize(frame, width=320, height=240)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if radius >10:
cv2.circle(frame, (int(x), int(y)), int(radius),(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
a=int(x)
b=int(y)
r=int(radius)
print "x=",a, "y=",b, "z=",r
if x>140 and x<200:
forward()
elif x<140:
left()
elif x>200:
right()
else:
stop()
print("!!!!!!!!!!!")
# cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()