1989浏览
查看: 1989|回复: 5

【行空板】手势控风扇

[复制链接]
本帖最后由 云天 于 2022-5-11 08:31 编辑

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

MonMay-202205096429..png
MonMay-202205092930..png

MonMay-202205099837..png
【测试风扇】 MonMay-202205093558..png
金手指:引脚编号兼容micro:bit, 19路独立I/O(支持1路I2C、1路UART、2路SPI、6路12位ADC、5路12位PWM)
MonMay-202205091020..png
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)
复制代码
MonMay-202205099278..png

MonMay-202205099827..png

【测试摄像头】

安装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
复制代码
MonMay-202205092661..png
【手指距离识别】
MonMay-202205093842..png

  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

复制代码
MonMay-202205096781..png
mind+中测试

MonMay-202205096296..png
行空板中测试

【控制风扇完整程序】

  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)

复制代码
MonMay-202205094556..png 【演示视频】



rzegkly  版主

发表于 2022-5-10 09:17:33

神奇的手势控制
回复

使用道具 举报

Mary  高级技师

发表于 2022-5-11 15:53:09

感谢分享,行空板的项目已经开始造起来了,学习啦。
回复

使用道具 举报

云天  初级技神
 楼主|

发表于 2022-5-12 08:18:58

Mary 发表于 2022-5-11 15:53
感谢分享,行空板的项目已经开始造起来了,学习啦。

很好用的板子
回复

使用道具 举报

老曾  高级技匠

发表于 2023-3-2 23:22:29

大神,请教下你的作品手势控风扇,我用你的完整程序运行后摄像头可出来,但手一放程序就结束,提示Traceback (most recent call last):
  File "/root/mindplus/cache/手势控风扇.mp/shoushifengshan.py", line 33, in <module>
    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请问哪里出错了
回复

使用道具 举报

云天  初级技神
 楼主|

发表于 2023-3-4 20:48:12

老曾 发表于 2023-3-2 23:22
大神,请教下你的作品手势控风扇,我用你的完整程序运行后摄像头可出来,但手一放程序就结束,提示Tracebac ...

detector.findDistance,我在不同的机器上使用时,也出现Mediapipe有时会出现,参数不一致的情况,我会去看他的源程序,有时要改一下,才行。
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail