-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_source.py
43 lines (33 loc) · 1.01 KB
/
stream_source.py
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
40
41
42
43
import io
import socket
import struct
import time
import picamera2
# Connect a client socket to my laptop's IP address and port 8000
client_socket = socket.socket()
client_socket.connect(('192.168.4.106', 8000))
# Make a file-like object out of the connection
connection = client_socket.makefile('rwb')
try:
with picamera2.PiCamera() as camera:
camera.resolution = (640, 480)
camera.framerate = 24
time.sleep(2) # Let camera warm up
start = time.time()
stream = io.BytesIO()
for _ in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
# Send the image length and data over the network
connection.write(struct.pack('<L', stream.tell()))
connection.flush()
stream.seek(0)
connection.write(stream.read())
stream.seek(0)
stream.truncate()
time.sleep(0.01)
except Exception:
raise
finally:
connection.close()
client_socket.close()
time.sleep(1)
exit(1)