Skip to content
Merged
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
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# lobe-python
Code to run exported Lobe models in Python.
# Lobe Python API
Code to run exported Lobe models in Python using the TensorFlow, TensorFlow Lite, or ONNX options.

## Install
```
### Linux
```shell script
# Install Python3
sudo apt update
sudo apt install -y python3-dev python3-pip
Expand All @@ -13,15 +14,36 @@ sudo apt install -y \
libatlas-base-dev \
libopenjp2-7 \
libtiff5 \
libjpeg62-turbo
libjpeg62-dev

# Install lobe-python
pip3 install setuptools git
pip3 install git+https://github.com/lobe/lobe-python
```

## Usage
_Note for Raspbian OS (Raspberry Pi)_: Please install `libjpeg62-turbo` instead of `libjpeg62-dev`

### Mac/Windows
Use a virtual environment with Python 3.7
```shell script
python3 -m venv .venv

# Mac:
source .venv/bin/activate

# Windows:
.venv\Scripts\activate
```
Install the library
```shell script
# make sure pip is up to date
python -m pip install --upgrade pip
# install
pip install git+https://github.com/lobe/lobe-python
```

## Usage
```python
from lobe import ImageModel

model = ImageModel.load('path/to/exported/model')
Expand All @@ -30,7 +52,7 @@ model = ImageModel.load('path/to/exported/model')
result = model.predict_from_file('path/to/file.jpg')

# OPTION 2: Predict from an image url
result = model.predict_from_url('http://path/to/file.jpg')
result = model.predict_from_url('http://url/to/file.jpg')

# OPTION 3: Predict from Pillow image
from PIL import Image
Expand All @@ -41,21 +63,12 @@ result = model.predict(img)
print(result.prediction)

# Print all classes
for label, prop in result.labels:
print(f"{label}: {prop*100}%")
for label, confidence in result.labels:
print(f"{label}: {confidence*100}%")

```
Note: model predict functions should be thread-safe. If you find bugs please file an issue.

## Resources

If you're running this on a Pi and having issues, and seeing this error:

```bash
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/tflite-runtime/
```

running this may help:

```bash
pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
```
See the [Raspberry Pi Trash Classifier](https://github.com/microsoft/TrashClassifier) example, and its [Adafruit Tutorial](https://learn.adafruit.com/lobe-trash-classifier-machine-learning).
12 changes: 12 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Release 0.3.0
___
## Breaking Changes
* Previous use of Signature should be ImageClassificationSignature. `from lobe.signature import Signature` ->
`from lobe.signature import ImageClassificationSignature`

## Bug Fixes and Other Improvements
* Update to TensorFlow 2.4 from 1.15.4
* Add ONNX runtime backend
* Use requests instead of urllib
* Make backends thread-safe
* Added constants file for signature keys to enable backwards-compatibility
6 changes: 3 additions & 3 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
result = model.predict_from_file('path/to/file.jpg')

# Predict from an image url
result = model.predict_from_url('http://path/to/file.jpg')
result = model.predict_from_url('http://url/to/file.jpg')

# Predict from Pillow image
from PIL import Image
Expand All @@ -18,5 +18,5 @@
print("Top prediction:", result.prediction)

# Print all classes
for label, prop in result.labels:
print(f"{label}: {prop*100:.6f}%")
for label, confidence in result.labels:
print(f"{label}: {confidence*100:.6f}%")
61 changes: 49 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
from setuptools import setup, find_packages
import sys
import platform


python_version = platform.python_version().rsplit('.', maxsplit=1)[0]

requirements = [
"pillow",
"requests",
"numpy==1.19.3",
"tensorflow==2.4;platform_machine!='armv7l'",
"onnxruntime==1.6.0;platform_machine!='armv7l'"
]

# get the right TF Lite runtime packages based on OS and python version: https://www.tensorflow.org/lite/guide/python#install_just_the_tensorflow_lite_interpreter
tflite_python = None
tflite_machine = None

# get the right python string for the version
if python_version == '3.5':
tflite_python = 'cp35-cp35m'
elif python_version == '3.6':
tflite_python = 'cp36-cp36m'
elif python_version == '3.7':
tflite_python = 'cp37-cp37m'
elif python_version == '3.8':
tflite_python = 'cp38-cp38'

# get the right machine string
if sys.platform == 'win32':
tflite_machine = 'win_amd64'
elif sys.platform == 'darwin':
tflite_machine = 'macosx_10_15_x86_64'
elif sys.platform == 'linux':
if platform.machine() == 'x86_64':
tflite_machine = 'linux_x86_64'
elif platform.machine() == 'armv7l':
tflite_machine = 'linux_armv7l'

# add it to the requirements, or print the location to find the version to install
if tflite_python and tflite_machine:
requirements.append(f"tflite_runtime @ https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-{tflite_python}-{tflite_machine}.whl")
else:
print(
f"Couldn't find tflite_runtime for your platform {sys.platform}, machine {platform.machine()}, and python version {python_version}, please see the install guide for the right version: https://www.tensorflow.org/lite/guide/python#install_just_the_tensorflow_lite_interpreter"
)

setup(
name="lobe",
version="0.2.1",
version="0.3.0",
packages=find_packages("src"),
package_dir={"": "src"},
install_requires=[
"numpy",
"pillow",
"requests",
"tensorflow>=1.15.2,<2;platform_machine!='armv7l'",
"tflite_runtime ; platform_machine=='armv7l'"
],
dependency_links=[
"https://www.piwheels.org/simple/tensorflow",
"https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl"
]
install_requires=requirements,
)
47 changes: 0 additions & 47 deletions src/lobe/ImageModel.py

This file was deleted.

89 changes: 0 additions & 89 deletions src/lobe/Signature.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/lobe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .signature import Signature
from .model.image_model import ImageModel
24 changes: 0 additions & 24 deletions src/lobe/_model.py

This file was deleted.

Loading