我一直在尝试建立一个 PoC,在其中通过实时摄像头检测人脸。到目前为止,摄像头馈送工作正常,但由于以下错误,面部识别未触发:W/FA: Failed to Retrieve Firebase Instance ID
. 我已经检查了相关问题,但这些似乎都围绕着我不使用的消息服务。
这是我的依赖项:
implementation fileTree(dir: 'libs', include: ['*.jar'])
annotationProcessor 'com.google.auto.value:auto-value:1.4'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.d-max:spots-dialog:1.1@aar'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.camerakit:camerakit:1.0.0-beta3.11'
implementation 'com.camerakit:jpegkit:0.1.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
implementation 'com.google.firebase:firebase-analytics:17.4.2'
implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
implementation 'com.google.firebase:firebase-ml-vision-face-model:20.0.1'
这是我的活动代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_face_recognition);
FirebaseOptions options = new FirebaseOptions.Builder()
.setDatabaseUrl("https://mobile-fitness-787c0.firebaseio.com")
.build();
FirebaseApp.initializeApp(this, options);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[] {Manifest.permission.CAMERA},
50
);
}
faceDetectButton = (Button)findViewById(R.id.capture_button);
graphicOverlay = (GraphicOverlay)findViewById(R.id.graphic_overlay);
cameraKitView = (CameraKitView)findViewById(R.id.camera_frame);
alertDialog = new SpotsDialog.Builder()
.setContext(this)
.setMessage("Please wait, Loading...")
.setCancelable(false).build();
cameraKitView.setFacing(CameraKit.FACING_FRONT);
cameraKitView.startVideo();
faceDetectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cameraKitView.captureImage(new CameraKitView.ImageCallback() {
@Override
public void onImage(CameraKitView cameraKitView, byte[] bytes) {
alertDialog.show();
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
mutableBitmap = Bitmap.createScaledBitmap(mutableBitmap,
cameraKitView.getWidth(),
cameraKitView.getHeight(),
true
);
processFaceDetection(mutableBitmap);
}
});
cameraKitView.invalidate();
}
});
}
private void processFaceDetection(Bitmap bitmap) {
FirebaseVisionImage fbvImage = FirebaseVisionImage.fromBitmap(bitmap);
FirebaseVisionFaceDetectorOptions fbvOptions = new FirebaseVisionFaceDetectorOptions
.Builder().setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
.enableTracking().build();
FirebaseVisionFaceDetector fbvDetector = FirebaseVision.getInstance()
.getVisionFaceDetector(fbvOptions);
fbvDetector.detectInImage(fbvImage).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionFace>>() {
@Override
public void onSuccess(List<FirebaseVisionFace> firebaseVisionFaces) {
getFaceResults(firebaseVisionFaces);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FaceActivity.this, "Error: " + e, Toast.LENGTH_LONG).show();
}
});
}
private void getFaceResults(List<FirebaseVisionFace> fvbFaces) {
int counter = 0;
for (FirebaseVisionFace face : fvbFaces) {
Rect rect = face.getBoundingBox();
RectOverlay rectOverlay = new RectOverlay(graphicOverlay, rect);
counter++;
graphicOverlay.add(rectOverlay);
alertDialog.dismiss();
}
}
我的清单:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity android:name=".FaceActivity">
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="face" />
</activity>
<activity
android:name=".GameActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_game"
android:theme="@style/FullscreenTheme" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我已经尝试了其他问题中的大部分建议,但无济于事。活动加载,前置摄像头打开,我看到实时视频,但没有面部检测,我收到上述错误。按下具有 clickListener 的按钮会触发,但 cameraKitView 不会捕获或分析图像。
我该怎么办?