Last active
January 12, 2020 00:09
-
-
Save saltzmanjoelh/8e8c2029581df349cadc20157efe5b8b to your computer and use it in GitHub Desktop.
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
# <script src="https://gist.github.com/saltzmanjoelh/8e8c2029581df349cadc20157efe5b8b.js"></script> | |
import io | |
import time | |
from io import BytesIO | |
import picamera | |
import cv2 | |
duration = 5 | |
# Capture slowly with opencv. Still port as well? | |
cap = cv2.VideoCapture(0) | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) | |
# cap.set(cv2.CAP_PROP_FPS, 30) | |
if (cap.isOpened() == False): | |
print("Unable to read camera feed") | |
# Camera warm-up time | |
time.sleep(2) | |
frame_num = 0 | |
start = time.time() | |
while int(time.time()) - int(start) < duration: | |
ret, frame = cap.read() | |
# cap.set(cv2.CAP_PROP_FPS, 30) | |
cv2.imwrite(f'opencv_image_{frame_num}.jpg', frame) | |
if ret == False: | |
print("something went wrong") | |
break | |
frame_num = frame_num + 1 | |
finish = time.time() | |
print('Captured %d frames at %.2ffps' % ( | |
frame_num, | |
frame_num / (finish - start))) | |
cap.release() | |
# Captured 14 frames at 2.90fps fileSize: 670k | |
class SplitFrames(object): | |
output = None | |
def __init__(self): | |
self.frame_num = 0 | |
self.output = None | |
def write(self, buf): | |
if buf.startswith(b'\xff\xd8'): | |
# Start of new frame; close the old one (if any) and | |
# open a new output | |
if self.output: | |
self.output.close() | |
self.frame_num += 1 | |
self.output = io.open('recording_image%02d.jpg' % | |
self.frame_num, 'wb') | |
self.output.write(buf) | |
# Capture using video port at acceptable speed | |
with picamera.PiCamera(resolution='1920X1080') as camera: | |
camera.vflip = True | |
camera.hflip = True | |
camera.start_preview() | |
# Give the camera some warm-up time | |
time.sleep(2) | |
output = SplitFrames() | |
start = time.time() | |
camera.start_recording(output, format='mjpeg') | |
camera.wait_recording(duration) | |
camera.stop_recording() | |
finish = time.time() | |
print('Recorded %d frames at %.2ffps' % ( | |
output.frame_num, | |
output.frame_num / (finish - start))) | |
# Recorded 143 frames at 28.34fps fileSize: 75k | |
# #Capture with still port at slow speed | |
with picamera.PiCamera(resolution='1920X1080') as camera: | |
frame_num = 0 | |
camera.start_preview() | |
captured_image = open('captured_image.jpg', 'wb') | |
# my_stream = BytesIO() | |
time.sleep(2) | |
start = time.time() | |
while int(time.time()) - int(start) < duration: | |
# camera.capture(my_stream, 'jpeg') | |
camera.capture(captured_image) | |
frame_num = frame_num + 1 | |
finish = time.time() | |
captured_image.close() | |
print('Captured %d frames at %.2ffps' % ( | |
frame_num, | |
frame_num / (finish - start))) | |
# Captured 8 frames at 1.75fps 12.3 MB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment