~martijnbraam/python-pinecamera

f710b187151464876bff61cb169f2e8b70ff5dff — Martijn Braam 2 months ago bbee468 master
Raw and front sensor support
1 files changed, 43 insertions(+), 10 deletions(-)

M camera.py
M camera.py => camera.py +43 -10
@@ 40,13 40,25 @@ def get_orientation():
    return "cw"


def setup(node, res, debug=False):
def setup(node, res, debug=False, pixfmt=None):
    speed = 30
    if '@' in res:
        res, speed = res.split('@')

    busfmts = {
        'UYVY': 'UYVY8_2X8',
        'BA81': 'SBGGR8_1X8',
        'GBRG': 'SGBRG8_1X8',
        'GRBG': 'SGRBG8_1X8',
        'RGGB': 'SRGGB8_1X8',
    }
    if pixfmt in busfmts:
        busfmt = busfmts[pixfmt]
    else:
        busfmt = 'UYVY8_2X8'

    command = ['sudo', 'media-ctl', '-d', '/dev/media1', '--set-v4l2',
               '"{}":0[fmt:UYVY8_2X8/{}@1/{}]'.format(node, res, speed)]
               '"{}":0[fmt:{}/{}@1/{}]'.format(node, busfmt, res, speed)]
    if debug:
        print(command)
    p = subprocess.run(command, timeout=5)


@@ 54,7 66,7 @@ def setup(node, res, debug=False):
        return False

    width, height = res.split('x')
    fmt = ['width=' + width, 'height=' + height, 'pixelformat=UYVY']
    fmt = ['width=' + width, 'height=' + height, 'pixelformat={}'.format(pixfmt)]
    command = ['sudo', 'v4l2-ctl', '--device', '/dev/video1', '--set-fmt-video={}'.format(','.join(fmt))]
    if debug:
        print(command)


@@ 63,8 75,10 @@ def setup(node, res, debug=False):
        return False


def take_snapshot(node, res, name, rotate, skip=5, debug=False):
    setup(node, res, debug=debug)
def take_snapshot(node, res, name, rotate, skip=5, debug=False, pixfmt=None, raw=False):
    if pixfmt is None:
        pixfmt = 'uyvy'
    setup(node, res, debug=debug, pixfmt=pixfmt)

    speed = 30
    if '@' in res:


@@ 77,6 91,10 @@ def take_snapshot(node, res, name, rotate, skip=5, debug=False):
    if p.returncode != 0:
        return False

    if raw:
        os.rename('/tmp/frame.raw', name)
        return True

    command = ['convert', '-size', res, 'uyvy:/tmp/frame.raw', '-rotate', rotate, name]
    if debug:
        print(command)


@@ 93,7 111,7 @@ def take_snapshot(node, res, name, rotate, skip=5, debug=False):


def record(node, res, name, rotate, debug=False):
    setup(node, res, debug=debug)
    setup(node, res, debug=debug, pixfmt='uyvy')

    speed = 30
    if '@' in res:


@@ 133,6 151,7 @@ def main():
        '1080p': '1920x1080@20',
        '1080p20': '1920x1080@20',
        '1080p15': '1920x1080@15',
        '1080p5': '1920x1080@5',
        '720p': '1280x720@30',
        '720p60': '1280x720@60',
        '720p50': '1280x720@50',


@@ 141,8 160,12 @@ def main():
        '720p24': '1280x720@24',
    }
    modes_gc2145 = {
        '1080p': '1920x1080@30',
        'max': '1600x1200@15',
        '1200p': '1600x1200@15',
        '1200p15': '1600x1200@15',
        '1200p1': '1600x1200@1',
        '720p': '1280x720@30',
        '720p1': '1280x720@1',
        '1080p15': '1920x1080@15',
    }



@@ 154,9 177,12 @@ def main():
    parser.add_argument('--resolution', '-r', choices=options, default='1080p')
    parser.add_argument('--camera', '-c', choices=['rear', 'front'], default='rear')
    parser.add_argument('--debug', '-d', action="store_true")
    parser.add_argument('--raw', action="store_true", help="store raw frame")
    parser.add_argument('--pixfmt', '-p', help="pixelformat for raw frame", default="UYVY")
    parser.add_argument('filename')
    args = parser.parse_args()

    skip = 5
    orientation = get_orientation()
    if args.camera == "rear":
        set_route("ov5640")


@@ 173,13 199,21 @@ def main():
    else:
        set_route("gc2145")
        node = 'gc2145 3-003c'
        angle = '270'
        if orientation == "portrait":
            angle = '270'
        elif orientation == "cw":
            angle = '0'
        elif orientation == "ccw":
            angle = '180'
        else:
            angle = '0'
        modes = modes_gc2145
        skip = 0

    mode = modes[args.resolution]

    if args.action == "still":
        take_snapshot(node, mode, args.filename, angle, debug=args.debug)
        take_snapshot(node, mode, args.filename, angle, skip=skip, debug=args.debug, pixfmt=args.pixfmt, raw=args.raw)
    elif args.action == "movie":
        record(node, mode, args.filename, angle, debug=args.debug)
    else:


@@ 189,4 223,3 @@ def main():

if __name__ == "__main__":
    main()