2017-5-22 22:26:02 [显示全部楼层]
8265浏览
查看: 8265|回复: 4

[讨论] 【CurieNano试用】processing+图形检测+机器学习

[复制链接]
之前看见CurieNano的试用,一直想学点机器学习方面的知识,所以一直很关注,但是看了一圈试用项目,很多都拿这块板子做了普通Arduino板子就能做的东西,没有用到居里的128个神经单元,感觉很可惜,我都斗胆向Luna大大申请了一块CurieNano,做点机器学习的研究,希望抛砖引玉,大神上点更多的创意想法。
1、下载arduino IDE 1.8.2      可以去www.arduino.cc 下载.
版本不够,编译不过,别怪我通过IDE自带的板卡管理器下载,速度太慢了,且有时候还会被墙,导致我这更新过慢,实在不好意思


2、打开IDE,下载支持文件
【CurieNano试用】processing+图形检测+机器学习图1
【CurieNano试用】processing+图形检测+机器学习图2【CurieNano试用】processing+图形检测+机器学习图3
当然也可以通过其他方式,这里要感谢奈何大大制作的安装包
所以这里我把都传到了百度运,如果你等不急的话,直接从百度云下载吧(里面有版本编号,推荐下载版本号高的)
http://pan.baidu.com/s/1bogLHF5

3、驱动安装
驱动下载:
http://pan.baidu.com/s/1nuz3Wjn

如果你已经安装了 101/CurieNano扩展包,那么可以通过如下地址找到驱动,并安装。
C:\Users\Administrator\AppData\Local\Arduino15\packages\Intel\hardware\arc32\2.0.2\drivers
32位系统运行dpinst-x86.exe,64位系统运行dpinst-amd64.exe


安装完成后,查看设备管理器,如果有名为Genuino 101/CurieNano的串口出现,则说明安装成功,如图:
【CurieNano试用】processing+图形检测+机器学习图4

项目原理1.电脑上运行Processing客户端,负责收集实时视频,发送给Intel Curie Genduino 101/CurieNano,
2.电脑USB外接Intel Curie Genduino101/CurieNano,负责对视频进行训练和识别,可直接在arduino sketch中编译传送给CurieNano:
当然有人会说CurieNano处理不了视频,实际处理的是光流向量。


接下来部署Processing开发环境
首先在电脑安装Processing开发平台。在https://www.processing.org下载安装最新版本Processing软件。

然后打开Processing软件,给Processing平台添加OpenCV库。
【CurieNano试用】processing+图形检测+机器学习图5

选择菜单栏 工具-添加工具。点击鼠标。打开工具管理器。
【CurieNano试用】processing+图形检测+机器学习图6


选择第一页, Libraries,在下拉菜单中选择OpenV,点击Install安装。(注意,这一步需翻墙)

安装成功后,就可以使用本文后附的Processing程序代码了。程序代码启动电脑摄像头,分析拍摄的实时录像光流方向,并传递给Intel Curie nano / Genduino 101进行处理。

再回到arduino  IDE
确保sketch的开发板管理器已经安装和正确识别Curie以后,再添加CuireNeuron库。
下载CuireNeuron库
现在这个库官网收费了,我这里提供个免费版本
链接:http://pan.baidu.com/s/1eRNrRLg 密码:j7w5
【CurieNano试用】processing+图形检测+机器学习图7

然后添加刚才我们下载好的CuireNeuron.zip库文件。
再然后arduino代码

  1. /**
  2. *processing control a 2-4 servos platform
  3. */
  4. #include <CurieNeurons.h>
  5. CurieNeurons hNN;
  6. int catL = 0;
  7. int prevcat = 0;
  8. int dist, cat, nid, nsr, ncount;
  9.   
  10. const short sampleNbr = 20;
  11. const short signalNbr = 2;
  12. byte vector[sampleNbr*signalNbr];
  13. byte flowX[sampleNbr];
  14. byte flowY[sampleNbr];
  15. boolean a = false;
  16. void setup()   
  17. {
  18.    pinMode(13, OUTPUT);
  19.    Serial.begin(115200);
  20.    while (!Serial);
  21.    if(hNN.begin()==0){
  22.       Serial.print("neurons initialized");
  23.       hNN.forget(500);
  24.    }
  25.    else{
  26.       Serial.print("error.");
  27.    }
  28. }
  29. void loop()
  30. {
  31.    while(Serial.available()==0);
  32.    char data=Serial.read();
  33.    for(int i = 0; i < sampleNbr; i++)
  34.    {
  35.        while(Serial.available()==0);
  36.        vector[i*signalNbr]=Serial.read();
  37.    }
  38.    for(int i = 0; i < sampleNbr; i++)
  39.    {
  40.        while(Serial.available()==0);
  41.        vector[i*signalNbr+1]=Serial.read();
  42.    }
  43.    a = !a;
  44.    digitalWrite(13, a);   // turn the LED on (HIGH is the voltage level)
  45.    switch(data)
  46.    {   
  47.        case '%':
  48.            //learn 1
  49.            catL = 1;         
  50.            /*for(int i = 0; i <sampleNbr; i++){
  51.               Serial.write(vector[i*signalNbr]);
  52.               Serial.write(vector[i*signalNbr+1]);
  53.            }*/
  54.            ncount = hNN.learn(vector, sampleNbr*signalNbr, catL);
  55.            Serial.write(ncount);
  56.            Serial.write("#");
  57.            Serial.flush();
  58.            break;
  59.        case '^':
  60.            //learn 1
  61.            catL = 2;         
  62.            /*for(int i = 0; i <sampleNbr; i++){
  63.               Serial.write(vector[i*signalNbr]);
  64.               Serial.write(vector[i*signalNbr+1]);
  65.            }*/
  66.            ncount = hNN.learn(vector, sampleNbr*signalNbr, catL);
  67.            Serial.write(ncount);
  68.            Serial.write("#");
  69.            Serial.flush();
  70.            break;
  71.        case '[i][i][font=Microsoft Yahei, Simsun]这个时候你还需要下载个video库[/font]
  72. [font=Microsoft Yahei, Simsun]
  73. [/font]收工
  74. [/i][/i]
  75. :
  76.            // Recognize
  77.            hNN.classify(vector, sampleNbr*signalNbr,&dist, &cat, &nid);
  78.            Serial.write(cat);
  79.            Serial.write("#");
  80.            Serial.flush();
  81.            break;
  82.                        
  83.    }
  84.    
  85. }[/mw_shl_code]
  86. Processing程序
  87. [mw_shl_code=applescript,true]import gab.opencv.*;
  88. import processing.video.*;
  89. import processing.serial.*;
  90. Serial port;
  91. OpenCV opencv;
  92. Capture video;
  93. byte lastx, lasty;
  94. byte currentx, currenty;
  95. byte[] rawVecX;
  96. byte[] rawVecY;
  97. int index;
  98. int count;
  99. boolean action;
  100. final int sampleNbr = 20;
  101. byte state;
  102. byte[] result;
  103. int P_result;
  104. void setup() {
  105.   size(640, 240);
  106.   result = new byte[3];
  107.   P_result = 0;
  108.   rawVecX = new byte[sampleNbr];
  109.   rawVecY = new byte[sampleNbr];
  110.   lastx = 0;
  111.   lasty = 0;
  112.   count = 0;
  113.   port = new Serial(this, Serial.list()[0], 115200);
  114.   video = new Capture(this, 320, 240);
  115.   opencv = new OpenCV(this, 320, 240);  
  116.   video.start();
  117. }
  118. void draw() {
  119.   if(keyPressed){
  120.      if (key == 'a' || key == 'A') {
  121.        state = 1;
  122.        println("change state to learning 1");
  123.      }
  124.      else if (key == 's' || key == 'S') {
  125.        state = 2;
  126.        println("change state to learning 2");;
  127.         
  128.      }
  129.      else{
  130.        state = 3;
  131.        println("change state to classifying");
  132.      }
  133.   }
  134.   background(0);
  135.   opencv.loadImage(video);
  136.   opencv.calculateOpticalFlow();
  137.   image(video, 0, 0);
  138.   translate(video.width,0);
  139.   stroke(255,0,0);
  140.   opencv.drawOpticalFlow();
  141.   PVector aveFlow = opencv.getAverageFlow();
  142.   int flowScale = 50;
  143.   stroke(255);
  144.   strokeWeight(2);
  145.    
  146.   textSize(50);
  147.   if(result[2] == 1){   
  148.     text("Yes", 10, 230);
  149.   }
  150.   else if(result[2] == 2){
  151.     text("No", 10, 230);  
  152.   }
  153.   else{
  154.     text("?", 10, 230);
  155.   }
  156.   textSize(40);
  157.   if(result[1] == 1){   
  158.     text("Yes", 10, 175);
  159.   }
  160.   else if(result[1] == 2){
  161.     text("No", 10, 175);  
  162.   }
  163.   else{
  164.     text("?", 10, 175);
  165.   }
  166.   textSize(30);
  167.   if(result[0] == 1){   
  168.     text("Yes", 10, 130);
  169.   }
  170.   else if(result[0] == 2){
  171.     text("No", 10, 130);  
  172.   }
  173.   else{
  174.     text("?", 10, 130);
  175.   }
  176.    
  177.   line(video.width/2, video.height/2, video.width/2 + aveFlow.x*flowScale, video.height/2 + aveFlow.y*flowScale);
  178.    
  179.   currentx=byte(map(aveFlow.x, -1, 1, 0, 255));
  180.   currenty=byte(map(aveFlow.y, -1, 1, 0, 255));
  181.   if (action == true){
  182.       if(index < sampleNbr){
  183.           rawVecX[index] = currentx;
  184.           rawVecY[index] = currenty;
  185.           index++;  
  186.       }
  187.       else
  188.       {
  189.           action = false;
  190.           print("count=");
  191.           println(count);
  192.           for(int i = 0; i < sampleNbr; i++){
  193.               print(int(rawVecX));
  194.               print(",");
  195.           }
  196.           println();
  197.           for(int i = 0; i < sampleNbr; i++){
  198.               print(int(rawVecY));
  199.               print(",");
  200.           }
  201.           print("|");
  202.           println(state);
  203.           sendToCurie(state, rawVecX, rawVecY);
  204.           println("accepted");
  205.        }
  206.   }
  207.   else
  208.   {
  209.     if(abs(int(currentx)-int(lastx)) > 30 || abs(int(currenty)-int(lasty)) > 30){
  210.       action = true;
  211.       index = 0;
  212.       count = count + 1;
  213.       println("started");
  214.     }
  215.   }
  216.   lastx = currentx;
  217.   lasty = currenty;
  218. }
  219. void captureEvent(Capture c) {
  220.   c.read();
  221. }
  222. void sendToCurie(byte LorC, byte[] vecX, byte[] vecY) //send yaw and roll angel to MegaPi to control servos
  223. {
  224.    if(LorC == 1){
  225.         port.write('%');        
  226.    }
  227.    else if(LorC == 2){
  228.         port.write('^');
  229.    }
  230.    else{
  231.         port.write('[i][i][font=Microsoft Yahei, Simsun]这个时候你还需要下载个video库[/font]
  232. [font=Microsoft Yahei, Simsun]
  233. [/font]收工
  234. [/i][/i]
  235. );
  236.    }
  237.       
  238.   port.write(vecX);
  239.   port.write(vecY);
  240.    
  241.   byte inByte;
  242.   int ct = 0;
  243.   while (port.available() > 0){
  244.       inByte = byte(port.read());
  245.       print(int(inByte));
  246.       if(ct == 0){
  247.         result[0] = result[1];
  248.         result[1] = result[2];
  249.         result[2] = inByte;
  250.       }
  251.       print(">");
  252.       if(inByte == '#'){
  253.           break;
  254.       }
  255.       ct = ct + 1;
  256.   }
  257. }
复制代码
这个时候你还需要下载个video库

收工

无标题.png

hnyzcj  版主

发表于 2017-5-22 23:08:50

qidai
回复

使用道具 举报

luna  初级技神

发表于 2017-5-23 10:33:36

占楼~围观大项目
回复

使用道具 举报

gray6666  初级技神

发表于 2017-5-26 10:39:23

期待中。。。。。。。。。。。。。。。
回复

使用道具 举报

kaka  高级技师
 楼主|

发表于 2017-5-26 12:23:54

luna 发表于 2017-5-23 10:33
占楼~围观大项目

稍等哦
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail