施勢帆老師的教學網站

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 9|回復: 0

Face detect in Colab

[複製鏈接]

706

主題

717

帖子

3408

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
3408
發表於 前天 16:16 | 顯示全部樓層 |閱讀模式
import numpy as np
import cv2
import mediapipe as mp  # Import the mediapipe module
from mediapipe.tasks.python import vision
from mediapipe.tasks import python
from google.colab.patches import cv2_imshow  # Import cv2_imshow for Colab

# Define image path
IMAGE_FILE = 'image.jpg'

# Define visualize function
def visualize(image, detection_result):
    for detection in detection_result.detections:
        bbox = detection.bounding_box
        start_point = (int(bbox.origin_x), int(bbox.origin_y))
        end_point = (int(bbox.origin_x + bbox.width), int(bbox.origin_y + bbox.height))
        cv2.rectangle(image, start_point, end_point, (0, 255, 0), 2)
    return image

# Load model and image
base_options = python.BaseOptions(model_asset_path='detector.tflite')
options = vision.FaceDetectorOptions(base_options=base_options)
detector = vision.FaceDetector.create_from_options(options)
image = mp.Image.create_from_file(IMAGE_FILE)

# Run detection
detection_result = detector.detect(image)

# Visualize result
image_copy = np.copy(image.numpy_view())
annotated_image = visualize(image_copy, detection_result)
rgb_annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)

# Use cv2_imshow for Colab
cv2_imshow(rgb_annotated_image)


回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|施勢帆老師的教學網站

GMT+8, 2025-4-19 16:36 , Processed in 0.055822 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表