本帖最后由 gada888 于 2021-12-20 22:05 编辑
大家有没有想给朋友秀一下你的‘超能力’,你是怎么通过手势隔空控制一段音频的播放和关闭。不需要使用到手势传感,只要通过电脑或linux系统的USB摄像头就可以做到。
下面分享一下制作过程。
五指张开,打开音频

五指并拢,关闭音频

首先准备好以下硬件。


下面简单介绍一下硬件:
ISD1820录放音模块集成录放音功能为一体。板载按键和麦克风,可直接通过按键录音和放音,一按录音,一按放音。可实现10秒音频录放功能。
下面简单介绍一下软件。
编程用的ide用的是visual studio code
需要安装以下三个库文件:
opencv-python库
mediapipe库
pinpong库
介绍一下连线方式:
特别的简单,ISD1820接的是UNO上的D7.

ISD1820前置设置:
在ISD1820的REC按钮上按下,录一下自己10秒的录音。然后按下play按钮测试一下是不是OK。

项目展示效果视频:视频中可以看到,mediapipe识别到5根手指就会播放音频,小于5根手指就会停止播放。
下面是完整代码:
- # code written by gada888 from LUOYANG,in 18-12-2021
-
- import cv2
- import mediapipe as mp
- import time
- from pinpong.board import Board,Pin,Tone
-
- Board("uno","COM6").begin()
-
- tone = Tone(Pin(Pin.D7))
- tone.freq(200)
-
- mp_drawing = mp.solutions.drawing_utils
- mp_hands = mp.solutions.hands
- hands = mp_hands.Hands(max_num_hands = 1,min_detection_confidence=0.8, min_tracking_confidence=0.8)
- drawing_spec = mp_drawing.DrawingSpec(color=(255, 255, 255),thickness=8, circle_radius=8)
-
- tipIds = [4, 8, 12, 16, 20]
- cap = cv2.VideoCapture(1)
- while cap.isOpened():
- prev_time = time.time()
- success, frame = cap.read()
- if not success:
- print("Ignoring empty camera frame.")
- continue
- image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
- results = hands.process(image)
- lmList = []
- if results.multi_hand_landmarks:
- for hand_landmarks in results.multi_hand_landmarks:
- mp_drawing.draw_landmarks(frame, hand_landmarks,mp_hands.HAND_CONNECTIONS,drawing_spec,drawing_spec)
-
- for id, lm in enumerate(hand_landmarks.landmark):
- h, w, c = frame.shape
- cx, cy = int(lm.x * w), int(lm.y * h)
- lmList.append([id, cx, cy])
- # print(lmList)
- cv2.circle(frame, (cx, cy), 8, (38, 107, 170), cv2.FILLED)
- if id == 3 or id == 4:
- cv2.circle(frame, (cx, cy), 5, (0, 0, 255), cv2.FILLED)
- cv2.putText(frame,str(id),(cx+8, cy),cv2.FONT_HERSHEY_COMPLEX,.6,(0,0,255),1)
- if len(lmList) != 0:
- fingers = []
- # Thumb
- if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
- print(lmList[tipIds[0]][1], lmList[tipIds[0] - 1][1])
- fingers.append(1)
- tone.off()
- time.sleep(1)
- else:
- fingers.append(0)
- tone.on()
- time.sleep(1)
- # 4 Fingers
- for id in range(1, 5):
- if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
- fingers.append(1)
- else:
- fingers.append(0)
-
- totalFingers = fingers.count(1)
- print(totalFingers)
- else:
- totalFingers = 0
-
- cv2.rectangle(frame, (6, 121), (114, 229), (255, 255, 255), cv2.FILLED)
- cv2.rectangle(frame, (10, 125), (110, 225), (0, 0, 0), cv2.FILLED)
- cv2.putText(frame, str(totalFingers), (30, 210), cv2.FONT_HERSHEY_PLAIN, 6, (0, 0, 255), 8)
- cv2.putText(frame, 'fps:'+str(int(1 / (time.time() - prev_time))), (3, 40), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 3)
- print(1 / (time.time() - prev_time))
- cv2.imshow('finger counter', frame)
- if cv2.waitKey(1) & 0xFF == 27:
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
|