Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve controls #23

Merged
merged 4 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![License][license]
[![CI][CI]](https://github.com/tiagocoutinho/v4l2py/actions/workflows/ci.yml)

Video for linux 2 (V4L2) python library
Video for Linux 2 (V4L2) python library

A two purpose API:

Expand Down
7 changes: 5 additions & 2 deletions examples/qt/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# Copyright (c) 2021 Tiago Coutinho
# Distributed under the GPLv3 license. See LICENSE for more info.

# install requirements: pip install cv2 qtpy pyqt6
# run with: QT_API=pyqt6 python widget.py
# install extra requirements:
# python3 -m pip install opencv-python qtpy pyqt6

# run from this directory with:
# QT_API=pyqt6 python widget.py

import cv2
from qtpy import QtCore, QtGui, QtWidgets
Expand Down
3 changes: 3 additions & 0 deletions examples/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# Copyright (c) 2021 Tiago Coutinho
# Distributed under the GPLv3 license. See LICENSE for more info.

# Extra dependency required to run this example:
# python3 -m pip install flask

# run from this directory with: FLASK_APP=web flask run -h 0.0.0.0

import flask
Expand Down
5 changes: 3 additions & 2 deletions examples/web/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# Distributed under the GPLv3 license. See LICENSE for more info.

# Extra dependencies required to run this example:
# pip install fastapi jinja2 python-multipart cv2 pillow uvicorn
# python3 -m pip install fastapi jinja2 python-multipart opencv-python \
# pillow uvicorn

# run from this directory with:
# uvicorn async:app
Expand Down Expand Up @@ -88,7 +89,7 @@ def cameras() -> list[Camera]:
global CAMERAS
if CAMERAS is None:
cameras = {}
for device in iter_video_capture_devices():
for device in iter_video_capture_devices(legacy_controls=True):
cameras[device.index] = Camera(device)
CAMERAS = cameras
return CAMERAS
Expand Down
37 changes: 15 additions & 22 deletions examples/web/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
# Copyright (c) 2023 Tiago Coutinho
# Distributed under the GPLv3 license. See LICENSE for more info.

# Extra dependencies required to run this example:
# pip install fastapi jinja2 python-multipart cv2 pillow uvicorn

# run from this directory with:
# uvicorn async:app

"""Common tools for async and sync web app examples"""

import io
Expand All @@ -35,22 +29,21 @@ def __init__(self, device: Device) -> None:


def frame_to_image(frame, output="jpeg"):
match frame.pixel_format:
case PixelFormat.JPEG | PixelFormat.MJPEG:
if output == "jpeg":
return to_image_send(frame.data, type=output)
else:
buff = io.BytesIO()
image = PIL.Image.open(io.BytesIO(frame.data))
case PixelFormat.GREY:
data = frame.array
data.shape = frame.height, frame.width, -1
image = PIL.Image.frombuffer("L", (frame.width, frame.height), data)
case PixelFormat.YUYV:
data = frame.array
data.shape = frame.height, frame.width, -1
rgb = cv2.cvtColor(data, cv2.COLOR_YUV2RGB_YUYV)
image = PIL.Image.fromarray(rgb)
if frame.pixel_format in (PixelFormat.JPEG, PixelFormat.MJPEG):
if output == "jpeg":
return to_image_send(frame.data, type=output)
else:
buff = io.BytesIO()
image = PIL.Image.open(io.BytesIO(frame.data))
elif frame.pixel_format == PixelFormat.GREY:
data = frame.array
data.shape = frame.height, frame.width, -1
image = PIL.Image.frombuffer("L", (frame.width, frame.height), data)
elif frame.pixel_format == PixelFormat.YUYV:
data = frame.array
data.shape = frame.height, frame.width, -1
rgb = cv2.cvtColor(data, cv2.COLOR_YUV2RGB_YUYV)
image = PIL.Image.fromarray(rgb)

buff = io.BytesIO()
image.save(buff, output)
Expand Down
2 changes: 1 addition & 1 deletion examples/web/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Distributed under the GPLv3 license. See LICENSE for more info.

# Extra dependencies required to run this example:
# pip install pillow cv2 flask gunicorn gevent
# python3 -m pip install pillow opencv-python flask gunicorn gevent

# run from this directory with:
# gunicorn --bind=0.0.0.0:8000 --log-level=debug --worker-class=gevent sync:app
Expand Down
2 changes: 1 addition & 1 deletion examples/web_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Distributed under the GPLv3 license. See LICENSE for more info.

# install dependencies with:
# pip install uvicorn fastapi
# python3 -m pip install uvicorn fastapi
#
# run from this directory with:
# uvicorn web_async:app
Expand Down