def camera(request): camera = picamerax.PiCamera() def fin(): camera.close() request.addfinalizer(fin) return camera
async def openCamera(params): global continueLoop global newFreshParams try: log = logging.getLogger("openCam") shootresol = params["shootresol"] strtResolution = (shootresol["width"], shootresol["height"]) sensor_mode = shootresol["mode"] capture_format = params["capture_format"] log.info("OpeningCamera at resol %s mode:%s capture_format: %s", strtResolution, sensor_mode, capture_format) with picamerax.PiCamera(resolution=strtResolution, framerate_range=(0.01, 30), sensor_mode=sensor_mode) as camera: setParamsToCamera(camera, params) if params["capture_format"] == "rgb": with MyAnalyser(camera, params) as analyzer: camera.start_recording(analyzer, 'rgb') await continuousRecording(camera, analyzer) elif params["capture_format"] == "jpeg": out = JPGOutput(params) camera.start_recording(out, 'mjpeg') await continuousRecording(camera, out) except Exception as e: log.exception("whoaaw error %s" % str(e))
import picamerax with picamerax.PiCamera() as camera: camera.resolution = (800, 600) camera.start_preview() camera.start_recording('foo.h264') camera.wait_recording(10) camera.capture('foo.jpg', use_video_port=True) camera.wait_recording(10) camera.stop_recording()
# to get a buffer from the incoming queue if self.buffer is not None: self.outgoing.put(self.buffer) try: self.buffer = self.incoming.get_nowait() except queue.Empty: # No buffers available (means all threads are busy); skip # this frame self.buffer = None if self.buffer is not None: self.buffer.write(buf) def flush(self): # When told to flush (this indicates end of recording), shut # down in an orderly fashion. Tell all the processor's they're # terminated and wait for them to quit for proc in self.pool: proc.terminated = True for proc in self.pool: proc.join() with picamerax.PiCamera(resolution='VGA') as camera: camera.start_preview() time.sleep(2) output = ProcessOutput(4) camera.start_recording(output, format='mjpeg') while not output.done: camera.wait_recording(1) camera.stop_recording()
import picamerax import time import itertools s = "This message would be far too long to display normally..." camera = picamerax.PiCamera() camera.resolution = (640, 480) camera.framerate = 24 camera.start_preview() camera.annotate_text = ' ' * 31 for c in itertools.cycle(s): camera.annotate_text = camera.annotate_text[1:31] + c time.sleep(0.1)
def do_capture(queue, finished): with picamerax.PiCamera(resolution='VGA', framerate=30) as camera: output = QueueOutput(queue, finished) camera.start_recording(output, format='mjpeg') camera.wait_recording(10) camera.stop_recording()
import picamerax import datetime as dt camera = picamerax.PiCamera(resolution=(1280, 720), framerate=24) camera.start_preview() camera.annotate_background = picamerax.Color('black') camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S') camera.start_recording('timestamped.h264') start = dt.datetime.now() while (dt.datetime.now() - start).seconds < 30: camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S') camera.wait_recording(0.2) camera.stop_recording()
def test_dual_camera(camera): with pytest.raises(picamerax.PiCameraError): another_camera = picamerax.PiCamera()
class SplitFrames(object): 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('image%02d.jpg' % self.frame_num, 'wb') self.output.write(buf) with picamerax.PiCamera(resolution='720p', framerate=30) as camera: 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(2) camera.stop_recording() finish = time.time() print('Captured %d frames at %.2ffps' % (output.frame_num, output.frame_num / (finish - start)))
# Convert the color to hue, saturation, lightness h, l, s = c.hls c = 'none' if s > 1/3: if h > 8/9 or h < 1/36: c = 'red' elif 5/9 < h < 2/3: c = 'blue' elif 5/36 < h < 4/9: c = 'green' # If the color has changed, update the display if c != self.last_color: self.camera.annotate_text = c self.last_color = c with picamerax.PiCamera(resolution='160x90', framerate=24) as camera: # Fix the camera's white-balance gains camera.awb_mode = 'off' camera.awb_gains = (1.4, 1.5) # Draw a box over the area we're going to watch camera.start_preview(alpha=128) box = np.zeros((96, 160, 3), dtype=np.uint8) box[30:60, 60:120, :] = 0x80 camera.add_overlay(memoryview(box), size=(160, 90), layer=3, alpha=64) # Construct the analysis output and start recording data to it with MyColorAnalyzer(camera) as analyzer: camera.start_recording(analyzer, 'rgb') try: while True: camera.wait_recording(1) finally:
import picamerax camera = picamerax.PiCamera(resolution=(640, 480)) for filename in camera.record_sequence( '%d.h264' % i for i in range(1, 11)): camera.wait_recording(5)
def __init__(self): self.camera = picamera.PiCamera() self.mode = 0 ## 0 off 1 streaming 2 timelaps
self.y_queue[1:] = self.y_queue[:-1] self.x_queue[0] = a['x'].mean() self.y_queue[0] = a['y'].mean() # Calculate the mean of both queues x_mean = self.x_queue.mean() y_mean = self.y_queue.mean() # Convert left/up to -1, right/down to 1, and movement below # the threshold to 0 x_move = ('' if abs(x_mean) < self.THRESHOLD else 'left' if x_mean < 0.0 else 'right') y_move = ('' if abs(y_mean) < self.THRESHOLD else 'down' if y_mean < 0.0 else 'up') # Update the display movement = ('%s %s' % (x_move, y_move)).strip() if movement != self.last_move: self.last_move = movement if movement: print(movement) with picamerax.PiCamera(resolution='VGA', framerate=24) as camera: with GestureDetector(camera) as detector: camera.start_recording(os.devnull, format='h264', motion_output=detector) try: while True: camera.wait_recording(1) finally: camera.stop_recording()
output.condition.wait() frame = output.frame self.wfile.write(b'--FRAME\r\n') self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', len(frame)) self.end_headers() self.wfile.write(frame) self.wfile.write(b'\r\n') except Exception as e: logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) else: self.send_error(404) self.end_headers() class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True with picamerax.PiCamera(resolution='640x480', framerate=24) as camera: output = StreamingOutput() camera.start_recording(output, format='mjpeg') try: address = ('', 8000) server = StreamingServer(address, StreamingHandler) server.serve_forever() finally: camera.stop_recording()