手部探测器不仅仅是一个可以检测和跟踪手部的普通工具,它的意义远不止于此。你可以在从机器人到娱乐领域的各个领域构建令人惊叹的项目。
话不多说,让我们开始编码吧!👨💻
import cv2
import mediapipe as mp
这将导入OpenCV和Mediapipe 库
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
这将初始化 Mediapipe 绘图实用程序和用于检测手部标志的 Hands 模块。
cap = cv2.VideoCapture(0)
这将初始化 Mediapipe 绘图实用程序和用于检测手部标志的 Hands 模块。
if not cap.isOpened():
print("Unable to open camera")
exit()
这将检查相机是否成功打开。如果无法打开相机,则会打印一条错误消息并退出程序。
with mp_hands.Hands(static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5) as hands:
这将初始化 Mediapipe Hands 对象。它设置static_image_mode
为False
,启用实时视频处理。max_num_hands
设置要检测的最大手数,并设置min_detection_confidence
检测的最小置信度阈值。
while True:
ret, frame = cap.read()
if not ret:
print("Error reading frame from camera")
break
这将启动无限循环以连续读取视频捕获中的帧。它检查是否成功读取帧&#x