Android的人脸检测只与可绘作品不是从SD卡图像 [英] Android Face detection only works with drawables not with images from SD card

查看:101
本文介绍了Android的人脸检测只与可绘作品不是从SD卡图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有code检测多达10个面孔在任何特定的图像文件,并返回到我的信息像眼睛的位置和其他东西一样。所以,当我告诉它使用存储在资源文件夹绘制为我的项目它的伟大工程的图像文件。但是,当我有它尝试从一个位图我从SD卡它不会找到任何脸部导入找到面孔。但这些都是完全一样的图像。有任何想法吗?我的code是波纹管:

编辑:
经进一步检查,我发现当我将这一行code 了System.out.println(行字节:+ sourceImage.getRowBytes());
我得到的是绘制352和SD卡的图像是704,我认为意味着绘制正在COM $ P $的.apk文件pssed但SD卡图像显然不是。不知道这是否会影响什么。

 公共类FaceView扩展视图{
           私有静态最终诠释NUM_FACES = 10; //最大为64
           私有静态最终布尔DEBUG = TRUE;           私人FaceDetector arrayFaces;
           私人FaceDetector.Face getAllFaces [] =新FaceDetector.Face [NUM_FACES]
           私人FaceDetector.Face getFace = NULL;           私人的PointF eyesMidPts [] =新的PointF [NUM_FACES]
           私人浮动eyesDistance [] =新的浮动[NUM_FACES]           私人位图sourceImage;           私人油漆tmpPaint =新的油漆(Paint.ANTI_ALIAS_FLAG);
           私人油漆pOuterBullsEye =新的油漆(Paint.ANTI_ALIAS_FLAG);
           私人油漆pInnerBullsEye =新的油漆(Paint.ANTI_ALIAS_FLAG);           私人诠释picWidth,picHeight;
           私人浮动xRatio,yRatio;           公共FaceView(上下文的背景下){
                   超级(上下文);                   pInnerBullsEye.setStyle(Paint.Style.FILL);
                   pInnerBullsEye.setColor(Color.RED);                   pOuterBullsEye.setStyle(Paint.Style.STROKE);
                   pOuterBullsEye.setColor(Color.RED);                   tmpPaint.setStyle(Paint.Style.STROKE);
                   tmpPaint.setTextAlign(Paint.Align.CENTER);                   BitmapFactory.Options BFO =新BitmapFactory.Options();
                   bfo.in preferredConfig = Bitmap.Config.RGB_565;                   // ********这code SD卡不工作导入图像
                   字符串imageInSD = Environment.getExternalStorageDirectory()
                        .getAbsolutePath()+/ testfolder /+面1+的.png;                   位图sourceImage = BitmapFactory.de codeFILE(imageInSD,BFO);                   // **********这code用于绘制文件夹,这code工程项目的图像。
                   sourceImage = BitmapFactory.de codeResource(getResources(),R.drawable.face1,BFO);                   picWidth = sourceImage.getWidth();
                   picHeight = sourceImage.getHeight();                   arrayFaces =新FaceDetector(picWidth,picHeight,NUM_FACES);
                   arrayFaces.findFaces(sourceImage,getAllFaces);                   的for(int i = 0; I< getAllFaces.length;我++)
                   {
                           getFace = getAllFaces [I]
                           尝试{
                                   的PointF eyesMP =新的PointF();
                                   getFace.getMidPoint(eyesMP);
                                   eyesDistance [I] = getFace.eyesDistance();
                                   eyesMidPts [I] = eyesMP;                                   如果(调试)
                                   {
                                           Log.i(面子,
                                                   I ++ getFace.confidence()++ getFace.eyesDistance()+
                                                   +姿势:(+ getFace.pose(FaceDetector.Face.EULER_X)+,
                                                   + getFace.pose(FaceDetector.Face.EULER_Y)+,
                                                   + getFace.pose(FaceDetector.Face.EULER_Z)+)
                                                   +眼睛中点:(+ eyesMidPts [I] .X +,+ eyesMidPts [I] .Y +)
                                           );
                                   }
                           }
                           赶上(例外五)
                           {
                                   如果(调试)Log.e(脸,我+为空);
                           }                   }
           }           @覆盖
           保护无效的onDraw(帆布油画)
           {
                   xRatio =的getWidth()* 1.0F / picWidth;
                   yRatio =的getHeight()* 1.0F / picHeight;
                   canvas.drawBitmap(sourceImage,空,新的矩形(0,0,的getWidth()的getHeight()),tmpPaint);
                   的for(int i = 0; I< eyesMidPts.length;我++)
                   {
                           如果(eyesMidPts [I]!= NULL)
                           {
                                   pOuterBullsEye.setStrokeWidth(eyesDistance [I] / 6);
                                   canvas.drawCircle(eyesMidPts [I] .X * xRatio,eyesMidPts [I] .Y * yRatio,eyesDistance [I] / 2,pOuterBullsEye);
                                   canvas.drawCircle(eyesMidPts [I] .X * xRatio,eyesMidPts [I] .Y * yRatio,eyesDistance [I] / 6,pInnerBullsEye);
                           }
                   }
           }}


解决方案

原来,问题是由照​​相机拍摄的图像保存为PNG文件和人脸检测只能成功地从SD卡的工作,如果是使用JPG文件。只要转换的文件为JPG,它工作正常。

So I have code to detect up to 10 faces in any given image file and return to me info like the location of the eyes and other stuff like that. So when i tell it to use an image file that is stored in the drawable folder of resources for my project it works great. But when i have it try to find faces from a bitmap i import from the sd card it wont find any faces. But these are the same exact images. Any ideas? my code is bellow:

Edit: After further inspection I found that when i insert this line of code System.out.println("Row Bytes: " + sourceImage.getRowBytes()); I get the drawable is 352 and The SD card image is 704. Which I think means that the drawable is being compressed in the .apk file but the SD card image obviously is not. Not sure if this would effect anything.

 public class FaceView extends View {
           private static final int NUM_FACES = 10; // max is 64
           private static final boolean DEBUG = true;

           private FaceDetector arrayFaces;
           private FaceDetector.Face getAllFaces[] = new FaceDetector.Face[NUM_FACES];
           private FaceDetector.Face getFace = null;

           private PointF eyesMidPts[] = new PointF[NUM_FACES];
           private float  eyesDistance[] = new float[NUM_FACES];

           private Bitmap sourceImage;

           private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
           private Paint pOuterBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
           private Paint pInnerBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);

           private int picWidth, picHeight;
           private float xRatio, yRatio;

           public FaceView(Context context) {
                   super(context);

                   pInnerBullsEye.setStyle(Paint.Style.FILL);
                   pInnerBullsEye.setColor(Color.RED);

                   pOuterBullsEye.setStyle(Paint.Style.STROKE);
                   pOuterBullsEye.setColor(Color.RED);

                   tmpPaint.setStyle(Paint.Style.STROKE);
                   tmpPaint.setTextAlign(Paint.Align.CENTER);

                   BitmapFactory.Options bfo = new BitmapFactory.Options();
                   bfo.inPreferredConfig = Bitmap.Config.RGB_565;

                   //********This code imports the image from the SD card which does not work
                   String imageInSD = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/testfolder/" + "face1" + ".png";

                   Bitmap sourceImage = BitmapFactory.decodeFile(imageInSD,bfo);

                   //**********This code uses an image in the projects drawable folder, this code works.
                   sourceImage = BitmapFactory.decodeResource( getResources() ,R.drawable.face1, bfo);

                   picWidth = sourceImage.getWidth();
                   picHeight = sourceImage.getHeight();

                   arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
                   arrayFaces.findFaces(sourceImage, getAllFaces);

                   for (int i = 0; i < getAllFaces.length; i++)
                   {
                           getFace = getAllFaces[i];
                           try {
                                   PointF eyesMP = new PointF();
                                   getFace.getMidPoint(eyesMP);
                                   eyesDistance[i] = getFace.eyesDistance();
                                   eyesMidPts[i] = eyesMP;

                                   if (DEBUG)
                                   {
                                           Log.i("Face",
                                                   i +  " " + getFace.confidence() + " " + getFace.eyesDistance() + " "
                                                   + "Pose: ("+ getFace.pose(FaceDetector.Face.EULER_X) + ","
                                                   + getFace.pose(FaceDetector.Face.EULER_Y) + ","
                                                   + getFace.pose(FaceDetector.Face.EULER_Z) + ")"
                                                   + "Eyes Midpoint: ("+eyesMidPts[i].x + "," + eyesMidPts[i].y +")"
                                           );
                                   }
                           }
                           catch (Exception e)
                           {
                                   if (DEBUG) Log.e("Face", i + " is null");
                           }

                   }


           }

           @Override
           protected void onDraw(Canvas canvas)
           {
                   xRatio = getWidth()*1.0f / picWidth;
                   yRatio = getHeight()*1.0f / picHeight;
                   canvas.drawBitmap( sourceImage, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
                   for (int i = 0; i < eyesMidPts.length; i++)
                   {
                           if (eyesMidPts[i] != null)
                           {
                                   pOuterBullsEye.setStrokeWidth(eyesDistance[i] /6);
                                   canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 2 , pOuterBullsEye);
                                   canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 6 , pInnerBullsEye);
                           }
                   }


           }  

}

解决方案

Turns out the issue is that the pictures taken by the Camera are saved as PNG files and the face detection can only successfully work from the SD card if it is using JPG files. Simply convert the files to JPG and it works fine.

这篇关于Android的人脸检测只与可绘作品不是从SD卡图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆