add cv2 video stream

This commit is contained in:
Lennart Heimbs 2019-08-04 16:37:40 +02:00
parent 559fe7f09d
commit c3fea65346

View File

@ -12,33 +12,30 @@ client_socket.connect(('my_server', 8000))
# Make a file-like object out of the connection # Make a file-like object out of the connection
connection = client_socket.makefile('wb') connection = client_socket.makefile('wb')
try: try:
cap = cv2. cap = cv2.VideoCapture("run.mp4")
with picamera.PiCamera() as camera:
camera.resolution = (640, 480)
# Start a preview and let the camera warm up for 2 seconds
camera.start_preview()
time.sleep(2)
# Note the start time and construct a stream to hold image data while True:
# temporarily (we could write it directly to connection but in this ret, frame = cap.read()
# case we want to find out the size of each capture first to keep
# our protocol simple) if frame == None:
start = time.time() break
stream = io.BytesIO()
for foo in camera.capture_continuous(stream, 'jpeg'): image = cv2.imencode('.jpg', frame)
# Write the length of the capture to the stream and flush to stream = image.tobytes()
# ensure it actually gets sent
connection.write(struct.pack('<L', stream.tell())) # Write the length of the capture to the stream and flush to
connection.flush() # ensure it actually gets sent
# Rewind the stream and send the image data over the wire connection.write(struct.pack('<L', stream.tell()))
stream.seek(0) connection.flush()
connection.write(stream.read()) # Rewind the stream and send the image data over the wire
# If we've been capturing for more than 30 seconds, quit stream.seek(0)
if time.time() - start > 30: connection.write(stream.read())
break # If we've been capturing for more than 30 seconds, quit
# Reset the stream for the next capture if time.time() - start > 30:
stream.seek(0) break
stream.truncate() # Reset the stream for the next capture
stream.seek(0)
stream.truncate()
# Write a length of zero to the stream to signal we're done # Write a length of zero to the stream to signal we're done
connection.write(struct.pack('<L', 0)) connection.write(struct.pack('<L', 0))
finally: finally: