自駕車跟隨




DIVFL.tar

1. download DIVFL.tar onto your Raspberry Pi.
2. use tar xvf DIVFL.tar to unpack all files in a folder named as DIVFL.
3. cd DIVFL; ls you will find all files for this project.

▶自駕車跟隨(影片1)

▶自駕車跟隨(影片2)

  1. 自駕車跟隨
  2. DIVFL.py 跟隨主程式
    import numpy as np
    import cv2
    from PARAM import *
    from DIV2 import DIV
    from motorm import turn
    from Fcolor import Fcolor
    from time import time
    from time import sleep
    
    XC=int(Lx/2); YC=int(Ly/2)
    
    acc,r1,r2=Fcolor()
    
    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')
    frate=1/0.15
    out = cv2.VideoWriter('DIVFL.avi', fourcc, frate, (Lx,Ly))
    FW=open('DIVFL.txt','w')
    t=0; tm=0.
    while(tm < 120 and cap.isOpened()):
    #while(t < 10 and cap.isOpened()):
        ret,frame = cap.read(); t+=1;
        if(t==1): t0=round(time(),2); t2=t0;
        t1=round(time(),2); tm=round(t1-t0,2); dt=round(t1-t2,2); t2=t1
        if ret == True:
            out.write(frame)
            #cv2.imshow('frame',frame)
            img1,img2,NB1,BXY=DIV(r1,r2,frame)
            a=np.arange(3)
            if(NB1==0): DR=-1;X0=0;Y0=0; X1=XC; Y1=YC
            else:
                A=np.array(BXY); XA=A[:,0]; YA=A[:,1]
                X0=round(np.average(XA)+0.5,2); Y0=round(np.average(YA)+0.5,2)
                X1=int(X0*F*10); Y1=int(Y0*F*10)
                if(X0 < 2): DR=3
                elif(X0 > 6): DR=7
                else: DR=1
                if(NB1>12): DR=0
                img3=frame[Y1:Y1+10,X1:X1+10,:]
                for n in range(3):
                    a[n]=int(np.average(img3[:,:,n:n+1]))
            cv2.circle(img1,(X1,Y1), 10, (0, 255, 255), 2)
            img3=cv2.resize(frame, (XC,YC), interpolation=cv2.INTER_AREA)
            img4=cv2.resize(img1, (XC,YC), interpolation=cv2.INTER_AREA)
            #cv2.imshow('img2', img2)
            cv2.imshow('img3', img3)
            cv2.imshow('img4', img4)
            if(t==1):
                cv2.waitKey(0);t0=round(time(),2); t2=t0;
                t1=round(time(),2); tm=round(t1-t0,2); dt=round(t1-t2,2); t2=t1
            #print(t,tm,DR,NB1,X0,Y0,a)
            st=('%4d %6.2f %4d %4d %3d %3d %4d %4d %4d' \
                 %(t,tm,DR,NB1,X0,Y0,a[0],a[1],a[2]))
            print(st)
            if cv2.waitKey(1) & 0xFF == ord('q'): break
            #DR=0
            if(DR==1): turn(0.3,0.3,0.05); turn(0,0,0.01);
            if(DR==0): turn(0.,0.,0.05); turn(0,0,0.01);
            if(DR==-1): turn(-0.4,0.4,0.05); turn(0,0,0.05);
            if(DR==-2): turn(-0.3,-0.3,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.2,0.05); turn(0,0,0.01);
        else: break
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    


    DIV2.py 窗格分析
    import numpy as np
    import cv2
    from PARAM import *
    def DIV(r1,r2,img):
        img1=np.copy(img)
        img2=cv2.resize(img1, (Lx2,Ly2), interpolation=cv2.INTER_AREA)
        SS=0; cen=[]; NCC=[]; NCD=[]; NK=0
        D=10; NX=int(Lx2/D); NY=int(Ly2/D)
        NB1=0; NB=0; BXY=[]
        TH0=0.73; TH1=0.21; fl=0.5; fh=1.5
        TH0=r1; TH1=r2
        L0=fl*TH0; H0=fh*TH0; L1=fl*TH1; H1=fh*TH1
        Tc=0
        for i in range(NX):
            for j in range(NY):
                X1=D*i;X2=D*(i+1)
                Y1=D*j;Y2=D*(j+1)
                cv2.rectangle(img2,(X1,Y1),(X2,Y2),(0, 255, 0), 1)
                cv2.rectangle(img1,(X1*F,Y1*F),(X2*F,Y2*F),(255,0,0), 2)
                cv2.rectangle(img,(X1*F,Y1*F),(X2*F,Y2*F),(255,0,0), 1)
                img3=img2[Y1:Y2,X1:X2,:]
                blue=0
                for I in range(D):
                    for J in range(D):
                        #if(i==6 and j==3): print(I,J,img3[I][J])
                        c0=float(img3[I][J][0]);
                        c1=float(img3[I][J][1]);
                        c2=float(img3[I][J][2]);
                        Tc=c0+c1+c2
                        if(Tc>200*3): continue
                        if(Tc<100): continue
                        if(c0>1 and L0


    Fcolor.py 取得標的物的色碼
    import numpy as np
    import cv2
    from PARAM import *
    
    def Fcolor():
        cap = cv2.VideoCapture(0)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, Lx)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, Ly)
        XC=int(Lx/2); YC=int(Ly/2)
        t=0
        while t < 4:
            ret,frame = cap.read(); t+=1
            if(ret==True):
                img3=frame[YC:YC+10,XC:XC+10,:]
                a=np.arange(3)
                for n in range(3): a[n]=int(np.average(img3[:,:,n:n+1]))
                r1=round(float(a[1])/float(a[0]),2);
                r2=round(float(a[2])/float(a[0]),2);
                print(t,'focus:a=',a,r1,r2)
                cv2.circle(frame,(XC,YC), 10, (0, 155, 255), 2)
                cv2.imshow('frame',frame)
                cv2.waitKey(0)
        cv2.destroyAllWindows()
        cap.release()
        return a,r1,r2