Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name is not defined
#1
Video 
(**English is not my native language; please excuse typing errors.)
I am running this opencv code with pycharm, but the weird thing is that it shows the specific name is not defined:
import numpy as np
import cv2
import matplotlib.pyplot as plt

    

file = 'S9000.avi'
cap = cv2.VideoCapture(file)



while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
                
        img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)        
        thr, img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)

        
        w = img.shape[1] 
        h = img.shape[0]        

        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        

        gray_color = (100, 100, 100)
        
        img = cv2.line(img, (int(w/2), 0),(int(w/2), h), gray_color, 2, 1)  
        img = cv2.line(img, (0, int(h/2)),(w, int(h/2)), gray_color, 2, 1)

        

        upper_bound = np.argmin(abs(img.mean(1)-253)[:np.argmin(img.mean(1))+1])       
        lower_bound = np.argmin(abs(img.mean(1)-253)[upper_bound+5:])+upper_bound+5    
        side_bound = np.argmin(abs(img.mean(0)-253))       

         
        red_color = (0, 0, 255)     
        cv2.rectangle(img, (0, upper_bound), (side_bound, lower_bound), red_color, 2, cv2.LINE_AA)

        y_error = (upper_bound + lower_bound - h)/2   
        x_error = side_bound - w/2 

        
        cv2.namedWindow('video', cv2.WINDOW_NORMAL)
        cv2.imshow('video', img)
        cv2.waitKey(1)
        
    else:
        break
cap.release()
cv2.destroyAllWindows()

plt.plot(y_error,color='r')
plt.show()
Error:
Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users/elatm/Desktop/video_Deflection_show.py", line 54, in <module> plt.plot(y_error,color='r') NameError: name 'y_error' is not defined
I don't quite understand why y_error is not defined since it was clearly written in the code, and I try but fail to find out what is wrong.(my python version is 3.8, pycharm version 2020.2)
Reply
#2
If the body of if ret == True is not executed, the variable y_error is never defined. By the way, if ret: would be more colloquial.
Reply
#3
I have no problem with your code using mp4 video.

There is only an error at the end of the movie.

Output:
ValueError: attempt to get argmin of an empty sequence
Reply
#4
(Jun-10-2021, 04:46 PM)Gribouillis Wrote: If the body of if ret == True is not executed, the variable y_error is never defined. By the way, if ret: would be more colloquial.
Thanks, I just found that ret is false, that's another problem
Reply
#5
(Jun-10-2021, 04:50 PM)Axel_Erfurt Wrote: I have no problem with your code using mp4 video.

There is only an error at the end of the movie.

Output:
ValueError: attempt to get argmin of an empty sequence
I got the same result on the other computer, but then I can't re-run for the same result, y_error is always undefined, can you share your python/pycharm version?
Reply
#6
I have

Output:
Python 3.8.5 numpy 1.17.4 matplotlib 3.1.2 cv2 4.5.1
Reply
#7
(Jun-10-2021, 05:25 PM)Axel_Erfurt Wrote: I have

Output:
Python 3.8.5 numpy 1.17.4 matplotlib 3.1.2 cv2 4.5.1
I just found that there is something wrong with my path, and I get the same result as yours, but I appreciate your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 987 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020