Ejemplo n.º 1
0
def audio_feed(device, fifo, sample_rate, blocksize, latency, channels):
    send_request(action='sound.stream_recording',
                 device=device,
                 sample_rate=sample_rate,
                 dtype='int16',
                 fifo=fifo,
                 blocksize=blocksize,
                 latency=latency,
                 channels=channels)

    try:
        with open(fifo, 'rb') as f:
            send_header = True

            while True:
                audio = f.read(blocksize)

                if audio:
                    if send_header:
                        audio = gen_header(sample_rate=sample_rate,
                                           sample_width=16,
                                           channels=channels) + audio
                        send_header = False

                    yield audio
    finally:
        send_request(action='sound.stop_recording')
Ejemplo n.º 2
0
def get_feed(**args):
    try:
        while True:
            frame = send_request(action='camera.ir.mlx90640.capture',
                                 **args).output[0]
            frame = base64.decodebytes(frame.encode())
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
    finally:
        send_request(action='camera.ir.mlx90640.stop')
Ejemplo n.º 3
0
def get_frame_route():
    f = tempfile.NamedTemporaryFile(prefix='ir_camera_frame_',
                                    suffix='.jpg',
                                    delete=False)
    args = {
        'grayscale': bool(int(request.args.get('grayscale', 0))),
        'scale_factor': int(request.args.get('scale_factor', 1)),
        'rotate': int(request.args.get('rotate', 0)),
        'output_file': f.name,
    }

    send_request(action='camera.ir.mlx90640.capture', **args)
    return send_from_directory(os.path.dirname(f.name),
                               os.path.basename(f.name))
Ejemplo n.º 4
0
def get_frame_img():
    filename = os.path.join(tempfile.gettempdir(), 'camera_pi.jpg')
    response = send_request('camera.pi.take_picture', image_file=filename)
    frame_file = (response.output or {}).get('image_file')
    assert frame_file is not None

    return send_from_directory(os.path.dirname(frame_file),
                               os.path.basename(frame_file))
Ejemplo n.º 5
0
def video_feed(device_id=None):
    device_id = get_device_id(device_id)
    send_request(action='camera.start_recording', device_id=device_id)
    last_frame_file = None

    try:
        while True:
            frame_file = get_frame_file(device_id)
            if frame_file == last_frame_file:
                continue

            with open(frame_file, 'rb') as f:
                frame = f.read()

            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

            last_frame_file = frame_file
    finally:
        send_request(action='camera.stop_recording', device_id=device_id)
Ejemplo n.º 6
0
def get_frame_file(device_id=None):
    device_id = get_device_id(device_id)
    was_recording = True
    frame_file = None
    status = send_request(action='camera.status', device_id=device_id).output

    if device_id not in status:
        was_recording = False
        send_request(action='camera.start_recording', device_id=device_id)

    while not frame_file:
        frame_file = send_request(action='camera.status', device_id=device_id). \
            output.get(device_id, {}).get('image_file')

        if not frame_file:
            time.sleep(0.1)

    if not was_recording:
        with tempfile.NamedTemporaryFile(prefix='camera_capture_',
                                         suffix='.jpg',
                                         delete=False) as f:
            # stop_recording will delete the temporary frames. Copy the image file
            # to a temporary file before stopping recording
            tmp_file = f.name

        shutil.copyfile(frame_file, tmp_file)
        frame_file = tmp_file
        send_request(action='camera.stop_recording', device_id=device_id)

    return frame_file
Ejemplo n.º 7
0
def get_device_id(device_id=None):
    if device_id is None:
        device_id = str(
            send_request(action='camera.get_default_device_id').output)
    return device_id