mmaer 发表于 2016-12-6 15:14:41

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

通过USB摄像头获取图像,如果检测到人脸将计算出中心坐标,把坐标通过串口发送给Arduino,算出人脸坐标偏离画面中心点的距离,然后根据这个偏离值驱动舵机带动摄像头修正指向,从而可以跟随人脸移动。
软件Mathematica 10/11Arduino IDE
硬件Arduino开发板USB摄像头9g舵机舵机云台
连接线若干
连接把摄像头固定在舵机云台上,在Arduino开发板上插上IO传感器扩展板,舵机接在传感器扩展板的第9个数字引脚上。注意:如果选用的是大功率的云台和舵机,需要为舵机独立供电。本文使用的云台为一个自由度(水平移动),知道了原理后扩展为两个自由度也很简单(水平+垂直)。

演示


Mathematica 代码$ImagingDevice = $ImagingDevices[];

dev = DeviceOpen["Serial", "COM3"]

Dynamic[
i = CurrentImage[];
boxes = FindFaces;
If[boxes =!= {},
{X, Y} = Round;
Column@
   {
    HighlightImage, ImageSize -> {320, 240}],
    DeviceWrite]
    },
i]
]

Arduino 代码
#include <Servo.h>

#defineservomaxx   180   // max degree servo horizontal (x) can turn
#definescreenmaxx   320   // max screen horizontal (x)resolution
#definescreenmaxy   240    // max screen vertical (y) resolution
#defineservocenterx   90// center po#defineof x servo
#defineservopinx   9   // digital pin for servo x
#definebaudrate 9600// com port speed. Must match your setting
#define distancex 2// x servo rotation steps

int valx = 0;       // store x data from serial port
int posx = 0;
int incx = 10;// significant increments of horizontal (x) camera movement

Servo servox;

void setup() {
Serial.begin(baudrate);      // connect to the serial port
Serial.setTimeout(20);
Serial.println("Starting Cam-servo Face tracker");

pinMode(servopinx, OUTPUT);   // declare the LED's pin as output

servox.attach(servopinx);

// center servos
servox.write(servocenterx);
delay(200);
}


void loop () {
while (Serial.available() <= 0); // wait for incoming serial data
if (Serial.available() >= 1)
{
    // get X axis 2-byte integer from serial
    valx = Serial.parseInt();

    // read last servos positions
    posx = servox.read();

    //Find out if the X component of the face is to the left of the middle of the screen.
    if (valx < (screenmaxx / 2 - incx)) {
      if ( posx >= incx ) posx += distancex; //Update the pan position variable to move the servo to the left.
    }
    //Find out if the X component of the face is to the right of the middle of the screen.
    else if (valx > screenmaxx / 2 + incx) {
      if (posx <= servomaxx - incx) posx -= distancex; //Update the pan position variable to move the servo to the right.
    }

    // Servos will rotate accordingly
    servox.write(posx);

}
}

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!?
页: [1] 2 3
查看完整版本: Mathematica+Arduino 摄像头检测人脸并跟随