Use Firebase ML para rotular objetos reconhecidos em uma imagem. Consulte a visão geral para informações sobre os recursos dessa API.
Antes de começar
- Adicione o Firebase ao seu projeto para Android, caso ainda não tenha feito isso.
- 
    
    
    
    
    
    
    
    
No arquivo Gradle do módulo (nível do app) (geralmente <project>/<app-module>/build.gradle.ktsou<project>/<app-module>/build.gradle), adicione a dependência da biblioteca do Firebase ML para Android. Recomendamos o uso do Firebase Android BoM para controlar o controle de versões da biblioteca.dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.4.0")) // Add the dependency for the Firebase ML Vision library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-ml-vision' } Com o Firebase Android BoM, seu app sempre vai usar versões compatíveis das bibliotecas do Firebase para Android. (Alternativa) Adicionar dependências das bibliotecas do Firebase sem usar o BoM Se você preferir não usar o Firebase BoM, especifique cada versão das bibliotecas do Firebase na linha de dependência correspondente. Se você usa várias bibliotecas do Firebase no app, recomendamos utilizar o BoM para gerenciar as versões delas, porque isso ajuda a garantir a compatibilidade de todas as bibliotecas. dependencies { // Add the dependency for the Firebase ML Vision library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-ml-vision:24.1.0' } 
- 
  Se você ainda não ativou as APIs baseadas na nuvem para seu projeto, faça isso agora: - Abra a página do Firebase ML no console do Firebase.
- 
      Se você ainda não fez o upgrade do seu projeto para o plano de preços Blaze de pagamento por uso, clique em Fazer upgrade. Você só vai receber uma mensagem para fazer upgrade se o projeto não estiver no plano de preços Blaze. Apenas projetos no plano de preços do Blaze podem usar APIs baseadas na nuvem. 
- Caso as APIs baseadas na nuvem ainda não estejam ativadas, clique em Ativar APIs baseadas na nuvem.
 
Agora você já pode rotular imagens.
1. Preparar a imagem de entrada
Crie um objetoFirebaseVisionImage com base na sua imagem.
O rotulador de imagens é executado mais rapidamente quando você usa um Bitmap ou, se você usa a
API Camera2, um media.Image no formato JPEG. Essas opções são recomendadas quando possível.
- 
    Para criar um objeto FirebaseVisionImagea partir de um objetomedia.Image, como ao capturar uma imagem da câmera de um dispositivo, transmita o objetomedia.Imagee a rotação da imagem paraFirebaseVisionImage.fromMediaImage().Se você usar a biblioteca CameraX, as classes OnImageCapturedListenereImageAnalysis.Analyzervão calcular o valor de rotação para você. Basta converter a rotação em uma das constantesROTATION_do Firebase ML antes de chamarFirebaseVisionImage.fromMediaImage():Kotlinprivate class YourImageAnalyzer : ImageAnalysis.Analyzer { private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) { 0 -> FirebaseVisionImageMetadata.ROTATION_0 90 -> FirebaseVisionImageMetadata.ROTATION_90 180 -> FirebaseVisionImageMetadata.ROTATION_180 270 -> FirebaseVisionImageMetadata.ROTATION_270 else -> throw Exception("Rotation must be 0, 90, 180, or 270.") } override fun analyze(imageProxy: ImageProxy?, degrees: Int) { val mediaImage = imageProxy?.image val imageRotation = degreesToFirebaseRotation(degrees) if (mediaImage != null) { val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) // Pass image to an ML Vision API // ... } } } Javaprivate class YourAnalyzer implements ImageAnalysis.Analyzer { private int degreesToFirebaseRotation(int degrees) { switch (degrees) { case 0: return FirebaseVisionImageMetadata.ROTATION_0; case 90: return FirebaseVisionImageMetadata.ROTATION_90; case 180: return FirebaseVisionImageMetadata.ROTATION_180; case 270: return FirebaseVisionImageMetadata.ROTATION_270; default: throw new IllegalArgumentException( "Rotation must be 0, 90, 180, or 270."); } } @Override public void analyze(ImageProxy imageProxy, int degrees) { if (imageProxy == null || imageProxy.getImage() == null) { return; } Image mediaImage = imageProxy.getImage(); int rotation = degreesToFirebaseRotation(degrees); FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation); // Pass image to an ML Vision API // ... } } Se você não usar uma biblioteca de câmera que ofereça a rotação da imagem, será possível calculá-la usando a rotação do dispositivo e a orientação do sensor da câmera: Kotlinprivate val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 90) ORIENTATIONS.append(Surface.ROTATION_90, 0) ORIENTATIONS.append(Surface.ROTATION_180, 270) ORIENTATIONS.append(Surface.ROTATION_270, 180) } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Throws(CameraAccessException::class) private fun getRotationCompensation(cameraId: String, activity: Activity, context: Context): Int { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. val deviceRotation = activity.windowManager.defaultDisplay.rotation var rotationCompensation = ORIENTATIONS.get(deviceRotation) // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. val cameraManager = context.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360 // Return the corresponding FirebaseVisionImageMetadata rotation value. val result: Int when (rotationCompensation) { 0 -> result = FirebaseVisionImageMetadata.ROTATION_0 90 -> result = FirebaseVisionImageMetadata.ROTATION_90 180 -> result = FirebaseVisionImageMetadata.ROTATION_180 270 -> result = FirebaseVisionImageMetadata.ROTATION_270 else -> { result = FirebaseVisionImageMetadata.ROTATION_0 Log.e(TAG, "Bad rotation value: $rotationCompensation") } } return result } Javaprivate static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private int getRotationCompensation(String cameraId, Activity activity, Context context) throws CameraAccessException { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int rotationCompensation = ORIENTATIONS.get(deviceRotation); // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360; // Return the corresponding FirebaseVisionImageMetadata rotation value. int result; switch (rotationCompensation) { case 0: result = FirebaseVisionImageMetadata.ROTATION_0; break; case 90: result = FirebaseVisionImageMetadata.ROTATION_90; break; case 180: result = FirebaseVisionImageMetadata.ROTATION_180; break; case 270: result = FirebaseVisionImageMetadata.ROTATION_270; break; default: result = FirebaseVisionImageMetadata.ROTATION_0; Log.e(TAG, "Bad rotation value: " + rotationCompensation); } return result; } Em seguida, transmita o objeto media.Imagee o valor de rotação paraFirebaseVisionImage.fromMediaImage():Kotlinval image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation) JavaFirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation); 
- Para criar um objeto FirebaseVisionImagecom base no URI de um arquivo, transmita o contexto do aplicativo e o URI do arquivo paraFirebaseVisionImage.fromFilePath(). Isso é útil ao usar uma intentACTION_GET_CONTENTpara solicitar que o usuário selecione uma imagem no app de galeria dele.Kotlinval image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() } JavaFirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); } 
- Para criar um objeto FirebaseVisionImagecom base em umByteBufferou uma matriz de bytes, primeiro calcule a rotação da imagem conforme descrito acima para a entradamedia.Image.Em seguida, crie um objeto FirebaseVisionImageMetadataque contenha a altura, a largura, o formato de codificação de cores e a rotação da imagem:Kotlinval metadata = FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build() JavaFirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build(); Use o buffer ou a matriz e o objeto de metadados para criar um objeto FirebaseVisionImage:Kotlinval image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata) JavaFirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata); 
- Para criar um objeto FirebaseVisionImagecom base em um objetoBitmap:A imagem representada pelo objetoKotlinval image = FirebaseVisionImage.fromBitmap(bitmap) JavaFirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap); Bitmapprecisa estar na posição vertical, sem necessidade de girá-la novamente.
2. Configurar e executar o rotulador de imagens
Para rotular objetos em uma imagem, transmita o objetoFirebaseVisionImage para o método processImage do FirebaseVisionImageLabeler.
- Primeiro, receba uma instância de - FirebaseVisionImageLabeler.- Kotlin- val labeler = FirebaseVision.getInstance().getCloudImageLabeler() // Or, to set the minimum confidence required: // val options = FirebaseVisionCloudImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build() // val labeler = FirebaseVision.getInstance().getCloudImageLabeler(options)- Java- FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() .getCloudImageLabeler(); // Or, to set the minimum confidence required: // FirebaseVisionCloudImageLabelerOptions options = // new FirebaseVisionCloudImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build(); // FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() // .getCloudImageLabeler(options);
- Em seguida, transmita a imagem para o método - processImage():- Kotlin- labeler.processImage(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }- Java- labeler.processImage(image) .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() { @Override public void onSuccess(List<FirebaseVisionImageLabel> labels) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
3. Obter informações sobre os objetos rotulados
Se a operação de rotulagem da imagem for bem-sucedida, uma lista de objetosFirebaseVisionImageLabel será transmitida para o
listener correto. Cada objeto FirebaseVisionImageLabel representa algo que foi rotulado na imagem. Para cada rótulo, é possível conferir a descrição de texto
dele, o ID da entidade do Mapa de informações
(se disponível) e a pontuação de confiança da correspondência. Exemplo:
Kotlin
for (label in labels) {
  val text = label.text
  val entityId = label.entityId
  val confidence = label.confidence
}
Java
for (FirebaseVisionImageLabel label: labels) {
  String text = label.getText();
  String entityId = label.getEntityId();
  float confidence = label.getConfidence();
}
Próximas etapas
- Antes de implantar em produção um app que usa uma API do Cloud, é preciso seguir mais algumas etapas para evitar ou atenuar o efeito do acesso não autorizado à API.