Kinect手勢識別控制電腦音量
音量
建議聲音調大觀看
文章目錄
- Kinect手勢識別控制電腦音量
- 前言
- 一、Kinect是什么?
- 二、代碼實作
- 1.環境
- 2.效果展示
- 總結
前言
opencv可以做很多事,最近突發奇想用Kinect做一個手勢識別,結合電腦音量做到隔空控制音量的效果,
一、Kinect是什么?
Kinect的手勢識別功能讓用戶不再需要資料手套即可通過雙手作業系統界面,Kinect的動作識別功能讓用戶無需穿上資料衣即可進行系統互動,Kinect的語音識別功能讓用戶通過口令即可控制虛擬場景,Kinect在虛擬現實領域會有很大的發展,
Kinect——體感,
二、代碼實作
1.環境
有一個pycharm軟體即可,將下列代碼復制過去,缺少什么環境去安裝就可以了
import cv2
import time
import numpy as np
import mediapipe as mp
import pygame
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
length = math.hypot((x2 - x1), (y2 - y1))
Hand_range=[50, 200]
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)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
#cv2.imshow("Img", img)
cv2.waitKey(1)
2.效果展示
隔空控制電腦音量
總結
目前可以做到簡單手勢識別,后續可以用手勢識別加上絲桿滑軌做一個隔空取物,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/414014.html
標籤:其他
上一篇:Ubuntu18.04配置ork(ecto、ecto_image_pipeline、ecto_opencv、ork_reconstruction)
下一篇:AI編譯器技術淺析
