2021-12-18 17:14:38 [显示全部楼层]
1687浏览
查看: 1687|回复: 3

Python+Mediapipe项目--通过手势控制音频播放

[复制链接]
本帖最后由 gada888 于 2021-12-20 22:05 编辑

大家有没有想给朋友秀一下你的‘超能力’,你是怎么通过手势隔空控制一段音频的播放和关闭。不需要使用到手势传感,只要通过电脑或linux系统的USB摄像头就可以做到。
下面分享一下制作过程。
Python+Mediapipe项目--通过手势控制音频播放图5五指张开,打开音频
Python+Mediapipe项目--通过手势控制音频播放图6
五指并拢,关闭音频
Python+Mediapipe项目--通过手势控制音频播放图7
首先准备好以下硬件。
序号 产品名称 数量
1  Romeo 3in1(https://www.dfrobot.com.cn/goods-54.html 1
2 ISD1820(https://www.dfrobot.com.cn/goods-1310.html 1
3 电脑摄像头,任何牌子都可以,要USB接口的 1


Python+Mediapipe项目--通过手势控制音频播放图3

Python+Mediapipe项目--通过手势控制音频播放图4
下面简单介绍一下硬件:
ISD1820录放音模块
集成录放音功能为一体。板载按键和麦克风,可直接通过按键录音和放音,一按录音,一按放音。可实现10秒音频录放功能。


下面简单介绍一下软件。
编程用的ide用的是visual studio code


需要安装以下三个库文件:
opencv-python库
mediapipe库
pinpong库


介绍一下连线方式:
特别的简单,ISD1820接的是UNO上的D7.
Python+Mediapipe项目--通过手势控制音频播放图1
ISD1820前置设置:
在ISD1820的REC按钮上按下,录一下自己10秒的录音。然后按下play按钮测试一下是不是OK。
Python+Mediapipe项目--通过手势控制音频播放图2

项目展示效果视频:视频中可以看到,mediapipe识别到5根手指就会播放音频,小于5根手指就会停止播放。

下面是完整代码:
  1. # code written by gada888 from LUOYANG,in 18-12-2021
  2. import cv2
  3. import mediapipe as mp
  4. import time
  5. from pinpong.board import Board,Pin,Tone
  6. Board("uno","COM6").begin()
  7. tone = Tone(Pin(Pin.D7))
  8. tone.freq(200)
  9. mp_drawing = mp.solutions.drawing_utils
  10. mp_hands = mp.solutions.hands
  11. hands = mp_hands.Hands(max_num_hands = 1,min_detection_confidence=0.8, min_tracking_confidence=0.8)
  12. drawing_spec = mp_drawing.DrawingSpec(color=(255, 255, 255),thickness=8, circle_radius=8)
  13. tipIds = [4, 8, 12, 16, 20]
  14. cap = cv2.VideoCapture(1)
  15. while cap.isOpened():
  16.     prev_time = time.time()
  17.     success, frame = cap.read()
  18.     if not success:
  19.         print("Ignoring empty camera frame.")
  20.         continue
  21.     image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  22.     results = hands.process(image)
  23.     lmList = []
  24.     if results.multi_hand_landmarks:
  25.         for hand_landmarks in results.multi_hand_landmarks:
  26.             mp_drawing.draw_landmarks(frame, hand_landmarks,mp_hands.HAND_CONNECTIONS,drawing_spec,drawing_spec)
  27.             for id, lm in enumerate(hand_landmarks.landmark):
  28.                 h, w, c = frame.shape
  29.                 cx, cy = int(lm.x * w), int(lm.y * h)
  30.                 lmList.append([id, cx, cy])
  31.                 # print(lmList)
  32.                 cv2.circle(frame, (cx, cy), 8, (38, 107, 170), cv2.FILLED)
  33.                 if id == 3 or id == 4:
  34.                     cv2.circle(frame, (cx, cy), 5, (0, 0, 255), cv2.FILLED)
  35.                     cv2.putText(frame,str(id),(cx+8, cy),cv2.FONT_HERSHEY_COMPLEX,.6,(0,0,255),1)
  36.             if len(lmList) != 0:
  37.                 fingers = []
  38.                 # Thumb
  39.                 if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
  40.                     print(lmList[tipIds[0]][1], lmList[tipIds[0] - 1][1])
  41.                     fingers.append(1)
  42.                     tone.off()
  43.                     time.sleep(1)
  44.                 else:
  45.                     fingers.append(0)
  46.                     tone.on()
  47.                     time.sleep(1)
  48.                 # 4 Fingers
  49.                 for id in range(1, 5):
  50.                     if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
  51.                         fingers.append(1)
  52.                     else:
  53.                          fingers.append(0)
  54.                 totalFingers = fingers.count(1)
  55.                 print(totalFingers)
  56.     else:
  57.         totalFingers = 0
  58.     cv2.rectangle(frame, (6, 121), (114, 229), (255, 255, 255), cv2.FILLED)
  59.     cv2.rectangle(frame, (10, 125), (110, 225), (0, 0, 0), cv2.FILLED)
  60.     cv2.putText(frame, str(totalFingers), (30, 210), cv2.FONT_HERSHEY_PLAIN, 6, (0, 0, 255), 8)
  61.     cv2.putText(frame, 'fps:'+str(int(1 / (time.time() - prev_time))), (3, 40), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 3)
  62.     print(1 / (time.time() - prev_time))
  63.     cv2.imshow('finger counter', frame)
  64.     if cv2.waitKey(1) & 0xFF == 27:
  65.         break
  66. cap.release()
  67. cv2.destroyAllWindows()
复制代码





hnyzcj  版主

发表于 2021-12-19 08:41:24

哎呀,这个不错
回复

使用道具 举报

gada888  版主
 楼主|

发表于 2021-12-20 08:36:33

本帖最后由 gada888 于 2021-12-20 08:37 编辑

谢楼上,还望多指教
回复

使用道具 举报

QQQQQQQ  初级技匠

发表于 2022-3-14 19:15:52

厉害厉害
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail