A tensorflow object detection model served to the internet on a Rails server.
The tensorflow model integrated into this app is quite good at detecting people
Its also an excellent cat detector
Here are a few more detections using this application.
- The python environment is setup assuming that you are using pyenv and have already installed and set the python version in the project directory
- If you are setting up python from scratch you can use this gist
- 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
- If you are setting up rails from scratch (like on a server) you can use this gist: https://gist.github.com/donrestarone/0ebe1e87b39f0f92f850c1da861e85de
- 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
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
- 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}")
- 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/