在另一个线程中使用全局访问OpenCV + Python框架?

问题描述 投票:0回答:1

谢谢你检查我的问题。 我在Python36 32bit中使用OpenCV,并试图使用线程模块来完成视频混合.我有两个imshow窗口显示两个网络摄像机的帧。我想让第三个窗口显示一个使用两个webcams的帧作为输入计算出来的帧,也就是说,我使用cv2.addWeighted来混合两个cams。

下面是做我想做的单线程代码:。之后是无法工作的多线程代码。 另外我要注意的是,如果只有一张图片,那是因为你把两个摄像头连接到同一个USB集线器上,我想这样是不行的。

import numpy as np
import cv2

video_capture_0 = cv2.VideoCapture(0)
video_capture_1 = cv2.VideoCapture(1)

while True:
    # Capture frame-by-frame
    ret0, frame0 = video_capture_0.read()
    ret1, frame1 = video_capture_1.read()

    if (ret0):
        # Display the resulting frame
        cv2.imshow('Cam 0', frame0)

    if (ret1):
        # Display the resulting frame
        cv2.imshow('Cam 1', frame1)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    dst = cv2.addWeighted(frame0, 0.5, cv2.bitwise_not(frame1), 0.5, 0.0)
    cv2.imshow("Mix",dst)

# When everything is done, release the capture
video_capture_0.release()
video_capture_1.release()
cv2.destroyAllWindows()

这就是我最初想让它工作的多线程代码。 cv2.imshow("Mix", handle1)这一行就是我的问题所在. 我只是在那个窗口中得到白色,而且那个线程挂起。 有什么线索表明我在这里做错了什么吗,或者有什么线索表明发生了什么? 另外,由于全局解释器锁适用于多线程进程,使用多进程来获得更高的性能是否合适,还是会太复杂,因为你需要进行进程间通信? 先谢谢你

#https://stackoverflow.com/questions/29664399/capturing-video-from-two-cameras-in-opencv-at-once
import cv2
import threading

class camThread(threading.Thread):
    def __init__(self, previewName, camID):
        threading.Thread.__init__(self)
        self.previewName = previewName
        self.camID = camID
    def run(self):
        print("Starting " + self.previewName)
        camPreview(self.previewName, self.camID)

def camPreview(previewName, camID):
    cv2.namedWindow(previewName)
    cam = cv2.VideoCapture(camID)
    if cam.isOpened():  # try to get the first frame
        rval, frame = cam.read()
    else:
        rval = False

    while rval:
        if camID == 0:
            global handle1
            handle1 = frame
            print("cam0")
        elif camID == 1:
            global handle2
            handle2 = frame
            print("cam1")

        cv2.imshow(previewName, frame)
        rval, frame = cam.read()
        key = cv2.waitKey(20)
        if key == 27:  # exit on ESC
            break
    cv2.destroyWindow(previewName)

class mixThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.previewName = "Mix"
        self.camID = 2
    def run(self):
        print("Starting " + self.previewName)
        mixPreview()
def mixPreview():
    global handle1
    global handle2
    count = 0
    while 1:

        try:
            global handle1
            global handle2
            ###dst = cv2.addWeighted(frame0, 0.5, cv2.bitwise_not(frame1), 0.5, 0.0)
            ###cv2.imshow("Mix",dst)
            cv2.imshow("Mix", handle1)
        except:
            count = count + 1
            try:
                print(dir(handle1))
                print(dir(handle2))
            except:
                if count % 1000 == 0:
                    print("didnt work")

# Create two threads as follows
thread1 = camThread("Camera 1", 0)
thread2 = camThread("Camera 2", 1)
thread1.start()
thread2.start()
print("thread1 and thread2 .start() called")
thread3 = mixThread()
thread3.start()
print("thread3 started")

python opencv python-multithreading
1个回答
0
投票

好吧,我想明白了。问题是如果你在调用cv2.imshow之后不调用cv2.waitKey,当使用opencv工作时,会导致白屏和挂起冻结的无响应窗口。 所以我想我已经把多线程视频混合的东西弄好了,现在看看能不能用window.imshow调用cv2.waitKey,当用opencv工作时,会导致白屏和挂起冻结的无响应窗口。现在看看能不能用wxpython滑块来控制混合调制参数。

#https://stackoverflow.com/questions/29664399/capturing-video-from-two-cameras-in-opencv-at-once
import cv2
import threading

class camThread(threading.Thread):
    def __init__(self, previewName, camID):
        threading.Thread.__init__(self)
        self.previewName = previewName
        self.camID = camID
    def run(self):
        print("Starting " + self.previewName)
        camPreview(self.previewName, self.camID)

def camPreview(previewName, camID):
    cv2.namedWindow(previewName)
    cam = cv2.VideoCapture(camID)
    if cam.isOpened():  # try to get the first frame
        rval, frame = cam.read()
    else:
        rval = False

    while rval:
        if camID == 0:
            global handle1
            handle1 = frame
            print("cam0")
        elif camID == 1:
            global handle2
            handle2 = frame
            print("cam1")

        cv2.imshow(previewName, frame)
        rval, frame = cam.read()
        key = cv2.waitKey(20)
        if key == 27:  # exit on ESC
            break
    cv2.destroyWindow(previewName)

class mixThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.previewName = "Mix"
        self.camID = 2
    def run(self):
        print("Starting " + self.previewName)
        mixPreview()
def mixPreview():
    global handle1
    global handle2
    count = 0
    while 1:

        try:
            global handle1
            global handle2
            dst = cv2.addWeighted(handle1, 0.5, cv2.bitwise_not(handle2), 0.5, 0.0)
            cv2.imshow("Mix",dst)
            key = cv2.waitKey(20)
        except:
            count = count + 1
            try:
                print(dir(handle1))
                print(dir(handle2))
            except:
                if count % 1000 == 0:
                    print("didnt work")

# Create two threads as follows
thread1 = camThread("Camera 1", 0)
thread2 = camThread("Camera 2", 1)
thread1.start()
thread2.start()
print("thread1 and thread2 .start() called")
thread3 = mixThread()
thread3.start()
print("thread3 started")
© www.soinside.com 2019 - 2024. All rights reserved.