iOS पर एमएल किट की मदद से चेहरों की पहचान करें

इमेज और वीडियो में चेहरों की पहचान करने के लिए, एमएल किट का इस्तेमाल किया जा सकता है.

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

  1. अगर आपने पहले से अपने ऐप्लिकेशन में Firebase नहीं जोड़ा है, तो ऐसा करने के लिए शुरुआती निर्देश में दिए गए चरणों को पूरा करें.
  2. अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:
    pod 'Firebase/MLVision', '6.25.0'
    # If you want to detect face contours (landmark detection and classification
    # don't require this additional model):
    pod 'Firebase/MLVisionFaceModel', '6.25.0'
    
    प्रोजेक्ट के Pods को इंस्टॉल या अपडेट करने के बाद, अपना Xcode ज़रूर खोलें प्रोजेक्ट को .xcworkspace का इस्तेमाल करके बनाया गया है.
  3. अपने ऐप्लिकेशन में Firebase इंपोर्ट करें:
    SwiftObjective-C
    import Firebase
    @import Firebase;

इनपुट इमेज के लिए दिशा-निर्देश

एमएल किट में चेहरों की सटीक पहचान हो, इसके लिए इनपुट इमेज में चेहरे होने चाहिए जिन्हें काफ़ी पिक्सल डेटा से दिखाया जाता है. आम तौर पर, आपके हिसाब से बनाया गया हर चेहरा ताकि इमेज कम से कम 100x100 पिक्सल की हो. अगर आपको यह पता लगाना है कि चेहरों की बनावट, ML किट में हाई रिज़ॉल्यूशन इनपुट की ज़रूरत होती है: हर एक चेहरे यह कम से कम 200x200 पिक्सल का होना चाहिए.

अगर रीयल-टाइम ऐप्लिकेशन में चेहरों का पता लगाया जा रहा है, तो आपको इनपुट इमेज के कुल डाइमेंशन पर विचार किया जा सकता है. छोटी इमेज तेज़ी से प्रोसेस होता है, इसलिए इंतज़ार का समय कम करने के लिए, इमेज कम रिज़ॉल्यूशन में कैप्चर करें (ऊपर बताई गई सटीक जानकारी से जुड़ी ज़रूरी शर्तों को ध्यान में रखते हुए) और पक्का करें कि सब्जेक्ट का चेहरा, इमेज के ज़्यादातर हिस्से को घेर लेता है. यह भी देखें रीयल-टाइम में परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह.

खराब इमेज फ़ोकस की वजह से सटीक जानकारी पर असर पड़ सकता है. अगर आपको मान्य नतीजे नहीं मिल रहे हैं, तो उपयोगकर्ता से इमेज को फिर से कैप्चर करने के लिए कहें.

कैमरे के हिसाब से किसी चेहरे का ओरिएंटेशन इस बात पर भी असर डाल सकता है कि चेहरे की ML Kit की मदद से पता लगाना. यहां जाएं: चेहरे की पहचान Concepts.

1. चेहरे की पहचान करने वाली सुविधा को कॉन्फ़िगर करें

किसी इमेज पर चेहरे की पहचान करने वाली सुविधा लागू करने से पहले, अगर आप फ़ेस डिटेक्टर की डिफ़ॉल्ट सेटिंग का इस्तेमाल करती है, तो VisionFaceDetectorOptions ऑब्जेक्ट. इसे बदला जा सकता है निम्न सेटिंग:

सेटिंग
performanceMode fast (डिफ़ॉल्ट) | accurate

चेहरे की पहचान करते समय गति या सटीक जानकारी दें.

landmarkMode none (डिफ़ॉल्ट) | all

क्या चेहरे के "लैंडमार्क" को पहचानने की कोशिश करनी है—आंखों, पहचान किए गए सभी चेहरों के कान, नाक, गाल, मुंह.

contourMode none (डिफ़ॉल्ट) | all

चेहरे की बनावट का पता लगाना है या नहीं. कंटूर यह हैं जो किसी इमेज में सिर्फ़ सबसे साफ़ तौर पर दिखने वाले चेहरे के लिए पहचानी जाती है.

classificationMode none (डिफ़ॉल्ट) | all

"मुस्कुराते हुए" जैसी कैटगरी में चेहरों को कैटगरी में बांटना है या नहीं, और "आंखें खुली हों".

minFaceSize CGFloat (डिफ़ॉल्ट: 0.1)

इमेज के हिसाब से, पता लगाए जाने वाले चेहरों का कम से कम साइज़.

isTrackingEnabled false (डिफ़ॉल्ट) | true

चेहरों को कोई आईडी असाइन करें या नहीं, जिसका इस्तेमाल ट्रैक करने के लिए किया जा सकता है भी मिलते हैं.

ध्यान दें कि जब कंटूर पहचान सक्षम हो, तो केवल एक ही चेहरा चेहरा ट्रैक करने की सुविधा से काम के नतीजे नहीं मिलते. इसके लिए और जांच की स्पीड को बेहतर बनाने के लिए, दोनों कंटूर को चालू न करें की सुविधा दी जाती है.

उदाहरण के लिए, VisionFaceDetectorOptions बनाएं ऑब्जेक्ट सबमिट करते हैं, जैसा कि नीचे दिए गए उदाहरणों में बताया गया है:

SwiftObjective-C
// High-accuracy landmark detection and face classification
let options = VisionFaceDetectorOptions()
options.performanceMode = .accurate
options.landmarkMode = .all
options.classificationMode = .all

// Real-time contour detection of multiple faces
let options = VisionFaceDetectorOptions()
options.contourMode = .all
// High-accuracy landmark detection and face classification
FIRVisionFaceDetectorOptions *options = [[FIRVisionFaceDetectorOptions alloc] init];
options.performanceMode = FIRVisionFaceDetectorPerformanceModeAccurate;
options.landmarkMode = FIRVisionFaceDetectorLandmarkModeAll;
options.classificationMode = FIRVisionFaceDetectorClassificationModeAll;

// Real-time contour detection of multiple faces
FIRVisionFaceDetectorOptions *options = [[FIRVisionFaceDetectorOptions alloc] init];
options.contourMode = FIRVisionFaceDetectorContourModeAll;

2. चेहरे की पहचान करने वाला टूल चलाएं

किसी इमेज में चेहरों का पता लगाने के लिए, इमेज को UIImage या CMSampleBufferRef से VisionFaceDetector के detect(in:) के लिए तरीका:

  1. VisionFaceDetector का इंस्टेंस पाएं:
    SwiftObjective-C
    lazy var vision = Vision.vision()
    
    let faceDetector = vision.faceDetector(options: options)
    FIRVision *vision = [FIRVision vision];
    FIRVisionFaceDetector *faceDetector = [vision faceDetector];
    // Or, to change the default settings:
    // FIRVisionFaceDetector *faceDetector =
    //     [vision faceDetectorWithOptions:options];
  2. एक VisionImage ऑब्जेक्ट को UIImage या CMSampleBufferRef.

    UIImage का इस्तेमाल करने के लिए:

    1. अगर ज़रूरी हो, तो इमेज को घुमाएं, ताकि इसकी imageOrientation प्रॉपर्टी .up है.
    2. स्क्रीन की दिशा को सही तरीके से घुमाने के लिए, VisionImage ऑब्जेक्ट बनाएं UIImage. कोई भी रोटेशन मेटाडेटा तय न करें—डिफ़ॉल्ट .topLeft वैल्यू का इस्तेमाल करना ज़रूरी है.
      SwiftObjective-C
      let image = VisionImage(image: uiImage)
      FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];

    CMSampleBufferRef का इस्तेमाल करने के लिए:

    1. एक VisionImageMetadata ऑब्जेक्ट बनाएं, जो में शामिल इमेज डेटा का ओरिएंटेशन CMSampleBufferRef बफ़र.

      इमेज का ओरिएंटेशन पाने के लिए:

      SwiftObjective-C
      func imageOrientation(
          deviceOrientation: UIDeviceOrientation,
          cameraPosition: AVCaptureDevice.Position
          ) -> VisionDetectorImageOrientation {
          switch deviceOrientation {
          case .portrait:
              return cameraPosition == .front ? .leftTop : .rightTop
          case .landscapeLeft:
              return cameraPosition == .front ? .bottomLeft : .topLeft
          case .portraitUpsideDown:
              return cameraPosition == .front ? .rightBottom : .leftBottom
          case .landscapeRight:
              return cameraPosition == .front ? .topRight : .bottomRight
          case .faceDown, .faceUp, .unknown:
              return .leftTop
          }
      }
      - (FIRVisionDetectorImageOrientation)
          imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                                 cameraPosition:(AVCaptureDevicePosition)cameraPosition {
        switch (deviceOrientation) {
          case UIDeviceOrientationPortrait:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationLeftTop;
            } else {
              return FIRVisionDetectorImageOrientationRightTop;
            }
          case UIDeviceOrientationLandscapeLeft:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationBottomLeft;
            } else {
              return FIRVisionDetectorImageOrientationTopLeft;
            }
          case UIDeviceOrientationPortraitUpsideDown:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationRightBottom;
            } else {
              return FIRVisionDetectorImageOrientationLeftBottom;
            }
          case UIDeviceOrientationLandscapeRight:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationTopRight;
            } else {
              return FIRVisionDetectorImageOrientationBottomRight;
            }
          default:
            return FIRVisionDetectorImageOrientationTopLeft;
        }
      }

      इसके बाद, मेटाडेटा ऑब्जेक्ट बनाएं:

      SwiftObjective-C
      let cameraPosition = AVCaptureDevice.Position.back  // Set to the capture device you used.
      let metadata = VisionImageMetadata()
      metadata.orientation = imageOrientation(
          deviceOrientation: UIDevice.current.orientation,
          cameraPosition: cameraPosition
      )
      FIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init];
      AVCaptureDevicePosition cameraPosition =
          AVCaptureDevicePositionBack;  // Set to the capture device you used.
      metadata.orientation =
          [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                       cameraPosition:cameraPosition];
    2. VisionImage ऑब्जेक्ट बनाने के लिए, CMSampleBufferRef ऑब्जेक्ट और रोटेशन मेटाडेटा:
      SwiftObjective-C
      let image = VisionImage(buffer: sampleBuffer)
      image.metadata = metadata
      FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer];
      image.metadata = metadata;
  3. इसके बाद, detect(in:) तरीके से इमेज पास करें:
    SwiftObjective-C
    faceDetector.process(visionImage) { faces, error in
      guard error == nil, let faces = faces, !faces.isEmpty else {
        // ...
        return
      }
    
      // Faces detected
      // ...
    }
    [faceDetector detectInImage:image
                     completion:^(NSArray<FIRVisionFace *> *faces,
                                  NSError *error) {
      if (error != nil) {
        return;
      } else if (faces != nil) {
        // Recognized faces
      }
    }];

3. पहचाने गए चेहरों की जानकारी पाएं

अगर चेहरे की पहचान करने की कार्रवाई पूरी हो जाती है, तो चेहरे की पहचान करने वाला डिवाइस एक अरे को पास कर देता है में से VisionFace ऑब्जेक्ट को पूरा करने वाले हैंडलर में. हर VisionFace ऑब्जेक्ट, इमेज में मिले चेहरे को दिखाता है. इसके लिए हैं, तो आपको इनपुट इमेज में इसके बाउंडिंग कोऑर्डिनेट मिल सकते हैं. साथ ही, कोई और जानकारी जिसे आपने ढूंढने के लिए फ़ेस डिटेक्टर कॉन्फ़िगर किया था. उदाहरण के लिए:

SwiftObjective-C
for face in faces {
  let frame = face.frame
  if face.hasHeadEulerAngleY {
    let rotY = face.headEulerAngleY  // Head is rotated to the right rotY degrees
  }
  if face.hasHeadEulerAngleZ {
    let rotZ = face.headEulerAngleZ  // Head is rotated upward rotZ degrees
  }

  // If landmark detection was enabled (mouth, ears, eyes, cheeks, and
  // nose available):
  if let leftEye = face.landmark(ofType: .leftEye) {
    let leftEyePosition = leftEye.position
  }

  // If contour detection was enabled:
  if let leftEyeContour = face.contour(ofType: .leftEye) {
    let leftEyePoints = leftEyeContour.points
  }
  if let upperLipBottomContour = face.contour(ofType: .upperLipBottom) {
    let upperLipBottomPoints = upperLipBottomContour.points
  }

  // If classification was enabled:
  if face.hasSmilingProbability {
    let smileProb = face.smilingProbability
  }
  if face.hasRightEyeOpenProbability {
    let rightEyeOpenProb = face.rightEyeOpenProbability
  }

  // If face tracking was enabled:
  if face.hasTrackingID {
    let trackingId = face.trackingID
  }
}
for (FIRVisionFace *face in faces) {
  // Boundaries of face in image
  CGRect frame = face.frame;

  if (face.hasHeadEulerAngleY) {
    CGFloat rotY = face.headEulerAngleY;  // Head is rotated to the right rotY degrees
  }
  if (face.hasHeadEulerAngleZ) {
    CGFloat rotZ = face.headEulerAngleZ;  // Head is tilted sideways rotZ degrees
  }

  // If landmark detection was enabled (mouth, ears, eyes, cheeks, and
  // nose available):
  FIRVisionFaceLandmark *leftEar = [face landmarkOfType:FIRFaceLandmarkTypeLeftEar];
  if (leftEar != nil) {
    FIRVisionPoint *leftEarPosition = leftEar.position;
  }

  // If contour detection was enabled:
  FIRVisionFaceContour *upperLipBottomContour = [face contourOfType:FIRFaceContourTypeUpperLipBottom];
  if (upperLipBottomContour != nil) {
    NSArray<FIRVisionPoint *> *upperLipBottomPoints = upperLipBottomContour.points;
    if (upperLipBottomPoints.count > 0) {
      NSLog("Detected the bottom contour of the subject's upper lip.")
    }
  }

  // If classification was enabled:
  if (face.hasSmilingProbability) {
    CGFloat smileProb = face.smilingProbability;
  }
  if (face.hasRightEyeOpenProbability) {
    CGFloat rightEyeOpenProb = face.rightEyeOpenProbability;
  }

  // If face tracking was enabled:
  if (face.hasTrackingID) {
    NSInteger trackingID = face.trackingID;
  }
}

चेहरे की बनावट के उदाहरण

चेहरे की बनावट की पहचान करने वाली सुविधा चालू होने पर, आपको हर चेहरे की पहचान कर ली गई थी. ये बिंदु दिखाते हैं कि सुविधा. चेहरा देखें कंटूर के तरीके के बारे में जानने के लिए, डिटेक्शन कॉन्सेप्ट की खास जानकारी बताया गया है.

निम्न चित्र दिखाता है कि ये बिंदु किसी चेहरे से कैसे मैप होते हैं ( इमेज को बड़ा करने के लिए):

रीयल-टाइम में चेहरे की पहचान करने की सुविधा

अगर आपको रीयल-टाइम ऐप्लिकेशन में चेहरे की पहचान करने की सुविधा का इस्तेमाल करना है, तो इन निर्देशों का पालन करें सबसे सही फ़्रेमरेट हासिल करने के लिए दिशा-निर्देश:

  • इनमें से किसी एक का इस्तेमाल करने के लिए, चेहरे की पहचान करने वाले टूल को कॉन्फ़िगर करें चेहरे की बनावट की पहचान करने या क्लासिफ़िकेशन और लैंडमार्क की पहचान करने के लिए किया जा सकता है, लेकिन दोनों नहीं:

    कंटूर की पहचान
    लैंडमार्क की पहचान
    क्लासिफ़िकेशन
    लैंडमार्क की पहचान करना और उसे अलग-अलग कैटगरी में बांटना
    कंटूर की पहचान और लैंडमार्क की पहचान
    कंटूर की पहचान और क्लासिफ़िकेशन
    कंटूर की पहचान, लैंडमार्क की पहचान, और क्लासिफ़िकेशन

  • fast मोड चालू करें (डिफ़ॉल्ट रूप से चालू रहता है).

  • कम रिज़ॉल्यूशन वाली इमेज कैप्चर करें. हालांकि, यह भी ध्यान रखें कि इस एपीआई की इमेज डाइमेंशन से जुड़ी ज़रूरी शर्तें.

  • डिटेक्टर को कॉल थ्रॉटल करें. अगर कोई नया वीडियो फ़्रेम डिटेक्टर के चलने के दौरान उपलब्ध होने पर, फ़्रेम छोड़ें.
  • अगर ग्राफ़िक को ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जा रहा है इनपुट इमेज को चुनने के बाद, पहले एमएल किट से नतीजा पाएं. इसके बाद, इमेज को रेंडर करें और ओवरले को एक ही चरण में पूरा करें. ऐसा करके, डिसप्ले सरफ़ेस पर रेंडर हो जाता है हर इनपुट फ़्रेम के लिए सिर्फ़ एक बार. previewOverlayView देखें और FIRDetectionOverlayView उदाहरण के लिए शोकेस सैंपल ऐप्लिकेशन में क्लास.

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 को अपडेट किया गया