【发布时间】:2023-04-06 12:00:01
【问题描述】:
我正在尝试执行具有如下结构的代码:
import things
...
class MyThreadRead(Thread):
...
def run(self):
global cap
global frame_resized
global netMain
...
ret, frame = cap.read()
frame_resized = cv2.resize(...)
...
...
def YOLO():
...
global frame_resized
global cap
...
cap = cv2.VideoCapture(...)
...
while True:
...
readFrameThread.start()
detections = detect(a, b, frame_resized, c)
...
readFrameThread.join()
...
...
if __name__== "__main__":
readFrameThread = MyThreadRead(1)
YOLO()
当我执行此脚本时,我在 YOLO 函数内的函数检测行中收到此错误:
NameError: global name ´frame_resized´ is not defined
我应该在哪里声明全局变量?在 YOLO 函数内部还是外部?
【问题讨论】:
-
你根本不应该使用全局变量。
-
您可以将它包装在一个单格子类中,并具有属性的线程保存访问权限
标签: python python-2.7 global-variables python-multithreading