如何使用opencv检测多张面孔以生成闪烁的LED灯输出

Gab*_*ock 5 java processing opencv arduino led

我在使用processing和opencv方面还很陌生,所以请多多包涵。我已经在stackoverflow和处理论坛上都发布了我的问题,以覆盖更广泛的受众并找到解决方案-抱歉,这似乎是我不小心进行交叉发布,因为这不是我的意图。

我一直在使用Arduino和Processing进行轻型安装。我正在捕捉使用带有网络摄像头和opencv的面部检测的人,并将其转换为可在16x32 LED矩阵面板上工作的照明装置。灯光装置是对正在观看灯光并同时进行录制的观众数量的一种响应-想法是,录制的人越多,闪烁的灯光显示就越大,并且当没有人观看/正在观看时录制后,灯光不会闪烁。

我设法使opencv能够检测到面部并将其输出到Arduino以显示在灯光上,但是我只能使灯光在不被检测到的情况下(而不是在被检测到的情况下)更强烈地闪烁。因此,这些灯反向工作,并且在没有人检测到时,它们闪烁得更多,而在有人检测到时,它们闪烁得更少。

我已经附上了我的Processing和Arduino代码以供参考,尽管我认为根本不需要更改Arduino代码。

(我在处理代码中突出显示了一个区域,我认为这是导致问题的部分)

我非常感谢谁知道可以更改此方法,以便面部检测触发更多的光线

谢谢

处理代码

 import gab.opencv.*;
 import java.awt.*;
 import processing.video.*;
 import processing.serial.*;
 import java.awt.Color;

 Capture video;
 OpenCV opencv;

 Serial myPort;  // Create object from Serial class
 int inByte = -1; 

 void setup() 
 {
   size(640, 480);
   video = new Capture(this, 640/2, 480/2);
   opencv = new OpenCV(this, 640/2, 480/2);
   opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
   video.start();

   println(Serial.list());
   //colorMode(HSB, 100,100,100);
   String portName = Serial.list()[5];
   myPort = new Serial(this, Serial.list()[5], 9600);

 }

 void draw() {

   scale(2);
   opencv.loadImage(video);

   image(video, 0, 0 );

   noFill();
   stroke(0, 255, 0);
   strokeWeight(3);
   Rectangle[] faces = opencv.detect();
   println(faces.length);

 //  do cool stuff here:

     int x = int(random(32));
     int y = int(random(16));
     int H = int(222);
     int S = int(5);
     int L = int(random(3));


     int R =  int(random(255));
     int G =  int(random(255));
     int B =  int(random(255));


     int F = 0;
     String toard = x + ":" + y + ":" + R + ":" + G + ":" + B + ":" + F +".";

      myPort.write(toard);   


   **for (int i = 0; i < faces.length; i++) {

       rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
       toard = x + ":" + y + ":" + 0 + ":" + 0 + ":" + 0 + ".";
        myPort.write(toard);

       if (faces.length == 0){
        delay(faces[0].x); 


      } 
   }**


     // listen back to the serial data from arduino
     // this is handy for debugging

       while (myPort.available () > 0) {

         // send the string to the arduino over serial port
         inByte = myPort.read();

     }

  }


 void captureEvent(Capture c) {
   c.read();
 }
Run Code Online (Sandbox Code Playgroud)

Arduino代码

 const char EOPmarker = '.';
 char serialbuf[32];

 #include <Adafruit_GFX.h>
 #include <RGBmatrixPanel.h> // Hardware-specific library
 #define MAX_STRING_LEN 20
 #include <string.h>

 #define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
 #define LAT A3
 #define OE  9
 #define A   A0
 #define B   A1
 #define C   A2
 RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

 uint8_t r=7, g=7, b=7;

 void setup() {


   Serial.begin(9600);
   matrix.begin();
   pinMode(13, OUTPUT);
   digitalWrite(13, LOW);
 }

 void loop() {


     if (Serial.available() > 0) {
       static int bufpos = 0; 
       char inchar = Serial.read(); 

       if (inchar != EOPmarker) { 
         serialbuf[bufpos] = inchar; 
         bufpos++; 

       }

       else { 

         serialbuf[bufpos] = 0; 
         bufpos = 0; ![]()

       }


  // this is where we grab the x y HSB values and do whatever we thing is nice <span class="Emoticon Emoticon1"><span>:)</span></span> 
                // send back to processing for debugging 

          int x = atoi(subStr(serialbuf, ":", 1));
          int y = atoi(subStr(serialbuf, ":", 2));
          int R = atoi(subStr(serialbuf, ":", 3));
          int G = atoi(subStr(serialbuf, ":", 4));
          int B = atoi(subStr(serialbuf, ":", 5));
          int F = atoi(subStr(serialbuf, ":", 6));


          float vR = map(R, 0,255, 0,7);
          float vG = map(G, 0,255, 0,7);
          float vB = map(B, 0,255, 0,7);

           Serial.write(x);
       matrix.drawPixel(x, y, matrix.Color333(vR, vG, vB));


     }

 }


 // this is the function that allows us to easily grab an item from the string by index

 char* subStr (char* input_string, char *separator, int segment_number) {
   char *act, *sub, *ptr;
   static char copy[MAX_STRING_LEN];
   int i;
   strcpy(copy, input_string);
   for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
     sub = strtok_r(act, separator, &ptr);
     if (sub == NULL) break;
   }

  return sub;

 }
Run Code Online (Sandbox Code Playgroud)