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=="b": # print("1") # else: # print('2') if dataPacket=="a": if current_video!=cap1: #判断是否需要切换视频 if current_video is not None: current_video.release() current_video=cap1 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) current_video.set(cv2.CAP_PROP_FRAMES,0) elif dataPacket=="b": if current_video!=cap2: #判断是否需要切换视频 if current_video is not None: current_video.release() current_video=cap2 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) current_video.set(cv2.CAP_PROP_FRAMES,0) elif dataPacket=="c": if current_video!=cap3: #判断是否需要切换视频 if current_video is not None: current_video.release() current_video=cap3 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) current_video.set(cv2.CAP_PROP_FRAMES,0) elif dataPacket=="d": if current_video!=cap4: #判断是否需要切换视频 if current_video is not None: current_video.release() current_video=cap4 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) current_video.set(cv2.CAP_PROP_FRAMES,0) cv2.destroyAllWindows()代码运行播放a视频时接收到其他b或c或d;并不能播放相应的视频,该怎样修改代码才能让代码运行时实现这一功能?
时间: 2023-06-26 10:08:44 浏览: 210
在你的代码中,当收到指令时,会先判断当前播放的视频是不是需要切换的视频,如果是则释放当前视频,播放新的视频。但是在播放新的视频时,你使用了一个 while 循环读取视频帧并显示,这个循环是不会退出的,直到视频播放完毕或者用户按下 'q' 键退出。因此,即使收到了其他指令,也无法立即停止当前的视频播放。
为了解决这个问题,你可以在收到新指令时,先将当前正在播放的视频暂停或者停止,然后释放视频资源,最后根据接收到的指令切换到相应的视频,并开始播放新的视频。下面是修改后的代码:
```
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 current_video is not None:
current_video.release()
# 切换到新的视频
if dataPacket == "a":
current_video = cap1
elif dataPacket == "b":
current_video = cap2
elif dataPacket == "c":
current_video = cap3
elif dataPacket == "d":
current_video = cap4
# 开始播放新的视频
if current_video is not None:
current_video.set(cv2.CAP_PROP_POS_FRAMES, 0) # 将视频设置为第一帧
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()
```
在修改后的代码中,当收到新指令时,会先停止当前视频的播放,并释放资源。然后根据指令选择新的视频并开始播放。在播放新的视频时,每次读取一帧并显示,如果用户按下 'q' 键,则退出循环停止视频播放。
阅读全文