Archived
1

begining to work on python side

This commit is contained in:
Evan Pratten 2021-05-29 14:59:42 -04:00
parent 498eb7623c
commit 00d9758855
5 changed files with 251 additions and 74 deletions

143
.gitignore vendored
View File

@ -13,4 +13,145 @@ Cargo.lock
bazel-*
# Core files
core
core
# Generated python libraries
libpylibodm.so
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# 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
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .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/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/

22
odm/__main__.py Normal file
View File

@ -0,0 +1,22 @@
import argparse
import sys
import os
# Load in libodm
sys.path.append(os.getcwd() + "/target/debug/")
import pylibodm
def main() -> int:
# Handle program arguments
ap = argparse.ArgumentParser(prog='odm.py', description='Stream 3D data from a LeapMotion camera')
args = ap.parse_args()
# Connect to the leapmotion device
print("Connecting to LeapMotion")
print(dir(pylibodm))
pylibodm.connect(4)
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@ -22,93 +22,88 @@ mod errors {
}
}
mod device {
use std::time::Duration;
use std::time::Duration;
use libodm::{
image::Image,
leapmotion::device::{DeviceFrame, LeapMotionDevice},
};
use pyo3::prelude::*;
use libodm::{image::Image, leapmotion::device::DeviceFrame};
use pyo3::prelude::*;
use crate::{errors::PyDeviceError, DEVICE_INSTANCE};
use crate::errors::PyDeviceError;
#[pyclass]
struct PyImage {
pub width: u32,
pub height: u32,
pub buffer: Vec<u8>,
}
#[pyclass]
struct PyImage {
pub width: u32,
pub height: u32,
pub buffer: Vec<u8>,
}
impl IntoPy<PyImage> for Image<'_> {
fn into_py(self, py: Python) -> PyImage {
PyImage {
width: self.get_width(),
height: self.get_height(),
buffer: self.get_image().into_iter().map(|e| *e).collect(),
}
impl IntoPy<PyImage> for Image<'_> {
fn into_py(self, py: Python) -> PyImage {
PyImage {
width: self.get_width(),
height: self.get_height(),
buffer: self.get_image().into_iter().map(|e| *e).collect(),
}
}
}
#[pyclass]
struct PyDeviceFrame {
pub bytes_per_pixel: u8,
pub left_camera: PyImage,
pub right_camera: PyImage,
}
#[pyclass]
struct PyDeviceFrame {
pub bytes_per_pixel: u8,
pub left_camera: PyImage,
pub right_camera: PyImage,
}
impl IntoPy<PyDeviceFrame> for DeviceFrame<'_> {
fn into_py(self, py: Python) -> PyDeviceFrame {
PyDeviceFrame {
bytes_per_pixel: self.bytes_per_pixel,
left_camera: self.left_camera.into_py(py),
right_camera: self.right_camera.into_py(py),
}
impl IntoPy<PyDeviceFrame> for DeviceFrame<'_> {
fn into_py(self, py: Python) -> PyDeviceFrame {
PyDeviceFrame {
bytes_per_pixel: self.bytes_per_pixel,
left_camera: self.left_camera.into_py(py),
right_camera: self.right_camera.into_py(py),
}
}
}
#[pymodule]
pub fn leap_device(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "connect")]
fn connect(timeout_secs: u64) -> Result<(), PyDeviceError> {
// Create a leap device
let device = LeapMotionDevice::create_device(Duration::from_secs(timeout_secs))?;
#[pymodule]
pub fn pylibodm(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "connect")]
fn connect(timeout_secs: u64) -> Result<(), PyDeviceError> {
// Create a leap device
let device = LeapMotionDevice::create_device(Duration::from_secs(timeout_secs))?;
// Override the instance
unsafe {
DEVICE_INSTANCE = Some(device);
}
Ok(())
}
#[pyfn(m, "set_cameras_flipped")]
fn set_cameras_flipped(flipped: bool) -> PyResult<()> {
unsafe {
DEVICE_INSTANCE
.as_mut()
.unwrap()
.set_cameras_flipped(flipped);
}
Ok(())
}
#[pyfn(m, "get_cameras_flipped")]
fn get_cameras_flipped() -> PyResult<bool> {
Ok(unsafe { DEVICE_INSTANCE.as_ref().unwrap().cameras_flipped() })
}
#[pyfn(m, "has_frame")]
fn has_frame() -> PyResult<bool> {
Ok(unsafe { DEVICE_INSTANCE.as_mut().unwrap().has_frame() })
}
#[pyfn(m, "get_frame")]
fn get_frame(py: Python) -> Result<PyDeviceFrame, PyDeviceError> {
Ok(unsafe { DEVICE_INSTANCE.as_mut().unwrap().get_frame()?.into_py(py) })
// Override the instance
unsafe {
DEVICE_INSTANCE = Some(device);
}
Ok(())
}
#[pyfn(m, "set_cameras_flipped")]
fn set_cameras_flipped(flipped: bool) -> PyResult<()> {
unsafe {
DEVICE_INSTANCE
.as_mut()
.unwrap()
.set_cameras_flipped(flipped);
}
Ok(())
}
#[pyfn(m, "get_cameras_flipped")]
fn get_cameras_flipped() -> PyResult<bool> {
Ok(unsafe { DEVICE_INSTANCE.as_ref().unwrap().cameras_flipped() })
}
#[pyfn(m, "has_frame")]
fn has_frame() -> PyResult<bool> {
Ok(unsafe { DEVICE_INSTANCE.as_mut().unwrap().has_frame() })
}
#[pyfn(m, "get_frame")]
fn get_frame(py: Python) -> Result<PyDeviceFrame, PyDeviceError> {
Ok(unsafe { DEVICE_INSTANCE.as_mut().unwrap().get_frame()?.into_py(py) })
}
Ok(())
}

1
requirements-dev.txt Normal file
View File

@ -0,0 +1 @@
setuptools-rust>=0.10.2

18
setup.py Normal file
View File

@ -0,0 +1,18 @@
from setuptools import setup
from setuptools_rust import RustExtension
setup(
name="odm",
version="0.1.0",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Programming Language :: Python",
"Programming Language :: Rust",
],
packages=["odm"],
rust_extensions=[RustExtension("pylibodm.pylibodm", "pylibodm/Cargo.toml", debug=False)],
include_package_data=True,
zip_safe=False,
)