-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectColorV3.py
111 lines (73 loc) · 2.15 KB
/
detectColorV3.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
import cv2
import numpy as np
import time
import GUImove as move
import servo
import RPi.GPIO as GPIO
import Adafruit_PCA9685
line_pin_right = 19
line_pin_middle = 16
line_pin_left = 20
angle_rate=0.8
speed = 70
servo=1
pwm=Adafruit_PCA9685.PCA9685()
pwm.set_pwm_freq(50)
def setup():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(line_pin_right,GPIO.IN)
GPIO.setup(line_pin_middle,GPIO.IN)
GPIO.setup(line_pin_left,GPIO.IN)
#motor.setup()
def contient_vert(image):
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_green = (36, 25, 25)
upper_green = (86, 255, 255)
green_mask = cv2.inRange(hsv_image, lower_green, upper_green)
has_green = cv2.countNonZero(green_mask) > 0
return 1 if has_green else 0
def contient_rouge(image):
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_red1 = (0, 70, 50)
upper_red1 = (10, 255, 255)
lower_red2 = (170, 70, 50)
upper_red2 = (180, 255, 255)
red_mask1 = cv2.inRange(hsv_image, lower_red1, upper_red1)
red_mask2 = cv2.inRange(hsv_image, lower_red2, upper_red2)
red_mask = cv2.bitwise_or(red_mask1, red_mask2)
has_red = cv2.countNonZero(red_mask) > 0
return 1 if has_red else 0
def fleche() :
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("1")
exit()
while True:
ret, frame = cap.read()
if not ret:
break;
image = frame
if contient_rouge(image)==1 :
print("rouge")
return 0
if contient_vert(image)==1 :
print("vert")
return 1
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
try:
setup()
move.setup()
a=0
pwm.set_pwm(servo, 0, 180)
while a==0:
a=fleche()
time.sleep(1)
move.move(speed, 'forward')
time.sleep(1)
pass
except KeyboardInterrupt:
move.destroy()