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

Added autodetection of camera #89

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion skunkbooth/data/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"SETTINGS_FILE": f"{SKUNKBOOTH_DIR}/.settings/settings.conf",
"PIC_DIR": f"{SKUNKBOOTH_DIR}/pictures",
"IMG_FORMAT": "JPG",
"LANGUAGE": "en"
"LANGUAGE": "en",
"DEVICE": str(0)
}


Expand Down
5 changes: 3 additions & 2 deletions skunkbooth/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ def CamDimensions(height: int, width: int) -> Tuple[int, int, int]:
Scene(effects, -1, name="Main"),
Scene([GalleryFrame(screen, model=image_selection)], -1, name="Gallery"),
Scene([fFrame], -1, name="Filters"),
Scene([SettingsFrame(screen)], -1, name="Settings"),
Scene([SettingsFrame(screen, webcam)], -1, name="Settings"),
Scene([PreviewFrame(screen, model=image_selection)], -1, name="Preview"),
]
screen.set_scenes(scenes, unhandled_input=global_shortcuts)
screen.lang_switch = False
screen.device_switch = False
b = a = 0
frame = 1 / 40
while True:
Expand All @@ -137,7 +138,7 @@ def CamDimensions(height: int, width: int) -> Tuple[int, int, int]:
Scene(effects, -1, name="Main"),
Scene([GalleryFrame(screen, model=image_selection)], -1, name="Gallery"),
Scene([fFrame], -1, name="Filters"),
Scene([SettingsFrame(screen)], -1, name="Settings"),
Scene([SettingsFrame(screen, webcam)], -1, name="Settings"),
Scene([PreviewFrame(screen, model=image_selection)], -1, name="Preview"),
]

Expand Down
8 changes: 4 additions & 4 deletions skunkbooth/utils/CamReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
class CamReader:
"""Utility class to operate camera hardware and capture"""

def __init__(self):
self.cap = self._open_camera()
def __init__(self, device_id: int = 0):
self.cap = self._open_camera(device_id)
self.cap.set(cv.CAP_PROP_BUFFERSIZE, 1)

@staticmethod
def _open_camera() -> cv.VideoCapture:
def _open_camera(device_id: int) -> cv.VideoCapture:
"""Opens the camera"""
# 0 -> camera number, if external camera is installed this number needs to be changed
# Since a hardware can be only accessible via one user, I/O limitation
cap = cv.VideoCapture(0)
cap = cv.VideoCapture(device_id)
if not cap.isOpened():
logging.error("Cannot open camera")
return None
Expand Down
40 changes: 38 additions & 2 deletions skunkbooth/utils/frames/settings_frame.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import logging
from gettext import translation
from typing import Any
from typing import Any, List, Tuple

import cv2 as cv
from asciimatics.event import Event, KeyboardEvent
from asciimatics.exceptions import NextScene
from asciimatics.screen import Screen
from asciimatics.widgets import Button, Frame, Label, Layout

from skunkbooth.utils.CamReader import CamReader
from skunkbooth.utils.dropdownlist import DropdownList
from skunkbooth.utils.frame import APP_TITLE
from skunkbooth.utils.settings import settings
from skunkbooth.utils.webcam import Webcam

MAX_DEVICES = 10


class SettingsFrame(Frame):
"""Recreatable frame to implement settings ui"""

def __init__(self, screen: Any) -> None:
def __init__(self, screen: Any, webcam: Webcam) -> None:
"""Initialize frame"""
super().__init__(
screen,
Expand All @@ -25,6 +30,7 @@ def __init__(self, screen: Any) -> None:
can_scroll=True,
title=APP_TITLE,
)
self._webcam = webcam
self._back_camera_button = Button(_("👈 Back to 📷"), self._switch_to_camera, add_box=True)

title_layout = Layout([1])
Expand Down Expand Up @@ -55,6 +61,36 @@ def _switchLanguage() -> None:
language._on_change = _switchLanguage
settings_layout.add_widget(language)

def _list_device_ids() -> List[int]:
"""Returns a list of device IDs."""
is_working = True
dev_port = 0
working_ports = []
while is_working and dev_port < MAX_DEVICES:
camera = cv.VideoCapture(dev_port)
if camera.isOpened():
is_working = camera.read()[0]

if is_working:
working_ports.append(dev_port)
dev_port += 1
return working_ports

def _make_device_dropdown_list(device_ids: List[int]) -> List[Tuple[str, str]]:
return list((str(id), str(id)) for id in device_ids)

def _update_device() -> None:
settings.update({"DEVICE": device.value})
webcam.camera = CamReader(int(device.value))
screen.device_switch = True

device_ids = _list_device_ids()
device = DropdownList(_make_device_dropdown_list(device_ids), _("Device"))
device.value = settings["DEVICE"]
device._on_change = _update_device

settings_layout.add_widget(device)

controls_layout = Layout([1, 1, 1])
self.add_layout(controls_layout)
controls_layout.add_widget(self._back_camera_button, 1)
Expand Down