android - Google Vision 人脸检测需要很长时间
问题描述
直接从google复制代码:link
使用 2MB 的图像,让我有 7 秒的等待时间来检测人脸。如果我将位图压缩 10 倍,它会给我 0.05 秒的人脸检测时间。但是,我在其他任何地方都找不到建议您在运行人脸检测之前压缩位图的人,事实上,文档建议检测人脸只需要 10 毫秒,并且当我使用相机作为图像源时它会实时完成。
相关未回答的问题: 移动视觉 API 检测人脸的时间太长
只是想知道这是否正常,或者我做错了什么:
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);
}
});
}
解决方案
我对视觉 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'
}
如果您使用分析或 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'
}
推荐阅读
- reactjs - 故事书迁移到 CSF 的打字稿问题
- java - Selenium 中的 NullPointerException 与 java
- xslt - 在忽略空白值并在 XSLT 中添加 LineAmount 时删除重复项
- python - Pipenv shell 命令创建新的 venv 而不是加载现有的
- javascript - 在中使用 window.history.replaceState() 是否安全?
- javascript - 如何从不是数据库中的列的实体中返回额外的字段?
- node.js - 获取“被 CORS 策略阻止:请求的资源上不存在‘Access-Control-Allow-Origin’标头。” 使用 Axios 使用 MERN 堆栈
- database-design - 如何将自定义日志存储到数据库
- flutter - 解码飞镖中的音频缓冲区
- sql - 使用来自jsonb的milis将间隔添加到时间戳