我正在尝试使用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图像。朴素的帮助。
答案 0 :(得分:1)
您似乎需要在服务器端使用base64.b64decode
。
为了进行测试,我从文件中读取了图像,而不是使用camera.read()
。
我将代码转换为Python 3(对不起)...
b64encode
和b64decode
替换了encodebytes
和decodebytes
。 send
替换为sendall
(不确定是否需要)。 cv2.waitKey(1000)
之后添加了cv2.imshow('img.jpg',image)
请记住,我仍在学习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()