import timefrom serial import Serialimport serial.tools.list_portsimport cv2import numpy as npcap1 = cv2.VideoCapture("/Users/yankaipan/Desktop/stand.mp4")cap2 = cv2.VideoCapture("/Users/yankaipan/Desktop/apple.mp4")cap3 = cv2.VideoCapture("/Users/yankaipan/Desktop/bamboo.mp4")cap4 = cv2.VideoCapture("/Users/yankaipan/Desktop/rubbish.mp4")port_list = list(serial.tools.list_ports.comports())port_list_1 = list(port_list[2])port_serial = port_list_1[0]arduinoData = serial.Serial(port_serial, 9600)time.sleep(1)while True: while (arduinoData.inWaiting() == 0): pass dataPacket = arduinoData.readline() dataPacket = dataPacket.decode().strip() print(dataPacket) time.sleep(3) if dataPacket == "a": while cap1.isOpened(): ret, frame = cap1.read() if ret == True: cv2.imshow('Frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: cap1.set(cv2.CAP_PROP_POS_FRAMES, 0) elif dataPacket == "b": while cap2.isOpened(): ret, frame = cap2.read() if ret == True: cv2.imshow('Frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: cap2.set(cv2.CAP_PROP_POS_FRAMES, 0) elif dataPacket == "c": while cap3.isOpened(): ret, frame = cap3.read() if ret == True: cv2.imshow('Frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: cap3.set(cv2.CAP_PROP_POS_FRAMES, 0) elif dataPacket == "d": while cap4.isOpened(): ret, frame = cap4.read() if ret == True: cv2.imshow('Frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: cap4.set(cv2.CAP_PROP_POS_FRAMES, 0)cv2.destroyAllWindows()怎样修改代码能够在其中一个a视频播放时,接收到arduino发送的消息时能够触发另一视频b或c或d并播放?
时间: 2023-06-26 11:09:08 浏览: 120
可以使用一个变量来记录当前正在播放的视频,当接收到新的消息时,根据消息内容判断应该播放哪个视频,并将当前正在播放的视频暂停或关闭,然后切换到新的视频播放。
以下是修改后的代码:
```
import time
from serial import Serial
import serial.tools.list_ports
import cv2
import numpy as np
cap1 = cv2.VideoCapture("/Users/yankaipan/Desktop/stand.mp4")
cap2 = cv2.VideoCapture("/Users/yankaipan/Desktop/apple.mp4")
cap3 = cv2.VideoCapture("/Users/yankaipan/Desktop/bamboo.mp4")
cap4 = cv2.VideoCapture("/Users/yankaipan/Desktop/rubbish.mp4")
port_list = list(serial.tools.list_ports.comports())
port_list_1 = list(port_list[2])
port_serial = port_list_1[0]
arduinoData = serial.Serial(port_serial, 9600)
time.sleep(1)
current_video = None # 记录当前正在播放的视频
while True:
while (arduinoData.inWaiting() == 0):
pass
dataPacket = arduinoData.readline()
dataPacket = dataPacket.decode().strip()
print(dataPacket)
time.sleep(3)
if dataPacket == "a":
if current_video != cap1: # 判断是否需要切换视频
if current_video is not None:
current_video.release()
current_video = cap1
while current_video.isOpened():
ret, frame = current_video.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
current_video.set(cv2.CAP_PROP_POS_FRAMES, 0)
elif dataPacket == "b":
if current_video != cap2:
if current_video is not None:
current_video.release()
current_video = cap2
while current_video.isOpened():
ret, frame = current_video.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
current_video.set(cv2.CAP_PROP_POS_FRAMES, 0)
elif dataPacket == "c":
if current_video != cap3:
if current_video is not None:
current_video.release()
current_video = cap3
while current_video.isOpened():
ret, frame = current_video.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
current_video.set(cv2.CAP_PROP_POS_FRAMES, 0)
elif dataPacket == "d":
if current_video != cap4:
if current_video is not None:
current_video.release()
current_video = cap4
while current_video.isOpened():
ret, frame = current_video.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
current_video.set(cv2.CAP_PROP_POS_FRAMES, 0)
cv2.destroyAllWindows()
```
注意,在切换视频时需要将上一个视频释放掉,否则会导致内存泄漏。
阅读全文