Skip to content

Instantly share code, notes, and snippets.

@kittinan
Last active November 15, 2024 15:35
Show Gist options
  • Save kittinan/e7ecefddda5616eab2765fdb2affed1b to your computer and use it in GitHub Desktop.
Save kittinan/e7ecefddda5616eab2765fdb2affed1b to your computer and use it in GitHub Desktop.
Python OpenCV webcam send image frame over socket
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)
@lhlong
Copy link

lhlong commented Aug 30, 2019

Any idea on how to add timestamps on the image

Why don't we send other string?

@lhlong
Copy link

lhlong commented Sep 2, 2019

@kittinan
how can we apply async for it? I'm stuck while try to send a large image.

@hariprasadr92
Copy link

hariprasadr92 commented Sep 20, 2019

Hi @kittinan. Thanks for your work. I ran the files in my local and could see the logs. I was wondering if the image file could be viewed on the browser in someway ?

Edited:
Figured out the issue. I ran in pycharm. Run the py server file in cmd and the frame pops up.

@Syeong2
Copy link

Syeong2 commented Oct 10, 2019

Hi! I have a question. I want to save the frame to video in server. I use VideoWirter, but it dosen't work. Do you know how to save video?

@datacrystals
Copy link

Hi @Syeong2,
You probably already found a solution, but if you are using VideoWriter you MUST make sure that the resolution you set is actually the resolution of the frames you're trying to save. VideoWriter fails silently otherwise and just makes an unplayable ~4kb video file with no actual content. I eventually figured that out a while ago after bashing my head into a table for a couple days...

@Ajithbalakrishnan
Copy link

Hi all, Can you tell me what all the code level modifications that i have to do for sending a image file from server to client. Please help me.

@hariprasadr92
Copy link

hariprasadr92 commented Dec 18, 2019

@ajith , Nothing. It just works as it is. Just run the server from command prompt.

if u just stop after the first iteration, u get one single image

@Ajithbalakrishnan
Copy link

Thanks, got it... It's working.

Copy link

ghost commented Dec 28, 2019

Do you have an idea how the server can recover when client close connection? When I close the client and try to reconnect the server just doesn't seem to want to reconnect.

@Ajithbalakrishnan
Copy link

@CodeAndChoke, if u r using python, plase use Try & Except method. Make sure the except section, again tries to reconnect to the server. Because client has to repeat the "socket.connection" once it is disconnected from server.
Hopes the below script skelton might help u. Please correct me if am wrong.
eg:
#begining script

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
while:
try:
# what ever u need to send
except:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))

@ZhongxuanWang
Copy link

Thanks so much! You are the one that I've been searching for a long time!

@zeyuyang
Copy link

If there is only one computer. Change the IP to 'localhost'. It works. Thank you for the code!

@mhfa1380
Copy link

mhfa1380 commented May 9, 2020

thank you

@VedantBhalgama
Copy link

Hi! I am a penetration tester. And I want that the Webcam Windows shouldnt open on windows machine. Instead I want the live feed on Kali Linux Machine. What Changes could be made

@patrick29-koto
Copy link

thank you for your code but i have a problem:
opencv error: unknown error code -10 (Raw image encoder error: empty JPEG image (DNL not supported)) in throwOnError
can you explain

@mhfa1380
Copy link

mhfa1380 commented Jul 2, 2020

Maybe it's because the cv2 module is not installed properly, maybe the webcam port is busy, maybe the webcam driver is not installed.

Did you run the server first and then the client? Did you run with Linux?

@mhfa1380
Copy link

mhfa1380 commented Jul 2, 2020

In Linux, you need to install the required modules with the pip command, and run pycharm or any other ide , first run the server and run then the client.
It reads information from the client webcam and it displayed in server program

@AjjuKrishna
Copy link

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.

@serkansafi
Copy link

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

@aybasaran
Copy link

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 using upack they return the same result.

What is the purpose of arranging the bytes like that?

Yes, Could you possibly explain this?

@utacc
Copy link

utacc commented Mar 4, 2021

The full documentation for the "struct.pack(">L", size)" clause can be found right here: https://docs.python.org/3/library/struct.html

@xoy232
Copy link

xoy232 commented Mar 23, 2021

thanks a lot it.It's working.

@IricsDo
Copy link

IricsDo commented May 16, 2021

It still work for me, thanks your update

@QiangZiBro
Copy link

It works for me, but the speed is slow, anyone has suggestions?

@Spedley2142
Copy link

Amazing code and really helpfull. It makes so much difference when learning if you can start from code that is functional!

Thanks. :)

@AayushBajaj2000
Copy link

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.

@fischer7
Copy link

This is working, while chat gpt didn't the job.
So, good job ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment