Mediapipe วิเคราะห์ข้อมูลจากภาพและวิดีโอแบบเรียลไทม์ แถมใช้งานบนเว็บได้ด้วย

Mediapipe เป็นไลบรารีของ Google ที่ช่วยในการทำงานเกี่ยวกับการรู้จำและวิเคราะห์ข้อมูลจากภาพและวิดีโอแบบเรียลไทม์ โดยมีความสามารถหลากหลาย เช่น การติดตามใบหน้า การรู้จำท่าทางมือ และการตรวจจับวัตถุ เป็นต้น

การใช้งานเบื้องต้น

  1. ฟีเจอร์อื่น ๆ:
    • Pose: การติดตามท่าทางของร่างกาย
    • Hand Tracking: การติดตามมือและการวิเคราะห์การเคลื่อนไหว
    • Objectron: การตรวจจับวัตถุสามมิติ

ใช้งานการติดตามใบหน้า: นี่คือตัวอย่างการใช้ Mediapipe สำหรับติดตามใบหน้าในวิดีโอ:

import cv2
import mediapipe as mp

# Initialize MediaPipe Face Detection
mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.2)
mp_drawing = mp.solutions.drawing_utils

# Start capturing video
cap = cv2.VideoCapture(0)

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        continue

    # Convert the BGR image to RGB
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = face_detection.process(rgb_frame)

    # Draw face detections
    if results.detections:
        for detection in results.detections:
            mp_drawing.draw_detection(frame, detection)

    # Display the resulting frame
    cv2.imshow('Face Detection', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

ติดตั้ง Mediapipe: ใช้คำสั่ง pip ติดตั้ง:

pip install mediapipe

Mediapipe มีเอกสารและตัวอย่างที่ดีสำหรับการเริ่มต้นใช้งาน สามารถศึกษาเพิ่มเติมได้ที่ Mediapipe Documentation.

ตัวอย่างการใช้งาน Mediapipe ด้วย JavaScript สามารถทำได้โดยใช้ WebAssembly (WASM) และ TensorFlow.js เพื่อให้ทำงานในเบราว์เซอร์ได้ดีขึ้น นี่คือตัวอย่างการติดตามใบหน้าด้วย Mediapipe ใน JavaScript:

ขั้นตอนการใช้งาน

เพิ่ม Mediapipe และ TensorFlow.js ลงในโปรเจ็กต์: ใช้ CDN เพื่อโหลด Mediapipe และ TensorFlow.js ใน HTML ของคุณ:

<!DOCTYPE html>
<html>
<head>
  <title>Mediapipe Face Detection</title>
  <script src="https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]"></script>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
</head>
<body>
  <video id="video" width="640" height="480" autoplay></video>
  <script>
    const video = document.getElementById('video');

    // Set up video capture
    async function setupCamera() {
      const stream = await navigator.mediaDevices.getUserMedia({
        video: true
      });
      video.srcObject = stream;
      return new Promise((resolve) => {
        video.onloadedmetadata = () => {
          resolve();
        };
      });
    }

    // Load Mediapipe Face Detection
    async function detectFace() {
      const faceDetection = new FaceDetection.FaceDetection({
        locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/${file}`
      });

      faceDetection.setOptions({
        modelSelection: 1,
        minDetectionConfidence: 0.2
      });

      faceDetection.onResults((results) => {
        if (results.detections.length > 0) {
          console.log(results.detections);
        }
      });

      const camera = new Camera(video, {
        onFrame: async () => {
          await faceDetection.send({ image: video });
        },
        width: 640,
        height: 480
      });
      camera.start();
    }

    async function main() {
      await setupCamera();
      await detectFace();
    }

    main();
  </script>
</body>
</html>

อธิบายโค้ด:

  1. โหลด Mediapipe และ TensorFlow.js: ใช้ CDN ในการโหลด Mediapipe และ TensorFlow.js เพื่อง่ายต่อการเริ่มต้นใช้งาน
  2. setupCamera: ฟังก์ชันนี้จะใช้ navigator.mediaDevices.getUserMedia เพื่อเปิดกล้องและแสดงภาพบนแท็ก <video>
  3. detectFace: ฟังก์ชันนี้จะตั้งค่า Face Detection และเริ่มต้นการตรวจจับใบหน้าโดยส่งภาพจากกล้องไปยัง Mediapipe
  4. main: ฟังก์ชันนี้จะเรียกใช้งานกล้องและเริ่มต้นการตรวจจับใบหน้า

Read more

Google ส่ง Gemini ลง Mac แบบ Native พร้อมตัวช่วย AI สุดล้ำ ยกระดับงานเดสก์ท็อป

Google ส่ง Gemini ลง Mac แบบ Native พร้อมตัวช่วย AI สุดล้ำ ยกระดับงานเดสก์ท็อป

Google เปิดตัว Gemini เวอร์ชัน Native บน Mac พร้อมฟีเจอร์ AI ล้ำสมัย ช่วยเพิ่มประสิทธิภาพการทำงาน ปลดล็อกความคิดสร้างสรรค์ และเชื่อมต่อข้อมูลส่วนตัวได้อย่างชาญฉลาด

By ทีมงาน devdog
เจาะลึก UEFA Champions League: สุดยอดความตื่นเต้นที่แฟนบอลทั่วโลกรอคอย

เจาะลึก UEFA Champions League: สุดยอดความตื่นเต้นที่แฟนบอลทั่วโลกรอคอย

เจาะลึก UEFA Champions League การแข่งขันระดับโลกที่แฟนบอลรอคอย พร้อมติดตามข่าวสารรอบโลกและการถ่ายทอดสดสุดพิเศษ ไม่พลาดทุกความมันส์!

By ทีมงาน devdog
Google อัปเกรด Chrome ครั้งใหญ่ เพิ่มฟีเจอร์ "Skills" ให้ AI จำคำสั่งโปรดของคุณ

Google อัปเกรด Chrome ครั้งใหญ่ เพิ่มฟีเจอร์ "Skills" ให้ AI จำคำสั่งโปรดของคุณ

อัปเกรด Chrome ด้วยฟีเจอร์ Skills ใหม่ ให้ AI จดจำและเรียกใช้คำสั่งโปรดของคุณได้ทันที ไม่ต้องพิมพ์ซ้ำ พร้อมเชื่อมต่อ Gemini ทั่วระบบ

By ทีมงาน devdog
CARTIER Santos-Dumont โฉมใหม่: เมื่อออบซิเดียนผสานตำนานนักบิน สู่ความงามเหนือกาลเวลา

CARTIER Santos-Dumont โฉมใหม่: เมื่อออบซิเดียนผสานตำนานนักบิน สู่ความงามเหนือกาลเวลา

คาร์เทียร์เปิดตัว Santos-Dumont หน้าปัดออบซิเดียน หินภูเขาไฟธรรมชาติผสานดีไซน์นักบินระดับตำนาน สะท้อนงานฝีมือร่วมสมัยและความหรูหรา

By ทีมงาน devdog