Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize face frame matting

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Recently, I am trying to do a face recognition project. After comparing several mainstream face recognition SDK, I use Hong soft's Arcface SDK, because it provides a free version, can be used offline, and is relatively easy to access. One of the requirements in the project is to display the detected face, but how to pick the right size face from a picture? This article describes how to implement it from the following steps:

1. How to get a face frame

two。 How to cut according to the face frame

3. How to rotate the result graph

4. Examples of application scenarios

1. How to get a face frame

First, let's take a look at the detectFaces function of Rainbow soft Android ArcFace SDK for face detection and the face data class FaceInfo:

DetectFaces function:

Parameter type description databyte [] image data memory widthint image wide heightint image high formatint image format faceInfoListList face detection result list

FaceInfo definition:

Parameter type describes the coordinate of rectRect face in the image orientint face facing faceIdint face id, which is used to identify face

There are many articles about face detection function, so we won't do much introduction here. Rect in FaceInfo is an important parameter that we use to pick out human faces. The following picture is the human face frame drawn according to it.

two。 How to cut according to the face frame

Taking the Android platform as an example, the Bitmap class provides functions createBitmap (Bitmap source, int x, int y, int width, int height).

Parameter type description sourceBitmap original image xint face frame upper left corner y coordinate yint face box y coordinate widthint new image width heightint new image height

Use this function to extract the content of the image in any area:

/ / the original Bitmap source;// face frame Rect faceRect;// creates BitmapBitmap.createBitmap (source,faceRect.left,faceRect.top,faceRect.width (), faceRect.height ())

3. How to rotate the result graph

The orient in FaceInfo represents the orientation of the face in the image. When it is not 0 °, it needs to be rotated according to the actual situation.

The type of rotation angle indicates that ASF_OC_ 0int0 °ASF _ OC_90int 90 °counterclockwise ASF_OC_ 180int180 °ASF _ OC_270int 90 °clockwise

The following is the rotation code

/ / the original Bitmap source;// face frame Rect faceRect;// creates Bitmap, assuming that you need to rotate 90 °Matrix matrix = new Matrix (); matrix.postRotate (90); Bitmap.createBitmap (source, faceRect.left, faceRect.top, faceRect.width (), faceRect.height (), matrix, true); 4. Examples of application scenarios

For example, in an access control scenario, you need to display a human face (matting) or upload a human face image to the server. If you upload a complete image, it will take up a lot of storage space and network resources, so uploading the matted face image is more appropriate, but the face extracted according to the detected face frame is incomplete, so you need to do some post-processing on the face frame. The simplest solution is to expand the width and height of the face frame outward. The sample code is as follows:

/ / the original Bitmap source;// face frame Rect faceRect;// adjusts the face frame Rect newRect = newRect (faceRect); / / make sure the face frame is if in the image (newRect.left

< 0) { newRect.left = 0;}if (newRect.top < 0) { newRect.top = 0;}if (newRect.right >

Source.getWidth () {newRect.right = source.getWidth ();} if (newRect.bottom > source.getHeight ()) {newRect.bottom = source.getHeight ();} / / int offsetX = Math.min (Math.min (faceRect.width () / 2source.getWidth newRect.left), source.getWidth ()-newRect.right); int offsetY = Math.min (Math.min (faceRect.height () / 2MagneNewRect.top), source.getHeight ()-newRect.bottom); newRect.inset (- offsetX,-offsetY) / / create a Bitmap, assuming that you need to rotate 90 °Matrix matrix = new Matrix (); matrix.postRotate (90); Bitmap.createBitmap (source, newRect.left, newRect.top, newRect.width (), newRect.height (), matrix, true)

Warm reminder:

Rainbow soft face recognition Android Demo provides many face recognition related features, such as: face frame adaptation scheme, asynchronous face feature extraction, asynchronous face feature comparison and so on. If necessary, you can download it at the following link:

Android Demo can be downloaded from the Rainbow soft face recognition Open platform

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report