Created
March 15, 2017 01:18
-
-
Save flyboy74/1baaa0205dcd04e857400466a15ea34e to your computer and use it in GitHub Desktop.
Line Follow using CV green sign dection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from picamera.array import PiRGBArray | |
from picamera import PiCamera | |
import time | |
import cv2 | |
import numpy as np | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(40, GPIO.OUT) | |
GPIO.output(40, GPIO.HIGH) | |
camera = PiCamera() | |
camera.resolution = (640, 360) | |
camera.rotation = 180 | |
rawCapture = PiRGBArray(camera, size=(640, 360)) | |
time.sleep(0.1) | |
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): | |
Greendected = False | |
image = frame.array | |
roi = image[200:250, 0:639] | |
Blackline = cv2.inRange(roi, (0,0,0), (50,50,50)) | |
Greensign = cv2.inRange(roi, (0,65,0), (100,200,100)) | |
kernel = np.ones((3,3), np.uint8) | |
Blackline = cv2.erode(Blackline, kernel, iterations=5) | |
Blackline = cv2.dilate(Blackline, kernel, iterations=9) | |
Greensign = cv2.erode(Greensign, kernel, iterations=5) | |
Greensign = cv2.dilate(Greensign, kernel, iterations=9) | |
img_blk,contours_blk, hierarchy_blk = cv2.findContours(Blackline.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) | |
img_grn,contours_grn, hierarchy_grn = cv2.findContours(Greensign.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) | |
if len(contours_grn) > 0 : | |
Greendected = True | |
x_grn, y_grn , w_grn, h_grn = cv2.boundingRect(contours_grn[0]) | |
centerx_grn = x_grn + (w_grn/2) | |
cv2.line(image, (centerx_grn, 200), (centerx_grn, 250),(0,0,255),3) | |
if len(contours_blk) > 0 : | |
x_blk, y_blk , w_blk, h_blk = cv2.boundingRect(contours_blk[0]) | |
centerx_blk = x_blk + (w_blk/2) | |
cv2.line(image, (centerx_blk, 200), (centerx_blk, 250),(255,0,0),3) | |
if Greendected : | |
if centerx_grn > centerx_blk : | |
cv2.putText(image, "Turn Right", (350,180), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,255,0),3) | |
else : | |
cv2.putText(image, "Turn Left", (50,180), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,255,0),3) | |
else : | |
setpoint = 320 | |
error = centerx_blk - setpoint | |
centertext = "Error = " + str(error) | |
cv2.putText(image, centertext, (200,340), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,0,0),3) | |
cv2.imshow("orginal with line", image) | |
rawCapture.truncate(0) | |
key = cv2.waitKey(1) & 0xFF | |
if key == ord("q"): | |
break | |
GPIO.output(40, GPIO.LOW) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment