Zebra crossing Python Opencv project help.. I'm getting the following error

Viewed 134

I'm trying to build a project using opencv to detect crosswalk and provide details to navigate it's a project already done and the link to it is https://www.youtube.com/watch?v=b-BnqgAI5NY

cap = cv2.VideoCapture('Friday the 13th_ The Game_20170527134320.mp4') #load a video

W = cap.get(3) #get width
H = cap.get(4) #get height

#Define a new resolution 
ratio = H/W
W = 800
H = int(W * ratio)

#setup the parameters for saving the processed file
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('processedVideo.mp4',fourcc, 15.0, (int(W),int(H)))

Dx = []
Dy = []
after =0
DxAve =0
Dxold =0
DyAve =0
Dyold =0
i = 0
state = ""

while(cap.isOpened()):
    ret, frame = cap.read()
    img = scipy.misc.imresize(frame, (H,W))

    #np.array(Image.fromarray(frame,(H,W)).resize())
    #draw camera's POV
    cv2.circle(img,(int(W/2),int(H/2)),5,(0,0,255),8)

here in the try block I have defined processedFrame properly but in the end it says not defined

    try:
        processedFrame,dx,dy = process(img)

        if (i < 6):
            Dx.append(dx)
            Dy.append(dy)
            i=i+1

    else:
        DxAve = sum(Dx)/len(Dx)
        DyAve = sum(Dy)/len(Dy)
        del Dx[:]
        del Dy[:]
        i=0

    if (DyAve > 30) and (abs(DxAve) < 300):        
        #check if the vanishing point and the next vanishing point aren't too far from each other 
        if (((DxAve - Dxold)**2 + (DyAve - Dyold)**2) < 150**2) == True:  ##distance 150 px max 
            cv2.line(img,(int(W/2),int(H/2)),(int(W/2)+int(DxAve),int(H/2)+int(DyAve)),(0,0,255),7)
            
            #walking directions
            if abs(DxAve) < 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                state = 'Straight'
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,0),2,cv2.LINE_AA)

            elif DxAve > 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                state = 'Right'
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)

            elif DxAve < 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                state = 'Left'
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)
        else:
            cv2.line(img,(int(W/2),int(H/2)),(int(W/2)+int(Dxold),int(H/2)+int(Dyold)),(0,0,255),)
        
        #walking directions 
        if state == 'Straight':
            cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,0),2,cv2.LINE_AA)
        else:
            cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)           

        Dxold = DxAve
        Dyold = DyAve

except:
    print('Failed to process frame')

#show & save
img = cv2.imshow('Processed',processedFrame)
out.write(processedFrame)

if cv2.waitKey(1) & 0xFF == ord('q') or cv2.waitKey(1) & 0xFF == ord('Q'):
    break

out.release()
cap.release()
cv2.destroyAllWindows()

I'm getting the following error:

Failed to process frame
Traceback (most recent call last):
  File "C:\Users\Ragha\AppData\Local\Programs\Python\Python36\crosswalk.py", line 280, in <module>
    img = cv2.imshow('Processed',processedFrame)
NameError: name 'processedFrame' is not defined
2 Answers

put the code

img = cv2.imshow('Processed',processedFrame)
out.write(processedFrame)

inside the try block

and always take care of the indentations in python.

cap = cv2.VideoCapture('Friday the 13th_ The Game_20170527134320.mp4') #load a video
W = cap.get(3) #get width
H = cap.get(4) #get height

#Define a new resolution 
ratio = H/W
W = 800
H = int(W * ratio)

#setup the parameters for saving the processed file
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('processedVideo.mp4',fourcc, 15.0, (int(W),int(H)))

Dx = []
Dy = []
after =0
DxAve =0
Dxold =0
DyAve =0
Dyold =0
i = 0
state = ""

while(cap.isOpened()):
    ret, frame = cap.read()
    img = scipy.misc.imresize(frame, (H,W))

    #np.array(Image.fromarray(frame,(H,W)).resize())
    #draw camera's POV
    cv2.circle(img,(int(W/2),int(H/2)),5,(0,0,255),8)
    try:
        processedFrame,dx,dy = process(img)

        if (i < 6):
            Dx.append(dx)
            Dy.append(dy)
            i=i+1
        else:
            DxAve = sum(Dx)/len(Dx)
            DyAve = sum(Dy)/len(Dy)
            del Dx[:]
            del Dy[:]
            i=0

        if (DyAve > 30) and (abs(DxAve) < 300):        
            #check if the vanishing point and the next vanishing point aren't too far from each other 
            if (((DxAve - Dxold)**2 + (DyAve - Dyold)**2) < 150**2) == True:  ##distance 150 px max 
                cv2.line(img,(int(W/2),int(H/2)),(int(W/2)+int(DxAve),int(H/2)+int(DyAve)),(0,0,255),7)
                
                #walking directions
                if abs(DxAve) < 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                    state = 'Straight'
                    cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,0),2,cv2.LINE_AA)

                elif DxAve > 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                    state = 'Right'
                    cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)

                elif DxAve < 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                    state = 'Left'
                    cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)
                else:
                    cv2.line(img,(int(W/2),int(H/2)),(int(W/2)+int(Dxold),int(H/2)+int(Dyold)),(0,0,255),)
            
            #walking directions 
            if state == 'Straight':
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,0),2,cv2.LINE_AA)
            else:
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)           

            Dxold = DxAve
            Dyold = DyAve

            img = cv2.imshow('Processed',processedFrame)
            out.write(processedFrame)

    except:
        print('Failed to process frame')

    if cv2.waitKey(1) & 0xFF == ord('q') or cv2.waitKey(1) & 0xFF == ord('Q'):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

processedFrame is assigned in the try block, but that assignment may not be made if process() raises an exception.

You need to assign processedFrame before the try block if you want to make sure it's assigned to some value outside of the try/except:

# Ellipses denote that the code used is the same as in the original question.
# They have been removed for brevity.
processedFrame = None
try:
    processedFrame,dx,dy = process(img)
    # ...
except:
    print('Failed to process frame')

# This should no longer produce an error.
img = cv2.imshow('Processed',processedFrame)
out.write(processedFrame)
# ...
Related