Python 摄影机流-OpenCV错误:(-215:断言失败)大小。宽度>;0&&;尺寸、高度>;函数'中的0;cv::imshow';
我正在尝试将我相机的图像()从带有Raspberry OS的Raspberry Pi 3 B+流式传输到我的Windows 10 PC。 它可以工作,但当我在相机前拿着白色的东西,图像很小,但不足以在一个包中发送时,我会得到OpenCV错误:(-215:断言失败)函数“cv::imshow”中的size.width>0&&size.height>0 下面是python代码和控制台输出: 寄件人Python 摄影机流-OpenCV错误:(-215:断言失败)大小。宽度>;0&&;尺寸、高度>;函数'中的0;cv::imshow';,python,opencv,streaming,Python,Opencv,Streaming,我正在尝试将我相机的图像()从带有Raspberry OS的Raspberry Pi 3 B+流式传输到我的Windows 10 PC。 它可以工作,但当我在相机前拿着白色的东西,图像很小,但不足以在一个包中发送时,我会得到OpenCV错误:(-215:断言失败)函数“cv::imshow”中的size.width>0&&size.height>0 下面是python代码和控制台输出: 寄件人 import socket import struct import math import cv2
import socket
import struct
import math
import cv2
import time
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 60339
fs = FrameSegment(s, port, "192.168.1.29")
cap = cv2.VideoCapture(0)
print("Startet video capture")
while (cap.isOpened()):
_, frame = cap.read()
fs.udp_frame(frame)
cap.release()
cv2.destroyAllWindows()
s.close()
class FrameSegment(object):
MAX_DGRAM = 2**16
MAX_IMAGE_DGRAM = MAX_DGRAM - 64
def __init__(self, sock, port, addr):
self.s = sock
self.port = port
self.addr = addr
def udp_frame(self, img):
compress_img = cv2.imencode('.jpg', img)[1]
dat = compress_img.tostring()
size = len(dat)
num_of_segments = math.ceil(size/(self.MAX_IMAGE_DGRAM))
array_pos_start = 0
while num_of_segments:
array_pos_end = min(size, array_pos_start + self.MAX_IMAGE_DGRAM)
self.s.sendto(
struct.pack("B", num_of_segments) +
dat[array_pos_start:array_pos_end],
(self.addr, self.port)
)
array_pos_start = array_pos_end
num_of_segments -= 1
main()
接受者
import numpy as np
import struct
import socket
import cv2
MAX_DGRAM = 2**16
index2 = False
def dump_buffer(s):
""" Emptying buffer frame """
while True:
print("Emptying Buffer")
seg, addr = s.recvfrom(MAX_DGRAM)
if struct.unpack("B", seg[0:1])[0] == 1:
print("finish emptying buffer")
break
def imageReceiver():
print("Socket setup")
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('192.168.1.29', 60339))
dat = b''
dump_buffer(s)
while True:
seg, addr = s.recvfrom(MAX_DGRAM)
index = struct.unpack("B", seg[0:1])[0]
if index > 1:
dat += seg[1:]
else:
dat += seg[1:]
img = cv2.imdecode(np.frombuffer(dat, dtype=np.uint8), 1)
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
dat = b''
cv2.destroyAllWindows()
s.close()
imageReceiver()
接收机控制台输出
Socket setup
Emptying Buffer
Emptying Buffer
finish emptying buffer
Traceback (most recent call last):
File ".\RemoteControl.py", line 43, in <module>
imageReceiver()
File ".\RemoteControl.py", line 34, in imageReceiver
cv2.imshow('frame', img)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
套接字设置
清空缓冲区
清空缓冲区
完成清空缓冲区
回溯(最近一次呼叫最后一次):
文件“\RemoteControl.py”,第43行,在
图像接收器()
imageReceiver中第34行的文件“\RemoteControl.py”
cv2.imshow(“帧”,img)
cv2.error:OpenCV(4.5.1)C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\OpenCV\modules\highgui\src\window.cpp:376:error:(-215:断言失败)函数“cv::imshow”中的size.width>0和size.height>0
可能由于某种原因,imshow接收到空图像。
在调用imshow之前,您可以记录img的形状和类型吗
我已经运行了你的发送者和接收者脚本,效果非常好。
在我的相机前快速移动,不要发现你的错误
while True:
seg, addr = s.recvfrom(MAX_DGRAM)
index = struct.unpack("B", seg[0:1])[0]
if index > 1:
dat += seg[1:]
else:
dat += seg[1:]
img = cv2.imdecode(np.frombuffer(dat, dtype=np.uint8), 1)
#--- Prevent crash if img is None
if img is not None:
cv2.imshow('frame', img)
#-----------------------------------
if cv2.waitKey(1) & 0xFF == ord('q'):
break
dat = b''
您需要创建一个窗口
cv2.namedWindow('frame')
No这不是问题所在。它在一开始起作用,当我快速移动时,它就崩溃了@DrBwtsI发现,只有当图像小到可以用一个包发送时,它才会崩溃。因此,现在您可以传递
或继续
或以正确的大小重塑img,或者如果框架仅适合一个包,则不发送框架。很好的项目。抱歉,如果它很小,但还不够小,不能用一个包发送。对于带有print(imp.shape)
的输出,我得到:(480,640,3)。。。(480,640,3)(480,640,3)编辑答案。查看“如果img为无,则防止碰撞”