# Unable to use camera functions

**URL:** https://discourse.processing.org/t/unable-to-use-camera-functions/18355
**Category:** Libraries
**Created:** [March 3, 2020, 6:32pm UTC](https://discourse.processing.org/t/unable-to-use-camera-functions/18355 "2020-03-03T18:32:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ghy](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@ghy](https://discourse.processing.org/u/ghy)
#### Post date: [March 3, 2020, 6:32pm UTC](https://discourse.processing.org/t/unable-to-use-camera-functions/18355/1 "2020-03-03T18:32:00Z")

</div>

/\*\*

- [https://github.com/fiebrink1/wekinator\_examples/blob/master/inputs/VideoInput/SimpleVideoInputWithProcessing\_100Inputs/SimpleVideoInputWithProcessing\_100Inputs.pde](https://github.com/fiebrink1/wekinator_examples/blob/master/inputs/VideoInput/SimpleVideoInputWithProcessing_100Inputs/SimpleVideoInputWithProcessing_100Inputs.pde)
- REALLY simple processing sketch for using webcam input
- This sends 100 input values to port 6448 using message /wek/inputs  
\*\*/

import processing.video._;  
import oscP5._;  
import netP5.\*;

int numPixelsOrig;  
int numPixels;  
boolean first = true;

int boxWidth = 64;  
int boxHeight = 48;

int numHoriz = 640/boxWidth;  
int numVert = 480/boxHeight;

color[] downPix = new color[numHoriz \* numVert];

Capture video;

OscP5 oscP5;  
NetAddress dest;

void setup() {  
// colorMode(HSB);  
size(640, 480, P2D);

String[] cameras = Capture.list();

if (cameras == null) {  
println(“Failed to retrieve the list of available cameras, will try the default…”);  
video = new Capture(this, 640, 480);  
} if (cameras.length == 0) {  
println(“There are no cameras available for capture.”);  
exit();  
} else {  
/\* println(“Available cameras:”);  
for (int i = 0; i \< cameras.length; i++) {  
println(cameras[i]);  
} \*/

video = new Capture(this, 640, 480);

```
// Start capturing the images from the camera
video.start();

numPixelsOrig = video.width * video.height;
loadPixels();
noStroke();

```

}

/\* start oscP5, listening for incoming messages at port 12000 \*/  
oscP5 = new OscP5(this,9000);  
dest = new NetAddress(“127.0.0.1”,6448);

}

void draw() {

if (video.available() == true) {  
video.read();

```
video.loadPixels(); // Make the pixels of video available
/*for (int i = 0; i < numPixels; i++) {
  int x = i % video.width;
  int y = i / video.width;
  float xscl = (float) width / (float) video.width;
  float yscl = (float) height / (float) video.height;
  
  float gradient = diff(i, -1) + diff(i, +1) + diff(i, -video.width) + diff(i, video.width);
  fill(color(gradient, gradient, gradient));
  rect(x * xscl, y * yscl, xscl, yscl);
} */

```

int boxNum = 0;  
int tot = boxWidth\*boxHeight;  
for (int x = 0; x \< 640; x += boxWidth) {  
for (int y = 0; y \< 480; y += boxHeight) {  
float red = 0, green = 0, blue = 0;

```
    for (int i = 0; i < boxWidth; i++) {
       for (int j = 0; j < boxHeight; j++) {
          int index = (x + i) + (y + j) * 640;
          red += red(video.pixels[index]);
          green += green(video.pixels[index]);
          blue += blue(video.pixels[index]);
       } 
    }
   downPix[boxNum] = color(red/tot, green/tot, blue/tot);
  // downPix[boxNum] = color((float)red/tot, (float)green/tot, (float)blue/tot);
   fill(downPix[boxNum]);
   
   int index = x + 640*y;
   red += red(video.pixels[index]);
   green += green(video.pixels[index]);
   blue += blue(video.pixels[index]);
  // fill (color(red, green, blue));
   rect(x, y, boxWidth, boxHeight);
   boxNum++;
  /* if (first) {
     println(boxNum);
   } */
 } 

```

}  
if(frameCount % 2 == 0)  
sendOsc(downPix);

}  
first = false;  
fill(0);  
text(“Sending 100 inputs to port 6448 using message /wek/inputs”, 10, 10);

}

float diff(int p, int off) {  
if(p + off \< 0 || p + off \>= numPixels)  
return 0;  
return red(video.pixels[p+off]) - red(video.pixels[p]) +  
green(video.pixels[p+off]) - green(video.pixels[p]) +  
blue(video.pixels[p+off]) - blue(video.pixels[p]);  
}

void sendOsc(int[] px) {  
OscMessage msg = new OscMessage("/wek/inputs");  
// msg.add(px);  
for (int i = 0; i \< px.length; i++) {  
msg.add(float(px[i]));  
}  
oscP5.send(msg, dest);  
}

 ![截屏2020-03-03下午6.54.53](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5cc191388697388b3e2aec45125719fb3d81d4e6.png)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [March 3, 2020, 6:33pm UTC](https://discourse.processing.org/t/unable-to-use-camera-functions/18355/2 "2020-03-03T18:33:20Z")

</div>

Welcome to the forum!

When you post, please help us help you by [formatting your code](https://discourse.processing.org/faq#format-your-code).
