|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import selectors |
| 4 | + |
| 5 | +from OpenGL import GL |
| 6 | + |
| 7 | +from linuxpy.video.device import Device, PixelFormat, VideoCapture |
| 8 | + |
| 9 | +OPENCV_FORMATS = { |
| 10 | + PixelFormat.MJPEG, |
| 11 | + PixelFormat.YUYV, |
| 12 | + PixelFormat.YVYU, |
| 13 | + PixelFormat.UYVY, |
| 14 | + PixelFormat.YUV420, |
| 15 | + PixelFormat.NV12, |
| 16 | + PixelFormat.NV21, |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def opencv_decode_frame(frame): |
| 21 | + import cv2 |
| 22 | + |
| 23 | + data, fmt = frame.array, frame.pixel_format |
| 24 | + if fmt == PixelFormat.MJPEG: |
| 25 | + result = cv2.imdecode(data, cv2.IMREAD_COLOR) |
| 26 | + else: |
| 27 | + YUV_MAP = { |
| 28 | + PixelFormat.YUYV: cv2.COLOR_YUV2RGB_YUYV, |
| 29 | + PixelFormat.YVYU: cv2.COLOR_YUV2RGB_YVYU, |
| 30 | + PixelFormat.UYVY: cv2.COLOR_YUV2RGB_UYVY, |
| 31 | + PixelFormat.YUV420: cv2.COLOR_YUV2RGB_I420, |
| 32 | + PixelFormat.NV12: cv2.COLOR_YUV2RGB_NV12, |
| 33 | + PixelFormat.NV21: cv2.COLOR_YUV2RGB_NV21, |
| 34 | + } |
| 35 | + data = frame.array |
| 36 | + if fmt in {PixelFormat.NV12, PixelFormat.NV21, PixelFormat.YUV420}: |
| 37 | + data.shape = frame.height * 3 // 2, frame.width, -1 |
| 38 | + else: |
| 39 | + data.shape = frame.height, frame.width, -1 |
| 40 | + result = cv2.cvtColor(data, YUV_MAP[fmt]) |
| 41 | + return result, GL.GL_BGR |
| 42 | + |
| 43 | + |
| 44 | +def decode_frame(frame): |
| 45 | + fmt = frame.pixel_format |
| 46 | + if fmt == PixelFormat.RGB24: |
| 47 | + return frame.data, GL.GL_RGB |
| 48 | + elif fmt == PixelFormat.BGR24: |
| 49 | + return frame.data, GL.GL_BGR |
| 50 | + elif fmt in OPENCV_FORMATS: |
| 51 | + return opencv_decode_frame(frame) |
| 52 | + |
| 53 | + |
| 54 | +def maybe_frames(capture): |
| 55 | + device = capture.device |
| 56 | + selector = selectors.DefaultSelector() |
| 57 | + selector.register(device, selectors.EVENT_READ) |
| 58 | + stream = iter(capture) |
| 59 | + try: |
| 60 | + while True: |
| 61 | + if selector.select(0): |
| 62 | + frame = next(stream) |
| 63 | + frame.user_data = decode_frame(frame) |
| 64 | + yield frame |
| 65 | + else: |
| 66 | + yield None |
| 67 | + finally: |
| 68 | + selector.unregister(device) |
| 69 | + |
| 70 | + |
| 71 | +def device_text(text): |
| 72 | + try: |
| 73 | + return Device.from_id(int(text)) |
| 74 | + except ValueError: |
| 75 | + return Device(text) |
| 76 | + |
| 77 | + |
| 78 | +def frame_size(text): |
| 79 | + w, h = text.split("x", 1) |
| 80 | + return int(w), int(h) |
| 81 | + |
| 82 | + |
| 83 | +def frame_format(text): |
| 84 | + return PixelFormat[text] |
| 85 | + |
| 86 | + |
| 87 | +def cli(): |
| 88 | + parser = argparse.ArgumentParser() |
| 89 | + parser.add_argument("--log-level", choices=["debug", "info", "warning", "error"], default="info") |
| 90 | + parser.add_argument("--nb-buffers", type=int, default=2) |
| 91 | + parser.add_argument("--frame-rate", type=float, default=10) |
| 92 | + parser.add_argument("--frame-size", type=frame_size, default="640x480") |
| 93 | + parser.add_argument("--frame-format", type=frame_format, default="RGB24") |
| 94 | + parser.add_argument("device", type=device_text) |
| 95 | + return parser |
| 96 | + |
| 97 | + |
| 98 | +def main(run, args=None): |
| 99 | + parser = cli() |
| 100 | + args = parser.parse_args(args=args) |
| 101 | + fmt = "%(threadName)-10s %(asctime)-15s %(levelname)-5s %(name)s: %(message)s" |
| 102 | + logging.basicConfig(level=args.log_level.upper(), format=fmt) |
| 103 | + width, height = args.frame_size |
| 104 | + aspect = height / width |
| 105 | + try: |
| 106 | + with args.device as device: |
| 107 | + device.set_input(0) |
| 108 | + capture = VideoCapture(device, size=args.nb_buffers) |
| 109 | + capture.set_fps(args.frame_rate) |
| 110 | + capture.set_format(width, height, args.frame_format) |
| 111 | + fmt = capture.get_format() |
| 112 | + window_width = 1980 |
| 113 | + window_height = int(window_width * aspect) |
| 114 | + run(capture, fmt, window_width, window_height) |
| 115 | + except KeyboardInterrupt: |
| 116 | + logging.info("Ctrl-C pressed. Bailing out") |
0 commit comments