它們之間,調(diào)用了time.sleep(0.07)控制按鍵的時長,按鍵時間長,則飛機移動距離就長,反之,按鍵時間短,則飛機移動距離就短,大家可以根據(jù)自己的需求來調(diào)節(jié)。
今天我們來擴展一下,用人臉玩飛機大戰(zhàn)。雖然思路跟手勢識別類似,但代碼量比手勢識別版稍多。
使用的人臉?biāo)惴ㄊ呛撩爰壍?,幀率能?30,用電腦CPU運行也很流暢。
下面我分享下項目實現(xiàn)過程,文末獲取項目完整的源代碼。
準(zhǔn)備飛機大戰(zhàn)程序
Github上找到一個Python版本的飛機大戰(zhàn)程序,安裝Pygame即可運行。

用鍵盤的A、D、W、S鍵用來控制飛機的移動方向,分別對應(yīng)左、右、上、下。
所以,接下來我們要做的是識別人臉,并對人臉姿態(tài)進行估計,將估計后的結(jié)果映射到左、右、上、下,從而控制飛機的運行。
人臉識別
這里,我們用opencv讀取攝像頭中的視頻流。
將視頻流中的每一幀送入mediapipe中的人臉識別模型,進行識別。

圖片
mediapipe 不止能識別人臉,還能標(biāo)注出人臉 6 個關(guān)鍵點左眼、右眼、左耳、右耳、鼻子、嘴巴。
核心代碼:
with self.mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.9) as face_detection:
while cap.isOpened():
success, image = cap.read()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_detection.process(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.detections:
for detection in results.detections:
# 獲取人臉框坐標(biāo)
face_box = detection.location_data.relative_bounding_box
face_w, face_h = int(face_box.width * frame_w), int(face_box.height * frame_h)
face_l = int(face_box.xmin * frame_w) + face_w
face_t = int(face_box.ymin * frame_h)
face_r, face_b = face_l - face_w, face_t + face_h
# 顯示人臉框
cv2.rectangle(image, (face_l, face_t), (face_r, face_b), (0, 255, 255), 2)
self.draw_zh_img(image, self.face_box_name_img, (face_r + face_l) // 2, face_t - 5)
pose_direct, pose_key_points = self.pose_estimate(detection)
# 顯示人臉 6 個關(guān)鍵點
for point_name in FaceKeyPoint:
mp_point = self.mp_face_detection.get_key_point(detection, point_name)
point_x = int(mp_point.x * frame_w)
point_y = int(mp_point.y * frame_h)
point_color = (0, 255, 0) if point_name in pose_key_points else (255, 0, 255)
cv2.circle(image, (point_x, point_y), 4, point_color, -1)
# 顯示關(guān)鍵點中文名稱
point_name_img = self.face_key_point_name_img[point_name]
self.draw_zh_img(image, point_name_img, point_x, point_y-5)
這里有個小知識點需要大家注意下。
代碼中用draw_zh_img來顯示中文,由于opencv不支持直接顯示中文。因此,我用PIL模塊中Image方法提前繪制中文圖片,并轉(zhuǎn)為opencv格式。

在需要的時候,直接與視頻流合并,效率高,不掉幀。
人臉姿態(tài)估計
之前手勢識別,我們用相鄰幀來判斷手勢的移動。人臉姿態(tài)估計只用當(dāng)前幀就可以,相對容易一些。
我們通過人臉 6 個關(guān)鍵點的坐標(biāo)距離,就可以判斷出人臉的姿態(tài)

這里,左耳和鼻子的水平距離很近,因此,我們可以估計臉向左轉(zhuǎn),從而可以用只飛機向左移動。
同樣的,用其他關(guān)鍵點,我們可以估計出人臉向右轉(zhuǎn)、向上(抬頭)和向下(低頭)
核心代碼:
# 左耳與鼻子水平距離,判斷面部左轉(zhuǎn)
left_ear_to_nose_dist = left_ear.x - nose_pos.x
# 右耳與鼻子水平距離,判斷面部右轉(zhuǎn)
nose_to_right_ear_dist = nose_pos.x - right_ear.x
# 鼻子與左眼垂直距離,判斷面部向上
nose_to_left_eye_dist = nose_pos.y - left_eye.y
# 左耳與左眼垂直距離,判斷面部向下
left_ear_to_left_eye_dist = left_ear.y - left_eye.y
if left_ear_to_nose_dist < 0.07:
# print('左轉(zhuǎn)')
self.key_board.press_key('A')
time.sleep(0.07)
self.key_board.release_key('A')
return 'A', [FaceKeyPoint.NOSE_TIP, FaceKeyPoint.LEFT_EAR_TRAGION]
if nose_to_right_ear_dist < 0.07:
# print('右轉(zhuǎn)')
self.key_board.press_key('D')
time.sleep(0.07)
self.key_board.release_key('D')
return 'D', [FaceKeyPoint.NOSE_TIP, FaceKeyPoint.RIGHT_EAR_TRAGION]
姿態(tài)控制飛機移動
識別出人臉姿態(tài),我們就可以用程序控制鍵盤,從而控制飛機移動。
這里我用PyKeyboard模塊控制鍵盤按鍵。
self.key_board = PyKeyboard()
# print('左轉(zhuǎn)')
self.key_board.press_key('A')
time.sleep(0.07)
self.key_board.release_key('A')
press_key和release_key函數(shù)分別是按鍵和釋放按鍵。
它們之間,調(diào)用了time.sleep(0.07)控制按鍵的時長,按鍵時間長,則飛機移動距離就長,反之,按鍵時間短,則飛機移動距離就短,大家可以根據(jù)自己的需求來調(diào)節(jié)。