I'm new to processing but have made a couple of sketches. I still dont fully understand how to form the code so please excuse me.
I am trying to make a simple sketch that detects presence of a viewer by detecting their face using openCV lib. When a face is detected it changes the image displayed (from image_1 to image_2). I am trying to speed the whole thing up and was wondering if using threads to split the tasks would work?
1 thread for detecting the face and one for changing the image. I am having trouble getting the threads to work and i'm not sure this is the right way to go about speeding up the program. Here is the code for the simpler one that works that i havent tried to thread.
I'm very new to processing so apologies if this is covered else where.
I'm working on a image capture processing sketch, the idea is that the image is captured from a webcam and then layered with the previous layer, it also sends out a trigger to a PureData audio patch that records and layers the sound.
The problem is that tint() is applying everytime the sketch reloads the composite with a new image. Meaning that the previous images are becoming too faint too fast. What we want to happen is to retain more of the older images and add the new changes (a person in shot) to them. I have tried various blending modes and pixel changes to get the effect but i was wondering if anyone has a better idea of how to do it. One idea is to detect a pixel change within the image a turn these to black but i have no idea how to implement this once we have detected the pixel change. I don't know if i have explained this very well but here is the code. Some of the variables are now redundant.
Any help or ideas of how to get a better image would be amazing.
// Variables
int counter = 0;
int displaycounter = 0;
int framerate = 25;
PImage jpgimg;
Capture myCapture;//somehow setting up camera
int saveCount = 0;
int imgcounter = 0;
// Image manipulation variables
int num_images_to_use = 20;
PImage[] img_array = new PImage[num_images_to_use];
int start_img = 0;
long time_of_last_shot = -5L;
long time_lapse = 500; // millis
String latest_filename = null;
public static final int port = 11000;
public static final String client_host = "127.0.0.1";
OpenCV opencv;
OscP5 oscP5;
private NetAddress client_destination = new NetAddress(client_host,port);
//----------------------------------------------------------
void setup(){
myCapture = new Capture(this, 800, 600);
size(myCapture.width,myCapture.height);
frameRate(framerate);
createOutput("imgs/tmp.txt");
{
size(800, 500);// to open camera settings and add cameras
// myCapture = new Capture(this, 800, 600);
//myCapture.settings();
// Initialise oscP5 variable
initOSCP5();
}
}
void initOSCP5() {
/* create a new osc properties object */
OscProperties properties = new OscProperties();
/* set a default NetAddress. sending osc messages with no NetAddress parameter
* in oscP5.send() will be sent to the default NetAddress.
*/
properties.setRemoteAddress("127.0.0.1",11000);
/* the port number you are listening for incoming osc packets. */
properties.setListeningPort(11000);
/* Send Receive Same Port is an option where the sending and receiving port are the same.
* this is sometimes necessary for example when sending osc packets to supercolider server.
* while both port numbers are the same, the receiver can simply send an osc packet back to
* the host and port the message came from.
*/
properties.setSRSP(OscProperties.ON);
/* set the datagram byte buffer size. this can be useful when you send/receive
* huge amounts of data, but keep in mind, that UDP is limited to 64k
*/
properties.setDatagramSize(1024);
/* initialize oscP5 with our osc properties */
oscP5 = new OscP5(this,properties);
/* print your osc properties */
println(properties.toString());
}
// If no images have been captured, then show what is in view of the camera...
if(imgcounter == 0 && myCapture.available()) {
myCapture.read();
image(myCapture, 0,0);
}
else
drawImage();
void keyPressed() {
// If the space key (' ') was pressed, then capture the image and increment the counter
// Also, process the image using the 'process_image' function
if(key == ' ') {
captureAndProcess();
}
}
void captureAndProcess() {
if(myCapture.available()) {
println("capture " + imgcounter + "OscMessage myMessage");
myCapture.read();
image(myCapture, 0, 0);
tint(255, 100);// this is the alpha that is applied to the composite
String filename = "imgs/frame_" + imgcounter + ".jpg";
saveFrame(filename);
imgcounter++;
// Trigger audio capture in Pure Data
OscMessage myMessage = new OscMessage("/trigger");
myMessage.add("/trigger");
oscP5.send(myMessage, client_destination);
void process_image() {
println("Processing new image...");
// Set the image to start with..
start_img = imgcounter - num_images_to_use;
if(start_img<0) start_img = 0;
// Load images into memory
println("Loading "+imgcounter+"images...");
size(800, 600);
for (int i = 0; i < num_images_to_use && i < imgcounter; i = i+1)
img_array[i] = loadImage("imgs/frame_"+(i+start_img)+".jpg"); // Load an image into the array
println("Loading images...DONE");
// Draw images
println("Drawing "+imgcounter+" images...");
for (int i = 0; i < num_images_to_use && i < imgcounter; i = i+1) {
image(img_array[i], 0, 0);
}
println("Drawing images...DONE");
// Save the composite
latest_filename = "composites/tomisawesome_" + imgcounter + ".jpg";
saveFrame(latest_filename);