In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how Android uses OpenCV to make face detection APP. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
No picture, no truth, first show you the results of APP operation.
As shown in the image above, after APP runs, click "Select Picture", select a picture from the phone, and then click "deal with". APP will frame the face in a rectangle and detect the nose. Because the purpose is to give you a demonstration, so the design of APP is very simple, and it only realizes the detection of face and nose, but does not realize the detection of other facial features. And this APP can only detect very simple images, if the background in the picture is too complex, it can not detect faces.
Next I will teach you step by step how to implement the above APP!
Step 1: download and install Android studio
In order to ensure that you can download the same version of Android Studio as I do, I uploaded the installation package to Weiyun. Address: https://share.weiyun.com/bHVOWGC9
After downloading, click the next step all the way to install it. Of course, you need to connect to the Internet during the installation, so you may fail halfway, if you fail, try again a few times, and if there is still a problem, you may want to turn on VPN.
Step 2: download SDK tools
After opening Android studio, click "File"-> "Settings"
Click "Appearance & Behavior"-> "System Settings"-> "Android SDK"-> "SDK Tools".
Then select "NDK" and "CMake" and click "OK". It may take some time to download these two tools. If you fail, please try again or open VPN.
Step 3: create a new Android APP project
Click "File"-> "New"-> "New Project"
Select "Empty Activity" and click "Next"
"Language" select "Java" and Minimum SDK select "API 21". Click "Finish"
Step 4: download Opencv
Download address
Decompress it after download.
Step 5: import OpenCV
Copy the sdk under opencv-4.5.4-android-sdk\ OpenCV-android-sdk to the Android project you created in step 3. This is under D:\ programming\ MyApplication in the figure in step 3. Then rename the sdk folder to openCVsdk.
Select "Project"-> "settings.gradle". Add include': openCVsdk' to the file
Select Project-> openCVsdk-> build.gradle.
Change apply plugin: 'kotlin-android' to / / apply plugin:' kotlin-android'
Change compileSdkVersion and minSdkVersion,targetSdkVersion to 31, 21, and 31.
Click "File"-> "Project Structure"
Click "Dependencies"-> "app"-> "+"-> "Module Dependency"
Select "openCVsdk", click "OK", and "OK" of the parent window
Create a new folder jniLibs in the app\ src of the Android project folder, and then copy everything in the opencv-4.5.4-android-sdk\ OpenCV-android-sdk\ sdk\ native\ staticlibs of the openCV folder into the jniLibs folder.
Download the classifier. After unzipping, copy the files in the following figure to the app\ src\ main\ res\ raw folder of the project folder.
Step 6: add code
Double-click "activity_main.xml" under "Project"-> "app"-"main"-"res". Then click "code" in the upper right corner.
Then replace all the code with the following code
Double click "Project"-> "app"-"" main "-"java"-"" com.example... " The following "MainActivity". Then replace all the code with the following code (keep the first line of code in the original file)
Import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;import org.opencv.android.OpenCVLoader;import org.opencv.android.Utils;import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Point;import org.opencv.imgproc.Imgproc Import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import org.opencv.core.MatOfRect;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.core.Size;import org.opencv.objdetect.CascadeClassifier;import android.content.Context;public class MainActivity extends AppCompatActivity {private double max_size = 1024; private int PICK_IMAGE_REQUEST = 1; private ImageView myImageView; private Bitmap selectbp Private static final String TAG = "OCVSample::Activity"; private static final Scalar FACE_RECT_COLOR = new Scalar (0,255,0,255); public static final int JAVA_DETECTOR = 0; public static final int NATIVE_DETECTOR = 1; private Mat mGray; private File mCascadeFile; private CascadeClassifier mJavaDetector,mNoseDetector Private int mDetectorType = JAVA_DETECTOR; private float mRelativeFaceSize = 0.2f; private int mAbsoluteFaceSize = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); staticLoadCVLibraries (); myImageView = (ImageView) findViewById (R.id.imageView) MyImageView.setScaleType (ImageView.ScaleType.FIT_CENTER); Button selectImageBtn = (Button) findViewById (R.id.select_btn); selectImageBtn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / makeText (MainActivity.this.getApplicationContext (), "start to browser image", Toast.LENGTH_SHORT). Show (); selectImage () } private void selectImage () {Intent intent = new Intent (); intent.setType ("image/*"); intent.setAction (Intent.ACTION_GET_CONTENT); startActivityForResult (Intent.createChooser (intent, "Select Image..."), PICK_IMAGE_REQUEST);}}) Button processBtn = (Button) findViewById (R.id.process_btn); processBtn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / makeText (MainActivity.this.getApplicationContext (), "hello, image process", Toast.LENGTH_SHORT). Show (); convertGray ();}) } private void staticLoadCVLibraries () {boolean load = OpenCVLoader.initDebug (); if (load) {Log.i ("CV", "OpenCV Libraries loaded...");}} private void convertGray () {Mat src = new Mat (); Mat temp = new Mat (); Mat dst = new Mat (); Utils.bitmapToMat (selectbp, src) Imgproc.cvtColor (src, temp, Imgproc.COLOR_BGRA2BGR); Log.i ("CV", "image type:" + (temp.type () = = CvType.CV_8UC3)); Imgproc.cvtColor (temp, dst, Imgproc.COLOR_BGR2GRAY); Utils.matToBitmap (dst, selectbp); myImageView.setImageBitmap (selectbp); mGray = dst; mJavaDetector = loadDetector (R.raw.lbpcascade_frontalface, "lbpcascade_frontalface.xml") MNoseDetector = loadDetector (R.raw.haarcascade_mcs_nose, "haarcascade_mcs_nose.xml"); if (mAbsoluteFaceSize = = 0) {int height = mGray.rows (); if (Math.round (height * mRelativeFaceSize) > 0) {mAbsoluteFaceSize = Math.round (height * mRelativeFaceSize);}} MatOfRect faces = new MatOfRect () If (mJavaDetector! = null) {mJavaDetector.detectMultiScale (mGray, faces, 1.1,2,2, / / TODO: objdetect.CV_HAAR_SCALE_IMAGE new Size (mAbsoluteFaceSize, mAbsoluteFaceSize), new Size ();} Rect [] facesArray = faces.toArray (); for (int I = 0; I
< facesArray.length; i++) { Log.e(TAG, "start to detect nose!"); Mat faceROI = mGray.submat(facesArray[i]); MatOfRect noses = new MatOfRect(); mNoseDetector.detectMultiScale(faceROI, noses, 1.1, 2, 2, new Size(30, 30)); Rect[] nosesArray = noses.toArray(); Imgproc.rectangle(src, new Point(facesArray[i].tl().x + nosesArray[0].tl().x, facesArray[i].tl().y + nosesArray[0].tl().y) , new Point(facesArray[i].tl().x + nosesArray[0].br().x, facesArray[i].tl().y + nosesArray[0].br().y) , FACE_RECT_COLOR, 3); Imgproc.rectangle(src, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3); } Utils.matToBitmap(src, selectbp); myImageView.setImageBitmap(selectbp); } private CascadeClassifier loadDetector(int rawID,String fileName) { CascadeClassifier classifier = null; try { // load cascade file from application resources InputStream is = getResources().openRawResource(rawID); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); mCascadeFile = new File(cascadeDir, fileName); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); Log.e(TAG, "start to load file: " + mCascadeFile.getAbsolutePath()); classifier = new CascadeClassifier(mCascadeFile.getAbsolutePath()); if (classifier.empty()) { Log.e(TAG, "Failed to load cascade classifier"); classifier = null; } else Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); cascadeDir.delete(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "Failed to load cascade. Exception thrown: " + e); } return classifier; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { Uri uri = data.getData(); try { Log.d("image-tag", "start to decode selected image now..."); InputStream input = getContentResolver().openInputStream(uri); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(input, null, options); int raw_width = options.outWidth; int raw_height = options.outHeight; int max = Math.max(raw_width, raw_height); int newWidth = raw_width; int newHeight = raw_height; int inSampleSize = 1; if (max >Max_size) {newWidth = raw_width / 2; newHeight = raw_height / 2; while ((newWidth / inSampleSize) > max_size | | (newHeight / inSampleSize) > max_size) {inSampleSize * = 2;}} options.inSampleSize = inSampleSize Options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; selectbp = BitmapFactory.decodeStream (getContentResolver (). OpenInputStream (uri), null, options); myImageView.setImageBitmap (selectbp);} catch (Exception e) {e.printStackTrace () Step 7: connect the phone to run the program
First of all, it is necessary to open the developer mode of Android phones, each mobile phone brand opens in a different way, you can know it by yourself. For example, search Baidu for "how to open developer mode on Xiaomi mobile phone".
Then connect the phone to the computer with a data cable. After success, your phone model will be displayed in the Android studio. The figure below shows "Xiaomi MI 8 UD". In this case, the developer phone is Xiaomi phone.
Click "Run"-"Run 'app'" in the image above to run APP on the phone. Note that the phone screen is open. Your selfie picture can be detected to be unsuccessful, you can download my test picture to try.
This is the end of the article on "how Android uses OpenCV to make face detection APP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.