-
-
Save kittinan/e7ecefddda5616eab2765fdb2affed1b to your computer and use it in GitHub Desktop.
import cv2 | |
import io | |
import socket | |
import struct | |
import time | |
import pickle | |
import zlib | |
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
client_socket.connect(('192.168.1.124', 8485)) | |
connection = client_socket.makefile('wb') | |
cam = cv2.VideoCapture(0) | |
cam.set(3, 320); | |
cam.set(4, 240); | |
img_counter = 0 | |
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90] | |
while True: | |
ret, frame = cam.read() | |
result, frame = cv2.imencode('.jpg', frame, encode_param) | |
# data = zlib.compress(pickle.dumps(frame, 0)) | |
data = pickle.dumps(frame, 0) | |
size = len(data) | |
print("{}: {}".format(img_counter, size)) | |
client_socket.sendall(struct.pack(">L", size) + data) | |
img_counter += 1 | |
cam.release() |
import socket | |
import sys | |
import cv2 | |
import pickle | |
import numpy as np | |
import struct ## new | |
import zlib | |
HOST='' | |
PORT=8485 | |
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
print('Socket created') | |
s.bind((HOST,PORT)) | |
print('Socket bind complete') | |
s.listen(10) | |
print('Socket now listening') | |
conn,addr=s.accept() | |
data = b"" | |
payload_size = struct.calcsize(">L") | |
print("payload_size: {}".format(payload_size)) | |
while True: | |
while len(data) < payload_size: | |
print("Recv: {}".format(len(data))) | |
data += conn.recv(4096) | |
print("Done Recv: {}".format(len(data))) | |
packed_msg_size = data[:payload_size] | |
data = data[payload_size:] | |
msg_size = struct.unpack(">L", packed_msg_size)[0] | |
print("msg_size: {}".format(msg_size)) | |
while len(data) < msg_size: | |
data += conn.recv(4096) | |
frame_data = data[:msg_size] | |
data = data[msg_size:] | |
frame=pickle.loads(frame_data, fix_imports=True, encoding="bytes") | |
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR) | |
cv2.imshow('ImageWindow',frame) | |
cv2.waitKey(1) |
Hello, the Server side code gives an syntax error on line 34 over Jetson Nano. But on my Windows PC run very well. What is the difference, Do you have an idea. both system use Python 3.7.
Thanks in advance
First Thank you for the great code,
Can you explain more what this code does?
struct.pack(">L", size)
And why you chose">L"
.
From here, you can see that regardless the format entered in the struct pack method, when usingupack
they return the same result.What is the purpose of arranging the bytes like that?
Yes, Could you possibly explain this?
The full documentation for the "struct.pack(">L", size)" clause can be found right here: https://docs.python.org/3/library/struct.html
thanks a lot it.It's working.
It still work for me, thanks your update
It works for me, but the speed is slow, anyone has suggestions?
Amazing code and really helpfull. It makes so much difference when learning if you can start from code that is functional!
Thanks. :)
Hello, the Server side code gives an syntax error on line 34 over Jetson Nano. But on my Windows PC run very well. What is the difference, Do you have an idea. both system use Python 3.7.
Thanks in advance
Hey did you figure anything out for this? Im trying to do the same.
This is working, while chat gpt didn't the job.
So, good job ;)
Thanks 👍 This code is working well. Using this code , How many frames we can send per second ? My requirment is to send 28 frames per second. The server code will be deployed in AWS ec2 instance and the client will be my laptop.