0 介绍
目的:使用mediapipe库做手部的实时跟踪
检测流程:(1)手掌检测;(2)手掌特征检测
手掌特征分布:mediapipe手掌特征分布如下:
1.环境要求
后续代码运行环境(跟教程略有版本上的差异):
opencv-python 4.7.0.72
mediapipe 0.10.14
2.代码
import cv2 #图像处理库
import mediapipe as mp #手部跟踪库
import time
# 将手部检测封装成类
class handDetector():
def __init__(self, mode=False, maxHands=2, model_complexity=1,detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.model_complexity=model_complexity #注意此处,低版本无此参数
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.model_complexity,self.detectionCon, self.trackCon)# 注意多了一个模块参数
self.mpDraw = mp.solutions.drawing_utils
# 检测全部手掌并绘制相应的手部特征
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img
# 对第handNo编号的手掌进行特殊绘制处理
def findPosition(self, img, handNo=0, draw=True):
lmList = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
return lmList
def test():
pTime = 0
detector = handDetector()
cap = cv2.VideoCapture('test.mp4') #可以替换为实时镜头
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Image", 800, 600)
while True:
success, img = cap.read()
print(success)
if not success:
break
img = detector.findHands(img)
lmList = detector.findPosition(img,draw=True)
cTime = time.time()
fps = 1 / (cTime - pTime) if (cTime - pTime) != 0 else 0
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
test()
4.参考
(1)B站视频-实时手部跟踪视频教程
(2)gitee代码仓库
(3)教程源地址cv zone(1)(2)均来自此正式教程。