本代码基于 Advance Computer Vision with Python 进行修改,更加适合中国宝宝体质
我的相关代码及数据集已经上传GitHub仓库,欢迎使用 Advance-Computer-Vision-with-Python
Basics.py
import cv2
import mediapipe as mp
import time
# 打开视频文件
cap = cv2.VideoCapture(
"E:\\Advance Computer Vision with Python\\main\\Chapter 3 Face Detection\\Videos\\3.mp4"
)
if not cap.isOpened():
print("Error: Could not open video.")
exit()
pTime = 0
# 初始化 MediaPipe 的绘图工具和面部网格模型
mpDraw = mp.solutions.drawing_utils # 导入 MediaPipe 的绘图工具模块
mpFaceMesh = mp.solutions.face_mesh # 导入 MediaPipe 的面部网格模块,用于检测和处理面部特征点
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2) # 初始化面部网格模型,设置最多检测两张人脸
drawSpec = mpDraw.DrawingSpec(thickness=1, circle_radius=2) # 创建一个绘图规格对象,用于定义标志点和连接线的绘制样式
# thickness 指定线的厚度,circle_radius 指定标志点的半径
while True:
print("Reading video frame...")
success, img = cap.read()
print("Read success:", success)
if not success:
print("Finished processing video or error occurred.")
break
# 将图像转换为 RGB
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 处理图像以检测面部网格
results = faceMesh.process(imgRGB)
if results.multi_face_landmarks:
for faceLms in results.multi_face_landmarks:
# 绘制面部网格
mpDraw.draw_landmarks(img, faceLms, mpFaceMesh.FACEMESH_TESSELATION, drawSpec, drawSpec