当前位置:   article > 正文

手势控制电脑音量_from comtypes import clsctx_all

from comtypes import clsctx_all

tiktok看到的计算机视觉大佬恩培,然后跟着一起完成了这个简单的计算机视觉的小项目。

  1. """
  2. Date: 2021-11-16
  3. 功能:手势操作电脑音量
  4. 1、使用OpenCV读取摄像头视频流;
  5. 2、识别手掌关键点像素坐标;
  6. 3、根据拇指和食指指尖的坐标,利用勾股定理计算距离;
  7. 4、将距离等比例转为音量大小,控制电脑音量
  8. """
  9. # 导入OpenCV
  10. import cv2
  11. # 导入mediapipe
  12. import mediapipe as mp
  13. # 导入电脑音量控制模块
  14. from ctypes import cast, POINTER
  15. from comtypes import CLSCTX_ALL
  16. from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
  17. # 导入其他依赖包
  18. import time
  19. import math
  20. import numpy as np
  21. class HandControlVolume:
  22. def __init__(self):
  23. # 初始化medialpipe
  24. self.mp_drawing = mp.solutions.drawing_utils
  25. self.mp_drawing_styles = mp.solutions.drawing_styles
  26. self.mp_hands = mp.solutions.hands
  27. # 获取电脑音量范围
  28. devices = AudioUtilities.GetSpeakers()
  29. interface = devices.Activate(
  30. IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
  31. self.volume = cast(interface, POINTER(IAudioEndpointVolume))
  32. self.volume.SetMute(0, None)
  33. self.volume_range = self.volume.GetVolumeRange()
  34. # 主函数
  35. def recognize(self):
  36. # 计算刷新率
  37. fpsTime = time.time()
  38. # OpenCV读取视频流
  39. cap = cv2.VideoCapture(0)
  40. # 视频分辨率
  41. resize_w = 640
  42. resize_h = 480
  43. # 画面显示初始化参数
  44. rect_height = 0
  45. rect_percent_text = 0
  46. with self.mp_hands.Hands(min_detection_confidence=0.7,
  47. min_tracking_confidence=0.5,
  48. max_num_hands=2) as hands:
  49. while cap.isOpened():
  50. success, image = cap.read()
  51. image = cv2.resize(image, (resize_w, resize_h))
  52. if not success:
  53. print("空帧.")
  54. continue
  55. # 提高性能
  56. image.flags.writeable = False
  57. # 转为RGB
  58. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  59. # 镜像
  60. image = cv2.flip(image, 1)
  61. # mediapipe模型处理
  62. results = hands.process(image)
  63. image.flags.writeable = True
  64. image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  65. # 判断是否有手掌
  66. if results.multi_hand_landmarks:
  67. # 遍历每个手掌
  68. for hand_landmarks in results.multi_hand_landmarks:
  69. # 在画面标注手指
  70. self.mp_drawing.draw_landmarks(
  71. image,
  72. hand_landmarks,
  73. self.mp_hands.HAND_CONNECTIONS,
  74. self.mp_drawing_styles.get_default_hand_landmarks_style(),
  75. self.mp_drawing_styles.get_default_hand_connections_style())
  76. # 解析手指,存入各个手指坐标
  77. landmark_list = []
  78. for landmark_id, finger_axis in enumerate(
  79. hand_landmarks.landmark):
  80. landmark_list.append([
  81. landmark_id, finger_axis.x, finger_axis.y,
  82. finger_axis.z
  83. ])
  84. if landmark_list:
  85. # 获取大拇指指尖坐标
  86. thumb_finger_tip = landmark_list[4]
  87. thumb_finger_tip_x = math.ceil(thumb_finger_tip[1] * resize_w)
  88. thumb_finger_tip_y = math.ceil(thumb_finger_tip[2] * resize_h)
  89. # 获取食指指尖坐标
  90. index_finger_tip = landmark_list[8]
  91. index_finger_tip_x = math.ceil(index_finger_tip[1] * resize_w)
  92. index_finger_tip_y = math.ceil(index_finger_tip[2] * resize_h)
  93. # 中间点
  94. finger_middle_point = (thumb_finger_tip_x + index_finger_tip_x) // 2, (
  95. thumb_finger_tip_y + index_finger_tip_y) // 2
  96. # print(thumb_finger_tip_x)
  97. thumb_finger_point = (thumb_finger_tip_x, thumb_finger_tip_y)
  98. index_finger_point = (index_finger_tip_x, index_finger_tip_y)
  99. # 画指尖2点
  100. image = cv2.circle(image, thumb_finger_point, 10, (255, 0, 255), -1)
  101. image = cv2.circle(image, index_finger_point, 10, (255, 0, 255), -1)
  102. image = cv2.circle(image, finger_middle_point, 10, (255, 0, 255), -1)
  103. # 画2点连线
  104. image = cv2.line(image, thumb_finger_point, index_finger_point, (255, 0, 255), 5)
  105. # 勾股定理计算长度
  106. line_len = math.hypot((index_finger_tip_x - thumb_finger_tip_x),
  107. (index_finger_tip_y - thumb_finger_tip_y))
  108. # 获取电脑最大最小音量
  109. min_volume = self.volume_range[0]
  110. max_volume = self.volume_range[1]
  111. # 将指尖长度映射到音量上
  112. vol = np.interp(line_len, [50, 300], [min_volume, max_volume])
  113. # 将指尖长度映射到矩形显示上
  114. rect_height = np.interp(line_len, [50, 300], [0, 200])
  115. rect_percent_text = np.interp(line_len, [50, 300], [0, 100])
  116. # 设置电脑音量
  117. self.volume.SetMasterVolumeLevel(vol, None)
  118. # 显示矩形
  119. cv2.putText(image, str(math.ceil(rect_percent_text)) + "%", (10, 350),
  120. cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
  121. image = cv2.rectangle(image, (30, 100), (70, 300), (255, 0, 0), 3)
  122. image = cv2.rectangle(image, (30, math.ceil(300 - rect_height)), (70, 300), (255, 0, 0), -1)
  123. # 显示刷新率FPS
  124. cTime = time.time()
  125. fpsTime = cTime
  126. cv2.putText(image, "FPS: " + str(int(fps_text)), (10, 70),
  127. cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
  128. # 显示画面
  129. cv2.imshow('MediaPipe Hands', image)
  130. break
  131. cap.release()
  132. # 开始程序
  133. control = HandControlVolume()
  134. control.recognize()

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/80263
推荐阅读
相关标签
  

闽ICP备14008679号