name is not defined - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: name is not defined (/thread-33929.html) |
name is not defined - pygauss10 - Jun-10-2021 (**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() 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)
RE: name is not defined - Gribouillis - Jun-10-2021 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.
RE: name is not defined - Axel_Erfurt - Jun-10-2021 I have no problem with your code using mp4 video. There is only an error at the end of the movie.
RE: name is not defined - pygauss10 - Jun-10-2021 (Jun-10-2021, 04:46 PM)Gribouillis Wrote: If the body ofThanks, I just found that ret is false, that's another problem RE: name is not defined - pygauss10 - Jun-10-2021 (Jun-10-2021, 04:50 PM)Axel_Erfurt Wrote: I have no problem with your code using mp4 video.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? RE: name is not defined - Axel_Erfurt - Jun-10-2021 I have
RE: name is not defined - pygauss10 - Jun-10-2021 (Jun-10-2021, 05:25 PM)Axel_Erfurt Wrote: I haveI just found that there is something wrong with my path, and I get the same result as yours, but I appreciate your help. |