ऑब्जेक्ट डिटेक्टर इंटिग्रेट करें

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

अपनी पसंद के मुताबिक ऑब्जेक्ट डिटेक्टर डिप्लॉय करने के लिए, टास्क लाइब्रेरी ObjectDetector एपीआई का इस्तेमाल करें जिन्हें आपके मोबाइल ऐप्लिकेशन में पहले से ट्रेनिंग दी गई है.

ObjectDetector API की मुख्य सुविधाएं

  • इनपुट इमेज प्रोसेसिंग, जिसमें रोटेशन, साइज़ बदलना, और कलर स्पेस शामिल हैं कन्वर्ज़न होता है.

  • मैप स्थान-भाषा को लेबल करें.

  • नतीजों को फ़िल्टर करने के लिए, स्कोर थ्रेशोल्ड.

  • टॉप-k की पहचान के नतीजे.

  • अनुमति वाली सूची और ब्लॉकलिस्ट को लेबल करें.

इस्तेमाल किए जा सकने वाले ऑब्जेक्ट डिटेक्टर के मॉडल

ये मॉडल, ObjectDetector के साथ काम करते हैं एपीआई.

Java में अनुमान चलाएं

ज़्यादा जानकारी के लिए, ऑब्जेक्ट डिटेक्शन के लिए रेफ़रंस ऐप्लिकेशन उदाहरण के लिए, Android ऐप्लिकेशन में ObjectDetector को इस्तेमाल करने का तरीका.

पहला चरण: Gradle डिपेंडेंसी और अन्य सेटिंग इंपोर्ट करना

.tflite मॉडल फ़ाइल को, Android मॉड्यूल की ऐसेट डायरेक्ट्री में कॉपी करें जहां मॉडल को चलाया जाएगा. तय करें कि फ़ाइल कंप्रेस नहीं की जानी चाहिए, और मॉड्यूल की build.gradle फ़ाइल में TensorFlow Lite लाइब्रेरी जोड़ें:

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }
}

dependencies {
    // Other dependencies

    // Import the Task Vision Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-vision'
    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

दूसरा चरण: मॉडल का इस्तेमाल करना

// Initialization
ObjectDetectorOptions options =
    ObjectDetectorOptions.builder()
        .setBaseOptions(BaseOptions.builder().useGpu().build())
        .setMaxResults(1)
        .build();
ObjectDetector objectDetector =
    ObjectDetector.createFromFileAndOptions(
        context, modelFile, options);

// Run inference
List<Detection> results = objectDetector.detect(image);

ज़्यादा जानकारी के लिए, सोर्स कोड और javadoc ObjectDetector को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

iOS में अनुमान चलाएं

पहला चरण: डिपेंडेंसी इंस्टॉल करना

टास्क लाइब्रेरी में, CocoaPods का इस्तेमाल करके इंस्टॉल किए जा सकते हैं. पक्का करें कि CocoaPods आपके सिस्टम पर इंस्टॉल हो. कृपया CocoaPods को इंस्टॉल करने की गाइड देखें.

कृपया इनके लिए CocoaPods की गाइड Xcode प्रोजेक्ट में पॉड जोड़ने के बारे में जानकारी.

Podfile में TensorFlowLiteTaskVision पॉड जोड़ें.

target 'MyAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskVision'
end

पक्का करें कि अनुमान के लिए, जिस .tflite मॉडल का इस्तेमाल करना है वह इसमें मौजूद हो आपका ऐप्लिकेशन बंडल.

दूसरा चरण: मॉडल का इस्तेमाल करना

Swift

// Imports
import TensorFlowLiteTaskVision

// Initialization
guard let modelPath = Bundle.main.path(forResource: "ssd_mobilenet_v1",
                                            ofType: "tflite") else { return }

let options = ObjectDetectorOptions(modelPath: modelPath)

// Configure any additional options:
// options.classificationOptions.maxResults = 3

let detector = try ObjectDetector.detector(options: options)

// Convert the input image to MLImage.
// There are other sources for MLImage. For more details, please see:
// https://developers.google.com/ml-kit/reference/ios/mlimage/api/reference/Classes/GMLImage
guard let image = UIImage (named: "cats_and_dogs.jpg"), let mlImage = MLImage(image: image) else { return }

// Run inference
let detectionResult = try detector.detect(mlImage: mlImage)

Objective-C

// Imports
#import <TensorFlowLiteTaskVision/TensorFlowLiteTaskVision.h>

// Initialization
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"ssd_mobilenet_v1" ofType:@"tflite"];

TFLObjectDetectorOptions *options = [[TFLObjectDetectorOptions alloc] initWithModelPath:modelPath];

// Configure any additional options:
// options.classificationOptions.maxResults = 3;

TFLObjectDetector *detector = [TFLObjectDetector objectDetectorWithOptions:options
                                                                     error:nil];

// Convert the input image to MLImage.
UIImage *image = [UIImage imageNamed:@"dogs.jpg"];

// There are other sources for GMLImage. For more details, please see:
// https://developers.google.com/ml-kit/reference/ios/mlimage/api/reference/Classes/GMLImage
GMLImage *gmlImage = [[GMLImage alloc] initWithImage:image];

// Run inference
TFLDetectionResult *detectionResult = [detector detectWithGMLImage:gmlImage error:nil];

ज़्यादा जानकारी के लिए, सोर्स कोड TFLObjectDetector को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

Python में इन्फ़रेंस चलाना

पहला चरण: पीआईपी पैकेज इंस्टॉल करना

pip install tflite-support

दूसरा चरण: मॉडल का इस्तेमाल करना

# Imports
from tflite_support.task import vision
from tflite_support.task import core
from tflite_support.task import processor

# Initialization
base_options = core.BaseOptions(file_name=model_path)
detection_options = processor.DetectionOptions(max_results=2)
options = vision.ObjectDetectorOptions(base_options=base_options, detection_options=detection_options)
detector = vision.ObjectDetector.create_from_options(options)

# Alternatively, you can create an object detector in the following manner:
# detector = vision.ObjectDetector.create_from_file(model_path)

# Run inference
image = vision.TensorImage.create_from_file(image_path)
detection_result = detector.detect(image)

ज़्यादा जानकारी के लिए, सोर्स कोड ObjectDetector को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

C++ में अनुमान चलाएं

// Initialization
ObjectDetectorOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<ObjectDetector> object_detector = ObjectDetector::CreateFromOptions(options).value();

// Create input frame_buffer from your inputs, `image_data` and `image_dimension`.
// See more information here: tensorflow_lite_support/cc/task/vision/utils/frame_buffer_common_utils.h
std::unique_ptr<FrameBuffer> frame_buffer = CreateFromRgbRawBuffer(
      image_data, image_dimension);

// Run inference
const DetectionResult result = object_detector->Detect(*frame_buffer).value();

ज़्यादा जानकारी के लिए, सोर्स कोड ObjectDetector को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

परिणामों के उदाहरण

यहां के डिटेक्शन के नतीजों का एक उदाहरण दिया गया है एसएसडी मोबाइलनेट वर्शन 1 TensorFlow हब से लिया गया है.

कुत्ते

Results:
 Detection #0 (red):
  Box: (x: 355, y: 133, w: 190, h: 206)
  Top-1 class:
   index       : 17
   score       : 0.73828
   class name  : dog
 Detection #1 (green):
  Box: (x: 103, y: 15, w: 138, h: 369)
  Top-1 class:
   index       : 17
   score       : 0.73047
   class name  : dog

इनपुट इमेज पर बाउंडिंग बॉक्स दिखाएं:

डिटेक्शन आउटपुट

आसान तरीके आज़माएं ObjectDetector के लिए सीएलआई डेमो टूल की मदद से कैसे डिज़ाइन किया गया है.

मॉडल के साथ काम करने से जुड़ी ज़रूरी शर्तें

ObjectDetector API के लिए, TFLite मॉडल ज़रूरी है. TFLite मॉडल का मेटाडेटा. कॉन्टेंट बनाने के उदाहरण देखें इसका इस्तेमाल करके, ऑब्जेक्ट डिटेक्टर के लिए मेटाडेटा TensorFlow Lite Metadata Writer API.

साथ काम करने वाले ऑब्जेक्ट डिटेक्टर मॉडल को इन ज़रूरी शर्तों को पूरा करना होगा:

  • इनपुट इमेज टेंसर: (kTfLiteUInt8/kTfLiteFloat32)

    • [batch x height x width x channels] साइज़ का इमेज इनपुट.
    • बैच का अनुमान काम नहीं करता (batch को 1 होना ज़रूरी है).
    • सिर्फ़ आरजीबी इनपुट इस्तेमाल किए जा सकते हैं (channels का नंबर 3 होना ज़रूरी है).
    • अगर टाइप kTfLiteFloat32 है, तो NormalizationOptions को इनपुट नॉर्मलाइज़ेशन के लिए मेटाडेटा से जोड़ा गया है.
  • आउटपुट टेंसर, DetectionPostProcess op के चार आउटपुट होने चाहिए, जैसे:

    • लोकेशन टेंसर (kTfLiteFloat32)
      • [1 x num_results x 4] साइज़ का टेंसर, जो अंदरूनी अरे दिखा रहा है [ऊपर, बाएं, दाएं, नीचे] फ़ॉर्म में बाउंडिंग बॉक्स.
      • BoundingBoxProperties को मेटाडेटा के साथ अटैच करना ज़रूरी है और type=BOUNDARIES और `coordinate_type=RATIO को तय करें.
    • क्लास टेंसर (kTfLiteFloat32)

      • टेंसर आकार [1 x num_results], हर एक मान जो किसी क्लास का पूर्णांक इंडेक्स.
      • वैकल्पिक (लेकिन सुझाया गया) लेबल मैप को TENSOR_VALUE_LABEL टाइप वाली सहयोगी फ़ाइल, जिसमें एक लेबल है क्लिक करें. ज़्यादा जानकारी के लिए, उदाहरण के तौर पर लेबल फ़ाइल. ऐसी पहली AssociatedFile (अगर कोई है) का इस्तेमाल नतीजों के class_name फ़ील्ड. display_name फ़ील्ड यह है को AssociatedFile (अगर कोई हो) से भरा जाता है, जिसका स्थान ObjectDetectorOptions में से display_names_locale फ़ील्ड का इस्तेमाल किया गया बनाने का समय (डिफ़ॉल्ट रूप से "hi"), जैसे कि अंग्रेज़ी. अगर इनमें से कोई भी उपलब्ध है, तो नतीजों का सिर्फ़ index फ़ील्ड भरा जाएगा.
    • स्कोर टेंसर (kTfLiteFloat32)

      • टेंसर आकार [1 x num_results], हर एक मान जो पता लगाए गए ऑब्जेक्ट का स्कोर.
    • डिटेक्शन टेंसर की संख्या (kTfLiteFloat32)

      • [1] आकार के टेंसर के रूप में पूर्णांक num_results.