You can do it in 5 minutes !? Create a face detection API with FastAPI and OpenCV and publish it on Heroku

Introduction

Hello. This article is a copy of the one on My site, but I thought it would be widely used, so I dared to post it on Qiita. I did.

Now, I'm studying computer vision and I'm exploring how to use this technology concretely. I think that the method of creating an API is one of the good ideas that can be used as a common module in various environments and languages. Of course, there are some disadvantages. When I was looking for a Python framework that could easily create an API, I found something called FastAPI, so I created a face detection API using FastAPI, and since it's a big deal, I'd like to go to release on Heroku at once.

Well, can you do it in 5 minutes (laughs) Timer start!

What is FastAPI

FastAPI is a modern, fast (high performance) web framework for building APIs in Python 3.6+ based on standard Python type hints. It's a very easy-to-use framework, so I hope many people will use it. The official website is https://fastapi.tiangolo.com/.

What is the face detection API?

When you upload the image you want to detect the face, create an API that returns the number detected by JSON and the rectangular coordinates. Using the result, if you try to add a frame in a common way, it will look like this. screenshot.181.png

Image: http://gahag.net/

Premise

Please do the following in advance.

--Heroku user registered (Free plan) --Github user registered

I think you should do the following, but this time it is not mandatory.

--Python installation on your PC for debugging --Installing debug environment such as VS Code

procedure

  1. Create GitHub remote repository
  2. Deploy to Heroku
  3. API completed !?

① Create GitHub remote repository

I would like to write the details of the source code on my site later. The source code used this time can be found on Github. Create your own Github remote repository and put the source in it.

The configuration is as follows.

.
├── cascades
│   └── haarcascade_frontalface_default.xml
├── detect
│   └── detect_face.py
├── .gitignore
├── Aptfile
├── Procfile
├── const.py
├── main.py
├── requirements.txt
└── women.jpg

"Cascades / haarcascade_frontalface_default.xml" is a trained file of the cascading classifier that can detect faces. The logic for face detection is written in "detect / detect_face.py". ".Gitignore" is what you don't want to manage with git. "Aptfile" is a file required to use OpenCV on Heroku. Details will be described later. "Procfile" is a file required to run Fast API on Heroku. "Const.py" is used because it is easy to use CONST. "Main.py" is the file executed by uvicorn. "Requirements.txt" is a file that manages dependent libraries. Required to deploy with Heruku. "Women.jpg " is a sample image.

② Deploy to Heroku

Now that the source is ready, we're ready to publish the API on Heroku. First of all, Creat New App. screenshot.173.png

Give it an app name. screenshot.174_.png

Select the Deploy tab, connect to your Github account, and then select the repository you want to deploy. screenshot.182_.png

OpenCV installs the "opencv-contrib-python" package, but with this alone I get an import error. "ImportError: libSM.so.6: cannot open shared object file: No such file or directory" Therefore, "Add buildpacks" and "Create Aptfile file" are required. The Aptfile is provided in the source set, so only add buildpacks on Heroku. Add "https://github.com/heroku/heroku-buildpack-apt" in Buildpacks on the Settings tab.

screenshot.179.png

Go back to the Deploy tab and deploy. This time, select Manual Deploy. screenshot.177.png

③ API completed !?

If Deploy is lucky and successful, press "Open app" at the top right of the Heroku screen. {“detail”:”Not Found”} Did you get a display like this? It's an error! !! I'm sorry for those who say.

To call the API you created, it's easy to add / docs after the URL. screenshot.182.png

You can actually try it by pressing "Try it Out". screenshot.183.png

Select the image you want to detect the face and execute. Did the result come back like this? screenshot.184.png

** (Note) About image size ** The image size is 0.2Mb or less. It cannot be detected properly when the size is large. It seems that various adjustments are necessary.

in conclusion

That's all. Timer stop! Was it within 5 minutes? (Lol) If it didn't work, I'm really sorry: sweat_smile: I'm sorry I just apologized and didn't solve it: sweat_smile :: sweat_smile: I didn't know how to deploy FastAPI on Heroku, so I created an article. I hope it helps someone: blush:

Recommended Posts

You can do it in 5 minutes !? Create a face detection API with FastAPI and OpenCV and publish it on Heroku
If you guys in the scope kitchen can do it with a margin ~ ♪
Consideration when you can do a good job in 10 years with Python3 and Scala3.
You can do it in 3 minutes! How to make a moving QR code (GIF)!
Until you create a machine learning environment with Python on Windows 7 and run it
Draw a watercolor illusion with edge detection in Python3 and openCV3
What you can do with API vol.1
[Can be done in 10 minutes] Create a local website quickly with Django
Until you publish (deploy) a web application made with bottle on Heroku
Create a clean DB for testing with FastAPI and unittest the API with pytest
Hello World and face detection with OpenCV 4.3 + Python
You can easily create a GUI with Python
[For beginners] You can do it from scratch! Creating APIs with AWS SAM and outputting OpenAPI documentation in Python
Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV
What you can and cannot do with Tensorflow 2.x
Create a list in Python with all followers on twitter
Create a web surveillance camera with Raspberry Pi and OpenCV
If you write TinderBot in Python, she can do it
A note on touching Microsoft's face recognition API in Python
Create SVG graph with matplotlib on heroku (displayed in Japanese)
Create a deploy script with fabric and cuisine and reuse it
Create a web API that can deliver images with Django
Face detection with Python + OpenCV
Anime face detection with OpenCV
How to install OpenCV on Cloud9 and run it in Python
Create a temporary file with django as a zip file and return it
Create a striped illusion with gamma correction for Python3 and openCV3
I want to create a pipfile and reflect it in docker
How to deploy a Django app on heroku in just 5 minutes
Replace your face with Twitter icon with openCV face recognition and do ZOOM
I made a POST script to create an issue on Github and register it in the Project
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
Procedure until you can create a general user and execute the sudo command on CentOs (memorial note)