基于百度api的人脸识别
by Rohit Arya
由Rohit Arya
人脸居中的Android库基于Google Vision API构建 (Face centering Android library build on top of Google Vision API)
In our Android apps, when we crop photos to display them, we often encounter the problem of positioning faces properly.
在我们的Android应用程序中,当我们裁剪照片以显示它们时,经常会遇到正确放置面部的问题。
This inspired me to create a tool that will locate faces and in an image (if there are any) and center the cropped image around them.
这启发了我创建一个工具,该工具可以定位人脸和图像(如果有的话)并将裁剪后的图像围绕它们居中。
Here’s how I did it.
这是我的方法。
I started with Face Detection API of Google’s mobile vision. This API provides:
我从Google移动视觉的人脸检测API开始。 该API提供:
- Face detection (not face recognition) 人脸检测(非人脸识别)
- Face tracking (extends face detection to video sequences) 人脸跟踪(将人脸检测扩展到视频序列)
A landmark — a point of interest within a face (like eyes, nose, etc)
地标 -脸部内的兴趣点(例如眼睛,鼻子等)
- Classification of a face to determine if the face is smiling, eyes are open or closed, and other features 面部分类,以确定面部是否在微笑,眼睛睁着还是闭着,以及其他特征
Since I just wanted the position of the face, I only used the face detection component. To start with that, I created the face detector:
由于我只是想要人脸的位置,因此只使用了人脸检测组件。 首先,我创建了面部检测器:
FaceDetector detector = new FaceDetector.Builder(context)
.setTrackingEnabled(false)
.build();
Now given a bitmap, I created a frame instance from the bitmap to supply to the detector:
现在给出一个位图,我从该位图创建了一个帧实例以提供给检测器:
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
Now, I tried to detect faces synchronously in the frame:
现在,我尝试在帧中同步检测人脸:
SparseArray<Face> faces = detector.detect(frame);
Once I got faces, I chose one face (for now) to crop the image around, keeping that face in the center.
一旦获得面Kong,我就选择一张面Kong(暂时)来裁剪图像,使该面Kong保持在中心。
Now to begin cropping the image:
现在开始裁剪图像:
- I created a scaled new bitmap to fit the target view (ImageView). 我创建了一个缩放的新位图以适合目标视图(ImageView)。
- I then recalculated the position of the face in the new bitmap. 然后,我重新计算了新位图中人脸的位置。
- Keeping the face in the center, I cropped the original bitmap to get a new bitmap. 保持脸部居中,我裁剪原始位图以获取新的位图。
- If no face is detected, then I fallback to CENTER CROP. 如果未检测到脸部,则我将退回到CENTER CROP。
You can find the full code in my GitHub repositories below.
您可以在下面的GitHub存储库中找到完整的代码。
Here are some results of it:
这是一些结果:
I finally exported this as a library, which you can find below.
我最终将其导出为一个库,您可以在下面找到它。
For Glide:
对于滑行 :
aryarohit07/GlideFaceDetectionTransformationGlideFaceDetectionTransformation - An Android image transformation library providing cropping above Face Detection…github.com
aryarohit07 / GlideFaceDetectionTransformation GlideFaceDetectionTransformation-一个Android图像转换库,可在人脸检测上方进行裁剪… github.com
For Picasso:
对于毕加索 :
aryarohit07/PicassoFaceDetectionTransformationPicassoFaceDetectionTransformation - An Android image transformation library providing cropping above Face Detection…github.com
aryarohit07 / PicassoFaceDetectionTransformation PicassoFaceDetectionTransformation-一个Android图像转换库,可在人脸检测上方进行裁剪… github.com
I am planning to release it for Fresco too.
我也打算将其发布给Fresco 。
Feel free to make use of this tool, and help me improve it over on GitHub.
随时使用此工具,并帮助我在GitHub上进行改进。
If you enjoyed reading this article, it would mean a lot if you recommend it using the ❤ icon and share with your colleagues and friends. Thanks!
如果您喜欢阅读这篇文章,那么使用❤图标进行推荐并与您的同事和朋友分享对您来说意义重大。 谢谢!
基于百度api的人脸识别