【行空板】手势控风扇

2022-05-092万
精华项目
AI 快速预览详细 收起
本文介绍了如何使用行空板和Mediapipe库实现手势控风扇项目。通过识别手指间距来控制风扇转速,适合STEM教育场景。项目包括硬件清单、最终效果和难度等级。


【项目设计】Mediapipe识别手势,利用手指间距来控制风扇转速,风扇连接在扩展板上单独供电。软件编程利用Mind+测试版中可以连接行空板进行编程。
【行空板】 行空板是一款专为Python学习和使用设计的新一代国产开源硬件,采用单板计算机架构,集成LCD彩屏、WiFi蓝牙、多种常用传感器和丰富的拓展接口。同时,其自带Linux操作系统和Python环境,还预装了常用的Python库,让广大师生只需两步就能进行Python教学。
【行空板】手势控风扇图10

【行空板】手势控风扇图2
【行空板】手势控风扇图3

【行空板】手势控风扇图4
【测试风扇】【行空板】手势控风扇图6
金手指:引脚编号兼容micro:bit, 19路独立I/O(支持1路I2C、1路UART、2路SPI、6路12位ADC、5路12位PWM)
【行空板】手势控风扇图7
micro:bit 电机驱动扩展板
  1. # -*- coding: UTF-8 -*-
  2. #实验效果: PWM输出实验,控制LED灯亮度变化
  3. #接线:LED灯接到行空板P21引脚上
  4. import time
  5. from pinpong.board import Board,Pin
  6. Board().begin()               #初始化
  7. # PWM模拟输出引脚支持: P0  P2  P3  P10  P16  P21  P22  P23
  8. #pwm21 = PWM(Pin(Pin.P21)) #将引脚传入PWM初始化  模拟输出方法1
  9. pwm2 = Pin(Pin.P2, Pin.PWM) #初始化引脚为PWM模式 模拟输出方法2
  10. while True:
  11.    
  12.         pwm2.write_analog(1023) #PWM输出
  13.         time.sleep(4)
  14.         pwm2.write_analog(0) #PWM输出
  15.         time.sleep(4)
复制代码
【行空板】手势控风扇图8

【行空板】手势控风扇图9

【测试摄像头】

安装Mediapipe库与OpenCV库,参考AI人工智能应用:https://wiki.unihiker.com/ai_project
  1. import cv2
  2. #False:不旋转屏幕(竖屏显示,上下会有白边)
  3. #True:旋转屏幕(横屏显示)
  4. screen_rotation = True
  5. cap = cv2.VideoCapture(0)
  6. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)  #设置摄像头图像宽度
  7. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置摄像头图像高度
  8. cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)     #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
  9. cv2.namedWindow('camera',cv2.WND_PROP_FULLSCREEN)    #窗口全屏
  10. cv2.setWindowProperty('camera', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)   #窗口全屏
  11. while True:
  12.   success, img = cap.read()
  13.   if not success:
  14.         print("Ignoring empty camera frame.")
  15.         continue
  16.   if screen_rotation: #是否要旋转屏幕
  17.         img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) #旋转屏幕
  18.   cv2.imshow("camera", img)
  19.   if cv2.waitKey(1) & 0xFF == ord('q'):
  20.     break
复制代码
【行空板】手势控风扇图1
【手指距离识别】
【行空板】手势控风扇图5
  1. import cv2
  2. from cvzone.HandTrackingModule import HandDetector
  3. cap = cv2.VideoCapture(0)
  4. detector = HandDetector(detectionCon=0.8, maxHands=2)
  5. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)  #设置摄像头图像宽度
  6. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置摄像头图像高度
  7. cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)     #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
  8. screen_rotation = False
  9. while True:
  10.         # Get image frame
  11.         success, img = cap.read()
  12.         # Find the hand and its landmarks
  13.         hands, img = detector.findHands(img)  # with draw
  14.         # hands = detector.findHands(img, draw=False)  # without draw
  15.         if hands:
  16.             # Hand 1
  17.             hand1 = hands[0]
  18.             lmList1 = hand1["lmList"]  # List of 21 Landmark points
  19.             bbox1 = hand1["bbox"]  # Bounding box info x,y,w,h
  20.             centerPoint1 = hand1['center']  # center of the hand cx,cy
  21.             handType1 = hand1["type"]  # Handtype Left or Right
  22.             fingers1 = detector.fingersUp(hand1)
  23.             # Find Distance between two Landmarks. Could be same hand or different hands
  24.             length, info, img = detector.findDistance(lmList1[8][0:2], lmList1[4][0:2],img,True)  # with draw
  25.             # length, info = detector.findDistance(lmList1[8], lmList2[8])  # with draw
  26.         # Display
  27.         cv2.imshow("Image", img)
  28.         if cv2.waitKey(1) & 0xFF == ord('q'):
  29.             break
复制代码
【行空板】手势控风扇图12
mind+中测试

【行空板】手势控风扇图11
行空板中测试

【控制风扇完整程序】
  1. import cv2
  2. from HandTrackingModule import HandDetector
  3. from pinpong.board import Board,Pin
  4. Board().begin()               #初始化
  5. pwm2 = Pin(Pin.P2, Pin.PWM) #初始化引脚为PWM模式 模拟输出方法2
  6. cap = cv2.VideoCapture(0)
  7. detector = HandDetector(detectionCon=0.8, maxHands=2)
  8. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)  #设置摄像头图像宽度
  9. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置摄像头图像高度
  10. cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)     #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
  11. def numberMap(x, in_min, in_max, out_min, out_max):
  12.   return int((x - in_min) * (out_max - out_min) / (in_max - in_min)) + out_min
  13. while True:
  14.         # Get image frame
  15.         success, img = cap.read()
  16.         # Find the hand and its landmarks
  17.         hands, img = detector.findHands(img)  # with draw
  18.         # hands = detector.findHands(img, draw=False)  # without draw
  19.         if hands:
  20.             # Hand 1
  21.             hand1 = hands[0]
  22.             lmList1 = hand1["lmList"]  # List of 21 Landmark points
  23.             bbox1 = hand1["bbox"]  # Bounding box info x,y,w,h
  24.             centerPoint1 = hand1['center']  # center of the hand cx,cy
  25.             handType1 = hand1["type"]  # Handtype Left or Right
  26.             fingers1 = detector.fingersUp(hand1)
  27.             length1, info = detector.findDistance(lmList1[20][0:2], lmList1[0][0:2],img,False)  # with draw
  28.             length, info, img = detector.findDistance(lmList1[8][0:2], lmList1[4][0:2],img,True)  # with draw
  29.             pwm2.write_analog(numberMap(int(length/length1*100),0,100,0,1024)) #PWM输出
  30.         else:
  31.             pwm2.write_analog(0) #PWM输出  
  32.        # Display
  33.         cv2.imshow("Image", img)
  34.         cv2.waitKey(1)
复制代码
【行空板】手势控风扇图13【演示视频】



创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(7)
花生编程
花生编程
好棒啊!!
花生编程
花生编程
厉害厉害
三春牛-创客
三春牛-创客
手势操作,棒棒的!赞一个!
三春牛-创客
三春牛-创客
厉害厉害!
老曾
老曾
大神,请教下你的作品手势控风扇,我用你的完整程序运行后摄像头可出来,但手一放程序就结束,提示Traceback (most recent call last):
File "/root/mindplus/cache/手势控风扇.mp/shoushifengshan.py", line 33, in
length1, info = detector.findDistance(lmList1[20][0:2], lmList1[0][0:2],img,False)
TypeError: findDistance() takes from 3 to 4 positional arguments but 5 were given请问哪里出错了
sky007
sky007回复老曾
后来有跑起来么?
老曾
老曾回复hsc
高手,可以分享下百度人脸识别和百度图像识别案例吗
共6条回复,已加载2条,点击查看更多共6条回复,已加载全部
Mary
Mary
感谢分享,行空板的项目已经开始造起来了,学习啦。
云天
云天
作者
回复Mary
很好用的板子
rzegkly
rzegkly
神奇的手势控制
- 没有更多了 -