Apple প্ল্যাটফর্মগুলিতে একটি AutoML-প্রশিক্ষিত মডেলের সাথে ছবিগুলিকে লেবেল করুন৷

আপনি AutoML Vision Edge ব্যবহার করে আপনার নিজের মডেলকে প্রশিক্ষণ দেওয়ার পরে, আপনি ছবিগুলিকে লেবেল করতে আপনার অ্যাপে এটি ব্যবহার করতে পারেন৷

অটোএমএল ভিশন এজ থেকে প্রশিক্ষিত মডেলগুলিকে সংহত করার দুটি উপায় রয়েছে৷ আপনি মডেলের ফাইলগুলিকে আপনার Xcode প্রকল্পে অনুলিপি করে মডেলটিকে বান্ডিল করতে পারেন, অথবা আপনি Firebase থেকে গতিশীলভাবে ডাউনলোড করতে পারেন।

মডেল bundling বিকল্প
আপনার অ্যাপে বান্ডিল
  • মডেলটি বান্ডিলের অংশ
  • অ্যাপল ডিভাইস অফলাইনে থাকলেও মডেলটি অবিলম্বে উপলব্ধ
  • ফায়ারবেস প্রকল্পের প্রয়োজন নেই
Firebase দিয়ে হোস্ট করা হয়েছে

তুমি শুরু করার আগে

  1. আপনার পডফাইলে এমএল কিট লাইব্রেরি অন্তর্ভুক্ত করুন:

    আপনার অ্যাপের সাথে একটি মডেল বান্ডিল করার জন্য:

    pod 'GoogleMLKit/ImageLabelingCustom'
    

    Firebase থেকে গতিশীলভাবে একটি মডেল ডাউনলোড করার জন্য, LinkFirebase নির্ভরতা যোগ করুন:

    pod 'GoogleMLKit/ImageLabelingCustom'
    pod 'GoogleMLKit/LinkFirebase'
    
  2. আপনি আপনার প্রোজেক্টের পড ইনস্টল বা আপডেট করার পরে, এটির .xcworkspace ব্যবহার করে আপনার Xcode প্রকল্পটি খুলুন। ML কিট Xcode সংস্করণ 12.2 বা উচ্চতর সমর্থিত।

  3. আপনি যদি একটি মডেল ডাউনলোড করতে চান , তাহলে নিশ্চিত করুন যে আপনি আপনার Android প্রকল্পে Firebase যোগ করেছেন , যদি আপনি ইতিমধ্যে তা না করে থাকেন। আপনি মডেল বান্ডিল যখন এটি প্রয়োজন হয় না.

1. মডেল লোড করুন

একটি স্থানীয় মডেল উৎস কনফিগার করুন

আপনার অ্যাপের সাথে মডেল বান্ডিল করতে:

  1. আপনি ফায়ারবেস কনসোল থেকে একটি ফোল্ডারে ডাউনলোড করা জিপ সংরক্ষণাগার থেকে মডেল এবং এর মেটাডেটা বের করুন:

    your_model_directory
      |____dict.txt
      |____manifest.json
      |____model.tflite
    

    তিনটি ফাইলই একই ফোল্ডারে থাকতে হবে। আমরা সুপারিশ করি যে আপনি ফাইলগুলি ডাউনলোড করার সময় ব্যবহার করুন, পরিবর্তন ছাড়াই (ফাইলের নাম সহ)।

  2. আপনার Xcode প্রকল্পে ফোল্ডারটি অনুলিপি করুন, যখন আপনি এটি করবেন তখন ফোল্ডার রেফারেন্স তৈরি করুন নির্বাচন করার যত্ন নিন। মডেল ফাইল এবং মেটাডেটা অ্যাপ বান্ডেলে অন্তর্ভুক্ত করা হবে এবং ML Kit-এ উপলব্ধ হবে।

  3. মডেল ম্যানিফেস্ট ফাইলের পথ নির্দিষ্ট করে LocalModel অবজেক্ট তৈরি করুন:

    সুইফট

    guard let manifestPath = Bundle.main.path(
        forResource: "manifest",
        ofType: "json",
        inDirectory: "your_model_directory"
    ) else { return true }
    let localModel = LocalModel(manifestPath: manifestPath)
    

    উদ্দেশ্য গ

    NSString *manifestPath =
        [NSBundle.mainBundle pathForResource:@"manifest"
                                      ofType:@"json"
                                 inDirectory:@"your_model_directory"];
    MLKLocalModel *localModel =
        [[MLKLocalModel alloc] initWithManifestPath:manifestPath];
    

একটি Firebase-হোস্টেড মডেল উৎস কনফিগার করুন

দূরবর্তীভাবে-হোস্ট করা মডেল ব্যবহার করতে, একটি CustomRemoteModel অবজেক্ট তৈরি করুন, আপনি মডেলটি প্রকাশ করার সময় যে নামটি নির্ধারণ করেছিলেন তা উল্লেখ করে:

সুইফট

// Initialize the model source with the name you assigned in
// the Firebase console.
let remoteModelSource = FirebaseModelSource(name: "your_remote_model")
let remoteModel = CustomRemoteModel(remoteModelSource: remoteModelSource)

উদ্দেশ্য গ

// Initialize the model source with the name you assigned in
// the Firebase console.
MLKFirebaseModelSource *firebaseModelSource =
    [[MLKFirebaseModelSource alloc] initWithName:@"your_remote_model"];
MLKCustomRemoteModel *remoteModel =
    [[MLKCustomRemoteModel alloc] initWithRemoteModelSource:firebaseModelSource];

তারপরে, আপনি যে শর্তে ডাউনলোড করার অনুমতি দিতে চান তা উল্লেখ করে মডেল ডাউনলোড টাস্ক শুরু করুন। যদি মডেলটি ডিভাইসে না থাকে, বা মডেলটির একটি নতুন সংস্করণ উপলব্ধ থাকলে, টাস্কটি অসিঙ্ক্রোনাসভাবে Firebase থেকে মডেলটি ডাউনলোড করবে:

সুইফট

let downloadConditions = ModelDownloadConditions(
  allowsCellularAccess: true,
  allowsBackgroundDownloading: true
)

let downloadProgress = ModelManager.modelManager().download(
  remoteModel,
  conditions: downloadConditions
)

উদ্দেশ্য গ

MLKModelDownloadConditions *downloadConditions =
    [[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
                                         allowsBackgroundDownloading:YES];

NSProgress *downloadProgress =
    [[MLKModelManager modelManager] downloadRemoteModel:remoteModel
                                             conditions:downloadConditions];

অনেক অ্যাপ তাদের ইনিশিয়ালাইজেশন কোডে ডাউনলোড টাস্ক শুরু করে, কিন্তু মডেল ব্যবহার করার আগে আপনি যেকোন সময়ে তা করতে পারেন।

আপনার মডেল থেকে একটি ইমেজ লেবেলার তৈরি করুন

আপনি আপনার মডেল উত্সগুলি কনফিগার করার পরে, তাদের মধ্যে একটি থেকে একটি ImageLabeler অবজেক্ট তৈরি করুন৷

যদি আপনার কাছে শুধুমাত্র স্থানীয়ভাবে বান্ডিল করা মডেল থাকে, তাহলে শুধু আপনার LocalModel অবজেক্ট থেকে একটি লেবেলার তৈরি করুন এবং আপনার প্রয়োজনীয় কনফিডেন্স স্কোর থ্রেশহোল্ড কনফিগার করুন ( আপনার মডেলের মূল্যায়ন দেখুন):

সুইফট

let options = CustomImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = NSNumber(value: 0.0)  // Evaluate your model in the Cloud console
                                                    // to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options)

উদ্দেশ্য গ

CustomImageLabelerOptions *options =
    [[CustomImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = @(0.0f);  // Evaluate your model in the Cloud console
                                        // to determine an appropriate value.
MLKImageLabeler *imageLabeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

আপনার যদি দূরবর্তীভাবে-হোস্ট করা মডেল থাকে, তাহলে আপনাকে এটি চালানোর আগে এটি ডাউনলোড করা হয়েছে কিনা তা পরীক্ষা করতে হবে। আপনি মডেল ম্যানেজারের isModelDownloaded(remoteModel:) পদ্ধতি ব্যবহার করে মডেল ডাউনলোড টাস্কের স্থিতি পরীক্ষা করতে পারেন।

যদিও আপনাকে শুধুমাত্র লেবেলার চালানোর আগে এটি নিশ্চিত করতে হবে, যদি আপনার কাছে একটি দূরবর্তীভাবে-হোস্ট করা মডেল এবং একটি স্থানীয়ভাবে-বান্ডিল মডেল উভয়ই থাকে, তাহলে ImageLabeler টি ইনস্ট্যান্টিয়েট করার সময় এই চেকটি সম্পাদন করা বোধগম্য হতে পারে: যদি এটি হয় তাহলে দূরবর্তী মডেল থেকে একটি লেবেলার তৈরি করুন ডাউনলোড করা হয়েছে, এবং অন্যথায় স্থানীয় মডেল থেকে।

সুইফট

var options: CustomImageLabelerOptions
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
  options = CustomImageLabelerOptions(remoteModel: remoteModel)
} else {
  options = CustomImageLabelerOptions(localModel: localModel)
}
options.confidenceThreshold = NSNumber(value: 0.0)  // Evaluate your model in the Firebase console
                                                    // to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options: options)

উদ্দেশ্য গ

MLKCustomImageLabelerOptions *options;
if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) {
  options = [[MLKCustomImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
  options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = @(0.0f);  // Evaluate your model in the Firebase console
                                        // to determine an appropriate value.
MLKImageLabeler *imageLabeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

যদি আপনার কাছে শুধুমাত্র একটি দূরবর্তীভাবে হোস্ট করা মডেল থাকে, তাহলে আপনার মডেল-সম্পর্কিত কার্যকারিতা অক্ষম করা উচিত-উদাহরণস্বরূপ, ধূসর-আউট বা আপনার UI-এর অংশ লুকান-যতক্ষণ না আপনি নিশ্চিত করেন যে মডেলটি ডাউনলোড করা হয়েছে।

আপনি ডিফল্ট বিজ্ঞপ্তি কেন্দ্রে পর্যবেক্ষকদের সংযুক্ত করে মডেল ডাউনলোডের অবস্থা পেতে পারেন। পর্যবেক্ষক ব্লকে self সম্পর্কে একটি দুর্বল রেফারেন্স ব্যবহার করতে ভুলবেন না, যেহেতু ডাউনলোডে কিছু সময় লাগতে পারে, এবং ডাউনলোড শেষ হওয়ার সময় থেকে উদ্ভূত বস্তুটি মুক্ত করা যেতে পারে। উদাহরণ স্বরূপ:

সুইফট

NotificationCenter.default.addObserver(
    forName: .mlkitMLModelDownloadDidSucceed,
    object: nil,
    queue: nil
) { [weak self] notification in
    guard let strongSelf = self,
        let userInfo = notification.userInfo,
        let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
            as? RemoteModel,
        model.name == "your_remote_model"
        else { return }
    // The model was downloaded and is available on the device
}

NotificationCenter.default.addObserver(
    forName: .mlkitMLModelDownloadDidFail,
    object: nil,
    queue: nil
) { [weak self] notification in
    guard let strongSelf = self,
        let userInfo = notification.userInfo,
        let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
            as? RemoteModel
        else { return }
    let error = userInfo[ModelDownloadUserInfoKey.error.rawValue]
    // ...
}

উদ্দেশ্য গ

__weak typeof(self) weakSelf = self;

[NSNotificationCenter.defaultCenter
    addObserverForName:MLKModelDownloadDidSucceedNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              MLKRemoteModel *model = note.userInfo[MLKModelDownloadUserInfoKeyRemoteModel];
              if ([model.name isEqualToString:@"your_remote_model"]) {
                // The model was downloaded and is available on the device
              }
            }];

[NSNotificationCenter.defaultCenter
    addObserverForName:MLKModelDownloadDidFailNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              NSError *error = note.userInfo[MLKModelDownloadUserInfoKeyError];
            }];

2. ইনপুট ইমেজ প্রস্তুত করুন

একটি UIImage বা একটি CMSampleBufferRef ব্যবহার করে একটি VisionImage অবজেক্ট তৈরি করুন।

আপনি একটি UIImage ব্যবহার করলে, এই পদক্ষেপগুলি অনুসরণ করুন:

  • UIImage দিয়ে একটি VisionImage অবজেক্ট তৈরি করুন। সঠিক .orientation উল্লেখ করতে ভুলবেন না।

    সুইফট

    let image = VisionImage(image: uiImage)
    visionImage.orientation = image.imageOrientation

    উদ্দেশ্য গ

    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

আপনি যদি একটি CMSampleBufferRef ব্যবহার করেন তবে এই পদক্ষেপগুলি অনুসরণ করুন:

  • CMSampleBufferRef বাফারে থাকা ইমেজ ডেটার ওরিয়েন্টেশন নির্দিষ্ট করুন।

    ইমেজ ওরিয়েন্টেশন পেতে:

    সুইফট

    func imageOrientation(
      deviceOrientation: UIDeviceOrientation,
      cameraPosition: AVCaptureDevice.Position
    ) -> UIImage.Orientation {
      switch deviceOrientation {
      case .portrait:
        return cameraPosition == .front ? .leftMirrored : .right
      case .landscapeLeft:
        return cameraPosition == .front ? .downMirrored : .up
      case .portraitUpsideDown:
        return cameraPosition == .front ? .rightMirrored : .left
      case .landscapeRight:
        return cameraPosition == .front ? .upMirrored : .down
      case .faceDown, .faceUp, .unknown:
        return .up
      }
    }
          

    উদ্দেশ্য গ

    - (UIImageOrientation)
      imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                             cameraPosition:(AVCaptureDevicePosition)cameraPosition {
      switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
          return position == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored
                                                          : UIImageOrientationRight;
    
        case UIDeviceOrientationLandscapeLeft:
          return position == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored
                                                          : UIImageOrientationUp;
        case UIDeviceOrientationPortraitUpsideDown:
          return position == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored
                                                          : UIImageOrientationLeft;
        case UIDeviceOrientationLandscapeRight:
          return position == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored
                                                          : UIImageOrientationDown;
        case UIDeviceOrientationUnknown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
          return UIImageOrientationUp;
      }
    }
          
  • CMSampleBufferRef অবজেক্ট এবং ওরিয়েন্টেশন ব্যবহার করে একটি VisionImage অবজেক্ট তৈরি করুন:

    সুইফট

    let image = VisionImage(buffer: sampleBuffer)
    image.orientation = imageOrientation(
      deviceOrientation: UIDevice.current.orientation,
      cameraPosition: cameraPosition)

    উদ্দেশ্য গ

     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image.orientation =
       [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition:cameraPosition];

3. ইমেজ লেবেলার চালান

অ্যাসিঙ্ক্রোনাসভাবে:

সুইফট

imageLabeler.process(image) { labels, error in
    guard error == nil, let labels = labels, !labels.isEmpty else {
        // Handle the error.
        return
    }
    // Show results.
}

উদ্দেশ্য গ

[imageLabeler
    processImage:image
      completion:^(NSArray<MLKImageLabel *> *_Nullable labels,
                   NSError *_Nullable error) {
        if (label.count == 0) {
            // Handle the error.
            return;
        }
        // Show results.
     }];

সিঙ্ক্রোনাসভাবে:

সুইফট

var labels: [ImageLabel]
do {
    labels = try imageLabeler.results(in: image)
} catch let error {
    // Handle the error.
    return
}
// Show results.

উদ্দেশ্য গ

NSError *error;
NSArray<MLKImageLabel *> *labels =
    [imageLabeler resultsInImage:image error:&error];
// Show results or handle the error.

4. লেবেলযুক্ত বস্তু সম্পর্কে তথ্য পান

ইমেজ লেবেলিং অপারেশন সফল হলে, এটি ImageLabel এর একটি অ্যারে প্রদান করে। প্রতিটি ImageLabel এমন কিছু উপস্থাপন করে যা ছবিতে লেবেল করা হয়েছিল। আপনি প্রতিটি লেবেলের পাঠ্য বিবরণ পেতে পারেন (যদি TensorFlow Lite মডেল ফাইলের মেটাডেটা পাওয়া যায়), আত্মবিশ্বাসের স্কোর এবং সূচক। উদাহরণ স্বরূপ:

সুইফট

for label in labels {
  let labelText = label.text
  let confidence = label.confidence
  let index = label.index
}

উদ্দেশ্য গ

for (MLKImageLabel *label in labels) {
  NSString *labelText = label.text;
  float confidence = label.confidence;
  NSInteger index = label.index;
}

রিয়েল-টাইম কর্মক্ষমতা উন্নত করার টিপস

আপনি যদি একটি রিয়েল-টাইম অ্যাপ্লিকেশনে চিত্রগুলিকে লেবেল করতে চান তবে সেরা ফ্রেমরেটগুলি অর্জন করতে এই নির্দেশিকাগুলি অনুসরণ করুন:

  • থ্রটল ডিটেক্টর কল. ডিটেক্টর চলাকালীন একটি নতুন ভিডিও ফ্রেম উপলব্ধ হলে, ফ্রেমটি ফেলে দিন।
  • আপনি যদি ইনপুট ইমেজে গ্রাফিক্স ওভারলে করার জন্য ডিটেক্টরের আউটপুট ব্যবহার করেন, তাহলে প্রথমে ফলাফল পান, তারপর একটি একক ধাপে চিত্র এবং ওভারলে রেন্ডার করুন। এটি করার মাধ্যমে, আপনি প্রতিটি ইনপুট ফ্রেমের জন্য শুধুমাত্র একবার প্রদর্শন পৃষ্ঠে রেন্ডার করবেন। উদাহরণের জন্য শোকেস নমুনা অ্যাপে প্রিভিউওভারলেভিউ এবং FIRDetectionOverlayView ক্লাসগুলি দেখুন।