gpt4 book ai didi

python - 来自网络摄像头的mjpeg流中的OpenCv Python框架为空

转载 作者:行者123 更新时间:2023-12-02 16:39:03 24 4
gpt4 key购买 nike

我正在创建一个在网格中显示4个mjpg相机的程序,我的问题是有时会发生一个帧为空并且流冻结的情况,我通过检查每个帧的大小来捕获错误,如果它等于1我继续循环,我的问题是我仍然停留在控制循环中,看来流无法获得有效的帧,有什么建议吗?

import socket
import numpy as np
import cv2

cap = cv2.VideoCapture("http://85.90.40.19/mjpg/video.mjpg")
cap2 = cv2.VideoCapture("http://85.90.40.19/mjpg/video.mjpg")
cap3 = cv2.VideoCapture("http://85.90.40.19/mjpg/video.mjpg")
cap4 = cv2.VideoCapture("http://85.90.40.19/mjpg/video.mjpg")



cv2.namedWindow('frame', cv2.WND_PROP_FULLSCREEN)

cv2.setWindowProperty('frame', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

while True:

try:
ret, frame = cap.read()
ret2, frame2 = cap2.read()
ret3, frame3 = cap2.read()
ret4, frame4 = cap2.read()
except:
print("try catch")
continue



size = np.size(frame)
size2 = np.size(frame2)
size3 = np.size(frame3)
size4 = np.size(frame4)


if (size or size2 or size3 or size4) == 1:
print("frame 0")
continue

# print("Dimensione" + str(size))

rframe = cv2.resize(frame, (640,480))
rframe2 = cv2.resize(frame2, (640,480))
rframe3 = cv2.resize(frame3, (640,480))
rframe4 = cv2.resize(frame4, (640,480))

print(ret,ret2,ret3,ret4)


vert1 = np.vstack((rframe,rframe2))
vert2 = np.vstack((rframe3,rframe4))

both = np.hstack((vert1,vert2))
print(both.shape)
# both.resize((1024, 768,3))

cv2.imshow('frame', both)
if cv2.waitKey(1) == 27:
break

cap.release()
cv2.destroyAllWindows()

谢谢,抱歉我的英语不好

最佳答案

我可以建议以下类型的方法:

try:
ret, frame = cap.read()
except:
ret, frame = (-1, blackframe)

try:
ret2, frame2 = cap2.read()
except:
ret2, frame2 = (-1, blackframe)

try:
ret3, frame3 = cap3.read()
except:
ret3, frame3 = (-1, blackframe)

try:
ret4, frame4 = cap4.read()
except:
ret4, frame4 = (-1, blackframe)
blackframe是捕获失败时可用的合适镜像。这将允许其他框架继续显示。

目的是删除 continue语句,该语句使执行返回到顶部,而不执行任何其他代码。

关于python - 来自网络摄像头的mjpeg流中的OpenCv Python框架为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com