OpenCV实战——手掌检测

手掌检测


import cv2
import time
import mediapipe as mp

cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands   # initial
hands = mpHands.Hands()        # detect hands
mpDraw = mp.solutions.drawing_utils  # draw key points of hands

pTime = 0
cTime = 0

while True:
    ret,img = cap.read()
    imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

    results = hands.process(imgRGB)
    if results.multi_hand_landmarks:
        for i,handLms  in enumerate(results.multi_hand_landmarks):  # 遍历每个手
            cv2.putText(img,
                        f'label:{results.multi_handedness[i].classification[0].label}',
                        (20, 95), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            cv2.putText(img,
                        f'score:{results.multi_handedness[i].classification[0].score.__round__(3)}',
                        (20, 115), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            lmlist = []
            for id ,lm in enumerate(handLms.landmark): # 遍历手的21个关键点
                h, w, c = imgRGB.shape
                cx,cy = int(lm.x*w), int(lm.y*h)
                lmlist.append([id, cx, cy])
                cv2.circle(img,(cx,cy),2,(255,0,0),cv2.FILLED)
            print(lmlist)
        mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

    cTime = time.time()
    fps = 1/(cTime-pTime)
    pTime = cTime
    cv2.putText(img,f'FPS:{int(fps)}',(20,70),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
    cv2.imshow('Text',img)

    if cv2.waitKey(10) == ord('q'):
        break

cap.release()
cv2.destroyAllWindow()
'''

[
[0, 888, 693],
[1, 799, 667], 
[2, 731, 598], 
[3, 685, 536], 
[4, 638, 487], 
[5, 780, 456], 
[6, 763, 361], 
[7, 756, 308], 
[8, 753, 261], 
[9, 834, 443],
[10, 816, 339],
[11, 808, 279], 
[12, 801, 231], 
[13, 885, 448], 
[14, 871, 350], 
[15, 862, 294], 
[16, 852, 246], 
[17, 937, 467], 
[18, 935, 394], 
[19, 934, 348], 
[20, 931, 304]]
'''

你可能感兴趣的:(OpenCV实战,opencv,人工智能,计算机视觉)