怎样用Wekinator和Arduino制作颜色分类器

电子说

1.3w人已加入

描述

电路图

Arduino

如何运行程序

首先,在Arduino IDE的文章末尾粘贴为Arduino提供的代码并上传代码。

然后您需要从Wekinator的示例页面下载草图。

下载源代码以进行处理实现10x10颜色网格。解压缩并在处理中运行代码。该程序将使用笔记本电脑的网络摄像头,根据您在摄像头前所做的操作,它将为Wekinator提供输入。

Wekinator的输出需要另一个草图。该草图的代码在本文末尾。将其粘贴到处理中并运行草图。该草图将从Wekinator输出并将其发送到Arduino,LED将亮起。

两个处理窗口应如下所示。

Arduino

现在打开Wekinator并进行如下图所示的设置。将输入设置为100,将输出设置为1.将类型设置为3的所有分类器,然后单击下一步。

Arduino

在前面拿一张彩色纸摄像头并开始录制半秒。

现在将班级更改为2.再次,向网络摄像头显示另一种颜色的纸张,开始录制半秒钟。

现在将类更改为3,显示另一种颜色的纸张,然后开始录制半秒钟。

之后,单击“Train”,然后单击“Run”。现在连接到Arduino的LED将根据您在网络摄像头前显示的颜色亮起。

ArduinoCode

#include //Including the library that will help us in receiving and sending the values from processing

ValueReceiver《1》 receiver; /*Creating the receiver that will receive only one value.

Put the number of values to synchronize in the brackets */

/* The below variable will be synchronized in the processing

and they should be same on both sides. */

int output;

// Initializing the pins for led‘s

int led1 = 8;

int led2 = 9;

int led3 = 10;

void setup()

{

/* Starting the serial communication because we are communicating with the

Arduino through serial. The baudrate should be same as on the processing side. */

Serial.begin(19200);

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

// Synchronizing the variable with the processing. The variables must be int type.

receiver.observe(output);

}

void loop()

{

// Receiving the output from the processing.

receiver.sync();

// Matching the received output to light up led’s

if (output == 1)

{

digitalWrite(led1, HIGH);

digitalWrite(led2, LOW);

digitalWrite(led3, LOW);

}

else if (output == 2)

{

digitalWrite(led1, LOW);

digitalWrite(led2, HIGH);

digitalWrite(led3, LOW);

}

else if (output == 3)

{

digitalWrite(led1, LOW);

digitalWrite(led2, LOW);

digitalWrite(led3, HIGH);

}

}

处理代码

import vsync.*; // Importing the library that will help us in sending and receiving the values from the Arduino

import processing.serial.*; // Importing the serial library

// Below libraries will connect and send, receive the values from Wekinator

import oscP5.*;

import netP5.*;

// Creating the instances

OscP5 oscP5;

NetAddress dest;

ValueSender sender;

// This variable will be syncronized with the Arduino and it should be same on the Arduino side.

public int output;

void setup()

{

// Starting the serial communication, the baudrate and the com port should be same as on the Arduino side.

Serial serial = new Serial(this, “COM10”, 19200);

sender = new ValueSender(this, serial);

// Synchronizing the variable as on the Arduino side. The order should be same.

sender.observe(“output”);

// Starting the communication with Wekinator. listen on port 12000, return messages on port 6448

oscP5 = new OscP5(this, 12000);

dest = new NetAddress(“127.0.0.1”, 6448);

}

// Recieve OSC messages from Wekinator

void oscEvent(OscMessage theOscMessage) {

if (theOscMessage.checkAddrPattern(“/wek/outputs”) == true) {

// Receiving the output from Wekinator

float value = theOscMessage.get(0).floatValue();

// Converting the output to int type

output = int(value);

}

}

void draw()

{

// Nothing to be drawn for this example

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分