forked from hamuchiwa/AutoRCCar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import io | ||
import socket | ||
import struct | ||
import time | ||
import picamera | ||
import sys | ||
|
||
class SplitFrames(object): | ||
def __init__(self, connection): | ||
self.connection = connection | ||
self.stream = io.BytesIO() | ||
self.count = 0 | ||
|
||
def write(self, buf): | ||
if buf.startswith(b'\xff\xd8'): | ||
# Start of new frame; send the old one's length | ||
# then the data | ||
size = self.stream.tell() | ||
if size > 0: | ||
self.connection.write(struct.pack('<L', size)) | ||
self.connection.flush() | ||
self.stream.seek(0) | ||
self.connection.write(self.stream.read(size)) | ||
self.count += 1 | ||
self.stream.seek(0) | ||
self.stream.write(buf) | ||
|
||
my_server = '192.168.1.100' | ||
res = (320, 240) | ||
client_socket = socket.socket() | ||
client_socket.connect((my_server, 8000)) | ||
connection = client_socket.makefile('wb') | ||
try: | ||
output = SplitFrames(connection) | ||
with picamera.PiCamera(resolution=res, framerate=30) as camera: | ||
time.sleep(2) | ||
start = time.time() | ||
camera.start_recording(output, format='mjpeg') | ||
camera.wait_recording(sys.maxint) | ||
camera.stop_recording() | ||
# Write the terminating 0-length to the connection to let the | ||
# server know we're done | ||
connection.write(struct.pack('<L', 0)) | ||
finally: | ||
finish = time.time() | ||
print('Sent %d images in %d seconds at %.2ffps' % ( | ||
output.count, finish-start, output.count / (finish-start))) | ||
connection.close() | ||
client_socket.close() |