Android पर एमएल किट की मदद से लैंडमार्क की पहचान करना

ML Kit का इस्तेमाल करके, किसी इमेज में मशहूर जगहों की पहचान की जा सकती है.

शुरू करने से पहले

  1. अगर आपने पहले से ऐसा नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.
  2. अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर app/build.gradle) में, ML Kit की Android लाइब्रेरी के लिए डिपेंडेंसी जोड़ें:
    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    
    dependencies {
      // ...
    
      implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
    }
  3. अगर आपने अब तक अपने प्रोजेक्ट के लिए, क्लाउड पर काम करने वाले एपीआई चालू नहीं किए हैं, तो अभी ऐसा करें:

    1. Firebase कंसोल का ML Kit APIs पेज खोलें.
    2. अगर आपने अब तक अपने प्रोजेक्ट को Blaze के प्लान पर अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें पर क्लिक करें. (आपको अपग्रेड करने के लिए तब ही कहा जाएगा, जब आपका प्रोजेक्ट Blaze प्लान पर नहीं होगा.)

      सिर्फ़ Blaze-लेवल के प्रोजेक्ट, क्लाउड-आधारित एपीआई का इस्तेमाल कर सकते हैं.

    3. अगर क्लाउड-आधारित एपीआई पहले से चालू नहीं हैं, तो क्लाउड-आधारित एपीआई चालू करें पर क्लिक करें.

लैंडमार्क डिटेक्टर को कॉन्फ़िगर करना

डिफ़ॉल्ट रूप से, क्लाउड डिटेक्टर, मॉडल के STABLE वर्शन का इस्तेमाल करता है और ज़्यादा से ज़्यादा 10 नतीजे दिखाता है. अगर आपको इनमें से किसी भी सेटिंग में बदलाव करना है, तो FirebaseVisionCloudDetectorOptions ऑब्जेक्ट का इस्तेमाल करके उन्हें बताएं.

उदाहरण के लिए, दोनों डिफ़ॉल्ट सेटिंग बदलने के लिए, यहां दिए गए उदाहरण के तौर पर FirebaseVisionCloudDetectorOptions ऑब्जेक्ट बनाएं:

JavaKotlin
FirebaseVisionCloudDetectorOptions options =
        new FirebaseVisionCloudDetectorOptions.Builder()
                .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
                .setMaxResults(15)
                .build();
val options = FirebaseVisionCloudDetectorOptions.Builder()
        .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
        .setMaxResults(15)
        .build()

डिफ़ॉल्ट सेटिंग का इस्तेमाल करने के लिए, अगले चरण में FirebaseVisionCloudDetectorOptions.DEFAULT का इस्तेमाल किया जा सकता है.

लैंडमार्क डिटेक्टर को चलाना

किसी इमेज में लैंडमार्क की पहचान करने के लिए, डिवाइस पर मौजूद Bitmap, media.Image, ByteBuffer, बाइट कलेक्शन या फ़ाइल में से किसी एक से FirebaseVisionImage ऑब्जेक्ट बनाएं. इसके बाद, FirebaseVisionImage ऑब्जेक्ट को FirebaseVisionCloudLandmarkDetector के detectInImage तरीके में पास करें.

  1. अपनी इमेज से FirebaseVisionImage ऑब्जेक्ट बनाएं.

    • media.Image ऑब्जेक्ट से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए, media.Image ऑब्जेक्ट और इमेज के रोटेशन को FirebaseVisionImage.fromMediaImage() में पास करें. जैसे, डिवाइस के कैमरे से इमेज कैप्चर करते समय.

      अगर CameraX लाइब्रेरी का इस्तेमाल किया जाता है, तो OnImageCapturedListener और ImageAnalysis.Analyzer क्लास आपके लिए रोटेशन वैल्यू का हिसाब लगाती हैं. इसलिए, FirebaseVisionImage.fromMediaImage() को कॉल करने से पहले, आपको रोटेशन को ML Kit के ROTATION_ कॉन्स्टेंट में बदलना होगा:

      JavaKotlin
      private 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 Kit Vision API
              // ...
          }
      }
      private 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 Kit Vision API
                  // ...
              }
          }
      }

      अगर आपने ऐसी कैमरा लाइब्रेरी का इस्तेमाल नहीं किया है जो इमेज के रोटेशन की जानकारी देती है, तो डिवाइस के रोटेशन और डिवाइस में मौजूद कैमरा सेंसर के ओरिएंटेशन से इसका हिसाब लगाया जा सकता है:

      JavaKotlin
      private 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;
      }
      private 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
      }

      इसके बाद, media.Image ऑब्जेक्ट और FirebaseVisionImage.fromMediaImage() में रोटेशन की वैल्यू पास करें:

      JavaKotlin
      FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
      val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
    • फ़ाइल यूआरआई से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन कॉन्टेक्स्ट और फ़ाइल यूआरआई को FirebaseVisionImage.fromFilePath() में पास करें. यह तब काम आता है, जब उपयोगकर्ता को अपने गैलरी ऐप्लिकेशन से कोई इमेज चुनने के लिए कहने के लिए, ACTION_GET_CONTENT इंटेंट का इस्तेमाल किया जाता है.
      JavaKotlin
      FirebaseVisionImage image;
      try {
          image = FirebaseVisionImage.fromFilePath(context, uri);
      } catch (IOException e) {
          e.printStackTrace();
      }
      val image: FirebaseVisionImage
      try {
          image = FirebaseVisionImage.fromFilePath(context, uri)
      } catch (e: IOException) {
          e.printStackTrace()
      }
    • ByteBuffer या बाइट कलेक्शन से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए, सबसे पहले media.Image इनपुट के लिए ऊपर बताए गए तरीके से इमेज के रोटेशन का हिसाब लगाएं.

      इसके बाद, एक FirebaseVisionImageMetadata ऑब्जेक्ट बनाएं, जिसमें इमेज की ऊंचाई, चौड़ाई, रंग कोडिंग फ़ॉर्मैट, और घुमाव की जानकारी हो:

      JavaKotlin
      FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
              .setWidth(480)   // 480x360 is typically sufficient for
              .setHeight(360)  // image recognition
              .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
              .setRotation(rotation)
              .build();
      val metadata = FirebaseVisionImageMetadata.Builder()
              .setWidth(480) // 480x360 is typically sufficient for
              .setHeight(360) // image recognition
              .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
              .setRotation(rotation)
              .build()

      FirebaseVisionImage ऑब्जेक्ट बनाने के लिए, बफ़र या कलेक्शन और मेटाडेटा ऑब्जेक्ट का इस्तेमाल करें:

      JavaKotlin
      FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata);
      // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
      val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata)
      // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
    • Bitmap ऑब्जेक्ट से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए:
      JavaKotlin
      FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
      val image = FirebaseVisionImage.fromBitmap(bitmap)
      Bitmap ऑब्जेक्ट से दिखाई गई इमेज, सीधी होनी चाहिए. इसे किसी और दिशा में घुमाने की ज़रूरत नहीं है.

  2. FirebaseVisionCloudLandmarkDetector का इंस्टेंस पाएं:

    JavaKotlin
    FirebaseVisionCloudLandmarkDetector detector = FirebaseVision.getInstance()
            .getVisionCloudLandmarkDetector();
    // Or, to change the default settings:
    // FirebaseVisionCloudLandmarkDetector detector = FirebaseVision.getInstance()
    //         .getVisionCloudLandmarkDetector(options);
    val detector = FirebaseVision.getInstance()
            .visionCloudLandmarkDetector
    // Or, to change the default settings:
    // val detector = FirebaseVision.getInstance()
    //         .getVisionCloudLandmarkDetector(options)
  3. आखिर में, इमेज को detectInImage तरीके में पास करें:

    JavaKotlin
    Task<List<FirebaseVisionCloudLandmark>> result = detector.detectInImage(image)
            .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionCloudLandmark>>() {
                @Override
                public void onSuccess(List<FirebaseVisionCloudLandmark> firebaseVisionCloudLandmarks) {
                    // Task completed successfully
                    // ...
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // Task failed with an exception
                    // ...
                }
            });
    val result = detector.detectInImage(image)
            .addOnSuccessListener { firebaseVisionCloudLandmarks ->
                // Task completed successfully
                // ...
            }
            .addOnFailureListener { e ->
                // Task failed with an exception
                // ...
            }

पहचाने गए लैंडमार्क के बारे में जानकारी पाना

अगर लैंडमार्क की पहचान करने की प्रोसेस पूरी हो जाती है, तो FirebaseVisionCloudLandmark ऑब्जेक्ट की सूची, सफलता के बारे में बताने वाले फ़ंक्शन को भेज दी जाएगी. हर FirebaseVisionCloudLandmark ऑब्जेक्ट, इमेज में पहचाने गए किसी लैंडमार्क को दिखाता है. हर लैंडमार्क के लिए, इनपुट इमेज में उसके बाउंडिंग निर्देशांक, लैंडमार्क का नाम, उसका अक्षांश और देशांतर, नॉलेज ग्राफ़ इकाई आईडी (अगर उपलब्ध हो) और मैच के कॉन्फ़िडेंस स्कोर की जानकारी मिल सकती है. उदाहरण के लिए:

JavaKotlin
for (FirebaseVisionCloudLandmark landmark: firebaseVisionCloudLandmarks) {

    Rect bounds = landmark.getBoundingBox();
    String landmarkName = landmark.getLandmark();
    String entityId = landmark.getEntityId();
    float confidence = landmark.getConfidence();

    // Multiple locations are possible, e.g., the location of the depicted
    // landmark and the location the picture was taken.
    for (FirebaseVisionLatLng loc: landmark.getLocations()) {
        double latitude = loc.getLatitude();
        double longitude = loc.getLongitude();
    }
}
for (landmark in firebaseVisionCloudLandmarks) {

    val bounds = landmark.boundingBox
    val landmarkName = landmark.landmark
    val entityId = landmark.entityId
    val confidence = landmark.confidence

    // Multiple locations are possible, e.g., the location of the depicted
    // landmark and the location the picture was taken.
    for (loc in landmark.locations) {
        val latitude = loc.latitude
        val longitude = loc.longitude
    }
}

अगले चरण

ML Kit for Firebase provided ready-to-use ML solutions for app developers. New apps should use the standalone ML Kit library for on-device ML and Firebase ML for cloud-based ML.

Feb 28, 2025 को अपडेट किया गया

ML Kit for Firebase provided ready-to-use ML solutions for app developers. New apps should use the standalone ML Kit library for on-device ML and Firebase ML for cloud-based ML.

Feb 28, 2025 को अपडेट किया गया

ML Kit for Firebase provided ready-to-use ML solutions for app developers. New apps should use the standalone ML Kit library for on-device ML and Firebase ML for cloud-based ML.

Feb 28, 2025 को अपडेट किया गया