ตัวตรวจจับวัตถุสามารถระบุชุดของวัตถุที่รู้จักที่อาจมีอยู่และให้ข้อมูลเกี่ยวกับตำแหน่งภายในรูปภาพที่กำหนดหรือสตรีมวิดีโอ เครื่องตรวจจับวัตถุได้รับการฝึกฝนเพื่อตรวจจับการมีอยู่และตำแหน่งของวัตถุหลายชั้น ตัวอย่างเช่น แบบจำลองอาจได้รับการฝึกอบรมด้วยรูปภาพที่มีชิ้นผลไม้ต่างๆ พร้อมด้วย ฉลาก ที่ระบุประเภทของผลไม้ที่เป็นตัวแทน (เช่น แอปเปิ้ล กล้วย หรือสตรอเบอรี่) และข้อมูลที่ระบุว่าแต่ละวัตถุปรากฏอยู่ที่ใด รูปภาพ. ดูการ แนะนำการตรวจจับวัตถุ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเครื่องตรวจจับวัตถุ
ใช้ Task Library ObjectDetector
API เพื่อปรับใช้ตัวตรวจจับอ็อบเจ็กต์แบบกำหนดเองหรือแบบฝึกล่วงหน้าในแอปมือถือของคุณ
คุณสมบัติหลักของ ObjectDetector API
ป้อนข้อมูลการประมวลผลภาพ รวมทั้งการหมุน การปรับขนาด และการแปลงพื้นที่สี
ป้ายกำกับสถานที่แผนที่
เกณฑ์คะแนนเพื่อกรองผลลัพธ์
ผลการตรวจจับ Top-k
ติดป้ายกำกับรายการที่อนุญาตและรายการที่ไม่อนุญาต
รุ่นเครื่องตรวจจับวัตถุที่รองรับ
รับประกันว่ารุ่นต่อไปนี้จะเข้ากันได้กับ ObjectDetector
API
โมเดลที่สร้างโดย AutoML Vision Edge Object Detection
โมเดลที่สร้างโดย TensorFlow Lite Model Maker สำหรับเครื่องตรวจจับวัตถุ
โมเดลแบบกำหนดเองที่ตรงตาม ข้อกำหนดความเข้ากันได้ ของโมเดล
เรียกใช้การอนุมานใน Java
ดู แอปอ้างอิงการตรวจจับวัตถุ สำหรับตัวอย่างวิธีใช้ ObjectDetector
ในแอป Android
ขั้นตอนที่ 1: นำเข้าการพึ่งพา Gradle และการตั้งค่าอื่นๆ
คัดลอกไฟล์โมเดล .tflite
ไปยังไดเร็กทอรีทรัพย์สินของโมดูล 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 (NNAPI is included)
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: ติดตั้งการพึ่งพา
ไลบรารีงานรองรับการติดตั้งโดยใช้ 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)
วัตถุประสงค์ 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
โมเดลเครื่องตรวจจับวัตถุที่เข้ากันได้ควรเป็นไปตามข้อกำหนดต่อไปนี้:
อินพุตภาพเทนเซอร์: (kTfLiteUInt8/kTfLiteFloat32)
- อินพุตรูปภาพขนาด
[batch x height x width x channels]
- ไม่รองรับการอนุมานแบบแบตช์ (
batch
ตช์ต้องเป็น 1) - รองรับเฉพาะอินพุต RGB เท่านั้น (
channels
ต้องเป็น 3) - หากประเภทเป็น kTfLiteFloat32 จำเป็นต้องแนบ NormalizationOptions กับข้อมูลเมตาเพื่อทำให้อินพุตเป็นมาตรฐาน
- อินพุตรูปภาพขนาด
เทนเซอร์เอาต์พุตต้องเป็น 4 เอาต์พุตของ op
DetectionPostProcess
เช่น:- ตำแหน่งเทนเซอร์ (kTfLiteFloat32)
- เทนเซอร์ของขนาด
[1 x num_results x 4]
อาร์เรย์ภายในแทนกล่องขอบเขตในรูปแบบ [บน ซ้าย ขวา ล่าง] - ต้องแนบ BoundingBoxProperties กับข้อมูลเมตา และต้องระบุ
type=BOUNDARIES
และ `coordinate_type=RATIO
- เทนเซอร์ของขนาด
คลาสเทนเซอร์ (kTfLiteFloat32)
- ขนาดเมตริกซ์
[1 x num_results]
แต่ละค่าแสดงถึงดัชนีจำนวนเต็มของคลาส - แผนที่ป้ายกำกับที่ไม่บังคับ (แต่แนะนำ) สามารถแนบเป็น AssociatedFile-s ที่มีประเภท TENSOR_VALUE_LABELS ซึ่งมีหนึ่งป้ายกำกับต่อบรรทัด ดู ตัวอย่างไฟล์ป้ายกำกับ AssociatedFile แรกดังกล่าว (ถ้ามี) ใช้เพื่อเติมฟิลด์
class_name
ของผลลัพธ์ ฟิลด์display_name
ถูกกรอกจาก AssociatedFile (ถ้ามี) ซึ่ง locale ตรงกับฟิลด์display_names_locale
ของObjectDetectorOptions
ที่ใช้ในเวลาที่สร้าง ("en" โดยค่าเริ่มต้น เช่น ภาษาอังกฤษ) หากไม่มีสิ่งเหล่านี้ ระบบจะเติมเฉพาะฟิลด์index
ของผลลัพธ์เท่านั้น
- ขนาดเมตริกซ์
คะแนนเทนเซอร์ (kTfLiteFloat32)
- ขนาดเมตริกซ์
[1 x num_results]
แต่ละค่าแสดงถึงคะแนนของวัตถุที่ตรวจพบ
- ขนาดเมตริกซ์
จำนวนเทนเซอร์การตรวจจับ (kTfLiteFloat32)
- จำนวนเต็ม num_results เป็นเทนเซอร์ของขนาด
[1]
- จำนวนเต็ม num_results เป็นเทนเซอร์ของขนาด
- ตำแหน่งเทนเซอร์ (kTfLiteFloat32)