直接从Google复制代码:link
使用2MB图像给我7秒钟的等待时间,可检测到面部。如果将位图压缩10倍,则可以得到0.05秒的人脸检测时间。但是我找不到其他人建议您在运行人脸检测之前压缩位图,实际上文档建议它只需10毫秒即可检测到人脸,并且当我使用相机作为图像源时它会实时进行。 / p>
未解决的相关问题: mobile vision API takes too long to detect face
只是想知道这是否正常,或者我做错了什么:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private int FACE_DETECTION_RATIO_MINIMUM = 10;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: 1");
ImageView myImageView = (ImageView) findViewById(R.id.imageView);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap myBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.test2,
options);
Log.d(TAG, "onClick: 2");
Log.d(TAG, "onClick: width: " + myBitmap.getWidth() + " height: " + myBitmap.getHeight());
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
Log.d(TAG, "onClick: 3");
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, myBitmap.getWidth()/10, myBitmap.getHeight() /10, false);
Log.d(TAG, "scaled width: " + scaledBitmap.getWidth() + " height: " + scaledBitmap.getHeight());
//tempBitmap.compress(Bitmap.CompressFormat.JPEG, 40, tempBitmap)
Canvas tempCanvas = new Canvas(tempBitmap);
Canvas tempCanvasScaled = new Canvas(scaledBitmap);
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
Log.d(TAG, "onClick: 4");
FaceDetector faceDetector = new
FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).setMode(FAST_MODE).setLandmarkType(NO_LANDMARKS).setClassificationType(NO_CLASSIFICATIONS)
.build();
if(!faceDetector.isOperational()){
new AlertDialog.Builder(v.getContext()).setMessage("Could not set up the face detector!").show();
return;
}
Log.d(TAG, "onClick: 5");
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
Frame frameScaled = new Frame.Builder().setBitmap(scaledBitmap).build();
Log.d(TAG, "onClick: 5.1");
SparseArray<Face> faces = faceDetector.detect(frameScaled);
-->> SparseArray<Face> faces2 = faceDetector.detect(frame); <-- Takes 7 seconds
Log.d(TAG, "onClick: 6");
for(int i=0; i<faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
Log.d(TAG, "onClick: x,y,x,y: " + x1 +", " + y1 + ", " + x2 +", " + y2);
}
for(int i=0; i<faces2.size(); i++) {
Face thisFace = faces2.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
Log.d(TAG, "onClick: x,y,x,y: " + x1 +", " + y1 + ", " + x2 +", " + y2);
}
Log.d(TAG, "onClick: 5");
myImageView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));
faceDetector.release();
//cropImage(x1,y1,x2,y2);
}
});
}
答案 0 :(得分:0)
视觉17.0.2 / 18.0.0版本存在相同的问题,我通过降级视觉库版本克服了这个问题。
您可以使用这些设置
用于顶级build.gradle
dependencies {
//Google analytics
classpath 'com.google.gms:google-services:4.3.2'
}
用于应用程序级build.gradle
dependencies {
implementation 'com.google.android.gms:play-services-vision:16.2.0'
}
如果您使用的是Analytics(分析)或Firebase,则这些版本与vision 16.2.0兼容
dependencies {
//Google analytics
implementation 'com.google.android.gms:play-services-analytics:16.0.6'
//firebase
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
}