OpenCV 错误:断言失败 (size.width>0 && size.height>0) python



我正在尝试使用python套接字发送图像。但是 ı 有一个问题。我想将图像转换为字符串,然后将字符串转换为图像。我无法创建空的jpg文件,因为我将使用此代码视频流和录制。

我的客户端代码在这里

import socket
import cv2
import threading
import time 
import base64
Soket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
host = socket.gethostname()
port = 2344
#Buffer_Boyutu = 2360
Soket.bind(('', port)) 
Soket.listen(20)
c,asa = Soket.accept()
c.send('')
camera=cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
def img_to_str(image):
ret, buff = cv2.imencode('.jpg', image)
c.send(str(len(base64.b64encode(buff))))
str_image = base64.b64encode(buff)
return str_image

ret,image=camera.read()
str_image = img_to_str(image)
print(len(str_image))
time.sleep(0.02)
c.send(str_image)
time.sleep(5)
Soket.close()

我的服务器代码在这里

import socket
import cv2
import numpy as np
import base64
Soket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
port = 2344
Buffer_Size = 1024
Soket.connect(('',port))

def str_to_image(str_img):
file_bytes = np.asarray(bytearray(str_img), dtype=np.uint8)
image = cv2.imdecode(file_bytes, 0)
cv2.imshow('img.jpg',image)
size = ''
while size == '':
size= Soket.recv(1024)
if size != '':
Buffer_Size = int(size)     
str_img = Soket.recv(Buffer_Size)
if len(str_img) == Buffer_Size:
str_to_image(str_img)

Soket.close()

我有这个错误。

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window.cpp, line 331
Traceback (most recent call last):
File "server2.py", line 27, in <module>
str_to_image(str_img)
File "server2.py", line 15, in str_to_image
cv2.imshow('img.jpg',image)
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow

我将制作一个流视频节目,并且必须使用opencv图像。普拉斯帮助。

看起来您需要在服务器端使用base64.b64decode

为了进行测试,我从文件中读取图像,而不是使用camera.read().

我将代码转换为 Python 3(抱歉(...

  • Python 3 字符串使用 Unicode,所以我用encodebytesdecodebytes替换了b64encodeb64decode
  • 我用sendall替换了send(我不确定是否需要(。
  • 我在cv2.imshow('img.jpg',image)之后添加了cv2.waitKey(1000)
  • 添加了一些我不确定是否必要的修改(例如将 IP 地址设置为本地主机(。

请记住,我还在学习Python(我的代码可能不是最优雅的(。


客户端代码:

import socket
import cv2
import threading
import time 
import base64
Soket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 2344
#Buffer_Boyutu = 2360
Soket.bind(('127.0.0.1', port)) 
Soket.listen(20)
c,asa = Soket.accept()
c.sendall(b'')
camera=cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
def img_to_str(image):
ret, buff = cv2.imencode('.jpg', image)
str_image = base64.encodebytes(buff.tobytes())
c.sendall(str(len(str_image)).encode('utf-8'))
str_image = base64.encodebytes(buff.tobytes())
return str_image
#ret,image=camera.read()
# For testing, read image from file
image = cv2.imread('im.png')
str_image = img_to_str(image)
print(len(str_image))
time.sleep(0.02)
c.sendall(str_image)
time.sleep(5)
Soket.close()

服务器代码:

import socket
import cv2
import numpy as np
import base64
Soket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
port = 2344
Buffer_Size = 1024
Soket.connect(('127.0.0.1',port))
def str_to_image(str_img):
#file_bytes = np.asarray(bytearray(str_img), dtype=np.uint8)
buff = base64.decodebytes(str_img)  # Decode base64
file_bytes = np.frombuffer(buff, np.uint8)  # Convert to numpy array
image = cv2.imdecode(file_bytes, cv2.IMREAD_UNCHANGED)
if image is None:
print('Invalid image')
else:
cv2.imshow('img.jpg',image)
cv2.waitKey(1000)
size = ''
while size == '':
size= Soket.recv(1024)
if size != '':
Buffer_Size = int(size)     
str_img = Soket.recv(Buffer_Size)
if len(str_img) == Buffer_Size:
str_to_image(str_img)
Soket.close()
cv2.destroyAllWindows()

最新更新