ตรวจจับท่าทางด้วย 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', '3.2.0'
    
    # If you want to use the accurate implementation:
    pod 'GoogleMLKit/PoseDetectionAccurate', '3.2.0'
    
  2. หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้ xcworkspace ทั้งนี้ ML Kit ได้รับการรองรับใน Xcode เวอร์ชัน 13.2.1 ขึ้นไป

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

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