Hi,
I'm doing a quick prototyping to check if Processing can do a motion detection via frame differencing and use the changing value to determine a movie playback speed. I stitched the FrameDifferencing and Speed examples from GSVideo example in Processing 2.0a8. The code is as follows:
- /**
- * GSVideo movie speed example.
- *
- * Use the Movie.speed() method to change
- * the playback speed.
- *
- */
- import processing.video.*;
- Movie movie;
- int numPixels;
- int[] previousFrame;
- Capture video;
- int movementSum;
- int newMovementSum;
- void setup() {
- size(320, 240);
- background(0);
- movie = new Movie(this, "balloon.ogg");
- movie.loop();
- PFont font = loadFont("DejaVuSans-24.vlw");
- textFont(font, 24);
- video = new Capture(this, width, height);
- video.start();
- numPixels = video.width * video.height;
- // Create an array to store the previously captured frame
- previousFrame = new int[numPixels];
- loadPixels();
- newMovementSum = 1;
- }
- void movieEvent(Movie movie) {
- movie.read();
- }
- void draw() {
- if (video.available()) {
- // When using video to manipulate the screen, use video.available() and
- // video.read() inside the draw() method so that it's safe to draw to the screen
- video.read(); // Read the new frame from the camera
- video.loadPixels(); // Make its pixels[] array available
- movementSum = 0; // Amount of movement in the frame
- for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
- color currColor = video.pixels[i];
- color prevColor = previousFrame[i];
- // Extract the red, green, and blue components from current pixel
- int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
- int currG = (currColor >> 8) & 0xFF;
- int currB = currColor & 0xFF;
- // Extract red, green, and blue components from previous pixel
- int prevR = (prevColor >> 16) & 0xFF;
- int prevG = (prevColor >> 8) & 0xFF;
- int prevB = prevColor & 0xFF;
- // Compute the difference of the red, green, and blue values
- int diffR = abs(currR - prevR);
- int diffG = abs(currG - prevG);
- int diffB = abs(currB - prevB);
- // Add these differences to the running tally
- movementSum += diffR + diffG + diffB;
- //newMovementSum = movementSum/1000;
- // Render the difference image to the screen
- pixels[i] = color(diffR, diffG, diffB);
- // The following line is much faster, but more confusing to read
- //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
- // Save the current color into the 'previous' buffer
- previousFrame[i] = currColor;
- }
- // To prevent flicker from frames that are all black (no movement),
- // only update the screen if the image has changed.
- if (movementSum > 0) {
- //updatePixels();
- newMovementSum = movementSum/1000;
- println(newMovementSum); // Print the total amount of movement to the console
- }
- }
- image(movie, 0, 0, width, height);
- //float newSpeed = map(mouseX, 0, width, 0.1, 2);
- float newSpeed = newMovementSum/1000;
- movie.speed(newSpeed);
- fill(240, 20, 30);
- text(nfc(newSpeed, 2) + "X", width - 80, 30);
- }
1. I keep on getting this error on the terminal line:
(Processing core video:1012): GStreamer-CRITICAL **: gst_element_send_event: assertion `event != NULL' failed
I have no idea what's that error about.
2. The video takes 3 seconds after reaching the end to loop again.
3. There's this black screen that keeps on showing every time I rapidly change the playback speed. This error also occurs in the Speed example.
Anybody has any solution for this? Or tell me if what I want is not feasible with Processing at this moment, so I can change to different tool.
Thank you :)
1