import numpy as np
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
# 音量控制类
class VolumeControl:
def __init__(self):
# 获取音频设备接口
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
self.volume = cast(interface, POINTER(IAudioEndpointVolume))
self.volume.SetMute(0, None)
self.volume_range = self.volume.GetVolumeRange()
# 设置音量
def set_volume(self, line_len):
min_volume = self.volume_range[0]
max_volume = self.volume_range[1]
volume_level = np.interp(line_len, [50, 300], [min_volume, max_volume])
self.volume.SetMasterVolumeLevel(volume_level, None)
# 手势控制类
class HandGestureControl:
def __init__(self):
self.mp_drawing = mp.solutions.drawing_utils
self.mp_hands = mp.solutions.hands
self.volume_control = VolumeControl()
# 计算矩形条高度
def calculate_rect_height(self, line_len):
return np.interp(line_len, [50, 300], [0, 200])
# 手势识别
def recognize(self):
fpsTime = time.time()
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
resize_w = 640
resize_h = 480
rect_height = 0
with self.mp_hands.Hands(min_detection_confidence=0.7,
min_tracking_confidence=0.5,
max_num_hands=2) as hands:
while cap.isOpened():
success, image = cap.read()
image = cv2.resize(image, (resize_w, resize_h))
if not success:
print("Empty frame.")
continue
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.flip(image, 1)
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
landmark_list = []
for landmark_id, finger_axis in enumerate(hand_landmarks.landmark):
landmark_list.append([
landmark_id, finger_axis.x, finger_axis.y, finger_axis.z
if landmark_list:
thumb_finger_tip = landmark_list[4]
thumb_finger_tip_x = math.ceil(thumb_finger_tip[1] * resize_w)
thumb_finger_tip_y = math.ceil(thumb_finger_tip[2] * resize_h)
index_finger_tip = landmark_list[8]
index_finger_tip_x = math.ceil(index_finger_tip[1] * resize_w)
index_finger_tip_y = math.ceil(index_finger_tip[2] * resize_h)
line_len = math.hypot((index_finger_tip_x - thumb_finger_tip_x),
(index_finger_tip_y - thumb_finger_tip_y))
self.volume_control.set_volume(line_len)
print("手势距离", line_len)
rect_height = self.calculate_rect_height(line_len)
# 显示音量百分比和矩形条
cv2.putText(image, str(int(rect_height / 2)) + "%", (10, 350),
cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
image = cv2.rectangle(image, (30, 100), (70, 300), (255, 0, 0), 3)
image = cv2.rectangle(image, (30, math.ceil(300 - rect_height)), (70, 300), (255, 0, 0), -1)
# 显示帧率信息
cTime = time.time()
fps_text = 1 / (cTime - fpsTime)
fpsTime = cTime
cv2.putText(image, "FPS: " + str(int(fps_text)), (10, 70),
cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
cv2.imshow('Hand Volume Control', image)
if cv2.waitKey(5) & 0xFF == 27 or cv2.getWindowProperty('Hand Volume Control',
cv2.WND_PROP_VISIBLE) < 1:
break
cap.release()
control = HandGestureControl()
control.recognize()
以上代码包括了几个部分
1. 音量控制类
VolumeControl
-
初始化时获取音频设备接口,并设置音量范围。
-
提供方法
set_volume(line_len)
,根据手势识别得到的线段长度来调整音量大小。
2. 手势控制类
HandGestureControl
-
初始化时加载 Mediapipe 库用于手部关键点检测,并创建
VolumeControl
实例。
-
提供方法
calculate_rect_height(line_len)
,根据线段长度计算矩形条的高度。
-
提供方法
recognize()
,进行手势识别并实时更新音量大小和界面展示。
3. 主程序
-
打开摄像头,获取实时图像。
-
使用 Mediapipe 进行手部关键点检测,获取手部关键点坐标。
-
根据拇指和食指指尖的坐标计算手势距离,并通过
VolumeControl
调整音量。
-
根据手势距离计算矩形条的高度,并在界面上显示音量百分比和矩形条。
-
显示帧率信息。
运行效果
通过以上功能,用户就可以通过手势控制音量的大小,并实时查看音量百分比和界面反馈。这个功能结合了图像处理、手部关键点检测和音频控制等技术,感兴趣的小伙伴还可以继续优化代码,利用该技术实现不同的控制功能
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
# 获取自己的音频设备及其参数
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_
### 基于
OpenCV
的
手势
识别
与
手势
控制系统
介绍
手势
识别
技术作为人机交互的一个重要分支,通过捕捉和分析人体的手部动作来
实现
对设备的
控制
。基于
OpenCV
的
手势
识别
系统
利用
计算机视觉
算法处理摄像头捕获的图像数据,从中提取出
手势
特征,并根据这些特征执行相应的命令。这种技术广泛应用于智能家居、虚拟现实、游戏娱乐等多个领域。
#### 1. 系统架构概述
一个典型的
手势
识别
系统主要由几个关键模块组成:图像采集、预处理、
手势
分割、特征提取和分类决策。首先,使用摄像头
实时
采集视频流,这是
手势
识别
的基础数据来
PyVerse项目中的
手势
音量
控制
功能
实现
解析
手势
交互作为人机交互的重要方式之一,在
计算机视觉
领域有着广泛的应用前景。本文将详细介绍在PyVerse项目中
实现
的
手势
音量
控制系统
,该系统通过
计算机视觉
技术
识别
用户
手势
并
实时
调节
系统
音量
。
系统架构与核心技术
该
手势
音量
控制系统
主要由三个核心模块组成:
手部检测模块:基于
计算机视觉
的手部关键点检测技术,能够
实时
追踪用户手部动作
手势
识别
模块:分析检...
记录解决运行Qt程序出现警告提示“Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland t”