发送图像时需要 str、字节或 os.PathLike 对象,而不是 numpy.ndarray

Expected str, bytes or os.PathLike object, not numpy.ndarray when sending image

我正在尝试通过套接字将单个帧从我的客户端发送到服务器。当图像路径明确时,我的代码有效。但是,我正在接收一个图像作为字符串并对其进行解码。我想将图像作为文件发送到我的服务器。

这是客户端代码:

import numpy as np
import cv2
from PIL import Image
import base64
import socket
import pickle
import struct
import io

def main(data):
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(('192.168.0.14', 9999))
    BUFFER_SIZE = 4096*4

    decoded_data = base64.b64decode(data)
    np_data = np.fromstring(decoded_data, np.uint8)
    img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)

    with open(img, 'rb') as file:
        file_data = file.read(BUFFER_SIZE)

    while file_data:
        client.send(file_data)
        file_data = file.read(BUFFER_SIZE)

我正在尝试将图像作为打开参数传递,但图像没有返回。我是 运行 java 上的 chaquopy 脚本,给出的错误是:

TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray
        at <python>.script.main

以及接收图像的服务器:

import socket
import time


date_string = time.strftime("%Y-%m-%d-%H:%M")



server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('192.168.0.14', 9999))

server.listen()

BUFFER_SIZE = 4096*4

while True:
    client_socket, _ = server.accept()
    
    
    with open('frames_saved/'+date_string+'.jpeg', 'wb') as file:
        recv_data = client_socket.recv(BUFFER_SIZE)
        while recv_data:
            file.write(recv_data)
            recv_data = client_socket.recv(BUFFER_SIZE)

如果decoded_data已经是一个完整的图像文件,那么我不明白你为什么要使用NumPy或OpenCV。直接发送到套接字即可:

decoded_data = base64.b64decode(data)
client.send(decoded_data)