'Threading for VideoCapture multi-camera in Python [duplicate]
Im using threading and opencv in Python.
I wanna display two windows at once.
But only one window can display at moment.
Here my code.
import cv2, time
class Core:
@staticmethod
def detection(ip):
capture = cv2.VideoCapture('rtsp://'+str(ip))
while (capture.isOpened()):
ret, frame = capture.read()
cv2.imshow('Video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
class Thread (threading.Thread):
def __init__(self, threadID,ip):
threading.Thread.__init__(self)
self.threadID = threadID
self.ip = ip
def run(self):
print("Start threadID" +str(self.threadID))
Core.detection(self.ip)
print("Exiting " + str(self.threadID))
threads = []
thread1 = Thread(1,'192.168.1.4:5554/playlist.m3u')
thread2 = Thread(2,'192.168.1.4:5554/playlist.m3u')
threads.append(thread1)
threads.append(thread2)
for i in threads:
i.start()
print("Exit to main thread")
I wonder if there were any solution to solve this problem.
Thanks you so much.
Solution 1:[1]
import threading
import cv2
class Thread (threading.Thread):
def __init__(self, threadID,ip):
threading.Thread.__init__(self)
self.threadID = threadID
self.ip = ip
def run(self):
print("Start threadID" +str(self.threadID))
Core.detection(self.ip)
print("Exiting " + str(self.threadID))
class Core:
@staticmethod
def detection(ip):
capture = cv2.VideoCapture(ip)
capture.set(cv2.CAP_PROP_BUFFERSIZE, 0)
while (capture.isOpened()):
ret, frame = capture.read()
if ret:
frame = cv2.resize(frame,(600,400))
cv2.imshow(ip,frame)
#cv2.waitKey(1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print('attempting to reconnect', ip)
# threads = []
# for i, cam in enumerate(ip):
# thread1 = Thread(i, cam)
# threads.append(thread1)
# for i in threads:
# i.start()
# thread1 = Thread(1, ip)
# thread1.start()
Core.detection(ip)
capture.release()
cv2.destroyAllWindows()
if __name__=='__main__':
cam_list=[
"rtsp://10.11.25.65:554/stream1","rtsp://10.11.25.66:554/stream1"
]
threads = []
for i, cam in enumerate(cam_list):
thread1 = Thread(i, cam)
threads.append(thread1)
for i in threads:
i.start()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
