【发布时间】:2020-02-13 09:11:02
【问题描述】:
我有两个线程类提取和检测。
Extract 从视频中提取帧并将其存储在文件夹中,Detect 从提取帧的文件夹中获取图像并检测对象。
但是当我运行下面的代码时,只有提取有效:
global q
q = Queue()
class extract(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print("T1")
cam = cv2.VideoCapture(video_name)
frameNum = 0
# isCaptured = True
frameCount = 0
while True:
isCapture, frame = cam.read()
if not isCapture:
break
if frameCount % 5 == 0:
frameNum = frameNum + 1
fileName = vid + str(frameNum) + '.jpg'
cv2.imwrite('images/extracted/' + fileName, frame)
q.put(fileName)
frameCount += 1
cam.release()
cv2.destroyAllWindows()
class detect(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print("T2")
#logic to detect objects.
if __name__ == '__main__':
thread1 = extract()
thread1.start()
thread2 = detect()
thread2.start()
这只会打印 T1 而不会打印 T2。 我想可能检测到先运行,队列是空的,所以什么也没发生,所以我在队列中添加了虚拟条目,它按照我想要的方式运行。
但它只针对虚拟条目运行,它不适用于提取函数添加到队列中的条目。 查找了其他问题,但似乎都没有解决问题,因此在此处发布此问题
【问题讨论】:
-
您对
detect的工作基本上是在extract完成工作之后开始的。那你为什么要它们并行运行 -
我正在进行实时检测,这就是为什么我需要它们一起运行
标签: python python-3.x multithreading python-multithreading