Как можно сделать так, чтобы видео воспроизводилось в другом потоке и её можно было заменять при необходимости по сигналу в реальном времени ?
import threading
import time
import logging
import queue as Queue
from MediaProvider import MediaProvider
from SerialPortProvider import SerialPortProvider
import cv2
import numpy as np
import asyncio
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-9s) %(message)s',)
BUF_SIZE = 10
q = Queue.Queue(BUF_SIZE)
class ProducerThread(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
super(ProducerThread,self).__init__()
self.target = target
self.name = name
def run(self):
while True:
if not q.full():
signal = SerialPortProvider.getSignalTest()
logging.info("Producer got message: %s", signal)
q.put(signal)
time.sleep(1)
return
class ConsumerThread(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
super(ConsumerThread,self).__init__()
self.target = target
self.name = name
return
async def videoPlayer(self, linkToVideo):
window_name = "window"
interframe_wait_ms = 30
cap = cv2.VideoCapture(linkToVideo)
if not cap.isOpened():
print("Error: Could not open video.")
exit()
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
while (True):
ret, frame = cap.read()
if ret:
cv2.imshow(window_name, frame)
else:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
if cv2.waitKey(interframe_wait_ms) & 0x7F == ord('q'):
print("Exit requested.")
break
cap.release()
cv2.destroyAllWindows()
def run(self):
while True:
if not q.empty():
item = q.get()
logging.info("Consumer storing message: %s (size=%d)", item, q.qsize())
self.videoPlayer(MediaProvider.getProccessingVideo())
time.sleep(1)
return
if __name__ == '__main__':
p = ProducerThread(name='producer')
c = ConsumerThread(name='consumer')
p.start()
time.sleep(2)
c.start()
time.sleep(2)