All Projects → robmarkcole → Tensorflow Lite Rest Server

robmarkcole / Tensorflow Lite Rest Server

Licence: apache-2.0
Expose tensorflow-lite models via a rest API

Projects that are alternatives of or similar to Tensorflow Lite Rest Server

Sod
An Embedded Computer Vision & Machine Learning Library (CPU Optimized & IoT Capable)
Stars: ✭ 1,460 (+3295.35%)
Mutual labels:  object-detection, face-detection, image-processing
Hass Google Coral
RETIRED - instead use https://github.com/robmarkcole/HASS-Deepstack-object
Stars: ✭ 16 (-62.79%)
Mutual labels:  home-assistant, jupyter-notebook, image-processing
Face recognition
🍎 My own face recognition with deep neural networks.
Stars: ✭ 328 (+662.79%)
Mutual labels:  object-detection, face-detection, image-processing
Computervision Recipes
Best Practices, code samples, and documentation for Computer Vision.
Stars: ✭ 8,214 (+19002.33%)
Mutual labels:  object-detection, jupyter-notebook, image-processing
Marvel
Marvel - Face Recognition With Android & OpenCV
Stars: ✭ 199 (+362.79%)
Mutual labels:  object-detection, face-detection, image-processing
Ownphotos
Self hosted alternative to Google Photos
Stars: ✭ 2,587 (+5916.28%)
Mutual labels:  object-detection, jupyter-notebook, face-detection
Getting Things Done With Pytorch
Jupyter Notebook tutorials on solving real-world problems with Machine Learning & Deep Learning using PyTorch. Topics: Face detection with Detectron 2, Time Series anomaly detection with LSTM Autoencoders, Object Detection with YOLO v5, Build your first Neural Network, Time Series forecasting for Coronavirus daily cases, Sentiment Analysis with BERT.
Stars: ✭ 738 (+1616.28%)
Mutual labels:  object-detection, jupyter-notebook, face-detection
Dataaugmentationforobjectdetection
Data Augmentation For Object Detection
Stars: ✭ 812 (+1788.37%)
Mutual labels:  object-detection, jupyter-notebook
Dmsmsgrcg
A photo OCR project aims to output DMS messages contained in sign structure images.
Stars: ✭ 18 (-58.14%)
Mutual labels:  object-detection, image-processing
Pytorch Toolbelt
PyTorch extensions for fast R&D prototyping and Kaggle farming
Stars: ✭ 942 (+2090.7%)
Mutual labels:  object-detection, image-processing
Keras Faster Rcnn
Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Stars: ✭ 28 (-34.88%)
Mutual labels:  object-detection, jupyter-notebook
Flyimg
Dockerized PHP7 application runs as a Microservice to resize and crop images on the fly. Get optimised images with MozJPEG, WebP or PNG using ImageMagick. Includes face detection, cropping, face blurring, image rotation and many other options. Abstract storage based on FlySystem in order to store images on any provider (local, AWS S3...).
Stars: ✭ 762 (+1672.09%)
Mutual labels:  face-detection, image-processing
Automatic Watermark Detection
Project for Digital Image Processing
Stars: ✭ 754 (+1653.49%)
Mutual labels:  jupyter-notebook, image-processing
Deep learning projects
Stars: ✭ 28 (-34.88%)
Mutual labels:  jupyter-notebook, image-processing
Mlkit
A collection of sample apps to demonstrate how to use Google's ML Kit APIs on Android and iOS
Stars: ✭ 949 (+2106.98%)
Mutual labels:  object-detection, face-detection
Concise Ipython Notebooks For Deep Learning
Ipython Notebooks for solving problems like classification, segmentation, generation using latest Deep learning algorithms on different publicly available text and image data-sets.
Stars: ✭ 23 (-46.51%)
Mutual labels:  jupyter-notebook, image-processing
Deepcamera
Open source face recognition on Raspberry Pi. SharpAI is open source stack for machine learning engineering with private deployment and AutoML for edge computing. DeepCamera is application of SharpAI designed for connecting computer vision model to surveillance camera. Developers can run same code on Raspberry Pi/Android/PC/AWS to boost your AI production development.
Stars: ✭ 757 (+1660.47%)
Mutual labels:  object-detection, face-detection
Geemap
A Python package for interactive mapping with Google Earth Engine, ipyleaflet, and folium
Stars: ✭ 959 (+2130.23%)
Mutual labels:  jupyter-notebook, image-processing
Simple Ssd For Beginners
This repository contains easy SSD(Single Shot MultiBox Detector) implemented with Pytorch and is easy to read and learn
Stars: ✭ 33 (-23.26%)
Mutual labels:  object-detection, jupyter-notebook
Albumentations
Fast image augmentation library and an easy-to-use wrapper around other libraries. Documentation: https://albumentations.ai/docs/ Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
Stars: ✭ 9,353 (+21651.16%)
Mutual labels:  object-detection, image-processing

tensorflow-lite-rest-server

Expose tensorflow-lite models via a rest API. Currently object, face & scene detection is supported. Can be hosted on any of the common platforms including RPi, linux desktop, Mac and Windows. A service can be used to have the server run automatically on an RPi.

Setup

In this process we create a virtual environment (venv), then install tensorflow-lite as per these instructions which is platform specific, and finally install the remaining requirements. Note on an RPi (only) it is necessary to system wide install pip3, numpy, pillow.

All instructions for mac:

python3 -m venv venv
source venv/bin/activate
pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl
pip3 install -r requirements.txt

Models

For convenience a couple of models are included in this repo and used by default. A description of each model is included in its directory. Additional models are available here.

If you want to create custom models, there is the easy way, and the longer but more flexible way. The easy way is to use teachablemachine, which I have done in this repo for the dogs-vs-cats model. The teachablemachine service is limited to image classification but is very straightforward to use. The longer way allows you to use any neural network architecture to produce a tensorflow model, which you then convert to am optimized tflite model. An example of this approach is described in this article, or jump straight to the code.

Usage

Start the tflite-server on port 5000 :

(venv) $ uvicorn tflite-server:app --reload --port 5000 --host 0.0.0.0

Or via Docker:

# Build
docker build -t tensorflow-lite-rest-server .
# Run
docker run -p 5000:5000 tensorflow-lite-rest-server:latest

You can check that the tflite-server is running by visiting http://ip:5000/ from any machine, where ip is the ip address of the host (localhost if querying from the same machine). The docs can be viewed at http://localhost:5000/docs

Post an image to detecting objects via cURL:

curl -X POST "http://localhost:5000/v1/vision/detection" -H  "accept: application/json" -H  "Content-Type: multipart/form-data" -F "[email protected]/people_car.jpg;type=image/jpeg"

Which should return:

{
  "predictions": [
    {
      "confidence": 0.93359375,
      "label": "car",
      "x_max": 619,
      "x_min": 302,
      "y_max": 348,
      "y_min": 120
    },
    {
      "confidence": 0.7890625,
      "label": "person",
      "x_max": 363,
      "x_min": 275,
      "y_max": 323,
      "y_min": 126
    },
.
.
.
'success': True}

An example request using the python requests package is in tests/live-test.py

Add tflite-server as a service

You can run tflite-server as a service, which means tflite-server will automatically start on RPi boot, and can be easily started & stopped. Create the service file in the appropriate location on the RPi using: sudo nano /etc/systemd/system/tflite-server.service

Entering the following (adapted for your tflite-server.py file location and args):

[Unit]
Description=Rest API exposing tensorflow lite models
After=network.target

[Service]
ExecStart=/home/pi/.local/bin/uvicorn tflite-server:app --reload --port 5000 --host 0.0.0.0
WorkingDirectory=/home/pi/github/tensorflow-lite-rest-server
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Once this file has been created you can to start the service using: sudo systemctl start tflite-server.service

View the status and logs with: sudo systemctl status tflite-server.service

Stop the service with: sudo systemctl stop tflite-server.service

Restart the service with: sudo systemctl restart tflite-server.service

You can have the service auto-start on rpi boot by using: sudo systemctl enable tflite-server.service

You can disable auto-start using: sudo systemctl disable tflite-server.service

Deepstack, Home Assistant & UI

This API can be used as a drop in replacement for deepstack object detection and deepstack face detection (configuring detect_only: True) in Home Assistant. I also created a UI for viewing the predictions of the object detection model here.

FastAPI vs Flask

The master branch is using FastAPI whilst I am also maintaining a Flask branch. FastAPI offers a few nice features like less boilerplate code and auto generated docs. FastAPI is also widely considered to be faster than Flask.

Platform Speed Number of predictions
Mac Pro with tflite-server (fastapi) 2.2 178
Mac Pro with tflite-server (flask) 2.1 176
RPi4 with tflite-server (fastapi) 7.6 176
RPi4 with tflite-server (flask) 8.9 159

Development

I am developing on a mac/pi4 using VScode, with black formatting & line length 120.

  • First time only, create venv: python3.7 -m venv venv
  • Activate venv: source venv/bin/activate
  • Install the dev requirements: pip3 install -r requirements.txtpip3 install -r requirements-dev.txt
  • Sort requirements: venv/bin/isort tflite-server.py
  • Black format: venv/bin/black tflite-server.py
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].