Showing posts with label Processing. Show all posts
Showing posts with label Processing. Show all posts

Giving Human Form to Audio Visualizers, Version 1

I'll upload a video of this audio visualization when I have some time. In the meantime, try the code out yourself. I'm still tweaking it to figure out what might look best.

Some requirements to run this code:
1. Download OpenCV for Processing from here: https://github.com/atduskgreg/opencv-processing/releases
2. And download and install from here: http://opencv.org/
2.b. Probably need v2.4.5 of opencv from opencv.org, but I was able to get it to work with v2.4.9

I'm also using processing 2.1.2.

And finally, copy this gist into processing:

Source Code: How to Make a Movie in Processing

The following code will allow one to save frames with text in processing.

Press 'r' to begin and stop recording.
Press 'n' to go to the next text.
Press 'p' to go back.

The output will be frames in a folder called 'output.' Then, you'll have to compile these frames in third party software, like QuickTime.


Source Code: Audio Visualizer with Processing and Minim

I was playing around with processing and the minim library without any goal in mind. The end result was a trippy audio visualizer that might work really well at dance clubs. Anyone know any DJs that might be interested?

I've set the visualizer to one song by Sufjan Stevens and another by Radiohead, so you can get a feel for how it responds to music. The code is waiting for microphone input and then turns the spectrum of sound into beautifully symmetric rotating and spinning circles.

I've uploaded the code to github in case anyone is interested in playing with it.







Tutorial: Using YouTube Data API in Processing

I recently used the YouTube Data API in Processing for an art project. The playlist below is the result of creating a physical booth people can enter, which then automatically uploads to YouTube.



I've outlined the necessary steps to make the YouTube Data API work in Processing 2.0b5 or 2.0b6 in Windows 7 64 bit.

A few items you'll have to take of before you begin:
1. Create a YouTube Developer Account.
2. Create a developer key specific to your project. You can do so here.
3. Download the YouTube API jars. You can down the jars at my github repo for this project.
4. Check out the processing files demonstrating how youtube data api connects and uploads videos automagically.
5. Post questions in the comment section if you have any.


The code below is the part that's uploading the video. You'll notice I'm extending the thread class because I wanted the Youtube video to upload on its own without running in the main draw thread.

Source Code: Multiple monitors, applets, and frames from the same processing sketch

A few years ago, I had to do a project that involved two screens running from the same processing sketch. The resulting project was about two participants playing against each other on two separate touch screens, similar to battleship. Figuring out the architecture took some trial and error, so I hope the following code will help anyone interested in doing the same:




Source Code: Multithreading in Processing

Multithreading a piece of software, is useful in processing when you reach the limits of computing within the draw loop. Examples of this could be doing some sort of image analysis from a webcam while also drawing a user interface. When I first started using processing, it took me a while to figure out the architecture for multithreading. So, if anyone's interested, you can see an example below:




Visualizing Volume with Processing JS

Tutorial: Using Xuggler in Processing

It took me a while to figure out, but Xuggler is a powerful audio and video java wrapper of ffmpeg. For a recent art project of mine titled "Lo and Behold, I am become as a God," I had to use Xuggler to sync audio and video in real time. In the processing forums, there's a lot of questions about how to use processing to sync audio and video in real time, but the GSVideo library doesn't capture audio, it only captures video. Another option is GSPipeline, which can sync audio and video, but seems to only work well in Linux - I haven't tried Mac To write a program that can capture audio and video in java within Windows 7, I turned to Xuggler.

The follow steps are what I've done to make Xuggler operate with Processing 2.0b5 or 2.0b6 with Windows 7 64 bit:

Step 1: Download and Install the Xuggler 64 bit Build for Windows. Available here.

Step 2: Download the necessary Xuggler jars, I've zipped them together and can be downloaded from here. I've placed the jars files in the zipped folder within my processing sketch, in a folder called 'code.'

Step 3: You can either download and run my example code from here (jars included), or look at the example code below the video.

Here's an example of some work I did with Xuggler in processing:



My next tutorial will be about how to set up the Youtube data API with processing, to be able to automatically upload these videos to your youtube account.




import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.concurrent.TimeUnit;
import java.awt.*;
import processing.video.*;
import javax.sound.sampled.*;

IMediaWriter imw;
IStreamCoder isc;
BufferedImage bgr;
int vidRate = 30;
long sTime;
long fTime;

Capture cam;

final int audioStreamIndex = 1;
final int audioStreamId = 1;
final int channelCount = 2;
int sampleRate;

AudioFormat audioFormat;
AudioInputStream audioInputStream;
TargetDataLine aline;
AudioFormat targetType;

byte[] audioBuf;
int audionumber;

int widthCapture=640;
int heightCapture=480;

boolean recording;

void setup() {
  frameRate(30);
  size(widthCapture, heightCapture, JAVA2D);
  cam = new Capture(this, widthCapture, heightCapture);
  cam.start();
  avSetup();
}

void draw() {
  if (cam.available()) {
    cam.read();
    image(cam.get(), 0, 0);

    if (recording) {
      if (imw.isOpen()) {
        //video recording stuff
        long cTime = System.nanoTime()-fTime;
        if (cTime >= (double)1000/vidRate) {
          bgr.getGraphics().drawImage(cam.getImage(), 0, 0,
          new ImageObserver() {
            public boolean imageUpdate(Image i, int a, int b, int c, int d, int e) {
              return true;
            }
          }
          );
          imw.encodeVideo(0, bgr, System.nanoTime()-sTime, TimeUnit.NANOSECONDS);
          //audio recording stuff
          if (aline.available() == 88200) {
            int nBytesRead = aline.read(audioBuf, 0, aline.available());//audioBuf.length);//aline.available());
            if (nBytesRead>0) {
              IBuffer iBuf = IBuffer.make(null, audioBuf, 0, nBytesRead);
              IAudioSamples smp = IAudioSamples.make(iBuf, channelCount, IAudioSamples.Format.FMT_S16);

              if (smp!=null) {
                long numSample = nBytesRead/smp.getSampleSize();
                smp.setComplete(true, numSample, (int) audioFormat.getSampleRate(), audioFormat.getChannels(), IAudioSamples.Format.FMT_S16, (System.nanoTime()-sTime) / 1000);
                smp.put(audioBuf, 1, 0, aline.available());
                try {
                  imw.encodeAudio(audionumber, smp);
                }
                catch(Exception e) {
                  println("EXCEPTION: " + e);
                }
              }
            }
          }
          fTime = System.nanoTime();
        }
      }
    }
  }
}

public void keyPressed() {
  if (key == 'r') {
    if (!recording) {
      println("recording");
      avRecorderSetup();
      recording = true;
    }
  }
  if (key == 's') {
    if (recording) {
      println("saving");
      imw.flush();
      imw.close();
      recording = false;
    }
  }
}

void avSetup() {
  audioFormat = new AudioFormat(44100.0F, 16, channelCount, true, false);
  sampleRate = (int) audioFormat.getSampleRate();
  DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
  try {
    aline = (TargetDataLine) AudioSystem.getLine(info);
    aline.open(audioFormat);
    aline.start();
    println("audio line");
  }
  catch (LineUnavailableException e)
  {
    println("unable to get a recording line");
    e.printStackTrace();
    exit();
  }
  int bufferSize = (int) audioFormat.getSampleRate() * audioFormat.getFrameSize();
  audioBuf = new byte[bufferSize];
  targetType = aline.getFormat();
  audioInputStream = new AudioInputStream(aline);
}

void avRecorderSetup() {
  imw = ToolFactory.makeWriter(sketchPath("D:/myVideo.mp4"));//or "output.avi" or "output.mov"
  imw.open();
  imw.setForceInterleave(true);
  imw.addVideoStream(0, 0, IRational.make((double)vidRate), widthCapture, heightCapture);
  audionumber = imw.addAudioStream(audioStreamIndex, audioStreamId, channelCount, sampleRate);
  isc = imw.getContainer().getStream(0).getStreamCoder();
  bgr = new BufferedImage(widthCapture, heightCapture, BufferedImage.TYPE_3BYTE_BGR);
  sTime = fTime = System.nanoTime();
}

-->

Robotic Telepresence via Skype and Face Tracking with OpenCV

A few months ago I hooked up the robotic head I built to Skype. I called up my brother in Los Angeles and had him control it with his head. Based upon where he was looking and turning his head, the robotic head would then match his head motions. By permitting the user to control the robotic head with their head, the goal has been to create a natural user interface. Furthermore, the user sees through the robots perspective, making the interaction feel natural. Connecting these two technologies, Skype and robotic telepresence, people may converse while also fully engaging in the environment.



Tutorial: How to build a Robotic Head that Follows People's Faces

Edit: See the working prototype here.

I've gotten a few requests to put up a tutorial for building a robotic head.

I'm assuming if you want to build a robotic head, you'll have some experience using an arduino, servos, and some other language that can interface between a webcam and the arduino. I used processing as my interface between the arduino and my webcam. I'm also using the OpenCV library for processing.

I'll describe the overarching structure behind the code to give the illusion that a robot is following a person's face. At a high level, there are only a few steps to have a webcam follow a person's face:
  1. Detect face. Take a look at some OpenCV examples on processing's website.
  2. Grab X-Y pixel coordinates of the face.
  3. Calculate the pixel distance between the center of the face and center of the webcam's view. In other words, you're going to take the images from the webcam, and calculate the distance between the center of that image and the center of the face.
  4. Write an algorithm that minimizes the distance between the webcam's center and the face's center.
    1. This algorithm will also control the servos.
You can download my processing code here and my arduino code is based on the code found here from letsmakerobots.

Let me know if you guys have any questions. I'll try to answer them as soon as I get a chance.

The following pictures are the first iteration:




Arduino Show & Tell at the Hacktory

Johnny Four and I made it out to the arduino show and tell at The Hacktory this past Saturday. The images and videos tell the rest of the story from the event.









And maybe Johnny Four may finally have a body?


Robotic Head Part 2

This is an update on a previous post, the robotic head part 1. I added eyes today and stabilized some of the jittery motions in the neck. This project was particularly inspired by Golan Levin's opto-isolator and double-taker. Watch the video for a demo.


Robotic Head Part 1

I've finally gotten around to combining opencv, processing, an arduino, servos, and a webcam. I've placed two servos perpendicular to each other, and then mounted a web cam on top of that. As of now I've got the entire system zeroing in on any face it sees and then maintains that face at the center of its field of view. I'll be adding a face very soon, with eyes that will follow a person and eye brows to make it more expressive. Watch the video for a demo.

Tutorial for how to build a robotic head using opencv, arduino, and processing.



Tutorial: Using the Emotiv headset with Processing and the Arduino

You guys might remember the Mind Controlled Crane I developed. I've also put together a brief tutorial with source code.

There are three parts to this project: the Emotiv software, the arduino software, and the processing software. The arduino code I used is found here on letsmakerobots. I used processing to then act as an interface between the arduino and the emotiv headset. My processing code can be found here. I believe there are still some bugs in my processing code, but you're more than welcome to use it. The EmoKey mapping that I used for the Emotiv Epoc can be downloaded here. Since I used the free Emotiv SDK, my EmoKey mapping is really busy with key commands being sent to processing, and my processing code is messy and lacks comments.

I will update this tutorial once I have more time to sit and write a detailed explanation. For the time being, feel free to download my code. I will also try to answer any questions as soon as I can.