I am using mobile vision API to detect face in android app.
I have used SparseArray of Face to store the references to faces, but the detector.detect(frame) method takes too long (15 seconds) to detect face.
Note: I am passing the bitmap of the image taken by camera to detectFaces method.
My code is below
void detectFaces(Context context, Bitmap picture){
com.google.android.gms.vision.face.FaceDetector detector = new com.google.android.gms.vision.face.FaceDetector.Builder(context)
.setTrackingEnabled(false)
.setClassificationType(com.google.android.gms.vision.face.FaceDetector.ALL_CLASSIFICATIONS)
.build();
//Build the frame
Frame frame = new Frame.Builder().setBitmap(picture).build();
//Detect the faces
SparseArray<Face> faces = detector.detect(frame);//**This takes approx 15 second**
if(faces.size() == 0)
Toast.makeText(context, "No Face Detected", Toast.LENGTH_SHORT).show();
else
{
Toast.makeText(context,"Face detected are : " + faces.size() , Toast.LENGTH_LONG).show();
getClassification(faces.valueAt(0));
}
//Release the detector
detector.release();
}
I am currently going through this sample app and I came across the same issue where the app keeps returning that the face is not detected.
After reading through the documentation found here I saw that I should be using detector.isOperational() to test if the required files have been downloaded in order for the detection to work. I used this method to advise that the files are being downloaded like this:
public static void detectFaces(Context context, Bitmap image){
// Create the face detector, disable tracking and enable classifications
FaceDetector detector = new FaceDetector.Builder(context)
.setTrackingEnabled(false)
.setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
.build();
if(detector.isOperational()){
// Build the frame
Frame frame = new Frame.Builder().setBitmap(image).build();
...
}else{
Toast.makeText(context, R.string.not_operational, Toast.LENGTH_LONG).show();
}
}
Next I realized that the detector was never operational. After more research I found the problem to be that my device did not have enough storage space to save the additional files needed. I switched to another device got the 'Not Operational' error on the first try and it has been working ever since then.
Also, freeing up some space and moving the app to external storage allowed it to work on my first device.
来源:https://stackoverflow.com/questions/43727348/mobile-vision-api-takes-too-long-to-detect-face