Face detection using Haar cascades is a machine learning based approach where a cascade function is trained with a set of input data. OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc.. Today we will be using the face classifier. You can experiment with other classifiers as well. You need to download the trained classifier XML file (haarcascade_frontalface_default.xml), which is available in OpenCv’s GitHub repository. Save it to your working location.
install Apache and WSGI sudo apt install apache2 libapache2-mod-wsgi-py3 python3-pip python3-virtualenv sudo pip3 install flask (skip this if you are planning to setup venv)
enable mod_wsgi sudo a2enmod wsgi
create files as below (the __init__.py and wsgi file are mandatory)
6. restart apache and verify the app systemctl restart apache2 then you can open http://192.168.0.51/
NOTE: if you want use python virtual env, we can setup venv in /var/www/FLASKAPPS: virtualenv venv source venv/bin/activate pip install flask then update WSGIDaemonProcess in /etc/apache2/sites-available/flaskhelloworldsite.com.conf with python-home: WSGIDaemonProcess helloworldapp user=www-data group=www-data threads=5 python-home=/var/www/FLASKAPPS/venv
zhub2@apache-vm1:~ $ python3
>>> from hello import db
>>> db.create_all()
>>> from hello import User
### INSERT NEW User
>>> user_john = User(username='john', passwd='carlos23')
>>> db.session.add(user_john)
>>> db.session.commit()
### LIST ALL User
>>> User.query.all()
[<User 'john'>]
### UPDATE User WHERE username='john'
>>> user_john = User.query.filter_by(username='john').first()
>>> user_john.passwd='test123'
>>> db.session.add(user_john)
>>> db.session.commit()
zhub2@apache-vm1:~ $ python3
>>> from hello import db
>>> db.create_all()
>>> from hello import User
### INSERT NEW User
>>> user_john = User(username='john', passwd='carlos23')
>>> db.session.add(user_john)
>>> db.session.commit()
### LIST ALL User
>>> User.query.all()
[<User 'john'>]
### UPDATE User WHERE username='john'
>>> user_john = User.query.filter_by(username='john').first()
>>> user_john.passwd='test123'
>>> db.session.add(user_john)
>>> db.session.commit()
Gevent is the use of simple, sequential programming in python to achieve scalability provided by asynchronous IO and lightweight multi-threading (as opposed to the callback-style of programming using Twisted’s Deferred). It is built on top of libevent/libev (for asynchronous I/O) and greenlets (lightweight cooperative multi-threading). we can use pip install gevent to install it. Here is the sample.py:
from gevent import monkey
monkey.patch_all()
from flask import Flask
from gevent import pywsgi
app = Flask(__name__)
@app.route('/')
defhello():
return"Hello World!"
if__name__ == '__main__':
#app.run()
server = pywsgi.WSGIServer(("0.0.0.0", 8080), app)
server.serve_forever()
from gevent import monkey
monkey.patch_all()
from flask import Flask
from gevent import pywsgi
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
#app.run()
server = pywsgi.WSGIServer(("0.0.0.0", 8080), app)
server.serve_forever()
from gevent import monkey
monkey.patch_all()
from flask import Flask
from gevent import pywsgi
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
#app.run()
server = pywsgi.WSGIServer(("0.0.0.0", 8080), app)
server.serve_forever()
you can run it in background: nohup python3 sample.py & then you can open the sample app on http://your_server_ip:8080
Use ApacheBench To Do Load Testing: 1. apt-get install apache2-utils 2. ab -r -n 2000 -c 200 http://127.0.0.1:8080/?delay=1
Concurrency Level: 200
Time taken for tests: 2.470 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 294000 bytes
HTML transferred: 24000 bytes
Requests per second: 809.60[#/sec] (mean)
Time per request: 247.035[ms](mean)
Time per request: 1.235[ms](mean, across all concurrent requests)
Transfer rate: 116.22[Kbytes/sec] received
Concurrency Level: 200
Time taken for tests: 2.470 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 294000 bytes
HTML transferred: 24000 bytes
Requests per second: 809.60 [#/sec] (mean)
Time per request: 247.035 [ms] (mean)
Time per request: 1.235 [ms] (mean, across all concurrent requests)
Transfer rate: 116.22 [Kbytes/sec] received
Concurrency Level: 200
Time taken for tests: 2.470 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 294000 bytes
HTML transferred: 24000 bytes
Requests per second: 809.60 [#/sec] (mean)
Time per request: 247.035 [ms] (mean)
Time per request: 1.235 [ms] (mean, across all concurrent requests)
Transfer rate: 116.22 [Kbytes/sec] received
compare it with sample_old.py without gevent:
from flask import Flask
app = Flask(__name__)
@app.route('/')
defhello():
return"Hello World!"
if __name__ == '__main__':
app.run(port=8080)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(port=8080)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(port=8080)
run same ab command, you will get:
Concurrency Level: 200
Time taken for tests: 4.493 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 330000 bytes
HTML transferred: 24000 bytes
Requests per second: 445.10[#/sec] (mean)
Time per request: 449.340[ms](mean)
Time per request: 2.247[ms](mean, across all concurrent requests)
Transfer rate: 71.72[Kbytes/sec] received
Concurrency Level: 200
Time taken for tests: 4.493 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 330000 bytes
HTML transferred: 24000 bytes
Requests per second: 445.10 [#/sec] (mean)
Time per request: 449.340 [ms] (mean)
Time per request: 2.247 [ms] (mean, across all concurrent requests)
Transfer rate: 71.72 [Kbytes/sec] received
Concurrency Level: 200
Time taken for tests: 4.493 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 330000 bytes
HTML transferred: 24000 bytes
Requests per second: 445.10 [#/sec] (mean)
Time per request: 449.340 [ms] (mean)
Time per request: 2.247 [ms] (mean, across all concurrent requests)
Transfer rate: 71.72 [Kbytes/sec] received
Step 3 — Setting Up a Flask Application pip install gunicorn flask nano ~/myproject/myproject.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
defhello():
return"<h1 style='color:blue'>Hello There!</h1>"
if__name__ == "__main__":
app.run(host='0.0.0.0')
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
Step 4 — Creating the WSGI Entry Point and Configuring Gunicorn nano ~/myproject/wsgi.py
from myproject import app
if__name__ == "__main__":
app.run()
from myproject import app
if __name__ == "__main__":
app.run()
from myproject import app
if __name__ == "__main__":
app.run()
cd ~/myproject gunicorn –bind 0.0.0.0:5000 wsgi:app then you should be able to open http://your_server_ip:5000
When you have confirmed that it’s functioning properly, press CTRL-C in your terminal window. We’re now done with our virtual environment, so we can deactivate it: deactivate
Step 5 — Setup Gunicorn service sudo nano /etc/systemd/system/myproject.service
Step 7 — Securing the Application Certbot provides a variety of ways to obtain SSL certificates through plugins. The Nginx plugin will take care of reconfiguring Nginx and reloading the config whenever necessary. sudo add-apt-repository ppa:certbot/certbot sudo apt install python-certbot-nginx sudo certbot –nginx -d your_domain -d www.your_domain then you can open you site with SSL https://your_domain