(**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:
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)