We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Re: playing slideshow while taking pictures
Pages: 1 2 
Re: playing slideshow while taking pictures (Read 7119 times)
Re: playing slideshow while taking pictures
Jun 9th, 2010, 5:00am
 
The problem might be in the line:
for (int i = fileCount; i < loadFilenames.length; i++)
I can be wrong, but I think Java offers no guarantee on the order of the list of files. It can be alphabetical, chronological, random... Might even depend on the underlying system.
It is more reliable to put the file names (or paths) in a HashSet: it allows to quickly check if a name is already loaded (if it is already in the HashSet).

Side note: You don't have to use dataPath with an absolute path (it just returns this path...).
Re: playing slideshow while taking pictures
Reply #1 - Jun 9th, 2010, 5:20am
 
He Philho,

I figured out I needed to export the program  Grin Undecided
It runs smoothly now. I playes it from the processing program itself.

Sorry!!
Re: playing slideshow while taking pictures
Reply #2 - Jun 9th, 2010, 5:26am
 
oh cheered to quickly :S I'm going to have a look at your answer philho!

Do you know where I can find something about a hashset?

Re: playing slideshow while taking pictures
Reply #3 - Jun 9th, 2010, 6:18am
 
Where is the head of this topic

Anyway, see HashSet reference. Can be seen as a kind of ArrayList with unique entries (no duplicates) and fast search.
Re: playing slideshow while taking pictures
Reply #4 - Jun 9th, 2010, 6:30am
 
Hello,

I got a very good response (help) on my previous posts so I thought I'll try it again.
I've two programs running at the same time. One is taking a picture when a face is recognized and saves the files to a folder. The other one is playing a slideshow and checks if new pictures are been made by the other program. The program which takes the photo's works properly accept the follow error once in a while.
sun.awt.image.PNGImageDecoder$PNGException: bogus length: -1
     at sun.awt.image.PNGImageDecoder.getChunk(PNGImageDecoder.java:661)
     at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:674)
     at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:221)
     at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
     at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
     at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
The file /Users/menno/Documents/Processing/taking_pics/faces2010-6-9 12.25.6.png contains bad image data, or may not be an image.
sun.awt.image.PNGImageDecoder$PNGException: bogus length: -1
     at sun.awt.image.PNGImageDecoder.getChunk(PNGImageDecoder.java:661)
     at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:674)
     at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:221)
     at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
     at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
     at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

The other program (the slideshow) don't work properly. If I start the program he read out the pictures from a folder. If there are 5 pictures in the folder he reads them out properly. When I start the 'taking pictures' program an it start making new pictures the slideshow recognizes new pictures but don't display them correctly. Or he loades the photos in for a small part or not at all.
When I restart the 'slideshow' program he does read the new photos correctly.

Anyone ideas?

Here's the code from the slideshow (rewritten by amnon) If some want to have a look at the 'taking pictures' program let me know.

Code:
//***************************************************************//
// Dynamic Image Slideshow
// by amnonP5 <http://amnonp5.wordpress.com>
//
// space = Add slide
//     z = Turn ON / OFF the autoplay function
//     x = Delete last slide
//     c = Clear all slides
//
// Comment IN / OUT: print added image message to console
// Comment IN / OUT: auto-reset slideshow at last slide
//
//***************************************************************//

import processing.opengl.*;

ArrayList photos = new ArrayList();
String[] loadFilenames;
PImage[] loadPhotos;
int counter, fileCount;
boolean autoPlay = true; // by default autoplay is OFF

void setup() {
 size(1280,720,OPENGL);
//  size(screen.width,screen.height,OPENGL); // full screen mode
 smooth();
 loadPhotos = new PImage[100]; // maximum of 10.000 images
 loadFilenames();
 loadPhotos(); // all existing images are loaded in setup
 fileCount = loadFilenames.length;
}

void draw() {
 background(51);
 checkNew(); // checks for new images in the data directory every draw cycle
 if(autoPlay) {autoPlay();}
 for (int i = 0; i < photos.size(); i++) {
   Photo s = (Photo) photos.get(i);
   s.display();
   s.move();
   
 }
if (photos.size() == fileCount) {photos.clear(); loadPhotos();} // reset slideshow when it reaches the last slide
}

void loadFilenames() {
 java.io.File folder = new java.io.File(dataPath("/Users/menno/Documents/Processing/taking_pics/")); // reads files from data folder
 java.io.FilenameFilter imgFilter = new java.io.FilenameFilter() {boolean accept(File dir, String name) {return name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png");} };
 loadFilenames = folder.list(imgFilter);
}

void loadPhotos() {
 for (int i = fileCount; i < loadFilenames.length; i++) { // only load new images
   loadPhotos[i] = loadImage("/Users/menno/Documents/Processing/taking_pics/" + loadFilenames[i]);

// println("Photo added: " + loadFilenames[i]); // print added image message to console
 }
}

void checkNew() {
 loadFilenames();
 if (loadFilenames.length > fileCount) {loadPhotos(); fileCount = loadFilenames.length;} // only call loadPhotos if there are new images
}

void autoPlay() {
 counter++;
 if (counter >= 60 && photos.size() < loadFilenames.length) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30))); counter=0;}
}

void keyPressed() {
 if (key == 'z') {if(autoPlay) {autoPlay = false;} else {autoPlay = true;}}
 if (key == ' ') {if (photos.size() < loadFilenames.length) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30)));}}
 if (key == 'x') {if (photos.size() > 0) {photos.remove(photos.size()-1);}}
 if (key == 'c') {photos.clear();}
}

class Photo {
 PImage photo;
 float targetXpos;
 float targetYpos;
 float rotation;
 float xpos;
 float ypos;
 float easing;

 Photo(float targetXposTemp, float targetYposTemp, float rotationTemp) {
   photo = loadPhotos[photos.size()];
   xpos = width/2;
   ypos = -130;
   targetXpos = targetXposTemp;
   targetYpos = targetYposTemp;
   rotation = rotationTemp;
   easing = 0.1;
 }

 void display() {
   rectMode(CENTER);
   imageMode(CENTER);
   noStroke();

   pushMatrix();

   translate(width/2,height/2);
   rotate(radians(rotation));
   translate(-width/2,-height/2);

   translate(xpos,ypos);

   fill(0,0,0,50);
   rect(4,4,330,250);
   fill(255,255,255);
   rect(0,0,330,250);
   image(photo,0,0,320,240);

   popMatrix();
 }

 void move() {
   if (dist(xpos,ypos,targetXpos,targetYpos) > 1) { // only move if the destination has not been reached
   xpos = (1-easing) * xpos + (easing) * targetXpos;
   ypos = (1-easing) * ypos + (easing) * targetYpos;
   }
 }
}

Re: playing slideshow while taking pictures
Reply #5 - Jun 9th, 2010, 6:38am
 
I removed it because I thought it was fixed.
So I thought exporting would help but I keep having the same problems.

I uploaded the whole code so someone would be so kind to copy it all and reply/ paste the right version since I'm not a programmer Cheesy Embarrassed

I hope Amnon sees it soon.


Re: playing slideshow while taking pictures
Reply #6 - Jun 9th, 2010, 6:52am
 
arie willem wrote on Jun 9th, 2010, 6:38am:
I removed it because I thought it was fixed.

Bad idea, the answered topics are still useful to people searching the forum... Smiley Plus as you see you can delete only your own messages, not whole threads.
Thanks for restoring the original question.
Re: playing slideshow while taking pictures
Reply #7 - Jun 9th, 2010, 7:01am
 
Code:
// Global, below definition of loadFilenames for example
HashSet loadedFileNames = new HashSet();

// Replace
void loadPhotos() {
for (int i = 0; i < loadFilenames.length; i++) {
if (!loadedFileNames.contains(loadFilenames[i])) {
loadPhotos[i] = loadImage("/Users/menno/Documents/Processing/taking_pics/" + loadFilenames[i]);
loadedFileNames.add(loadFilenames[i]);
}
}
}

Untested, might need to reset with photos (or not?)
Re: playing slideshow while taking pictures
Reply #8 - Jun 9th, 2010, 2:34pm
 
What do you meen by:might need to reset with photos?

It still does the same and the slide show script shows the following errors. I uploaded a screenshot.

http://www.idablo.nl/werk/program.png

The weird thing is that he does make a complete picture in the folder but in the slideshow programs it shows half of the image.
And he does read (count) the number of images.

Maybe the following error tells you something.

java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]

The file /Users/menno/Documents/Processing/taking_pics/faces2010-6-9 23.29.49.jpg contains bad image data, or may not be an image.

java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]

Premature end of JPEG file
Premature end of JPEG file

java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]
java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]
java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]

Premature end of JPEG file

java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]

Premature end of JPEG file
Exception in thread "Animation Thread" java.lang.NullPointerException
     at processing.core.PGraphics.image(PGraphics.java:2197)
     at processing.core.PApplet.image(PApplet.java:7286)
     at slideshow$Photo.display(slideshow.java:134)
     at slideshow.draw(slideshow.java:58)
     at processing.core.PApplet.handleDraw(PApplet.java:1425)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:613)

java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]
java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]
java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]
java.awt.Frame[frame0,197,72,320x262,title=taking_pics,normal]
Re: playing slideshow while taking pictures
Reply #9 - Jun 9th, 2010, 2:38pm
 
Either the server writing the image files is faulty, or you attempt to read a file while it is being written.
A possible workaround is to write the file under a temp name/extension, eg. faces2010-6-9 23.29.49.jtemp and when finished, to rename it to .jpg
Re: playing slideshow while taking pictures
Reply #10 - Jun 9th, 2010, 2:41pm
 
That sound like a solution. Did you see the screenshot at http://www.idablo.nl/werk/program.png

I'll try to write it to a temp file. do you know a good refrence?


Re: playing slideshow while taking pictures
Reply #11 - Jun 9th, 2010, 2:45pm
 
File
renameTo() method allows to rename the temp file to the final name/extension.
Re: playing slideshow while taking pictures
Reply #12 - Jun 9th, 2010, 3:06pm
 
Ok I tried to look up something and implement it in the code.

But when I fill in .jtemp as a filename it saving it as .....jtemp.tif

And if I change the oldname file extension to .jtemp.tif it doesn't do anything.
I guess you meant adding this code to the program which makes the pictures. And put it after making a picture.


Code:
  Rectangle[] faces = opencv.detect(1.2,2,OpenCV.HAAR_DO_CANNY_PRUNING,40,40);

 if (pictureJustTaken == false && faces.length >= 1) {
   saveFrame("faces" + year() + "-" + month() + "-"  + day() + " "  + hour() + "."  + minute() + "."  + second() + ".jtemp");
   pictureJustTaken = true;
   
   String oldname=dataPath("faces" + year() + "-" + month() + "-"  + day() + " "  + hour() + "."  + minute() + "."  + second() + ".jtemp.tif");
   File ff=new File(oldname);
   String newname=dataPath("faces" + year() + "-" + month() + "-"  + day() + " "  + hour() + "."  + minute() + "."  + second() + ".jpg");
   ff.renameTo(new File(newname));
   
   startTimer = millis();
   println(frame);
 }
Re: playing slideshow while taking pictures
Reply #13 - Jun 10th, 2010, 1:22am
 
Mmm, you are right, saveFrame() and save() relies on the given extension to choose the file format...
So you have instead to save to a different folder and then move to the scanned folder. Or save with a special prefix (eg. an underscore) and make the file filter in the scanner to skip these files.

Notes: you can use save() instead of saveFrame() since you don't use the special format for file names.
And since you build the oldname, you can just use it in save[Frame]()...

Code:
void loadFilenames() {
File folder = dataFile("")); // reads files from data folder
FilenameFilter imgFilter = new FilenameFilter() {
boolean accept(File dir, String name) {
return !name.startsWith("_") && (name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png"));
}
};
loadFilenames = folder.list(imgFilter);
}
Re: playing slideshow while taking pictures
Reply #14 - Jun 10th, 2010, 2:23am
 
ok these are the two codes now. Do you think this should work?

The first one reads the images and shows it as a slideshow

Code:
//***************************************************************//
// Dynamic Image Slideshow
// by amnonP5 <http://amnonp5.wordpress.com>
//
// space = Add slide
//     z = Turn ON / OFF the autoplay function
//     x = Delete last slide
//     c = Clear all slides
//
// Comment IN / OUT: print added image message to console
// Comment IN / OUT: auto-reset slideshow at last slide
//
//***************************************************************//

import processing.opengl.*;

ArrayList photos = new ArrayList();
String[] loadFilenames;
PImage[] loadPhotos;
int counter, fileCount;
boolean autoPlay = true; // by default autoplay is OFF
HashSet loadedFileNames = new HashSet();

void setup() {
 size(1024,768,OPENGL);
 //size(screen.width,screen.height,OPENGL); // full screen mode
 smooth();
 loadPhotos = new PImage[100]; // maximum of 10.000 images
 loadFilenames();
 loadPhotos(); // all existing images are loaded in setup
 fileCount = loadFilenames.length;
}

void draw() {
 background(51);
 checkNew(); // checks for new images in the data directory every draw cycle
 if(autoPlay) {autoPlay();}
 for (int i = 0; i < photos.size(); i++) {
   Photo s = (Photo) photos.get(i);
   s.display();
   s.move();
   
 }
if (photos.size() == fileCount) {photos.clear(); loadPhotos();} // reset slideshow when it reaches the last slide
}

 void loadFilenames() {
 File folder = dataFile("/Users/menno/Documents/Processing/taking_pics/"); // reads files from data folder
 FilenameFilter imgFilter = new FilenameFilter() {
 boolean accept(File dir, String name) {
 return !name.startsWith(".tif") && (name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".png"));
 }
 };
 loadFilenames = folder.list(imgFilter);
 }

//void loadFilenames() {
//  java.io.File folder = new java.io.File(dataPath("/Users/menno/Documents/Processing/taking_pics2/")); // reads files from data folder
//  java.io.FilenameFilter imgFilter = new java.io.FilenameFilter() {boolean accept(File dir, String name) {return name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".png");} };
//  loadFilenames = folder.list(imgFilter);
 
//}

void loadPhotos() {
for (int i = 0; i < loadFilenames.length; i++) {
 if (!loadedFileNames.contains(loadFilenames[i])) {
  loadPhotos[i] = loadImage("/Users/menno/Documents/Processing/taking_pics/" + loadFilenames[i]);
  loadedFileNames.add(loadFilenames[i]);
 }
}
}

void checkNew() {
 loadFilenames();
 if (loadFilenames.length > fileCount) {loadPhotos(); fileCount = loadFilenames.length;} // only call loadPhotos if there are new images
}


void autoPlay() {
 counter++;
 if (counter >= 60 && photos.size() < loadFilenames.length) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30))); counter=0;}
}

void keyPressed() {
 if (key == 'z') {if(autoPlay) {autoPlay = false;} else {autoPlay = true;}}
 if (key == ' ') {if (photos.size() < loadFilenames.length) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30)));}}
 if (key == 'x') {if (photos.size() > 0) {photos.remove(photos.size()-1);}}
 if (key == 'c') {photos.clear();}
}

class Photo {
 PImage photo;
 float targetXpos;
 float targetYpos;
 float rotation;
 float xpos;
 float ypos;
 float easing;

 Photo(float targetXposTemp, float targetYposTemp, float rotationTemp) {
   photo = loadPhotos[photos.size()];
   xpos = width/2;
   ypos = -130;
   targetXpos = targetXposTemp;
   targetYpos = targetYposTemp;
   rotation = rotationTemp;
   easing = 0.1;
 }

 void display() {
   rectMode(CENTER);
   imageMode(CENTER);
   noStroke();

   pushMatrix();

   translate(width/2,height/2);
   rotate(radians(rotation));
   translate(-width/2,-height/2);

   translate(xpos,ypos);

   fill(0,0,0,50);
   rect(4,4,330,250);
   fill(255,255,255);
   rect(0,0,330,250);
   image(photo,0,0,320,240);

   popMatrix();
 }

 void move() {
   if (dist(xpos,ypos,targetXpos,targetYpos) > 1) { // only move if the destination has not been reached
   xpos = (1-easing) * xpos + (easing) * targetXpos;
   ypos = (1-easing) * ypos + (easing) * targetYpos;
   }
 }
}





And the second one is taking the images!

Code:
import hypermedia.video.*;
import java.awt.Rectangle;

OpenCV opencv;
boolean pictureJustTaken = false;
int startTimer;
int interval = 5; // interval in seconds

void setup() {
 size(320,240);
 opencv = new OpenCV(this);
 opencv.capture(width,height);
 opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT);
}

void draw() {
 opencv.read();
 image(opencv.image(),0,0);

  Rectangle[] faces = opencv.detect(1.2,2,OpenCV.HAAR_DO_CANNY_PRUNING,40,40);

 if (pictureJustTaken == false && faces.length >= 1) {
   save("faces" + year() + "-" + month() + "-"  + day() + " "  + hour() + "."  + minute() + "."  + second() + ".jtemp");
   pictureJustTaken = true;
   
   String oldname=dataPath("/Users/menno/Documents/Processing/taking_pics/");
   File ff=new File(".tif");
   
   startTimer = millis();
   println(frame);
 }

 if (pictureJustTaken) {
   if (millis() - startTimer > (interval * 1000)) {pictureJustTaken = false;}
 }
}

public void stop() {
 opencv.stop();
 super.stop();
}

Pages: 1 2