【人体关键点定位】mediapipe_手部定位(一)
参考:https://google.github.io/mediapipe/solutions/hands
函数式
import cv2
import mediapipe as mp
import time
import os
import random
def video_ope(file):
switch = True
cap = cv2.VideoCapture(file)
myhands= mp.solutions.hands
hands = myhands.Hands()
myDraw = mp.solutions.drawing_utils
frame = 0
while(True):
ret, img = cap.read()
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(img_rgb)
if (results.multi_hand_landmarks):
for handLms in results.multi_hand_landmarks:
for id,lm in enumerate(handLms.landmark):
h,w,c = img.shape
cx,cy = int(lm.x*w),int(lm.y*h)
print (id,cx,cy)
if id == 0:
cv2.circle(img,(cx,cy),15,(255,0,0),cv2.FILLED)
else:
cv2.circle(img,(cx,cy),8,(0,255,5),cv2.FILLED)
myDraw.draw_landmarks(img,handLms,myhands.HAND_CONNECTIONS)
frame +=1
cv2.putText(img,str(int(frame)),(10,70),cv2.FONT_HERSHEY_PLAIN,3,(255,0,255),3)
cv2.imshow("Frame", img)
key = cv2.waitKey(1) & 0xFF
if key == ord(s):
switch = True
if key== ord(q):
switch = False
if key== 27:
break
cap.release()
cv2.destroyAllWindows()
def main():
file = "video/anime.mp4"
video_ope(file)
if __name__=="__main__":
main()
模块化
import cv2
import mediapipe as mp
import time
class handDetector():
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = self.hands.process(imgRGB)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
if draw:
for id,lm in enumerate(handLms.landmark):
h,w,c = img.shape
cx,cy = int(lm.x*w),int(lm.y*h)
if id == 0:
cv2.circle(img,(cx,cy),15,(255,0,0),cv2.FILLED)
else:
cv2.circle(img,(cx,cy),8,(0,255,5),cv2.FILLED)
self.mpDraw.draw_landmarks(img, handLms,
self.mpHands.HAND_CONNECTIONS)
return img
def main(file):
if file!="":
cap = cv2.VideoCapture(file)
else:
cap = cv2.VideoCapture(0)
detector = handDetector()
frame = 0
while True:
success, img = cap.read()
img = detector.findHands(img)
frame +=1
cv2.putText(img, str(int(frame)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 255, 255), 2)
cv2.imshow("Image", img)
cv2.waitKey(1)
key = cv2.waitKey(1) & 0xFF
if key == ord(s):
switch = True
if key== ord(q):
switch = False
if key== 27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
file = "video/anime.mp4"
main(file)
