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 › Scrolling through images
Pages: 1 2 
Scrolling through images (Read 5268 times)
Re: Scrolling through images
Reply #15 - Jan 25th, 2010, 2:44am
 
Hi again, got myself into a little pickle with the loading of images again! As the images are from webcams, I want to constantly reload the image to have a steady stream of pictures. I've tried to change the code, but I'm getting the error: "Cannot convert from PImage to PImage[]" on the line: "  images = loadImage(feed[picture]);"

Here is my code:
Code:

import processing.video.*; // import theProcessing video library
import processing.serial.*; // import the Processing serial library
Serial Arduino; // The serial port
Capture webCam; // The webcam feed
PFont font;

float xpos, ypos, onoff, x; // Starting position of the webcam feed
int picture, scrolltext;
float size = 700.0;

String[] feed = new String[] {
"http://www.adelaidecitycouncil.com/netcatapps/webcam/images/centralMkt.jpg", //Adelaide Market
"http://www.cbc.ca/bc/webcam/images/webcam.jpg", // Vancouver Building Site
"http://www.adelaidecitycouncil.com/netcatapps/webcam/images/rundleEast.jpg", //Rundle Mall East & Lantern
"http://www.draperbee.com/webcam/pic/image.jpg?1262444320177", //beehive
"http://www.adelaidecitycouncil.com//NetcatApps/webcam/images/bellsth.jpg" //Adelaide Bell Street South
};

String[] description = new String[]{
"Adelaide 1 Observer ONLINE",
"Vancouver Observer ONLINE",
"Adelaide 2 Observer ONLINE",
"Colony Observer ONLINE",
"Adelaide 3 Observer ONLINE",
};
PImage[] images;

void setup() {
size(678,510,P3D);

font = loadFont("01_Digit-23.vlw");
textFont(font, 23);
// List all the available serial ports & cameras
//println(Serial.list());
//println(Capture.list());

webCam = new Capture(this, width/2, height/2, "USB PC Camera-WDM", 30);
Arduino = new Serial(this, Serial.list()[1], 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
Arduino.bufferUntil('\n');

images = loadImage(feed[picture]);
}


void draw() {
background(0);

if (webCam.available()){
webCam.read();
}
if (onoff == 1){
image(webCam, xpos, ypos);
translate(-x, 0, 0);
text("Camera One Online", 0, 494);
}
else{
loadImage(feed[picture]);
image(images[picture], xpos, ypos, width/2, height/2);
translate(-x, 0, 0);
text(description[picture], 0, 494);
}

x = x +3;
if (x > width + size){
x = -size;
}

}

void serialEvent(Serial Arduino) {
// read the serial buffer:
String myString = Arduino.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {

myString = trim(myString);

// split the string at the commas
// and convert the sections into integers:
int sensor[] = int(split(myString, ','));

// print out the values you got:
for (int sensorNum = 0; sensorNum < sensor.length; sensorNum++) {
print("sensor " + sensorNum + ": " + sensor[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensor.length > 1) {
xpos = map(sensor[0], 0, 1023, 0, width/2);
ypos = map(sensor[1], 0, 1023, 0, (height/2 - 50));
picture = (int)map(sensor[2], 0, 1023, 0, images.length - 1);
onoff = map(sensor[3], 0, 1, 0, 1);
scrolltext = (int)map(sensor[4], 0, 1023, 0, description.length - 1);
}
}
}


I can't see what I've done wrong!
Re: Scrolling through images
Reply #16 - Jan 25th, 2010, 4:46am
 
Without having any clue what this program is about, your problem is cause by

PImage[] images;

As in, "images" is an array of images, not a single image.

loadImage() returns a single PImage object, not an array of PImage objects.

Maybe you want something like

Code:
PImage[] images;
// In setup()...
images = new PImage[10];
// later
images[0] = loadImage(...);


-spxl
Re: Scrolling through images
Reply #17 - Jan 25th, 2010, 5:32am
 
I'm getting closer - I now get the error: "NullPointerException" in the line:

   image(images[picture], xpos, ypos, width/2, height/2);

The goal of the program is to switch between live web cam feeds when a potentiometer is rotated.

Here's my (slightly modified) code again:

Code:

import processing.video.*; // import theProcessing video library
import processing.serial.*; // import the Processing serial library
Serial Arduino; // The serial port
Capture webCam; // The webcam feed
PFont font;


float xpos, ypos, onoff, x; // Starting position of the webcam feed
int picture, scrolltext;
float size = 700.0;

String[] feed = new String[] {
"http://www.adelaidecitycouncil.com/netcatapps/webcam/images/centralMkt.jpg", //Adelaide Market
"http://www.cbc.ca/bc/webcam/images/webcam.jpg", // Vancouver Building Site
"http://www.adelaidecitycouncil.com/netcatapps/webcam/images/rundleEast.jpg", //Rundle Mall East & Lantern
"http://www.draperbee.com/webcam/pic/image.jpg?1262444320177", //beehive
"http://www.adelaidecitycouncil.com//NetcatApps/webcam/images/bellsth.jpg" //Adelaide Bell Street South
};

String[] description = new String[]{
"Adelaide 1 Observer ONLINE",
"Vancouver Observer ONLINE",
"Adelaide 2 Observer ONLINE",
"Colony Observer ONLINE",
"Adelaide 3 Observer ONLINE",
};
PImage[] images;

void setup() {
size(678,510,P3D);

font = loadFont("01_Digit-23.vlw");
textFont(font, 23);
// List all the available serial ports & cameras
//println(Serial.list());
//println(Capture.list());

webCam = new Capture(this, width/2, height/2, "USB PC Camera-WDM", 30);
Arduino = new Serial(this, Serial.list()[1], 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
Arduino.bufferUntil('\n');
images = new PImage[feed.length];
}

void draw() {
background(0);

if (webCam.available()){
webCam.read();
}
if (onoff == 1){
image(webCam, xpos, ypos);
translate(-x, 0, 0);
text("Camera One Online", 0, 494);
}
else{
images[picture] = loadImage(feed[picture]);
image(images[picture], xpos, ypos, width/2, height/2);
translate(-x, 0, 0);
text(description[picture], 0, 494);
}

x = x +3;
if (x > width + size){
x = -size;
}

}

void serialEvent(Serial Arduino) {
// read the serial buffer:
String myString = Arduino.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {

myString = trim(myString);

// split the string at the commas
// and convert the sections into integers:
int sensor[] = int(split(myString, ','));

// print out the values you got:
for (int sensorNum = 0; sensorNum < sensor.length; sensorNum++) {
print("sensor " + sensorNum + ": " + sensor[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensor.length > 1) {
xpos = map(sensor[0], 0, 1023, 0, width/2);
ypos = map(sensor[1], 0, 1023, 0, (height/2 - 50));
picture = (int)map(sensor[2], 0, 1023, 0, images.length - 1);
onoff = map(sensor[3], 0, 1, 0, 1);
scrolltext = (int)map(sensor[4], 0, 1023, 0, description.length - 1);
}
}
}
Re: Scrolling through images
Reply #18 - Jan 25th, 2010, 11:43am
 
Okay, I've had a look at the code this time, grabbed a copy and played with it in Processing.

I don't have an Arduino board, and the Capture class doesn't work on my machine (with my webcam), so I've disabled that functionality in my version, but left it (mostly) there for you to enable and screw around with yourself - I've made some guesses at to what is going on with the Arduino stuff and left some comments for you in the code.

It is too long for me to post here, so I've uploaded it to OpenProcessing

Link: OpenProcessing: Live feed selector

It seems inappropriate for you to change a couple of lines then repost a big wad of code each time you encounter a basic problem, and you don't seem to have a good explanation of what it is you are trying to do. From examining your code there doesn't appear to be any reason to have an array of images, since every time draw() is called you call loadImage() to get the latest image for the current feed... and while you are waiting for that to load, the scrolling and everything else stops.

There is a requestImage() method which will load an image in a separate thread, so in the meantime you can continue with user interaction and so on. There are some complications that arise from this, such as working out when the image is loaded and things like that, and I also noticed that requesting a "new" image too often, at least in the case of the beehive feed, leads to an error; so I decided that the refresh should not be immediate, but to cater for that meant keeping track of the time and other things to remember what each feed is doing...

I've also "integrated" the web feeds and the local camera feed as types of "Feed" objects, and you may or may not like having the local camera feed in the same group as the web feeds, but that is all dependent on what the intention of the controls for the user are.

-spxl

PS: The applet on OpenProcessing doesn't show any feeds; not showing the webcam is intentional, and requestImage() probably fails due to it trying to access another webserver (and the applet isn't signed/trusted).

PPS: try sticking a println(picture); in your code before the image(images[picture], ...); call. My guess is that "picture" is less than zero or larger than the array size. Maybe use floor() instead of int() to convert your serial values to integers.
Re: Scrolling through images
Reply #19 - Jan 26th, 2010, 7:35am
 
wow, that's great, thank you! I've tried it without Arduino & it works great, but when I change the USE_ARDUINO to true, the program rejects it & says: "error, disabling serialEvent() for //./COM6"

A lot of the new code is new to me & completely over my head, so I don't know if I have to change something else in the code?

thanks again!
Re: Scrolling through images
Reply #20 - Jan 26th, 2010, 12:00pm
 
I'm sorry, I can't help you with the serial/arduino stuff, because I don't have any such device.

You should try patching in your original serial code for where I've made changes, and see if that works... from there you can look at my "suggestions" to see what makes sense to change from your original code. You also need to pay attention to where Processing indicates the problem is - it usually moves the cursor to the line where the error occurred.

Guessing at what might be wrong:
Arduino = new Serial(this, Serial.list()[ARDUINO_SERIAL_INDEX], 9600);

Your code just had a [1] for the index; have a look at the output from println(Serial.list()) as maybe the port index has changed (or your device isn't connected properly or something).

I assume you're just using the regular Processing editor ("PDE") and hence not using a debugger, so recommend you use print() / println() in places to check values that might be causing problems (especially any value you intend to use as an array index).

The code is a bit spaghetti-like, and the "inner classes" reference a lot of values in the main object - I made a new version with .java files for the classes and it seemed like a lot of hassle! - so I can't recommend the code you've seen as good coding practice by any means.

The "abstract class Feed" is just a way to create a generalised feed type where all specialties of the type have the things specified for "Feed" (you can have a "Feed" reference to any derived types, but you can't create a "Feed" object).

If you don't know anything about classes, have a read of the Object Oriented Programing section in the Leaning Processing stuff on this site (see the "Learning" link in the forum header navigation).

-spxl
Re: Scrolling through images
Reply #21 - Jan 28th, 2010, 3:31am
 
Hi, got it working! Thanks a lot for your help!
Re: Scrolling through images
Reply #22 - Feb 16th, 2010, 8:54am
 
Hi, I've got a new problem now - I want to turn on & off the display with a simple push button. The whole idea is that the feeds aren't visible until someone inserts a coin (which pushes the button) & the feeds are visible for ten minutes. How would I do achieve this?
Re: Scrolling through images
Reply #23 - Feb 16th, 2010, 10:46am
 
There are several steps / parts involved in what you suggest. Which parts do you know how to do, and which parts don't you know how to do?

  • Display disabled until coin inserted
  • Detect insert coin
  • Enable display on/after coin inserted event
  • Determine length of time since coin inserted event
  • Disable display after desired time period (10 minutes)


Other things to consider:
  • What happens if a coin inserted event occurs within 10 minutes of the previous coin inserted event?
  • ?


Hint: millis() returns the number of milliseconds elapsed since the sketch started (or something near the sketch start - I'm not exactly sure when the clock starts!).

Code:
/**
* spxlInsertCoin
* 2010-02-17 by subpixel
* http://subpixels.com
*/

// Time to end (in milliseconds, as reported by millis())
// Starting with 0 means we will begin in the "ended" state
long endTime = 0;

void keyPressed()
{
 // Insert coin: end 30 seconds from NOW.
 if (key == 'c')
   endTime = millis() + 30 * 1000;
 
 // Add coin: add 10 seconds to existing time left.
 // This could mean 10 seconds from now if endTime
 // was in the past (or still the initial 0 value).
 if (key == 'a')
   endTime = (int)max(endTime, millis()) + 10 * 1000;
}

void draw()
{
 long timeNow = millis();
 
 if (timeNow < endTime)
 {
   println("Time left: " +
           ((endTime - timeNow)*0.001) +
           " seconds");
 }
 else
 {
   println("[c] Insert Coin! - [a] Add Coin!");
 }
}


-spxl
Re: Scrolling through images
Reply #24 - Feb 17th, 2010, 3:40am
 
Hi, I modified & inserted the code into the main program & it works great! I don't need any new coins to be added while the feeds are displayed, just to simply insert a coin, view the feeds until the time runs out & repeat. I just need to make the coin mechanism now...

Thanks for your help, if you want to see my project it's on my blog:

http://findplaymaketalk.blogspot.com/
Pages: 1 2