ตรวจจับท่าทางด้วย ML Kit บน iOS

ML Kit มี SDK 2 รายการที่ปรับให้เหมาะสมสําหรับการตรวจจับท่าทาง

ชื่อ SDKPoseDetectionPoseDetectionAccurate
การใช้งานชิ้นงานสำหรับเครื่องตรวจจับฐานจะลิงก์กับแอปแบบคงที่ ณ เวลาที่สร้างชิ้นงานสำหรับเครื่องตรวจจับที่แม่นยำจะลิงก์กับแอปแบบคงที่ ณ เวลาที่สร้าง
ขนาดแอปไม่เกิน 29.6 MBไม่เกิน 33.2 MB
ประสิทธิภาพiPhone X: ~45FPSiPhone X: ~29FPS

ลองเลย

ก่อนเริ่มต้น

  1. รวมพ็อด ML Kit ต่อไปนี้ไว้ใน Podfile

    # If you want to use the base implementation:
    pod 'GoogleMLKit/PoseDetection', '7.0.0'
    
    # If you want to use the accurate implementation:
    pod 'GoogleMLKit/PoseDetectionAccurate', '7.0.0'
    
  2. หลังจากติดตั้งหรืออัปเดต Pods ของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ 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 นี้ด้วย
  • สําหรับการประมวลผลเฟรมวิดีโอ ให้ใช้ results(in:) synchronous API ของตัวตรวจจับ เรียกใช้เมธอดนี้จากฟังก์ชัน captureOutput(_, didOutput:from:) ของ AVCaptureVideoDataOutputSampleBufferDelegate เพื่อรับผลลัพธ์จากเฟรมวิดีโอที่ระบุแบบซิงค์ ตั้งค่า alwaysDiscardsLateVideoFrames ของ AVCaptureVideoDataOutput เป็น "จริง" เสมอเพื่อควบคุมการเรียกใช้เครื่องตรวจจับ หากมีเฟรมวิดีโอใหม่พร้อมใช้งานขณะที่ตัวตรวจจับทำงานอยู่ ระบบจะทิ้งเฟรมนั้น
  • หากคุณใช้เอาต์พุตของตัวตรวจจับเพื่อวางกราฟิกซ้อนทับบนรูปภาพอินพุต ให้รับผลลัพธ์จาก ML Kit ก่อน จากนั้นจึงแสดงผลรูปภาพและวางซ้อนในขั้นตอนเดียว ซึ่งจะทำให้คุณแสดงผลไปยังพื้นผิวการแสดงผลเพียงครั้งเดียวสำหรับเฟรมอินพุตที่ประมวลผลแต่ละเฟรม ดูตัวอย่างได้จากคลาส previewOverlayView และ MLKDetectionOverlayView ในแอปตัวอย่างของ Showcase

ขั้นตอนถัดไป