安装Anaconda
https://www.anaconda.com/download
下载并安装后,启动Anaconda
,在某个环境的运行按钮上点击鼠标左键,并在弹框中选择Open Terminal
:
在上一步打开的终端中,执行以下命令:
在某个目录下编写以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import cv2 import mediapipe as mp
mp_face_mesh = mp.solutions.face_mesh mp_draw = mp.solutions.drawing_utils
face_mesh = mp_face_mesh.FaceMesh( static_image_mode=False, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5 ) cap = cv2.VideoCapture(0) try: while cap.isOpened(): ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = face_mesh.process(frame) if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: mp_draw.draw_landmarks(frame, face_landmarks, mp_face_mesh.FACEMESH_TESSELATION) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break except KeyboardInterrupt: print("键盘中断")
cv2.destroyAllWindows()
cap.release()
|
然后,在之前的终端中执行以下命令(假定代码保存在face_mesh.py
文件中,path/to/face_mesh.py
需要是真实路径):
1
| python path/to/face_mesh.py
|
效果