-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstreamer.py
More file actions
40 lines (31 loc) · 1023 Bytes
/
Copy pathstreamer.py
File metadata and controls
40 lines (31 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import socket
import cv2
import time
# create socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind socket to a specific IP address and port
host = '192.168.137.172' # replace with your server's IP address
port = 5025
s.bind((host, port))
# set the maximum number of queued connections
s.listen(5)
while True:
# wait for a client to connect
print('Waiting for client to connect...')
client_socket, addr = s.accept()
print('Got connection from', addr)
# capture an image using the webcam
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if ret and frame is not None:
# encode the image
encoded, buffer = cv2.imencode('.jpg', frame)
data = buffer.tobytes()
# send the image size and data to the client
size = len(data)
header = f'{size:012d}'
client_socket.sendall(header.encode() + data)
# release the resources
cap.release()
client_socket.close()
# wait for 5 seconds before capturing the next image