forked from LittleSamakFox/RCcar_Rpi_LineTracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineTracker.py
77 lines (66 loc) · 2.09 KB
/
lineTracker.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
import numpy as np
import cv2
import serial
import time
ser = serial.Serial('/dev/serial/by-id/usb-Arduino_Srl_Arduino_Uno_754393137373514170C0-if00',9600)
if ser is None:
print("disconnected with arduino")
#camera setting
camera_pixel_x = 160
camera_pixel_y = 120
video_capture = cv2.VideoCapture(-1)
video_capture.set(3, camera_pixel_x)
video_capture.set(4, camera_pixel_y)
#line coordinate factor
gap = 1/10
right_g = 1/2 + gap
left_g = 1/2 - gap
#stop time factor
stop_count = 0
while(True):
# Capture the frames
ret, frame = video_capture.read()
# Crop the image
crop_img = frame[int(camera_pixel_y/2):camera_pixel_y, 0:camera_pixel_x]
# Image Transformation for Recognition of black line
# convert to grayscale, gaussian blur, and threshold
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ret,thresh = cv2.threshold(blur,35,255,cv2.THRESH_BINARY_INV)
# Erode to eliminate noise, Dilate to restore eroded parts of image
mask = cv2.erode(thresh, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
#contours insert
contours,hierarchy = cv2.findContours(mask.copy(), 1, cv2.CHAIN_APPROX_NONE)
#line detection
if len(contours) > 0:
c = max(contours, key=cv2.contourArea)
M = cv2.moments(c)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.line(crop_img,(cx,0),(cx,720),(255,0,0),1)
cv2.line(crop_img,(0,cy),(1280,cy),(255,0,0),1)
cv2.drawContours(crop_img, contours, -1, (0,255,0), 1)
print(cx, cy)
#Line recognition coordinates identify left, straight and right turns
if cx >= camera_pixel_x*(right_g):
print("R")
cmd = ("R").encode('ascii')
if cx < camera_pixel_x*(right_g) and cx > camera_pixel_x*(left_g):
print("S")
cmd = ("S").encode('ascii')
if cx <= camera_pixel_x*(left_g):
print("L")
cmd = ("L").encode('ascii')
ser.write(cmd)
else:
print("I don't see the line")
stop_count +=1
if stop_count > 20:
cmd = ("D").encode('ascii')
ser.write(cmd)
stop_count = 0
#Display the resulting frame
cv2.imshow('frame',crop_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break