Skip to content

Commit

Permalink
add camera gui example.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgineer85 committed Nov 3, 2024
1 parent c4194bd commit abf7643
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
89 changes: 89 additions & 0 deletions examples/gui/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import argparse
import os
import sys

import requests
from PySide6 import QtWidgets
from PySide6.QtCore import QUrl
from PySide6.QtGui import QIcon
from PySide6.QtMultimedia import QMediaPlayer
from PySide6.QtMultimediaWidgets import QVideoWidget
from PySide6.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget

from wigglecam.__version__ import __version__

basedir = os.path.dirname(__file__)
parser = argparse.ArgumentParser()
parser.add_argument("--base_url", action="store", default="http://127.0.0.1:8000", help="Base URL to connect to (default: %(default)s).")

session = requests.Session()


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, args):
super().__init__()

self._args = args

self.setWindowTitle(f"Wigglecam {__version__}")

layout = QVBoxLayout()
# layout.setContentsMargins(0, 0, 0, 0)

player = QMediaPlayer(parent=self) # why parent: https://stackoverflow.com/a/67093019
videoWidget = QVideoWidget()
# player.setSource(QUrl("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"))
player.setSource(QUrl(f"{self._args.base_url}/api/camera/stream.mjpg"))
player.setVideoOutput(videoWidget)
player.play()
layout.addWidget(videoWidget)

button = QPushButton("Capture")
button.setIcon(QIcon.fromTheme("camera-photo"))
button.pressed.connect(self.click_shutter)
layout.addWidget(button)

statusbar = QLabel(f"Wigglecam {__version__}")
statusbar.setMargin(0)
statusbar.setFixedHeight(20)
layout.addWidget(statusbar)

container = QWidget()
container.setLayout(layout)

self.setCentralWidget(container)

self.show()

def click_shutter(self):
try:
r = session.get(f"{self._args.base_url}/api/job/trigger")
r.raise_for_status()
except Exception as exc:
print("Error occured 😔")
print(exc)
else:
print("trigger successful")
# since trigger is asynchron executed using the GPIO, there is no way to get any results...


def main(args=None):
args = parser.parse_args(args) # parse here, not above because pytest system exit 2

app = QtWidgets.QApplication()
main_win = MainWindow(args)

available_geometry = main_win.screen().availableGeometry()
main_win.resize(available_geometry.width() / 4, available_geometry.height() / 3)

print("✨ starting camera gui")
try:
app.exec()
except KeyboardInterrupt:
pass

print("exit app ✨")


if __name__ == "__main__":
sys.exit(main(args=sys.argv[1:])) # for testing
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ dependencies = [
"opencv-python-headless",
]

[project.optional-dependencies]
gui = [
"pyside6>=6.8.0.2",
]

[project.urls]
homepage = "https://github.com/mgineer85/photobooth-wigglecam"
Expand All @@ -51,6 +55,7 @@ documentation = "https://github.com/mgineer85/photobooth-wigglecam"

[project.scripts]
wigglecam-mobile = "examples.lowlevel.gpio:main"
wigglecam-gui = "examples.gui.camera:main"
wigglecam-node = "wigglecam.__main__:main"

[tool.pdm]
Expand Down

0 comments on commit abf7643

Please sign in to comment.