2016-12-6 15:14:41 [显示全部楼层]
80257浏览
查看: 80257|回复: 55

[进阶] Mathematica+Arduino 摄像头检测人脸并跟随

  [复制链接]
通过USB摄像头获取图像,如果检测到人脸将计算出中心坐标,把坐标通过串口发送给Arduino,算出人脸坐标偏离画面中心点的距离,然后根据这个偏离值驱动舵机带动摄像头修正指向,从而可以跟随人脸移动。

软件
Mathematica 10/11
Arduino IDE

硬件
Arduino开发板
USB摄像头
9g舵机
舵机云台

连接线若干

连接
把摄像头固定在舵机云台上,在Arduino开发板上插上IO传感器扩展板,舵机接在传感器扩展板的第9个数字引脚上。
注意:
如果选用的是大功率的云台和舵机,需要为舵机独立供电。
本文使用的云台为一个自由度(水平移动),知道了原理后扩展为两个自由度也很简单(水平+垂直)。
Mathematica+Arduino 摄像头检测人脸并跟随图2

演示

Mathematica+Arduino 摄像头检测人脸并跟随图1

Mathematica 代码
  1. $ImagingDevice = $ImagingDevices[[2]];
  2. dev = DeviceOpen["Serial", "COM3"]
  3. Dynamic[
  4. i = CurrentImage[];
  5. boxes = FindFaces;
  6. If[boxes =!= {},
  7.   {X, Y} = Round[Mean @@ boxes];
  8.   Column@
  9.    {
  10.     HighlightImage[i, Circle[{X, Y}, 50], ImageSize -> {320, 240}],
  11.     DeviceWrite[dev, ToString[X]]
  12.     },
  13.   i]
  14. ]
复制代码


Arduino 代码

  1. #include <Servo.h>
  2. #define  servomaxx   180   // max degree servo horizontal (x) can turn
  3. #define  screenmaxx   320   // max screen horizontal (x)resolution
  4. #define  screenmaxy   240    // max screen vertical (y) resolution
  5. #define  servocenterx   90  // center po#define  of x servo
  6. #define  servopinx   9   // digital pin for servo x
  7. #define  baudrate 9600  // com port speed. Must match your setting
  8. #define distancex 2  // x servo rotation steps
  9. int valx = 0;       // store x data from serial port
  10. int posx = 0;
  11. int incx = 10;  // significant increments of horizontal (x) camera movement
  12. Servo servox;
  13. void setup() {
  14.   Serial.begin(baudrate);        // connect to the serial port
  15.   Serial.setTimeout(20);
  16.   Serial.println("Starting Cam-servo Face tracker");
  17.   pinMode(servopinx, OUTPUT);   // declare the LED's pin as output
  18.   servox.attach(servopinx);
  19.   // center servos
  20.   servox.write(servocenterx);
  21.   delay(200);
  22. }
  23. void loop () {
  24.   while (Serial.available() <= 0); // wait for incoming serial data
  25.   if (Serial.available() >= 1)  
  26.   {
  27.     // get X axis 2-byte integer from serial
  28.     valx = Serial.parseInt();
  29.     // read last servos positions
  30.     posx = servox.read();
  31.     //Find out if the X component of the face is to the left of the middle of the screen.
  32.     if (valx < (screenmaxx / 2 - incx)) {
  33.       if ( posx >= incx ) posx += distancex; //Update the pan position variable to move the servo to the left.
  34.     }
  35.     //Find out if the X component of the face is to the right of the middle of the screen.
  36.     else if (valx > screenmaxx / 2 + incx) {
  37.       if (posx <= servomaxx - incx) posx -= distancex; //Update the pan position variable to move the servo to the right.
  38.     }
  39.     // Servos will rotate accordingly
  40.     servox.write(posx);
  41.   }
  42. }
复制代码

本帖被以下淘专辑推荐:

  • · 毕设|主题: 1, 订阅: 0

wx1  学徒

发表于 2020-11-19 16:36:04

mmaer 发表于 2016-12-13 12:03
是在PC上跑的,LattePanda上也可以跑的,树莓派上自带的有Mathematica但功能不完整。
pcDuino上可以跑人 ...

只能pc上  来运行吗?   如果不连接  pc   他们还能正常运作吗   跟着人脸移动
回复

使用道具 举报

mmaer  初级技师
 楼主|

发表于 2016-12-13 12:03:35

吹口琴的钢铁侠 发表于 2016-12-13 10:19
是在pc上跑mathematic?Arduino只是控制舵机?
什么型号的Arduino能跑起人脸识别呢 ...

是在PC上跑的,LattePanda上也可以跑的,树莓派上自带的有Mathematica但功能不完整。
pcDuino上可以跑人脸检测的
回复

使用道具 举报

砖头墙  初级技师

发表于 2017-1-6 21:17:57

叼                       牛                       膜拜大神
回复

使用道具 举报

hnyzcj  版主

发表于 2016-12-6 19:09:13

这个有些牛逼
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-12-8 16:38:15

学习学习
回复

使用道具 举报

飞猪的小号  中级技师

发表于 2016-12-9 20:22:38

看起来好酷啊,学习学习
回复

使用道具 举报

HMark  初级技师

发表于 2016-12-11 08:26:13

厉害厉害
回复

使用道具 举报

gray6666  初级技神

发表于 2016-12-11 14:21:26

好东西,学习了
回复

使用道具 举报

iooops  中级技匠

发表于 2016-12-11 21:42:16

为什么代码这么短。。
回复

使用道具 举报

Memorainer  见习技师

发表于 2016-12-11 22:04:10

我想知道,如果出现多张脸会发生什么
回复

使用道具 举报

mmaer  初级技师
 楼主|

发表于 2016-12-12 10:52:23

iooops 发表于 2016-12-11 21:42
为什么代码这么短。。

案例本来就简单,还有Mathematica库函数丰富 语法比较简洁
回复

使用道具 举报

mmaer  初级技师
 楼主|

发表于 2016-12-12 10:53:19

Memorainer 发表于 2016-12-11 22:04
我想知道,如果出现多张脸会发生什么

这种情况还没有考虑,现在只检测一张脸
回复

使用道具 举报

吹口琴的钢铁侠  初级技匠

发表于 2016-12-13 10:19:58

是在pc上跑mathematic?Arduino只是控制舵机?
什么型号的Arduino能跑起人脸识别呢
回复

使用道具 举报

John管宁川  

发表于 2016-12-20 15:09:24

提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

DFrobothty  中级技师

发表于 2016-12-29 09:07:05

厉害了world哥
回复

使用道具 举报

Xpimach  见习技师

发表于 2017-1-5 14:28:30

很不错欸
回复

使用道具 举报

Xpimach  见习技师

发表于 2017-1-5 14:28:39

大神
回复

使用道具 举报

Xpimach  见习技师

发表于 2017-1-5 14:28:50

顶一个。
回复

使用道具 举报

vvvvv  学徒

发表于 2017-1-20 18:10:57

收藏 学习了
回复

使用道具 举报

hnyzcj  版主

发表于 2017-1-21 09:04:04

楼主用你的真脸试试看看行不行呀
回复

使用道具 举报

lauren  高级技师

发表于 2017-1-25 18:39:02

哟不错,简单好玩!有没有耍过Wolfram的Systemmodeler!?
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail