גלאי אובייקטים יכולים לזהות איזה מתוך קבוצה ידועה של אובייקטים עשוי להיות נוכח ולספק מידע על מיקומם בתמונה הנתונה או בזרם וידאו. גלאי עצמים מאומן לזהות נוכחות ומיקומם של מחלקות מרובות של עצמים. לדוגמה, מודל עשוי להיות מאומן עם תמונות המכילות חתיכות פרי שונות, יחד עם תווית המציינת את סוג הפרי שהם מייצגים (למשל תפוח, בננה או תות), ונתונים המציינים היכן מופיע כל אובייקט. התמונה. עיין במבוא של זיהוי אובייקטים למידע נוסף על גלאי אובייקטים.
השתמש ב-Task Library ObjectDetector
API כדי לפרוס את גלאי האובייקטים המותאמים אישית שלך או אלה שהוכשרו מראש באפליקציות לנייד שלך.
תכונות מפתח של ה-ObjectDetector API
עיבוד תמונה של קלט, כולל סיבוב, שינוי גודל והמרת מרחב צבע.
תווית אזור מפה.
ציון סף לסינון תוצאות.
תוצאות זיהוי מובילות.
תווית רשימת היתרים ורשימת דחייה.
דגמי גלאי אובייקטים נתמכים
הדגמים הבאים מובטחים להיות תואמים ל- ObjectDetector
API.
מודלים שנוצרו על ידי AutoML Vision Edge Object Detection .
מודלים שנוצרו על ידי TensorFlow Lite Model Maker עבור גלאי אובייקטים .
דגמים מותאמים אישית העומדים בדרישות תאימות המודל .
הפעל הסקה ב-Java
עיין באפליקציית ההפניה לזיהוי אובייקטים לקבלת דוגמה לשימוש ObjectDetector
באפליקציית Android.
שלב 1: ייבא תלות Gradle והגדרות אחרות
העתק את קובץ מודל .tflite
לספריית הנכסים של מודול האנדרואיד שבו המודל יופעל. ציין שאין לדחוס את הקובץ, והוסף את ספריית 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
שבו תשתמש להסקת מסקנות קיים בחבילת האפליקציות שלך.
שלב 2: שימוש במודל
מָהִיר
// 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)
מטרה ג
// 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 עם מודל ונתוני בדיקה משלך.
דרישות תאימות לדגם
ממשק ה-API ObjectDetector
מצפה למודל TFLite עם מטא נתונים חובה של מודל TFLite . ראה דוגמאות ליצירת מטא נתונים עבור גלאי אובייקטים באמצעות TensorFlow Lite Metadata Writer API .
דגמי גלאי האובייקטים התואמים צריכים לעמוד בדרישות הבאות:
טנזור תמונה קלט: (kTfLiteUInt8/kTfLiteFloat32)
- קלט תמונה בגודל
[batch x height x width x channels]
. - הסקת אצווה אינה נתמכת (
batch
נדרשת להיות 1). - רק כניסות RGB נתמכות (
channels
נדרשים להיות 3). - אם הסוג הוא kTfLiteFloat32, יש צורך לצרף את אפשרויות הנורמליזציה למטא נתונים לצורך נורמליזציה של קלט.
- קלט תמונה בגודל
טנסור פלט חייב להיות 4 הפלטים של
DetectionPostProcess
op, כלומר:- טנסור מיקומים (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 (אם יש) שהמקום שלו תואם לשדהdisplay_names_locale
שלObjectDetectorOptions
ששימשו בזמן היצירה ("en" כברירת מחדל, כלומר אנגלית). אם אף אחד מאלה אינו זמין, רק שדהindex
של התוצאות יתמלא.
- טנסור בגודל
טנזור ציונים (kTfLiteFloat32)
- טנסור בגודל
[1 x num_results]
, כל ערך מייצג את הניקוד של האובייקט שזוהה.
- טנסור בגודל
מספר טנזור הזיהוי (kTfLiteFloat32)
- מספר שלם num_results כטנזור בגודל
[1]
.
- מספר שלם num_results כטנזור בגודל
- טנסור מיקומים (kTfLiteFloat32)