# Using opencv with processing

**URL:** https://discourse.processing.org/t/using-opencv-with-processing/14148
**Category:** Libraries
**Created:** [September 23, 2019, 7:31pm UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148 "2019-09-23T19:31:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![maddyna](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@maddyna](https://discourse.processing.org/u/maddyna)
#### Post date: [September 23, 2019, 7:31pm UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/1 "2019-09-23T19:31:23Z")

</div>

I intend to use opencv with processing for face detection  
while the examples are explicit in how to do face detection on a static image when i am trying to apply that using video from a webcam the method is not working  
is thr a tutorial fr that  
this hw i tried without much luck-

```auto
import processing.video.*;

import gab.opencv.*;
import java.awt.Rectangle;

OpenCV opencv;
Rectangle[] faces;
Capture video;

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

void draw() {
  if(video.available()){
   video.read(); 
   image(opencv.getInput(), 0, 0);
  }
  noFill();
  stroke(0, 255, 0);
  strokeWeight(10);
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
}

```

please help  
thanks

---

<div class="post-metadata">

### Author: ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)
#### Post date: [September 23, 2019, 9:47pm UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/2 "2019-09-23T21:47:19Z")

</div>

If the video doesn’t need to be live you can use ffmpeg to split the video into frames

---

<div class="post-metadata">

### Author: ![maddyna](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@maddyna](https://discourse.processing.org/u/maddyna)
#### Post date: [September 24, 2019, 2:24am UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/3 "2019-09-24T02:24:39Z")

</div>

But I want the video to be live

---

<div class="post-metadata">

### Author: ![benja](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@benja](https://discourse.processing.org/u/benja)
#### Post date: [September 24, 2019, 10:22am UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/4 "2019-09-24T10:22:50Z")

</div>

Hey,

1. Please try to format your code properly when posting, otherwise it’s hard to read. (Preformatted Text “\</\>”-icon on the top).

2. I think the opencv-lib is image based and doesn’t handle videos by default. But you can pass the frames of your webcam to opencv with the OPENCV.loadImage() method. Here is an adapted example:

```auto
import processing.video.*;
import gab.opencv.*;
import java.awt.Rectangle;

Capture video;
OpenCV opencv;
Rectangle[] faces;

void setup() {
  size(320, 180);
  video = new Capture(this, width, height);
  video.start();  

  opencv = new OpenCV(this, video.width, video.height);  
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}

void draw() {
  opencv.loadImage(video);
  faces = opencv.detect();

  image(opencv.getInput(), 0, 0);

  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
}

void captureEvent(Capture c) {
  c.read();
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [September 24, 2019, 12:33pm UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/5 "2019-09-24T12:33:24Z")

</div>

Lotsa old forum threads about OpenCV::**detect()**: 👼

- [Forum.Processing.org/two/discussions/tagged?Tag=detect()](http://Forum.Processing.org/two/discussions/tagged?Tag=detect%28%29)

---

<div class="post-metadata">

### Author: ![maddyna](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@maddyna](https://discourse.processing.org/u/maddyna)
#### Post date: [September 25, 2019, 4:46am UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/6 "2019-09-25T04:46:32Z")

</div>

Hi  
for computer vision and face detection using the webcam what library should i use in processing.  
My programming skills are pretty limited so would like to know of a library that is well documented with tutorial.

---

<div class="post-metadata">

### Author: ![benja](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@benja](https://discourse.processing.org/u/benja)
#### Post date: [September 25, 2019, 9:00am UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/7 "2019-09-25T09:00:45Z")

</div>

Not sure about tutorials. But the opencv-library comes with some examples, which might serve as a starting point: [https://github.com/atduskgreg/opencv-processing](https://github.com/atduskgreg/opencv-processing)

---

<div class="post-metadata">

### Author: ![adam9233](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@adam9233](https://discourse.processing.org/u/adam9233)
#### Post date: [August 14, 2020, 5:43am UTC](https://discourse.processing.org/t/using-opencv-with-processing/14148/8 "2020-08-14T05:43:33Z")

</div>

/_You can may to use LiveCamTest, one of OpenCV libraries, i did use this with a webcam in real-time video, this detect one or two faces at the same time, I hope it helps you!  
 /  
import gab.opencv.;  
import processing.video.;  
import java.awt._;

Capture video;  
OpenCV opencv;

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();  
}

void draw() {  
scale(2);  
opencv.loadImage(video);

image(video, 0, 0 );

noFill();  
stroke(#FF0080);  
strokeWeight(3);  
Rectangle[] faces = opencv.detect();  
println(faces.length);

for (int i = 0; i \< faces.length; i++) {  
println(faces[i].x + “,” + faces[i].y);  
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);  
}  
}

void captureEvent(Capture c) {  
c.read();  
}
