赞
踩
机器视觉 python+mediapipe+opencv实现手部关键点检测(手势识别)(一)
代码如下:
import cv2 import time import numpy as np import mediapipe as mp from mediapipe import solutions import math from ctypes import cast, POINTER from comtypes import CLSCTX_ALL from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume ################################ wCam, hCam = 640, 480 ################################ cap = cv2.VideoCapture(0) cap.set(3, wCam) cap.set(4, hCam) pTime = 0 mpHands = solutions.hands hands = mpHands.Hands() devices = AudioUtilities.GetSpeakers() interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None) volume = cast(interface, POINTER(IAudioEndpointVolume)) volRange = volume.GetVolumeRange() minVol = volRange[0] maxVol = volRange[1] vol = 0 volBar = 400 volPer = 0 def findPosition(img, handNo=0, draw=True): xList = [] yList = [] lmList = [] imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) results = hands.process(imgRGB) if results.multi_hand_landmarks: for handLms in results.multi_hand_landmarks: mp.solutions.drawing_utils.draw_landmarks(img, handLms, mp.solutions.hands.HAND_CONNECTIONS) myHand = results.multi_hand_landmarks[handNo] for id, lm in enumerate(myHand.landmark): # print(id, lm) h, w, c = img.shape cx, cy = int(lm.x * w), int(lm.y * h) xList.append(cx) yList.append(cy) # print(id, cx, cy) lmList.append([id, cx, cy]) if draw: cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED) xmin, xmax = min(xList), max(xList) ymin, ymax = min(yList), max(yList) bbox = xmin, ymin, xmax, ymax if draw: cv2.rectangle(img, (bbox[0] - 20, bbox[1] - 20), (bbox[2] + 20, bbox[3] + 20), (0, 255, 0), 2) return lmList while True: success, img = cap.read() lmList = findPosition(img, draw=False) if len(lmList) != 0: x1, y1 = lmList[4][1], lmList[4][2] x2, y2 = lmList[8][1], lmList[8][2] cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED) cv2.circle(img, (x2, y2), 15, (255, 0, 255), cv2.FILLED) cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3) cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED) length = math.hypot(x2 - x1, y2 - y1) # print(length) Hand_range=[50, 200] # Volume Range -65 - 0 vol = np.interp(length, Hand_range, [minVol, maxVol]) volBar = np.interp(length, Hand_range, [400, 150]) volPer = np.interp(length, Hand_range, [0, 100]) print(int(length), vol) volume.SetMasterVolumeLevel(vol, None) if length < 50: cv2.circle(img, (cx, cy), 15, (0, 255, 0), cv2.FILLED) cv2.rectangle(img, (50, 150), (85, 400), (255, 0, 0), 3) cv2.rectangle(img, (50, int(volBar)), (85, 400), (255, 0, 0), cv2.FILLED) cv2.putText(img, f'{int(volPer)} %', (40, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 3) cTime = time.time() fps = 1 / (cTime - pTime) pTime = cTime cv2.putText(img, f'FPS: {int(fps)}', (40, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 3) cv2.imshow("Img", img) cv2.waitKey(1)
以上就是今天要讲的内容,本文仅仅简单介绍了mediapipe的使用,而mediapipe提供了大量关于图像识别等的应用和方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。