Skip to content

Commit

Permalink
Merge pull request #23 from otaku42/improve_controls
Browse files Browse the repository at this point in the history
Improve controls
  • Loading branch information
otaku42 committed May 19, 2023
2 parents 4a449fe + a98df12 commit f0a37ec
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
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

0 comments on commit f0a37ec

Please sign in to comment.