[英]Camera Streaming - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
I'm trying to stream the image of my camera ( Model ) from a Raspberry Pi 3 B+ with Raspberry OS to my Windows 10 PC.我正在尝试 stream 我的相机图像 ( Model ) 从带有 Raspberry OS 的 Raspberry Pi 3 B+ 到我的 Windows 10 PC。 It works, but when I hold something white in front of the camera and the image is small, but just not enough to be sent in one package, I get the OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'它有效,但是当我在相机前拿着白色的东西并且图像很小,但不足以发送一个 package 时,我收到 OpenCV 错误:(-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
Here's the python code and the console output:这是 python 代码和控制台 output:
Sender发件人
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()
Receiver接收者
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()
Receiver Console Output接收控制台 Output
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'
May be imshow for some reason receives an empty image.可能是 imshow 由于某种原因收到一个空图像。 Can you log the shape and type of img before calling imshow?你能在调用 imshow 之前记录 img 的形状和类型吗?
I have run your sender and recever scripts and works perfectly.我已经运行了您的发件人和接收脚本并且运行良好。 Moveing fast in front of my camera do not out your error.在我的镜头前快速移动不要出你的错误。
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''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.