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!
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/.
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.
Image: http://gahag.net/
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
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.
Now that the source is ready, we're ready to publish the API on Heroku. First of all, Creat New App.
Give it an app name.
Select the Deploy tab, connect to your Github account, and then select the repository you want to deploy.
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.
Go back to the Deploy tab and deploy. This time, select Manual Deploy.
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.
You can actually try it by pressing "Try it Out".
Select the image you want to detect the face and execute. Did the result come back like this?
** (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.
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