ผสานรวมตัวตรวจจับวัตถุ

ตัวตรวจจับวัตถุสามารถระบุได้ว่าวัตถุกลุ่มใดที่รู้จักอาจมีอยู่ และให้ข้อมูลเกี่ยวกับตำแหน่งของผู้ใช้ภายในรูปภาพหรือวิดีโอที่กำหนด สตรีม เครื่องมือตรวจจับวัตถุได้รับการฝึกให้ตรวจจับสถานที่ตั้งและตำแหน่งของ คลาสของวัตถุที่หลากหลาย ตัวอย่างเช่น โมเดลอาจได้รับการฝึกด้วยรูปภาพ ที่มีผลไม้หลายชิ้น พร้อมด้วยป้ายกำกับที่ระบุ ชนิดของผลไม้ที่เป็นตัวแทนของหมวดหมู่ (เช่น แอปเปิ้ล กล้วย หรือสตรอเบอร์รี่) และ ข้อมูลที่ระบุตำแหน่งที่แต่ละออบเจ็กต์ปรากฏในรูปภาพ โปรดดู ตัวอย่างการตรวจจับวัตถุ เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับตัวตรวจจับออบเจ็กต์

ใช้ Task Library ObjectDetector API เพื่อทำให้ตัวตรวจจับออบเจ็กต์ที่กำหนดเองใช้งานได้ หรือที่ฝึกไว้แล้วล่วงหน้า ลงในแอปบนอุปกรณ์เคลื่อนที่ของคุณ

ฟีเจอร์หลักของ ObjectDetector API

  • การประมวลผลรูปภาพที่ป้อน รวมถึงการหมุน การปรับขนาด และพื้นที่สี Conversion

  • ติดป้ายกำกับภาษาบนแผนที่

  • เกณฑ์คะแนนเพื่อกรองผลลัพธ์

  • ผลการตรวจจับ Top-k

  • ติดป้ายกำกับรายการที่อนุญาตและรายการที่ปฏิเสธ

โมเดลตัวตรวจจับวัตถุที่รองรับ

รุ่นต่อไปนี้ได้รับการรับประกันว่าใช้งานร่วมกับ ObjectDetector ได้ API

เรียกใช้การอนุมานใน Java

โปรดดู แอปอ้างอิงการตรวจจับออบเจ็กต์ สำหรับตัวอย่างวิธีใช้ ObjectDetector ในแอป Android

ขั้นตอนที่ 1: นำเข้าการอ้างอิง Gradle และการตั้งค่าอื่นๆ

คัดลอกไฟล์โมเดล .tflite ไปยังไดเรกทอรี Asset ของโมดูล Android ตำแหน่งที่โมเดลจะทำงาน ระบุว่าไม่ควรบีบอัดไฟล์ และ เพิ่มไลบรารี TensorFlow Lite ลงในไฟล์ build.gradle ของโมดูลด้วยคำสั่งต่อไปนี้

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'
}

ขั้นตอนที่ 2: การใช้โมเดล

// 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

ขั้นตอนที่ 1: ติดตั้งทรัพยากร Dependency

ไลบรารีงานรองรับการติดตั้งโดยใช้ CocoaPods ตรวจสอบว่า CocoaPods ติดตั้งอยู่ในระบบของคุณ โปรดดู คู่มือการติดตั้ง CocoaPods สำหรับคำแนะนำ

โปรดดู คู่มือ CocoaPods สำหรับ รายละเอียดการเพิ่มพ็อดลงในโปรเจ็กต์ Xcode

เพิ่มพ็อด TensorFlowLiteTaskVision ใน Podfile

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

ตรวจสอบว่าโมเดล .tflite ที่คุณจะใช้สำหรับการอนุมานมีอยู่ใน App Bundle ของคุณ

ขั้นตอนที่ 2: การใช้โมเดล

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

ขั้นตอนที่ 1: ติดตั้งแพ็กเกจ PIP

pip install tflite-support

ขั้นตอนที่ 2: การใช้โมเดล

# 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

ตัวอย่างผลการแข่ง

ต่อไปนี้เป็นตัวอย่างผลการตรวจหาของ ssd mobilenet v1 จาก TensorFlow Hub

สุนัข

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

แสดงผลกรอบล้อมรอบบนอิมเมจอินพุต:

เอาต์พุตจากการตรวจจับ

ลองใช้ เครื่องมือสาธิต CLI สำหรับ ObjectDetector กับโมเดลและข้อมูลทดสอบของคุณเอง

ข้อกำหนดความเข้ากันได้กับรุ่น

ObjectDetector API คาดว่าจะมีโมเดล TFLite ที่มีการบังคับ ข้อมูลเมตาของโมเดล TFLite ดูตัวอย่างการสร้าง ข้อมูลเมตาสำหรับตัวตรวจจับออบเจ็กต์ที่ใช้ TensorFlow Lite Metadata Writer API

โมเดลตัวตรวจจับวัตถุที่เข้ากันได้ควรเป็นไปตามข้อกำหนดต่อไปนี้

  • Tensor รูปภาพอินพุต: (kTfLiteUInt8/kTfLiteFloat32)

    • อินพุตภาพขนาด [batch x height x width x channels]
    • ไม่รองรับการอนุมานแบบกลุ่ม (batch ต้องเป็น 1)
    • รองรับอินพุต RGB เท่านั้น (channels ต้องเป็น 3)
    • หากประเภทคือ kTfLiteFloat32 จะต้องระบุ BottomizationOptions ซึ่งแนบมากับข้อมูลเมตา เพื่อการปรับอินพุตให้เป็นมาตรฐาน
  • Tensor เอาต์พุตต้องเป็นเอาต์พุต 4 รายการของการดำเนินการ DetectionPostProcess ดังนี้

    • tensor ตำแหน่ง (kTfLiteFloat32)
      • Tensor ขนาด [1 x num_results x 4] อาร์เรย์ภายในแสดง กรอบล้อมรอบในรูปแบบ [บน ซ้าย ขวา ล่าง]
      • ต้องแนบ BoundingBoxProperties เพื่อแนบกับข้อมูลเมตา และต้องระบุ type=BOUNDARIES และ `coordinate_type=RATIO
    • Tensor คลาส (kTfLiteFloat32)

      • tensor ของขนาด [1 x num_results] แต่ละค่าแสดงถึง ดัชนีจำนวนเต็มของคลาส
      • แนบแมปป้ายกำกับที่ไม่บังคับ (แต่แนะนำ) เป็น AssociatedFile-s ที่เป็นประเภท TENSOR_VALUE_LABELS ซึ่งมีป้ายกำกับเดียว ต่อบรรทัด โปรดดู example label file AssociatedFile แรก (หากมี) ใช้ในการกรอก class_name ของผลลัพธ์ ช่อง display_name คือ ที่กรอกจาก AssociatedFile (หากมี) ซึ่งมีภาษาตรงกับ ฟิลด์ display_names_locale ของ ObjectDetectorOptions ใช้ที่ เวลาที่สร้าง ("en" โดยค่าเริ่มต้น เช่น ภาษาอังกฤษ) ถ้าไม่มีลิงก์ที่กล่าวมา พร้อมใช้งาน ระบบจะเติมเฉพาะฟิลด์ index ของผลลัพธ์
    • tensor คะแนน (kTfLiteFloat32)

      • tensor ของขนาด [1 x num_results] แต่ละค่าแสดงถึง ของออบเจ็กต์ที่ตรวจพบ
    • จำนวน tensor ในการตรวจจับ (kTfLiteFloat32)

      • จำนวนเต็ม num_results เป็น tensor ของขนาด [1]