Compare commits
19 Commits
22-rework-
...
cpp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d01729c4b | ||
|
|
ac02297915 | ||
|
|
f63948b295 | ||
|
|
b3e4ba50a2 | ||
|
|
374d29629b | ||
|
|
0871c54dda | ||
|
|
a5e5f0f90d | ||
|
|
0b3ca62c4c | ||
|
|
a8ec8bdeab | ||
|
|
b4cb055995 | ||
|
|
cacd94da62 | ||
|
|
9b7daea177 | ||
|
|
0073d1f9c3 | ||
|
|
e750a4f523 | ||
|
|
130167b97d | ||
|
|
70198157fb | ||
|
|
0fcae4bdb1 | ||
|
|
169fe5e234 | ||
|
|
1a0e25bf83 |
160
.gitignore
vendored
160
.gitignore
vendored
@@ -1,133 +1,43 @@
|
|||||||
# Byte-compiled / optimized / DLL files
|
# Prerequisites
|
||||||
__pycache__/
|
*.d
|
||||||
*.py[cod]
|
|
||||||
*$py.class
|
|
||||||
|
|
||||||
# C extensions
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
*.so
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
# Distribution / packaging
|
# Fortran module files
|
||||||
.Python
|
*.mod
|
||||||
build/
|
*.smod
|
||||||
develop-eggs/
|
|
||||||
dist/
|
|
||||||
downloads/
|
|
||||||
eggs/
|
|
||||||
.eggs/
|
|
||||||
lib/
|
|
||||||
lib64/
|
|
||||||
parts/
|
|
||||||
sdist/
|
|
||||||
var/
|
|
||||||
wheels/
|
|
||||||
pip-wheel-metadata/
|
|
||||||
share/python-wheels/
|
|
||||||
*.egg-info/
|
|
||||||
.installed.cfg
|
|
||||||
*.egg
|
|
||||||
MANIFEST
|
|
||||||
|
|
||||||
# PyInstaller
|
# Compiled Static libraries
|
||||||
# Usually these files are written by a python script from a template
|
*.lai
|
||||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
*.la
|
||||||
*.manifest
|
*.a
|
||||||
*.spec
|
*.lib
|
||||||
|
|
||||||
# Installer logs
|
# Executables
|
||||||
pip-log.txt
|
*.exe
|
||||||
pip-delete-this-directory.txt
|
*.out
|
||||||
|
*.app
|
||||||
# Unit test / coverage reports
|
|
||||||
htmlcov/
|
|
||||||
.tox/
|
|
||||||
.nox/
|
|
||||||
.coverage
|
|
||||||
.coverage.*
|
|
||||||
.cache
|
|
||||||
nosetests.xml
|
|
||||||
coverage.xml
|
|
||||||
*.cover
|
|
||||||
*.py,cover
|
|
||||||
.hypothesis/
|
|
||||||
.pytest_cache/
|
|
||||||
|
|
||||||
# Translations
|
|
||||||
*.mo
|
|
||||||
*.pot
|
|
||||||
|
|
||||||
# Django stuff:
|
|
||||||
*.log
|
|
||||||
local_settings.py
|
|
||||||
db.sqlite3
|
|
||||||
db.sqlite3-journal
|
|
||||||
|
|
||||||
# Flask stuff:
|
|
||||||
instance/
|
|
||||||
.webassets-cache
|
|
||||||
|
|
||||||
# Scrapy stuff:
|
|
||||||
.scrapy
|
|
||||||
|
|
||||||
# Sphinx documentation
|
|
||||||
docs/_build/
|
|
||||||
|
|
||||||
# PyBuilder
|
|
||||||
target/
|
|
||||||
|
|
||||||
# Jupyter Notebook
|
|
||||||
.ipynb_checkpoints
|
|
||||||
|
|
||||||
# IPython
|
|
||||||
profile_default/
|
|
||||||
ipython_config.py
|
|
||||||
|
|
||||||
# pyenv
|
|
||||||
.python-version
|
|
||||||
|
|
||||||
# pipenv
|
|
||||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
||||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
||||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
||||||
# install all needed dependencies.
|
|
||||||
#Pipfile.lock
|
|
||||||
|
|
||||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
||||||
__pypackages__/
|
|
||||||
|
|
||||||
# Celery stuff
|
|
||||||
celerybeat-schedule
|
|
||||||
celerybeat.pid
|
|
||||||
|
|
||||||
# SageMath parsed files
|
|
||||||
*.sage.py
|
|
||||||
|
|
||||||
# Environments
|
|
||||||
.env
|
|
||||||
.venv
|
|
||||||
env/
|
|
||||||
venv/
|
|
||||||
ENV/
|
|
||||||
env.bak/
|
|
||||||
venv.bak/
|
|
||||||
|
|
||||||
# Spyder project settings
|
|
||||||
.spyderproject
|
|
||||||
.spyproject
|
|
||||||
|
|
||||||
# Rope project settings
|
|
||||||
.ropeproject
|
|
||||||
|
|
||||||
# mkdocs documentation
|
|
||||||
/site
|
|
||||||
|
|
||||||
# mypy
|
|
||||||
.mypy_cache/
|
|
||||||
.dmypy.json
|
|
||||||
dmypy.json
|
|
||||||
|
|
||||||
# Pyre type checker
|
|
||||||
.pyre/
|
|
||||||
|
|
||||||
|
# Images taken by the camera
|
||||||
images/
|
images/
|
||||||
|
|
||||||
.vscode
|
# VSCode stuff
|
||||||
|
.vscode/
|
||||||
|
.devcontainer/
|
||||||
|
*.code-workspace
|
||||||
|
|
||||||
|
# Build folder
|
||||||
|
build/
|
||||||
|
|||||||
57
.gitlab-ci.yml
Normal file
57
.gitlab-ci.yml
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
# This file is a template, and might need editing before it works on your project.
|
||||||
|
# To contribute improvements to CI/CD templates, please follow the Development guide at:
|
||||||
|
# https://docs.gitlab.com/ee/development/cicd/templates.html
|
||||||
|
# This specific template is located at:
|
||||||
|
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
|
||||||
|
|
||||||
|
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
|
||||||
|
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
|
||||||
|
# it uses echo commands to simulate the pipeline execution.
|
||||||
|
#
|
||||||
|
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
|
||||||
|
# Stages run in sequential order, but jobs within stages run in parallel.
|
||||||
|
#
|
||||||
|
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
|
||||||
|
|
||||||
|
image: debian:stable
|
||||||
|
|
||||||
|
stages: # List of stages for jobs, and their order of execution
|
||||||
|
- build
|
||||||
|
- test
|
||||||
|
- deploy
|
||||||
|
|
||||||
|
build-job: # This job runs in the build stage, which runs first.
|
||||||
|
stage: build
|
||||||
|
tags:
|
||||||
|
- linux
|
||||||
|
before_script:
|
||||||
|
- apt update
|
||||||
|
- apt install -y clang cmake libgtkmm-3.0-dev clang-tidy
|
||||||
|
script:
|
||||||
|
- echo "Configuring"
|
||||||
|
- export CC=/usr/bin/clang
|
||||||
|
- export CXX=/usr/bin/clang++
|
||||||
|
- cmake -B build .
|
||||||
|
- echo "Building"
|
||||||
|
- cmake --build build
|
||||||
|
- echo "Compile complete."
|
||||||
|
artifacts:
|
||||||
|
paths:
|
||||||
|
- build/src/JAMCS
|
||||||
|
|
||||||
|
# unit-test-job: # This job runs in the test stage.
|
||||||
|
# stage: test # It only starts when the job in the build stage completes successfully.
|
||||||
|
# tags:
|
||||||
|
# - linux
|
||||||
|
# script:
|
||||||
|
# - echo "Running unit tests... This will take about 60 seconds."
|
||||||
|
# - sleep 60
|
||||||
|
# - echo "Code coverage is 90%"
|
||||||
|
|
||||||
|
# deploy-job: # This job runs in the deploy stage.
|
||||||
|
# stage: deploy # It only runs when *both* jobs in the test stage complete successfully.
|
||||||
|
# tags:
|
||||||
|
# - linux
|
||||||
|
# script:
|
||||||
|
# - echo "Deploying application..."
|
||||||
|
# - echo "Application successfully deployed."
|
||||||
12
CMakeLists.txt
Normal file
12
CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,bugprone-*,concurrency-*,cppcoreguidelines-*,modernize-*,performance-*,readability-*,)
|
||||||
|
|
||||||
|
project(PiCamera)
|
||||||
|
|
||||||
|
# Find gtk+-4.0 library
|
||||||
|
find_package(PkgConfig REQUIRED)
|
||||||
|
pkg_check_modules(GTK REQUIRED gtkmm-3.0)
|
||||||
|
|
||||||
|
add_subdirectory(src)
|
||||||
63
README.md
63
README.md
@@ -1,47 +1,28 @@
|
|||||||
# JamesCam
|
# JAMCS - James Camera Software
|
||||||
Camera for Raspberry Pi
|
Camera for Raspberry Pi in C++ :)
|
||||||
|
|
||||||
## How to run
|
## To build
|
||||||
Either:
|
Requirements:
|
||||||
```
|
- CMake
|
||||||
chmod +x ./start.sh
|
- gtkmm 3.0
|
||||||
./start.sh
|
|
||||||
```
|
Optional:
|
||||||
Or:
|
- clang-tidy
|
||||||
```
|
|
||||||
python3 src/main.py
|
### Debian based
|
||||||
|
```bash
|
||||||
|
sudo apt install -y clang cmake libgtkmm-3.0-dev clang-tidy
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
### Fedora
|
||||||
Check `config.json` for an example configuration.
|
```bash
|
||||||
|
sudo dnf install clang cmake gtkmm30-devel clang-tools-extra
|
||||||
### `camera_type`
|
|
||||||
Specifies the camera type (`picam` interface implementation) to use.
|
|
||||||
```
|
|
||||||
1: legacy picamera implementation
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### `capture_resolution`
|
Then:
|
||||||
Specifies the resolution of the photos to take.
|
```bash
|
||||||
|
cmake -B build .
|
||||||
|
cmake --build build
|
||||||
|
```
|
||||||
|
|
||||||
The X value will be rounded up to a multiple of 32, and the Y value to a multiple of 16, otherwise we'll get bugs due to the behaviour described in the [docs](https://picamera.readthedocs.io/en/release-1.13/recipes2.html#unencoded-image-capture-yuv-format)
|
Alternatively you can just use the VSCode CMake Extension
|
||||||
|
|
||||||
|
|
||||||
### `preview_resolution`
|
|
||||||
Specifies the resolution of the viewfinder. I've set this to the resolution of the hyperpixel4 display
|
|
||||||
|
|
||||||
### `output_format`
|
|
||||||
Specifies the output format of the image. Check supported formats using `python3 -m PIL`
|
|
||||||
|
|
||||||
### `output_extension`
|
|
||||||
Specifies the file extension of the image. Should probably match the `output_format` option.
|
|
||||||
|
|
||||||
### `output_directory`
|
|
||||||
Specifies the directory to put taken images in to.
|
|
||||||
|
|
||||||
### `output_filename_format`
|
|
||||||
Specifies the format of the output filename.
|
|
||||||
> See [Python docs](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) for reference.
|
|
||||||
|
|
||||||
### `output_jpeg_quality`
|
|
||||||
Specify the quality of JPEG compression, best not to go over 95
|
|
||||||
|
|||||||
10
config.json
10
config.json
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"camera_type": 1,
|
|
||||||
"capture_resolution": [3000, 2000],
|
|
||||||
"preview_resolution": [800, 480],
|
|
||||||
"output_format": "JPEG",
|
|
||||||
"output_extension": ".jpg",
|
|
||||||
"output_directory": "./images",
|
|
||||||
"output_filename_format": "%d-%m-%Y %H-%M-%S",
|
|
||||||
"output_jpeg_quality": 90
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
pygame==2.1.2
|
|
||||||
pygame-gui==0.6.4
|
|
||||||
picamera==1.13
|
|
||||||
pillow==9.2.0
|
|
||||||
20
src/CMakeLists.txt
Normal file
20
src/CMakeLists.txt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
add_executable(
|
||||||
|
JAMCS
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
target_include_directories(
|
||||||
|
JAMCS
|
||||||
|
PRIVATE
|
||||||
|
${GTK_INCLUDE_DIRS}
|
||||||
|
)
|
||||||
|
target_link_directories(
|
||||||
|
JAMCS
|
||||||
|
PRIVATE
|
||||||
|
${GTK_LIBRARY_DIRS}
|
||||||
|
)
|
||||||
|
target_link_libraries(
|
||||||
|
JAMCS
|
||||||
|
PRIVATE
|
||||||
|
${GTK_LIBRARIES}
|
||||||
|
)
|
||||||
|
add_definitions (${GTK_CFLAGS_OTHER})
|
||||||
50
src/main.cpp
Normal file
50
src/main.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <gtkmm.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class CloseButton : public Gtk::Button
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void on_button_clicked();
|
||||||
|
Glib::RefPtr<Gtk::Application> app;
|
||||||
|
public:
|
||||||
|
CloseButton(const Glib::ustring &label, Glib::RefPtr<Gtk::Application> app);
|
||||||
|
};
|
||||||
|
|
||||||
|
CloseButton::CloseButton(const Glib::ustring &label, Glib::RefPtr<Gtk::Application> app)
|
||||||
|
{
|
||||||
|
this->app = app;
|
||||||
|
|
||||||
|
set_label(label);
|
||||||
|
|
||||||
|
signal_clicked().connect(sigc::mem_fun(*this, &CloseButton::on_button_clicked));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void CloseButton::on_button_clicked()
|
||||||
|
{
|
||||||
|
this->app.get()->quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyWindow : public Gtk::Window
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MyWindow();
|
||||||
|
};
|
||||||
|
|
||||||
|
MyWindow::MyWindow()
|
||||||
|
{
|
||||||
|
set_title("Basic application");
|
||||||
|
set_default_size(200, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
auto app = Gtk::Application::create("org.gtkmm.examples.base");
|
||||||
|
MyWindow window;
|
||||||
|
|
||||||
|
CloseButton button("Close", app);
|
||||||
|
window.add(button);
|
||||||
|
window.show_all();
|
||||||
|
|
||||||
|
return app->run(window);
|
||||||
|
}
|
||||||
74
src/main.py
74
src/main.py
@@ -1,74 +0,0 @@
|
|||||||
from picam_manager.picam_manager import PiCamManager
|
|
||||||
from picamui.picamui import PiCamUi
|
|
||||||
|
|
||||||
import json
|
|
||||||
from PIL import Image
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
CONFIG_PATH = "./config.json"
|
|
||||||
|
|
||||||
def load_config() -> dict:
|
|
||||||
"""Load JSON config from disk
|
|
||||||
|
|
||||||
:returns: Loaded JSON configuration as a dict
|
|
||||||
"""
|
|
||||||
with open(CONFIG_PATH, 'rt') as config_file:
|
|
||||||
return json.load(config_file)
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
"""Main function for JamesCam"""
|
|
||||||
config = load_config()
|
|
||||||
|
|
||||||
# Setup UI
|
|
||||||
ui = PiCamUi()
|
|
||||||
ui.createUi()
|
|
||||||
|
|
||||||
# Setup camera
|
|
||||||
cam_manager = PiCamManager(config)
|
|
||||||
camera = cam_manager.camera
|
|
||||||
|
|
||||||
loop = True
|
|
||||||
while loop:
|
|
||||||
preview_bytes = camera.capture_preview()
|
|
||||||
|
|
||||||
ui.updatePreview(preview_bytes, camera.preview_resolution)
|
|
||||||
ui.update()
|
|
||||||
|
|
||||||
ui_events = ui.getEvents()
|
|
||||||
for event in ui_events:
|
|
||||||
if event == 'keyDownEscape' or event == 'pygameQuit' or event == 'btnExitPressed':
|
|
||||||
loop = False
|
|
||||||
elif event == 'btnTakePressed':
|
|
||||||
now = datetime.now()
|
|
||||||
capture_bytes = camera.capture()
|
|
||||||
|
|
||||||
pil_image = Image.frombuffer(mode='RGB', size=camera.capture_resolution, data=bytes(capture_bytes))
|
|
||||||
|
|
||||||
formatted_filename = now.strftime(config['output_filename_format'])
|
|
||||||
filename = f'{formatted_filename}{config["output_extension"]}'
|
|
||||||
|
|
||||||
with open(f'{config["output_directory"]}/{filename}', "wb") as output_file:
|
|
||||||
if config['output_format'].lower() == 'jpeg':
|
|
||||||
pil_image.save(output_file, format=config["output_format"], quality=config["output_jpeg_quality"])
|
|
||||||
else:
|
|
||||||
pil_image.save(output_file, format=config["output_format"])
|
|
||||||
else:
|
|
||||||
print('Unknown event {}'.format(event))
|
|
||||||
|
|
||||||
ui.cleanup()
|
|
||||||
|
|
||||||
|
|
||||||
def print_ascii_art() -> None:
|
|
||||||
"""Extremely important function"""
|
|
||||||
print("""
|
|
||||||
██ █████ ███ ███ ███████ ███████ ██████ █████ ███ ███
|
|
||||||
██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ████ ████
|
|
||||||
██ ███████ ██ ████ ██ █████ ███████ ██ ███████ ██ ████ ██
|
|
||||||
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
||||||
█████ ██ ██ ██ ██ ███████ ███████ ██████ ██ ██ ██ ██
|
|
||||||
|
|
||||||
""")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print_ascii_art()
|
|
||||||
main()
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
from utils.utils import round_resolution
|
|
||||||
|
|
||||||
ROUND_MULTIPLES = (32, 16)
|
|
||||||
|
|
||||||
class PiCam:
|
|
||||||
def __init__(self, config: dict):
|
|
||||||
self.capture_resolution = round_resolution(config["capture_resolution"], ROUND_MULTIPLES)
|
|
||||||
self.preview_resolution = round_resolution(config["preview_resolution"], ROUND_MULTIPLES)
|
|
||||||
|
|
||||||
def set_capture_resolution(self, resolution: "tuple[int, int]") -> None:
|
|
||||||
"""Set capture resolution for camera
|
|
||||||
|
|
||||||
:param resolution: New resolution for camera captures
|
|
||||||
"""
|
|
||||||
rounded_resolution = round_resolution(resolution, ROUND_MULTIPLES)
|
|
||||||
self.capture_resolution = rounded_resolution
|
|
||||||
|
|
||||||
def set_preview_resolution(self, resolution: "tuple[int, int]") -> None:
|
|
||||||
"""Set resolution for camera preview (viewfinder)
|
|
||||||
|
|
||||||
:param resolution: New resolution for camera preview
|
|
||||||
"""
|
|
||||||
rounded_resolution = round_resolution(resolution, ROUND_MULTIPLES)
|
|
||||||
self.preview_resolution = rounded_resolution
|
|
||||||
|
|
||||||
def capture(self) -> bytearray:
|
|
||||||
"""Capture an image
|
|
||||||
|
|
||||||
:returns: Image data as a byte buffer
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def capture_preview(self) -> bytearray:
|
|
||||||
"""Capture a preview image for the viewfinder
|
|
||||||
|
|
||||||
:returns: Image data as a byte buffer
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
from picam.picam import PiCam
|
|
||||||
|
|
||||||
from io import BytesIO
|
|
||||||
from picamera import PiCamera
|
|
||||||
|
|
||||||
class PiCam1(PiCam):
|
|
||||||
"""Implementation of PiCam class using legacy picamera module"""
|
|
||||||
def __init__(self, config: dict):
|
|
||||||
super().__init__(config)
|
|
||||||
|
|
||||||
self.init_camera()
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
"""Destructor - cleans up picamera"""
|
|
||||||
self.close_camera()
|
|
||||||
|
|
||||||
def set_camera_resolution(self, resolution: "tuple[int, int]") -> None:
|
|
||||||
"""Set resolution of active camera
|
|
||||||
|
|
||||||
:param resolution: Resolution to set camera to
|
|
||||||
"""
|
|
||||||
self.camera.resolution = resolution
|
|
||||||
|
|
||||||
def set_preview_resolution(self, resolution: "tuple[int, int]") -> None:
|
|
||||||
super().set_preview_resolution(resolution)
|
|
||||||
|
|
||||||
self.set_camera_resolution(self.preview_resolution)
|
|
||||||
|
|
||||||
def init_camera(self) -> None:
|
|
||||||
"""Initialize picamera camera object"""
|
|
||||||
self.camera = PiCamera(resolution=self.preview_resolution)
|
|
||||||
|
|
||||||
def close_camera(self) -> None:
|
|
||||||
"""Close picamera camera object"""
|
|
||||||
self.camera.close()
|
|
||||||
self.camera = None
|
|
||||||
|
|
||||||
def capture(self, preview: bool = False) -> bytearray:
|
|
||||||
# Bytes to hold output buffer
|
|
||||||
if preview:
|
|
||||||
# Multiply by 3 for individual R, G, B values
|
|
||||||
out = bytearray((self.preview_resolution[0] * self.preview_resolution[1]) * 3)
|
|
||||||
else:
|
|
||||||
out = bytearray((self.capture_resolution[0] * self.capture_resolution[1]) * 3)
|
|
||||||
self.set_camera_resolution(self.capture_resolution) # If preview we'll already have the right resolution
|
|
||||||
|
|
||||||
bytes_stream = BytesIO()
|
|
||||||
self.camera.capture(bytes_stream, format='rgb', use_video_port=preview)
|
|
||||||
|
|
||||||
bytes_stream.seek(0)
|
|
||||||
bytes_stream.readinto(out)
|
|
||||||
bytes_stream.close()
|
|
||||||
|
|
||||||
if not preview:
|
|
||||||
# If preview we'll already have the right resolution
|
|
||||||
self.set_camera_resolution(self.preview_resolution)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def capture_preview(self) -> bytearray:
|
|
||||||
return self.capture(preview=True)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
from picam.picam1 import PiCam1
|
|
||||||
|
|
||||||
class PiCamManager:
|
|
||||||
def __init__(self, config: dict):
|
|
||||||
self.config = config
|
|
||||||
|
|
||||||
if config['camera_type'] == 1:
|
|
||||||
self.camera = PiCam1(config)
|
|
||||||
else:
|
|
||||||
raise Exception(f'Unsupported camera type {config["camera_type"]}')
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
import pygame
|
|
||||||
import pygame_gui
|
|
||||||
|
|
||||||
class PiCamUi:
|
|
||||||
screen = None
|
|
||||||
guiManager = None
|
|
||||||
clock = None
|
|
||||||
uiElements = []
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
pygame.init()
|
|
||||||
# Set the cursor to invisible. We use a transparent cursor for this as set_visible breaks the touch screen
|
|
||||||
pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
|
|
||||||
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # (0, 0) means same as display resolution
|
|
||||||
|
|
||||||
def createUi(self):
|
|
||||||
self.guiManager = pygame_gui.UIManager((self.screen.get_width(), self.screen.get_height()))
|
|
||||||
self.clock = pygame.time.Clock()
|
|
||||||
|
|
||||||
self.createUiElements()
|
|
||||||
|
|
||||||
def createUiElements(self):
|
|
||||||
btnTake = {
|
|
||||||
"name": "btnTake",
|
|
||||||
"element": pygame_gui.elements.UIButton(
|
|
||||||
relative_rect = pygame.Rect((720, 200), (80, 80)),
|
|
||||||
text = "Take",
|
|
||||||
manager = self.guiManager
|
|
||||||
)
|
|
||||||
}
|
|
||||||
self.uiElements.append(btnTake)
|
|
||||||
|
|
||||||
btnExit = {
|
|
||||||
"name": "btnExit",
|
|
||||||
"element": pygame_gui.elements.UIButton(
|
|
||||||
relative_rect=pygame.Rect((760, 440), (40, 40)),
|
|
||||||
text='X',
|
|
||||||
manager=self.guiManager
|
|
||||||
)
|
|
||||||
}
|
|
||||||
self.uiElements.append(btnExit)
|
|
||||||
|
|
||||||
def updatePreview(self, rgb, res):
|
|
||||||
img = pygame.image.frombuffer(rgb, res, 'RGB')
|
|
||||||
img = pygame.transform.scale(img, (self.screen.get_width(), self.screen.get_height()))
|
|
||||||
|
|
||||||
if img:
|
|
||||||
self.screen.blit(img, (0, 0))
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
self.guiManager.draw_ui(self.screen)
|
|
||||||
pygame.display.update()
|
|
||||||
|
|
||||||
def cleanup(self):
|
|
||||||
pygame.display.quit()
|
|
||||||
|
|
||||||
def getEvents(self):
|
|
||||||
events = []
|
|
||||||
time_delta = self.clock.tick(30)/1000.0
|
|
||||||
for event in pygame.event.get():
|
|
||||||
if event.type == pygame.KEYDOWN:
|
|
||||||
if event.key == pygame.K_ESCAPE:
|
|
||||||
events.append("keyDownEscape")
|
|
||||||
elif event.type == pygame.QUIT:
|
|
||||||
events.append("pygameQuit")
|
|
||||||
elif event.type == pygame.USEREVENT:
|
|
||||||
# Shoul be UI_BUTTON_PRESSED, but using on hover for now as having touch screen problems
|
|
||||||
if event.user_type == pygame_gui.UI_BUTTON_ON_HOVERED:
|
|
||||||
for element in self.uiElements:
|
|
||||||
if event.ui_element == element.get("element"):
|
|
||||||
events.append("{}Pressed".format(element.get("name")))
|
|
||||||
self.guiManager.process_events(event)
|
|
||||||
self.guiManager.update(time_delta)
|
|
||||||
|
|
||||||
return events
|
|
||||||
|
|
||||||
def getScreenResolution(self):
|
|
||||||
return (self.screen.get_width(), self.screen.get_height())
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
def round_value_up(value: int, multiple: int) -> int:
|
|
||||||
"""Round value up to the nearest of the given multiple
|
|
||||||
|
|
||||||
:param value: Value to round
|
|
||||||
:param multiple: Multiple to round the value up to
|
|
||||||
:returns: Rounded value
|
|
||||||
"""
|
|
||||||
value_mod = value % multiple
|
|
||||||
return value + (multiple - value_mod)
|
|
||||||
|
|
||||||
def round_resolution(resolution: "tuple[int, int]", multiples: "tuple[int, int]") -> "tuple[int, int]":
|
|
||||||
"""Round resolution up to the nearest of the given multiples
|
|
||||||
|
|
||||||
:param resolution: Resolution to round
|
|
||||||
:param multiples: Multiples to round the resolution values to
|
|
||||||
:returns: Tuple containing rounded resolution
|
|
||||||
"""
|
|
||||||
return (round_value_up(resolution[0], multiples[0]), round_value_up(resolution[1], multiples[1]))
|
|
||||||
Reference in New Issue
Block a user