在本文中,我们将使用Python来检测人脸和手部标志。我们将使用一个模块
检测所有面部和手部标志的解决方案。此外,我们亦会看看如何取得不同的面部及手上标志,这些标志可应用于不同的电脑视觉应用,例如手语侦测、睡意侦测等
所需模块
OpenCVPython库是一个广泛应用于图像分析、图像处理、检测、识别等领域的计算机视觉库。
安装所需的库
pip install opencv-python mediapipe msvc-runtime
复制代码
下面是一种分步骤的人脸和手部地标检测方法。
步骤1:导入所有必需的库,在本例中只需要两个库。
# Import Libraries
import
cv2
import
time
import
mediapipe as mp
步骤2:初始化整体模型和绘图功能,以检测和绘制图像上的地标。
# Grabbing the Holistic Model from Mediapipe and
# Initializing the Model
mp_holistic
=
mp.solutions.holistic
holistic_model
=
mp_holistic.Holistic(
min_detection_confidence
=
0.5
,
min_tracking_confidence
=
0.5
)
# Initializing the drawng utils for drawing the facial landmarks on image
mp_drawing
=
mp.solutions.drawing_utils
让我们研究一下整体模型的参数:
Holistic(
static_image_mode=False,
model_complexity=1,
smooth_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
复制代码
第三步:从图像中检测脸部和手部的地标。整体模型对图像进行处理,为面部、左手和右手生成地标,并检测
# (0) in VideoCapture is used to connect to your compyter's default camera
capture
=
cv2.VideoCapture(
0
)
# Initializing current time and precious time for calculating the FPS
previousTime
=
0
currentTime
=
0
while
capture.isOpened():
# capture frame by frame
ret, frame
=
capture.read()
# resizing the frame for better view
frame
=
cv2.resize(frame, (
800
,
600
))
# Converting the from from BGR to RGB
image
=
cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Making predictions using holistic model
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable
=
False
results
=
holistic_model.process(image)
image.flags.writeable
=
True
# Converting back the RGB image to BGR
image
=
cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Drawing the Facial Landmarks
mp_drawing.draw_landmarks(
image,
results.face_landmarks,
mp_holistic.FACE_CONNECTIONS,
mp_drawing.DrawingSpec(
color
=
(
255
,
0
,
255
),
thickness
=
1
,
circle_radius
=
1
),
mp_drawing.DrawingSpec(
color
=
(
0
,
255
,
255
),
thickness
=
1
,
circle_radius
=
1
)
)
# Drawing Right hand Land Marks
mp_drawing.draw_landmarks(
image,
results.right_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
)
# Drawing Left hand Land Marks
mp_drawing.draw_landmarks(
image,
results.left_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
)
# Calculating the FPS
currentTime
=
time.time()
fps
=
1
/
(currentTime
-
previousTime)
previousTime
=
currentTime
# Displaying FPS on the image
cv2.putText(image,
str
(
int
(fps))
+
" FPS"
, (
10
,
70
), cv2.FONT_HERSHEY_COMPLEX,
1
, (
0
,
255
,
0
),
2
)
# Display the resulting image
cv2.imshow(
"Facial and Hand Landmarks"
, image)
# Enter key 'q' to break the loop
if
cv2.waitKey(
5
) &
0xFF
=
=
ord
(
'q'
):
break
# When all the process is done
# Release the capture and destroy all windows
capture.release()
cv2.destroyAllWindows()
整体模型可产生468个正面地标、21个左侧地标和21个右侧地标.可以通过指定所需地标的索引来访问单个地标。例:结果.左_HAND_landmark.地标[0]。您可以使用以下代码获取所有单个地标的索引:
# Code to access landmarks
for
landmark
in
mp_holistic.HandLandmark:
print
(landmark, landmark.value)
print
(mp_holistic.HandLandmark.WRIST.value)
HandLandmark.WRIST 0
HandLandmark.THUMB_CMC 1
HandLandmark.THUMB_MCP 2
HandLandmark.THUMB_IP 3
HandLandmark.THUMB_TIP 4
HandLandmark.INDEX_FINGER_MCP 5
HandLandmark.INDEX_FINGER_PIP 6
HandLandmark.INDEX_FINGER_DIP 7
HandLandmark.INDEX_FINGER_TIP 8
HandLandmark.MIDDLE_FINGER_MCP 9
HandLandmark.MIDDLE_FINGER_PIP 10
HandLandmark.MIDDLE_FINGER_DIP 11
HandLandmark.MIDDLE_FINGER_TIP 12
HandLandmark.RING_FINGER_MCP 13
HandLandmark.RING_FINGER_PIP 14
HandLandmark.RING_FINGER_DIP 15
HandLandmark.RING_FINGER_TIP 16
HandLandmark.PINKY_MCP 17
HandLandmark.PINKY_PIP 18
HandLandmark.PINKY_DIP 19
HandLandmark.PINKY_TIP 20
0
复制代码
产出
注意读取器!现在不要停止学习。掌握所有重要的机器学习概念
到这里就结束了,你学会了吗?
完整源码点:这里领取