问题描述 投票:0回答:3
在这里是相同的代码:

rgb
,我的问题是,如果我从代码中删除
cam_handler.RGB1_stream
线,一切都按预期运行,并且我将两个相机输出保存到文件中。但是,使用
cam_handler.DEPTH1_stream
线,只有“空白”窗口正在创建,并且线程似乎是“卡住”的,根本没有输出。
我尝试了几个建议,包括将
import threading def capture_and_save(cam_handle, cam_id, dir_to_write, log_writer, rgb_stream, depth_stream, io): t = threading.currentThread() shot_idx = 0 rgb_window = 'RGB' + str(cam_id) depth_window = 'DEPTH' + str(cam_id) while getattr(t, "do_run", True): if rgb_stream is not None: rgb_array = cam_handle.get_rgb(rgb_stream) rgb_array_disp = cv2.cvtColor(rgb_array, cv2.COLOR_BGR2RGB) cv2.imshow(rgb_window, rgb_array_disp) cam_handle.save_frame('rgb', rgb_array, shot_idx, dir_to_write + str(cam_id + 1)) io.write_log(log_writer[cam_id], shot_idx, None) if depth_stream is not None: depth_array = cam_handle.get_depth(depth_stream) depth_array_disp = ((depth_array / 10000.) * 255).astype(np.uint8) cv2.imshow(depth_window, np.uint8(depth_array_disp)) cam_handle.save_frame('depth', depth_array, shot_idx, dir_to_write + str(cam_id + 1)) shot_idx = shot_idx + 1 key = cv2.waitKey(1) if key == 27: # exit on ESC break print "Stopping camera %d thread..." % (cam_id + 1) return def main(): # Setup camera threads cam_threads = [] dir_to_write = "some/save/path" for cam in range(cam_count): cam = (cam + 1) % cam_count cv2.namedWindow('RGB' + str(cam)) cv2.namedWindow('DEPTH' + str(cam)) one_thread = threading.Thread(target=capture_and_save, name="CamThread" + str(cam + 1), args=(cam_cap, cam, dir_to_write, log_writer, rgb_stream[cam], depth_stream[cam], io,)) cam_threads.append(one_thread) one_thread.daemon = True one_thread.start() try: while True: pass # cv2.waitKey(1) except KeyboardInterrupt: # Stop everything for each_thread in cam_threads: each_thread.do_run = False each_thread.join(1) cam_cap.stop_rgb(rgb_stream) cam_cap.stop_depth(depth_stream) # Stop and quit openni2.unload() cv2.destroyAllWindows() if __name__ == '__main__': main()
创建移至主线程以及
cv2.imshow()

线程。我还尝试在

cv2.imshow()

周中移动,因为有人说OpenCV仅允许主线程中
namedWindow
。但是,没有区别。

I通过使用mutables,将字典
capture_and_save
传递到线程并读取主线程中的值来解决问题。
waitKey()
在将主线程放在主线程中时效果最好,因此效果很好。我不确定这是否是“正确”的方法,因此欢迎所有建议。
    
trone移动
waitKey()
在线程目标内移动
cam_disp = {}
cv2.imshow()

python multithreading opencv
3个回答
6
投票

功能不是线程安全的。
只需将
capture_and_save
移到调用

cv2.imshow

0
投票
cv2.namedWindow

threading.Thread

我不知道:),但这是我最好的解释。

0
投票

聚会迟到了,但以为我会分享我的解决方案:

由于cv2.imshow即使在主线程中不会很好地发挥作用,如上所述。答案,我认为我会为子过程带来更多运气,而且效果很好。
cv2.imshow
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.