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]]
'''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值