OpenCV-2.4.8-android-sdk/sdk/java
folder from the archive to the libs/OpenCV
folder of your project (create if necessary).settings.gradle
and add our module: include ':app',':app:libs:OpenCV'
app/build.gradle
) we add the line compile project(':app:libs:OpenCV')
to the dependencies
section in order to get: dependencies { compile 'com.android.support:appcompat-v7:+' compile project(':app:libs:OpenCV') }
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } apply plugin: 'android-library' repositories { mavenCentral(); } android { compileSdkVersion 19 buildToolsVersion "19" defaultConfig { minSdkVersion 8 targetSdkVersion 19 } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } }
/PATH_TO_ANDROID_SDK/platform-tools/adb install /PATH_TO_OPENCV/OpenCV-2.4.8-android-sdk/apk/OpenCV_2.4.8_Manager_2.16_armv7a-neon.apk
(by selecting the appropriate one under the ABI apk). @Override public void onResume() { super.onResume(); // OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, mLoaderCallback); } private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { // OpenCV } break; default: { super.onManagerConnected(status); } break; } } };
HOGDescriptor.detectMultiScale
object (to which we first add a standard human detector contours from the HOGDescriptor.getDefaultPeopleDetector
method). After the call, the locations variable will contain objects of rectangular areas where people are located (x, y, width, height), and in weights - search relevance (but, as practice has shown, it does not quite correspond to reality with such images). public Bitmap peopleDetect ( String path ) { Bitmap bitmap = null; float execTime; try { // URL url = new URL( path ); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.ARGB_8888; bitmap = BitmapFactory.decodeStream(input, null, opts); long time = System.currentTimeMillis(); // OpenCV Mat mat = new Mat(); Utils.bitmapToMat(bitmap, mat); // RGB Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY, 4); HOGDescriptor hog = new HOGDescriptor(); // MatOfFloat descriptors = HOGDescriptor.getDefaultPeopleDetector(); hog.setSVMDetector(descriptors); // , ( locations - , weights - ( ) ) MatOfRect locations = new MatOfRect(); MatOfDouble weights = new MatOfDouble(); // , . locations weights hog.detectMultiScale(mat, locations, weights); execTime = ( (float)( System.currentTimeMillis() - time ) ) / 1000f; // Point rectPoint1 = new Point(); Point rectPoint2 = new Point(); Scalar fontColor = new Scalar(0, 0, 0); Point fontPoint = new Point(); // - if (locations.rows() > 0) { List<Rect> rectangles = locations.toList(); int i = 0; List<Double> weightList = weights.toList(); for (Rect rect : rectangles) { float weigh = weightList.get(i++).floatValue(); rectPoint1.x = rect.x; rectPoint1.y = rect.y; fontPoint.x = rect.x; fontPoint.y = rect.y - 4; rectPoint2.x = rect.x + rect.width; rectPoint2.y = rect.y + rect.height; final Scalar rectColor = new Scalar( 0 , 0 , 0 ); // Core.rectangle(mat, rectPoint1, rectPoint2, rectColor, 2); Core.putText(mat, String.format("%1.2f", weigh), fontPoint, Core.FONT_HERSHEY_PLAIN, 1.5, fontColor, 2, Core.LINE_AA, false); } } fontPoint.x = 15; fontPoint.y = bitmap.getHeight() - 20; // Core.putText(mat, "Processing time:" + execTime + " width:" + bitmap.getWidth() + " height:" + bitmap.getHeight() , fontPoint, Core.FONT_HERSHEY_PLAIN, 1.5, fontColor, 2, Core.LINE_AA, false); Utils.matToBitmap( mat , bitmap ); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
Source: https://habr.com/ru/post/217377/
All Articles