大数跨境
0
0

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

检测人脸并跟随:玩转Mathematica+Arduino 摄像头 DF创客社区
2017-05-02
3
导读:通过USB摄像头获取图像,如果检测到人脸将计算出中心坐标,把坐标通过串口发送给Arduino……
 
欢迎来到服“造”的世界



整理翻译:mmaer 

本文来源:www.dfrobot.com.cn



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


软件

Mathematica 10/11

Arduino IDE


硬件

Arduino开发板

USB摄像头

9g舵机

舵机云台

连接线若干


连接

把摄像头固定在舵机云台上,在Arduino开发板上插上IO传感器扩展板,舵机接在传感器扩展板的第9个数字引脚上。


注意:

*如果选用的是大功率的云台和舵机,需要为舵机独立供电。

*本文使用的云台为一个自由度(水平移动),知道了原理后扩展为两个自由度也很简单(水平+垂直)。



演示


 



Mathematica 代码


$ImagingDevice = $ImagingDevices[[2]];

 

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

 

Dynamic[

 i = CurrentImage[];

 boxes = FindFaces[i];

 If[boxes =!= {},

  {X, Y} = Round[Mean @@ boxes];

  Column@

   {

    HighlightImage[i, Circle[{X, Y}, 50], ImageSize -> {320, 240}],

    DeviceWrite[dev, ToString[X]]

    },

  i]

 ]


arduino代码


#include <Servo.h>

 

#define  servomaxx   180   // max degree servo horizontal (x) can turn

#define  screenmaxx   320   // max screen horizontal (x)resolution

#define  screenmaxy   240    // max screen vertical (y) resolution

#define  servocenterx   90  // center po#define  of x servo

#define  servopinx   9   // digital pin for servo x

#define  baudrate 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);

 

  }

}




*欢迎转发朋友圈。如需转载,请联系授权。 


更多项目干货戳这里

萌妹纸的体感猫耳朵

智能穿戴—将日记可视化的连衣裙

金刚狼爪制作集合   |  台湾创客的桌面神器

雾霾自救  VR黑科技  |  OTTO  |  宠物机器人

3D打印笔  |  智能手环  |  树莓派拍立得

互动纸艺装置  HIFI终极音箱



点击“阅读原文”,学习Arduino吧!……

【声明】内容源于网络
0
0
DF创客社区
我们是专注于创新和开源硬件开发的公司——DFRobot成立的创客社区,无论你是资深创客还是小白,这里都有你的一席之地。一个人玩自己的项目,你只是寂寞宅;一群人看你玩项目,你就是技术牛!快来分享你的项目吧!
内容 1282
粉丝 0
DF创客社区 我们是专注于创新和开源硬件开发的公司——DFRobot成立的创客社区,无论你是资深创客还是小白,这里都有你的一席之地。一个人玩自己的项目,你只是寂寞宅;一群人看你玩项目,你就是技术牛!快来分享你的项目吧!
总阅读1.9k
粉丝0
内容1.3k