ML Kit มี SDK ที่เพิ่มประสิทธิภาพ 2 รายการสำหรับการตรวจจับท่าทาง
ชื่อ SDK | PoseDetection | PoseDetectionAccurate |
---|---|---|
การใช้งาน | เนื้อหาสำหรับตัวตรวจจับพื้นฐานจะลิงก์แบบคงที่กับแอปในเวลาบิลด์ | ชิ้นงานสำหรับตัวตรวจจับที่แม่นยำจะลิงก์อยู่กับแอปของคุณแบบตายตัว ณ เวลาบิลด์ |
ขนาดแอป | ไม่เกิน 29.6 MB | ไม่เกิน 33.2 MB |
การแสดง | iPhone X: ประมาณ 45 FPS | iPhone X: ~29FPS |
ลองเลย
- ลองใช้แอปตัวอย่างเพื่อดูตัวอย่างการใช้งาน API นี้
ก่อนเริ่มต้น
รวมพ็อด ML Kit ต่อไปนี้ใน Podfile
# If you want to use the base implementation: pod 'GoogleMLKit/PoseDetection', '3.2.0' # If you want to use the accurate implementation: pod 'GoogleMLKit/PoseDetectionAccurate', '3.2.0'
หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์ ให้เปิดโปรเจ็กต์ Xcode โดยใช้
xcworkspace
ของโปรเจ็กต์ Xcode เวอร์ชัน 13.2.1 ขึ้นไปรองรับ ML Kit
1. สร้างอินสแตนซ์ของ PoseDetector
หากต้องการตรวจหาท่าทางในรูปภาพ ให้สร้างอินสแตนซ์ของ PoseDetector
และระบุการตั้งค่าตัวตรวจจับ (ไม่บังคับ)
PoseDetector
ตัวเลือก
โหมดการตรวจจับ
PoseDetector
ทำงานในโหมดการตรวจจับ 2 โหมด ตรวจดูว่าคุณเลือกเครื่องมือที่ตรงกับ
กรณีการใช้งานของคุณ
stream
(ค่าเริ่มต้น)- เครื่องมือตรวจสอบท่าทางจะตรวจหาบุคคลที่โดดเด่นที่สุดในรูปภาพก่อน แล้วจึงเรียกใช้การตรวจจับท่าทาง ในเฟรมต่อๆ ไป ระบบจะไม่ดำเนินการตามขั้นตอนการตรวจจับคน เว้นแต่บุคคลดังกล่าวจะถูกบดบังหรือตรวจไม่พบด้วยความมั่นใจสูงอีกต่อไป เครื่องมือตรวจสอบท่าทางจะพยายามติดตามบุคคลที่โดดเด่นที่สุดและส่งกลับท่าทางในการอนุมานแต่ละครั้ง ซึ่งจะช่วยลดเวลาในการตอบสนองและช่วยให้การตรวจจับราบรื่น ใช้โหมดนี้เมื่อต้องการ ตรวจจับท่าทางในสตรีมวิดีโอ
singleImage
- ตัวตรวจจับท่าทางจะตรวจหาบุคคลแล้วเรียกใช้การตรวจจับท่าทาง ขั้นตอนการตรวจจับคนจะทำงานสำหรับทุกรูปภาพ ดังนั้นเวลาในการตอบสนองจะสูงขึ้นและไม่มีการติดตามคน ใช้โหมดนี้เมื่อใช้การตรวจจับท่าทาง กับภาพนิ่งหรือเมื่อไม่ต้องการให้ติดตาม
ระบุตัวเลือกตัวตรวจจับท่าทาง
Swift
// Base pose detector with streaming, when depending on the PoseDetection SDK let options = PoseDetectorOptions() options.detectorMode = .stream // Accurate pose detector on static images, when depending on the // PoseDetectionAccurate SDK let options = AccuratePoseDetectorOptions() options.detectorMode = .singleImage
Objective-C
// Base pose detector with streaming, when depending on the PoseDetection SDK MLKPoseDetectorOptions *options = [[MLKPoseDetectorOptions alloc] init]; options.detectorMode = MLKPoseDetectorModeStream; // Accurate pose detector on static images, when depending on the // PoseDetectionAccurate SDK MLKAccuratePoseDetectorOptions *options = [[MLKAccuratePoseDetectorOptions alloc] init]; options.detectorMode = MLKPoseDetectorModeSingleImage;
ขั้นตอนสุดท้าย รับอินสแตนซ์ของ PoseDetector
ผ่านตัวเลือกที่คุณระบุ ได้แก่
Swift
let poseDetector = PoseDetector.poseDetector(options: options)
Objective-C
MLKPoseDetector *poseDetector = [MLKPoseDetector poseDetectorWithOptions:options];
2. เตรียมรูปภาพอินพุต
หากต้องการตรวจหาท่าทาง ให้ทำดังต่อไปนี้กับรูปภาพหรือเฟรมของวิดีโอแต่ละเฟรม
หากเปิดใช้โหมดสตรีม คุณต้องสร้างออบเจ็กต์ VisionImage
รายการจาก CMSampleBuffer
สร้างออบเจ็กต์ VisionImage
โดยใช้ UIImage
หรือ CMSampleBuffer
หากคุณใช้ UIImage
ให้ทำตามขั้นตอนต่อไปนี้
- สร้างออบเจ็กต์
VisionImage
ด้วยUIImage
ตรวจสอบว่าได้ระบุ.orientation
ที่ถูกต้องSwift
let image = VisionImage(image: UIImage) visionImage.orientation = image.imageOrientation
Objective-C
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
หากคุณใช้ CMSampleBuffer
ให้ทำตามขั้นตอนต่อไปนี้
-
ระบุการวางแนวของข้อมูลรูปภาพที่อยู่ใน
CMSampleBuffer
วิธีดูการวางแนวรูปภาพ
Swift
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 } }
Objective-C
- (UIImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored : UIImageOrientationRight; case UIDeviceOrientationLandscapeLeft: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored : UIImageOrientationUp; case UIDeviceOrientationPortraitUpsideDown: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored : UIImageOrientationLeft; case UIDeviceOrientationLandscapeRight: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored : UIImageOrientationDown; case UIDeviceOrientationUnknown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: return UIImageOrientationUp; } }
- สร้างออบเจ็กต์
VisionImage
โดยใช้ออบเจ็กต์CMSampleBuffer
และการวางแนวดังนี้Swift
let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
Objective-C
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
3. ประมวลผลรูปภาพ
ส่ง VisionImage
ไปยังวิธีประมวลผลรูปภาพของตัวตรวจจับท่าทาง คุณจะใช้เมธอด process(image:)
แบบไม่พร้อมกันหรือเมธอด results()
แบบซิงโครนัสก็ได้
หากต้องการตรวจหาวัตถุแบบพร้อมกัน ให้ทำดังนี้
Swift
var results: [Pose] do { results = try poseDetector.results(in: image) } catch let error { print("Failed to detect pose with error: \(error.localizedDescription).") return } guard let detectedPoses = results, !detectedPoses.isEmpty else { print("Pose detector returned no results.") return } // Success. Get pose landmarks here.
Objective-C
NSError *error; NSArray*poses = [poseDetector resultsInImage:image error:&error]; if (error != nil) { // Error. return; } if (poses.count == 0) { // No pose detected. return; } // Success. Get pose landmarks here.
หากต้องการตรวจหาออบเจ็กต์แบบไม่พร้อมกัน ให้ทำดังนี้
Swift
poseDetector.process(image) { detectedPoses, error in guard error == nil else { // Error. return } guard !detectedPoses.isEmpty else { // No pose detected. return } // Success. Get pose landmarks here. }
Objective-C
[poseDetector processImage:image completion:^(NSArray* _Nullable poses, NSError * _Nullable error) { if (error != nil) { // Error. return; } if (poses.count == 0) { // No pose detected. return; } // Success. Get pose landmarks here. }];
4. รับข้อมูลเกี่ยวกับท่าทางที่ตรวจพบ
หากระบบตรวจพบบุคคลในรูปภาพ API การตรวจจับท่าทางจะส่งอาร์เรย์ของออบเจ็กต์ Pose
ไปยังตัวแฮนเดิลการทำงานที่สมบูรณ์หรือแสดงผลอาร์เรย์ โดยขึ้นอยู่กับว่าคุณเรียกเมธอดแบบไม่พร้อมกันหรือแบบซิงโครนัส
หากบุคคลนั้นไม่ได้อยู่ในรูปภาพโดยสมบูรณ์ โมเดลจะกำหนดพิกัดของจุดสังเกตที่ขาดหายไปนอกเฟรมและให้ค่า InFrameConfidence ที่ต่ำ
ถ้าตรวจไม่พบบุคคล อาร์เรย์ว่างเปล่า
Swift
for pose in detectedPoses { let leftAnkleLandmark = pose.landmark(ofType: .leftAnkle) if leftAnkleLandmark.inFrameLikelihood > 0.5 { let position = leftAnkleLandmark.position } }
Objective-C
for (MLKPose *pose in detectedPoses) { MLKPoseLandmark *leftAnkleLandmark = [pose landmarkOfType:MLKPoseLandmarkTypeLeftAnkle]; if (leftAnkleLandmark.inFrameLikelihood > 0.5) { MLKVision3DPoint *position = leftAnkleLandmark.position; } }
เคล็ดลับในการปรับปรุงประสิทธิภาพ
คุณภาพของผลลัพธ์ขึ้นอยู่กับคุณภาพของรูปภาพที่ป้อน ดังนี้
- หากต้องการให้ ML Kit ตรวจจับท่าทางได้อย่างถูกต้อง บุคคลในรูปภาพควรมีข้อมูลพิกเซลที่เพียงพอ บุคคลในภาพควรมีขนาดอย่างน้อย 256x256 พิกเซลเพื่อประสิทธิภาพสูงสุด
- หากคุณตรวจพบท่าทางในแอปพลิเคชันแบบเรียลไทม์ คุณอาจต้องพิจารณาขนาดโดยรวมของรูปภาพอินพุต รูปภาพที่เล็กลงจะได้รับการประมวลผลเร็วขึ้น เพื่อลดเวลาในการตอบสนอง ให้ถ่ายภาพที่ความละเอียดต่ำลง แต่อย่าลืมคำนึงถึงข้อกำหนดเกี่ยวกับความละเอียดข้างต้นและตรวจสอบว่าวัตถุใช้พื้นที่ในรูปภาพมากที่สุด
- การโฟกัสรูปภาพที่ไม่ดีอาจส่งผลต่อความแม่นยำได้เช่นกัน ถ้าคุณไม่ได้รับผลลัพธ์ที่ยอมรับได้ ให้ขอให้ผู้ใช้รูปภาพกลับคืนมา
หากคุณต้องการใช้การตรวจหาท่าทางในแอปพลิเคชันแบบเรียลไทม์ ให้ทำตามหลักเกณฑ์ต่อไปนี้เพื่อให้ได้อัตราเฟรมที่ดีที่สุด
- ใช้ PoseDetection SDK พื้นฐานและโหมดการตรวจจับ
stream
- ลองถ่ายภาพที่ความละเอียดต่ำลง อย่างไรก็ตาม โปรดคำนึงถึงข้อกำหนดขนาดรูปภาพของ API นี้ด้วย
- สำหรับการประมวลผลเฟรมวิดีโอ ให้ใช้ API แบบซิงโครนัส
results(in:)
ของตัวตรวจจับ เรียกใช้เมธอดนี้จากฟังก์ชัน captureOutput(_, didOutput:from:) ของ AVCaptureVideoDataOutputSampleBufferDelegate เพื่อให้ได้ผลลัพธ์จากเฟรมวิดีโอที่ระบุแบบซิงโครนัส คงสถานะ alwaysDiscardsLateVideoFrames ของ AVCaptureVideoDataOutput ให้เป็นจริงเพื่อเร่งการเรียกใช้ตัวตรวจจับ หากเฟรมวิดีโอใหม่พร้อมใช้งานขณะที่ตัวตรวจจับกำลังทำงาน เฟรมจะหายไป - หากใช้เอาต์พุตของตัวตรวจจับเพื่อซ้อนทับกราฟิกบนรูปภาพอินพุต ก่อนอื่นให้รับผลลัพธ์จาก ML Kit จากนั้นจึงแสดงผลรูปภาพและการวางซ้อนในขั้นตอนเดียว การทำเช่นนี้จะทำให้คุณแสดงผลบนพื้นผิวแสดงผลเพียงครั้งเดียวต่อเฟรมอินพุตที่ประมวลผลแต่ละเฟรม ดูตัวอย่างคลาส previewOverlayView และ MLKDetectionOverlayView ในแอปตัวอย่าง Showcase
ขั้นตอนถัดไป
- หากต้องการเรียนรู้วิธีใช้จุดสังเกตของท่าทางเพื่อแยกประเภทท่าทาง โปรดดูเคล็ดลับการแยกประเภทท่าทาง
- ดูตัวอย่างการเริ่มต้นใช้งาน ML Kit อย่างรวดเร็วใน GitHub สำหรับตัวอย่าง API นี้ที่ใช้งานอยู่