pc作为客户端,树莓派作为服务端,pc使用socket发送图片的decode码给树莓派并在树莓派上重新编译出图片失败,搜索错误显示可能原因为路径存在中文,检查路径后同样报错,请求指教
pc端
import socket
import win32gui
from PIL import ImageGrab
import numpy as np
import cv2
print("客户端开启")
#套接字接口
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#设置ip和端口
host = '192.168.1.105'
port = 8000
try:
mySocket.connect((host, port)) ##连接到服务器
print("连接到服务器")
except : ##连接不成功,运行最初的ip
print ('连接不成功')
while True:
#发送消息
'''hwnd = win32gui.FindWindow(None, 'http_//192.168.1.105_8080/_action=stream')
try:
# 寻找窗口位置
position = win32gui.GetWindowRect(hwnd)
except Exception as e:
print(e)
break
# 对窗口截图
im = ImageGrab.grab(position)'''
#图片转码
im = cv2.imread("bus.jpg")
img_encode = cv2.imencode(".jpg",im)
#转为numpy array
data_encode= np.array(img_encode)
string_encode=data_encode.tostring()
#编码发送
mySocket.send(string_encode)
print("发送完成")
break
树莓派端
import socket
import time
import cv2
import numpy as np
print("服务端开启")
#套接字接口
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#设置IP和端口
host = '192.168.1.105'
port = 8000
#bind绑定该端口
mySocket.bind((host, port))
mySocket.listen(10)
while True:
#接收客户端连接
print("等待连接....")
client, address = mySocket.accept()
print("新连接")
print("IP is %s" % address[0])
print("port is %d\n" % address[1])
while True:
#读取消息
str_encode = client.recv(1)
nparr = np.fromstring(str_encode,np.uint8)
image =cv2.imdecode(nparr,cv2.IMREAD_COLOR)
#把接收到的数据进行解码
cv2.imshow('result',image)
break