cam1.py
import cv2 from PARAM import * cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, Lx) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, Ly) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('cam1.avi', fourcc, 20.0, (Lx,Ly)) t=0 while(t < 501 and cap.isOpened()): ret, frame = cap.read() t+=1 if ret == True: out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows() |
PARAM.py
Lx=640; Ly=480; F=8; Lx2=int(Lx/F); Ly2=int(Ly/F) X1=int(Lx2/8); X2=X1*2; X3=X1*3; X4=X1*4; X5=X1*5; X6=X1*6; X7=X1*7 XX1=X1*F; XX2=X2*F XL=X2; XR=X6; |
cam3.py
from PARAM import * from motorm import turn import cv2 from RED1 import RED cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, Lx) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, Ly) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('cam3.avi', fourcc, 10.0, (Lx,Ly)) t=0 while(t < 1401 and cap.isOpened()): ret,frame = cap.read(); t+=1 if ret == True: DR,LRED,MIDL,img1,img2=RED(frame) print(t,DR); out.write(img1); cv2.imshow('img1',img1) if(DR==1): turn(0.4,0.4,0.05); turn(0,0,0.01); if(DR==-2): turn(-0.4,-0.4,0.05); turn(0,0,0.01); if(DR==3): turn(0.0,0.4,0.05); turn(0,0,0.01); if(DR==7): turn(0.4,0.0,0.05); turn(0,0,0.01); if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows() |
RED1.py
import numpy as np import cv2 from PARAM import * def RED(img): img1=np.copy(img) img2 = cv2.resize(img1, (Lx2,Ly2), interpolation=cv2.INTER_AREA) Y=Ly2-1; XM=int(Lx2/2); RED=[] for x in range(Lx2): if(img2[Y][x][2] > 255*0.4 and img2[Y][x][2] > 1.5*img2[Y][x][1] and img2[Y][x][2] > 1.5*img2[Y][x][0]): RED.append(x) LRED=len(RED) if(LRED > 0): MIDL=int(np.median(RED)) else: MIDL=0 img2[:,XR,:]=[0,255,255]; img1[:,F*XR-1:F*XR+2,:]=[0,255,255] img2[:,XL,:]=[0,255,255]; img1[:,F*XL-1:F*XL+2,:]=[0,255,255] if(LRED < 1): DR=-2 else: if(XL <= MIDL <= XR): DR=1 elif(MIDL < XL): DR=3 elif(MIDL > XR): DR=7 return DR,LRED,MIDL,img1,img2 |
motorm.py
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) def turn(LS,RS,t): EnaA=22 In1A=3 In2A=23 EnaB=17 In1B=22 In2B=27 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) PA = 2 PB = 3 PC = 15 MAP = 22 MAN = 23 MBP = 17 MBN = 27 pwmf= 100 dc = 20 PR = 18 PL = 21 GPIO.setup(PA, GPIO.IN) GPIO.setup(PB, GPIO.IN) GPIO.setup(PC, GPIO.IN) GPIO.setup(PR, GPIO.IN) GPIO.setup(PL, GPIO.IN) GPIO.setup(MAP, GPIO.OUT) GPIO.setup(MAN, GPIO.OUT) GPIO.setup(MBP, GPIO.OUT) GPIO.setup(MBN, GPIO.OUT) pwmap=GPIO.PWM(MAP, pwmf) pwman=GPIO.PWM(MAN, pwmf) pwmbp=GPIO.PWM(MBP, pwmf) pwmbn=GPIO.PWM(MBN, pwmf) pwmap.start(0) pwman.start(0) pwmbp.start(0) pwmbn.start(0) #TURN(LS,RS,t); LS < RS for a left turn LS *=100 RS *=100 if(LS > 0): pwmap.start(LS) pwman.start(0) else: pwmap.start(0) pwman.start(-LS) if(RS > 0): pwmbp.start(RS) pwmbn.start(0) else: pwmbp.start(0) pwmbn.start(-RS) sleep(t) |