I am trying to send a single frame from my client to the server via sockets. My code works when the path of the image is explicit. But, I am receiving an image as a string and decoding it. I'd like to send the image as a file to my server.
Here is the client code:
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)
I am trying to pass the image as an argumento to open, however the image is not being returned. I am running a chaquopy script on java and the error given is:
TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray
at <python>.script.main
And the server that receives an image:
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)
CodePudding user response:
If decoded_data
is already a complete image file, then I don't see why you need to use NumPy or OpenCV. Just send it to the socket directly:
decoded_data = base64.b64decode(data)
client.send(decoded_data)