所以我有代码在任何给定的图像文件中检测多达10个面孔并返回给我信息,如眼睛的位置和其他类似的东西。因此,当我告诉它使用存储在我的项目的资源的drawable文件夹中的图像文件时,它工作得很好。但是,当我有它尝试从我从SD卡导入的位图找到面孔时,它不会找到任何面孔。但这些都是完全相同的图像。有任何想法吗?我的代码如下:
编辑:
经过进一步检查后,我发现当我插入这行代码时System.out.println("Row Bytes: " + sourceImage.getRowBytes());
我得到的drawable是352和SD卡图像是704.我认为这意味着drawable在.apk文件中被压缩,但SD卡图像显然不是。不确定这是否会影响任何事情。
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);
}
}
}
}
答案 0 :(得分:5)
好吧我相信我知道你的问题在这里。设备无法将图像渲染为位图图像,因为它位于外部存储器中。面部识别工作只是没有进入画布。所有设备都在我的xoom上具有渲染限制(2048x2048)我发现here。将图像作为资源添加时它起作用的原因是因为你的文件缩小了,因为它构建了.apk(说实话我不知道为什么会这样做,但是我留下了一些println用于测试,其他人可以更好地回答这个问题。无论如何,我只是在你的代码寻找面之后,在它尝试将位图渲染到画布之前,除以2来缩放位图。现在一切似乎都很好。您可能需要调整面部指示器但功能正常。我希望这会有所帮助。
public class FaceView extends View {
private static final int NUM_FACES = 1; // 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() + "/face1" + ".jpg";
System.out.println(imageInSD);
sourceImage = BitmapFactory.decodeFile(imageInSD, bfo);
//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();
System.out.println(picWidth + "x" + picHeight);
arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
arrayFaces.findFaces(sourceImage, getAllFaces);
sourceImage = Bitmap.createScaledBitmap (sourceImage, picWidth/2, picHeight/2, false);
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);
}
}
}
}
答案 1 :(得分:5)
原来问题是相机拍摄的照片会保存为PNG文件,如果使用JPG文件,只能从SD卡成功进行人脸检测。只需将文件转换为JPG即可正常工作。