-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLine Follower.py
169 lines (136 loc) · 5.16 KB
/
Line Follower.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
import cv2
import math
import numpy as np
# Create Video Object to capture frames
Video = cv2.VideoCapture(0)
flag = 0
# Split the Frame into 3 Vertical Regions
R1 = 0
R2 = 0
R3 = 0
# Split the Frame into 3 Horizontal Regions
R4 = 0
R5 = 0
R6 = 0
# Dummy Number for amount of red in each Region if > red => then this Region has a red line
red = 49000
current_dir = 'right'
# Check current state and amount of red in each Region to Change Direction
def change_dirr (dir,R1,R2,R3):
if R2 >= red and R1 < red and R3 < red:
return dir
if dir == 'down' or dir == 'up':
if R1 > red and R2 > red and R3 < red:
return 'left'
elif R1 < red and R2 > red and R3 > red:
return 'right'
elif dir == 'left' or dir == 'right':
if R1 > red and R2 > red and R3 < red:
return 'up'
elif R1 < red and R2 > red and R3 > red:
return 'down'
return dir
while True:
# img is the frame from Video
_, img = Video.read()
# img = cv2.imread('s2.jpg',cv2.IMREAD_COLOR)
# =======to draw the rectangles only=====
imgVeritcal = img
imgHorizontal = img
# =======================================
# detect red lines
# =======================================
# cvtColor convert the img from BGR Color system to HSV System (easier to detect color)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Range of Red Color to detect Red color
# ============ Camera Laptop Helaly =============
# min = np.array([0, 40, 40])
# max = np.array([100, 150, 150])
# ============ Camera Tape3ia =============
# min = np.array([0, 100, 50])
# max = np.array([20, 240, 240])
min = np.array([150, 40, 40])
max = np.array([180, 255, 255])
# make a mask(picture of just white and black color ... the white is the color we detect) to detect the red Color
mask = cv2.inRange(hsv, min, max)
# dimintions of img
height, width = img.shape[:2]
# res = frame + mask == image with just red color (color we detect in mask)
res1 = cv2.bitwise_and(img, img, mask=mask)
res2 = res1
# Clean Noise of Picture .. comment it if you want
kernal = np.ones((5, 5), np.uint8)
res1 = cv2.morphologyEx(res1, cv2.MORPH_CLOSE, kernal)
# ===============Vertical Reigons======================
roi1 = res1[0:height, 0:width // 3]
roi2 = res1[0:height, width // 3: 2 * width // 3]
roi3 = res1[0:height, 2 * width // 3:width]
roi1 = cv2.cvtColor(roi1, cv2.COLOR_HSV2BGR)
roi2 = cv2.cvtColor(roi2, cv2.COLOR_HSV2BGR)
roi3 = cv2.cvtColor(roi3, cv2.COLOR_HSV2BGR)
# =====================================================
# ===============Horizontal Reigons======================
roi4 = res1[0:height // 3, 0:width]
roi5 = res1[height // 3 :2 * height // 3, 0: width]
roi6 = res1[2 * height // 3:height,0:width]
roi4 = cv2.cvtColor(roi4, cv2.COLOR_HSV2BGR)
roi5 = cv2.cvtColor(roi5, cv2.COLOR_HSV2BGR)
roi6 = cv2.cvtColor(roi6, cv2.COLOR_HSV2BGR)
# =====================================================
# Get the dimensions of the Reigon!
roi1_h, roi1_w = roi1.shape[:2]
roi4_h, roi4_w = roi4.shape[:2]
# if key is pressed .. store it in w
w = cv2.waitKey(3) & 0xFF
if w == ord('a'):
flag = 1
for i in range(roi1_h - 1):
for j in range(roi1_w - 1):
k = roi1[i, j]
z = roi2[i, j]
q = roi3[i, j]
R1 += k[2] + k[1] + k[0]
R2 += z[2] + z[1] + z[0]
R3 += q[2] + q[1] + q[0]
current_dir = change_dirr(current_dir,R1,R2,R3)
print(current_dir)
# print (R1)
# print (R2)
# print (R3)
if w == ord('h'):
flag = 2
for i in range(roi4_h - 1):
for j in range(roi4_w - 1):
k1 = roi4[i, j]
z1 = roi5[i, j]
q1 = roi6[i, j]
R4 += k1[2] + k1[1] + k1[0]
R5+= z1[2] + z1[1] + z1[0]
R6 += q1[2] + q1[1] + q1[0]
current_dir = change_dirr(current_dir,R4,R5,R6)
print(current_dir)
# print (R4)
# print (R5)
# print (R6)
if (flag == 0) | (flag == 1):
cv2.rectangle(res1, (0, 0), (width // 3, height), (0, 255, 0), 1)
cv2.rectangle(res1, (width // 3, 0), (2 * width // 3, height), (0, 255, 0), 1)
cv2.rectangle(res1, (2 * width // 3, 0), (width, height), (0, 255, 0), 1)
cv2.imshow("img", res1)
if (flag == 2):
cv2.rectangle(res2, (0, 0), (width, height // 3), (0, 255, 0), 1)
cv2.rectangle(res2, (0, height // 3), (width, 2 * height // 3), (0, 255, 0), 1)
cv2.rectangle(res2, (0, 2 * height // 3), (width, height), (0, 255, 0), 1)
cv2.imshow("img", res2)
R1 = 0
R2 = 0
R3 = 0
R4 = 0
R5 = 0
R6 = 0
# cv2.imshow("H1", roi4)
# cv2.imshow("H2", roi5)
# cv2.imshow("H3", roi6)
if (w == ord('q')):
break
cv2.destroyAllWindows()