Skip to content

A tensorflow object detection model served to the internet on a Rails server (TensorFlow on Rails-- if you will)

Notifications You must be signed in to change notification settings

donrestarone/object-detectr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Object Detectr

A tensorflow object detection model served to the internet on a Rails server.

Table of Contents

Demo

Installation

Troubleshooting

The tensorflow model integrated into this app is quite good at detecting people

Demo of object detection (humans and cars)

Its also an excellent cat detector

Demo of object detection (cat)

Here are a few more detections using this application.

people detection

food detection

horse and human detection

cat and couch detection

FOR THE AI SIDE (setup protoc, python and install dependencies + script)

  1. The python environment is setup assuming that you are using pyenv and have already installed and set the python version in the project directory
  2. If you are setting up python from scratch you can use this gist
  3. ensure that python version is 3.7.9
python --version

Clone the repository jump to the models research directory and run the build and install scripts

cd object_detectr/models/research
python setup.py build
python setup.py install

grab dependencies

pip install TensorFlow==1.15 lxml pillow matplotlib jupyter contextlib2 cython tf_slim
# alternatively you can make sure that pip installs the libraries compatible with your specific python version (3.7.9 is recommended in our case)
python -m pip install TensorFlow==1.15 lxml pillow matplotlib jupyter contextlib2 cython tf_slim
# sometimes on EC2 there is not enough cache for installing these heavy libraries. pass the no cache option if installation is failing silently
python -m pip install --no-cache-dir TensorFlow==1.15 lxml pillow matplotlib jupyter contextlib2 cython tf_slim

FOR THE RAILS SIDE

  1. If you are setting up rails from scratch (like on a server) you can use this gist: https://gist.github.com/donrestarone/0ebe1e87b39f0f92f850c1da861e85de
  2. for a production nginx configuration (super hacky) you can use this gist: https://gist.github.com/donrestarone/e8b1157dc4a3adffe3450edd55a6f315
cd object_detectr/potash
bundle install
rails db:create
rails db:migrate
rails s

Running in production

create systemctl service for starting puma on boot. make sure ExecStart/ExecStop are pointing to the rbenv binary and the User has ownership over the var/www directory. Create the systemd file at: /etc/systemd/system/puma.service

[Unit]
Description=Puma Rails Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/var/www/object-detectr/potash
EnvironmentFile=/home/ubuntu/.potash_config
ExecStart=/home/ubuntu/.rbenv/bin/rbenv exec bundle exec puma -C /var/www/object-detectr/potash/config/puma.rb
ExecStop=/home/ubuntu/.rbenv/bin/rbenv exec bundle exec pumactl -S /var/www/object-detectr/potash/tmp/pids/puma.state stop
TimeoutSec=15
Restart=always


[Install]
WantedBy=multi-user.target

The EnvironmentFile should look something like this (~/.potash_config)

RAILS_ENV=production
RAILS_SERVE_STATIC_FILES=true

After double checking the environment file works and RAILS_ENV is pointing to production & RAILS_SERVE_STATIC_FILES=true, reload systemd and start puma

sudo systemctl daemon-reload
sudo systemctl enable puma.service
sudo systemctl start puma.service
sudo systemctl status puma.service

Create the nginx virtualhost configuration for reverse proxying requests to the puma/rails server

sudo nano /etc/nginx/sites-available/YOUR_SITE.conf

fill in the file with the details, proxying requests coming to port 80

server {
    listen 80;
    server_name  potassium.shashike.me;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    client_max_body_size 4G;
}

symlink the virtualhost, test and restart nginx

sudo ln -s /etc/nginx/sites-available/potassium.shashike.me.conf /etc/nginx/sites-enabled/potassium.shashike.me.conf
sudo service nginx configtest
sudo service nginx reload

setup SSL with Lets Encrypt and Certbot

sudo apt-get install python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
sudo certbot renew
  1. Most of the time python in your shell and python invoked from ruby's system call are two different pythons. If the application simply fails without error it means that Python cannot be found when Ruby makes the system call in potash/app/controllers/images_controller.rb you can try explicitly pointing to the python version in your $PATH like so:
    system("cd #{path_to_python} ; /home/yourUserName/.pyenv/shims/python run.py #{file_name}")
  1. If the rails app / python script cannot write to disk, allow write by all users (dont do this on a production server)
chmod -R 777 object-detectr/

About

A tensorflow object detection model served to the internet on a Rails server (TensorFlow on Rails-- if you will)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published