def gen_frames(mod): cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break results, names = mod.detect([frame]) frame = results[0][0] # 将处理后的帧编码为JPEG格式发送到前端 ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') cap.release() @gzip.gzip_page def live_feed(request): model = Models.objects.filter(state=1) if len(model) > 0: weights = settings.MODELS_ROOT + '/' + model[0].name mod = detect.detectapi(weights=weights) return StreamingHttpResponse(gen_frames(mod), content_type='multipart/x-mixed-replace; boundary=frame') # 检测到危险行为,生成JavaScript代码并发送给前端页面 js_code = '<script>showWarning();</script>' return HttpResponse(js_code) else: context = {'info': "没有模型启用,请联系管理员启用模型", 'action': '返回主页 →', 'url': 'index'} return render(request, 'myweb/info.html', context) 在这段代码里面添加一个报警功能
时间: 2023-06-10 20:04:09 浏览: 181
这是一个Python函数:gen_frames(mod),该函数使用OpenCV库从摄像头获取视频流,并使用一个已经训练好的对象检测模型(在参数mod中传入),对每一帧进行物体检测。如果没有检测到任何物体,将跳过该帧。如果有检测到物体,将在该帧上绘制出检测结果,并将该帧的返回值作为一个帧序列的一部分,以便后续的图像处理或显示。
相关问题
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()怎样修改代码能够让其中一个视频播放时,能够触发另一视频并播放?
你可以在每个视频播放的while循环中添加一个判断语句,当某个视频播放到一定程度时触发另一个视频的播放。例如:
```
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# 视频1播放到一定程度时触发播放视频2
if cap1.get(cv2.CAP_PROP_POS_FRAMES) == 50:
arduinoData.write(b'b')
else:
cap1.set(cv2.CAP_PROP_POS_FRAMES, 0)
```
其中,`cap1.get(cv2.CAP_PROP_POS_FRAMES)`可以获取当前视频1的播放帧数,当播放到第50帧时触发播放视频2的信号发送。你可以根据需要修改这个触发条件,以及发送的信号和对应的视频播放。
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;并不能播放相应的视频,该怎样修改代码才能让代码运行时实现这一功能?
在你的代码中,当收到指令时,会先判断当前播放的视频是不是需要切换的视频,如果是则释放当前视频,播放新的视频。但是在播放新的视频时,你使用了一个 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' 键,则退出循环停止视频播放。
阅读全文