Rotation of a puzzle by color detection through my webcam? [hard]
in
Core Library Questions
•
1 year ago
Hello my dear (wish I could say fellow) Processing Pro's out there!
First of all, I'd like to start with letting you all know that I'm almost entirely new to Processing, a.k.a. a total noob.
Second, Processing is required for my study.
Third, I'm from Holland so if I'm not completely clear with my questions or explanations, please say so!
What's the idea?
We had to create a concept for a processing project. What did I create:
With the code in this post you're screen will be filled with a webcam feed on the left side and a rotary puzzle on the right side. This rotary puzzle consists out of 5 circles which move independently from eachother.
At this very moment you can solve this puzzle using your UP, DOWN, LEFT and RIGHT arrow keys.
It looks, in my opinion, pretty neat so far.
However, we need to create some sort of interface through the webcam (that was the main assignment).
So what I want to do is to be able to rotate the puzzle pieces by holding my hand in front of the cam.
The selection of which inner or outer circle you'll be rotating will still be done with the UP & DOWN arrow keys.
The rotation, however, has to be done by hand as I said.
I was thinking of doing this by wearing a glove or holding an object with a Red and a Blue section.
So what I need (atleast that's what I think I need ) is something that recognizes/detects those colors (color detection), and calculates the distance and angle between them. Then I need to link this webcam feed to the puzzle. So when I rotate my hand or the object with the two colors in front of the camera, the puzzle will rotate as well.
This is our current code (that still works with the arrow keys):
- import processing.serial.*;
- import ddf.minim.*;
- import processing.video.*;
- Capture cam;
- PImage spiegel;
- AudioPlayer klik, roterendeklik, succes, verkeerd;
- Minim minim;
- Movie myMovie;
- int counter, counter2 = 0;
- int stage = 0;
- PImage[] puzzles;
- PImage[] masks;
- PImage backdrop, intro, einde;
- PImage pijllinks, pijlrechts, pijl2, pijl1;
- int selection = 4;
- int[] rads = {
- -40, 200, 20, 60, 60
- };
- void setup() {
- minim = new Minim(this);
- klik = minim.loadFile("klik.mp3");
- roterendeklik = minim.loadFile("roterendeklik.mp3");
- succes = minim.loadFile("succes.mp3");
- verkeerd = minim.loadFile("verkeerd.mp3");
- imageMode(CENTER);
- intro = loadImage("intro.jpg");
- einde = loadImage("einde.jpg");
- puzzles = new PImage[5];
- masks = new PImage[5];
- for ( int t=0; t < 5; t++ ) {
- puzzles[t] = loadImage("puzzle" + str( t+1 ) + ".jpg");
- masks[t] = loadImage("puzzle" + str( t+1 ) + "mask.jpg");
- }
- pijllinks = loadImage("pijl_links.png");
- pijlrechts = loadImage("pijl_rechts.png");
- pijl1 = loadImage("pijl_omhoog.png");
- pijl2 = loadImage("pijl_omlaag.png");
- size(1200, 700, P2D);
- cam = new Capture(this, 500, 480, 60);
- spiegel = createImage (500, 480, RGB);
- }
- void draw() {
- switch(stage) {
- case 0:
- background(0, 0, 0);
- image(intro, 700, height/2);
- break;
- case 1:
- background(0, 0, 0);
- image(pijllinks, 75, 350);
- image(pijlrechts, 485, 350);
- image(pijl1, 275, 155);
- image(pijl2, 275, 545);
- if (cam.available() == true) {
- cam.read();
- }
- cam.loadPixels();
- for (int i = 0; i <cam.pixels.length; i++) {
- spiegel.pixels[i] = cam.pixels[cam.width + (cam.width*(i/cam.width)) - 1 - iÊm.width]; //hier gebeurd de spiegel magie.
- }
- spiegel.updatePixels();
- image(spiegel, 280, height/2);
- for ( int t = 0; t < rads.length; t++ ) {
- rads[t]%=360;
- }
- selection = constrain( selection, 0, 4 );
- for ( int t = 4; t >= 0; t-- ) {
- pushMatrix();
- translate(850, height/2);
- tint(255, (selection==t)?255:153);
- rotate(radians(rads[t]));
- puzzles[t].mask(masks[t]);
- image(puzzles[t], 0, 0);
- popMatrix();
- }
- break;
- case 2:
- fill(0, 10);
- rectMode(CORNER);
- rect(0, 0, width, height);
- counter++;
- if (counter > 10) {
- stage = 3;
- }
- break;
- case 3:
- background(0);
- image(einde, 700, height/2);
- break;
- case 4:
- rectMode(CORNER);
- fill(0, 0, 0);
- rect(0, 0, width, height);
- selection = 4;
- rads[0] = -40;
- rads[1] = 200;
- rads[2] = 20;
- rads[3] = 60;
- rads[4] = 60;
- counter = 0;
- counter2 = 0;
- stage = 0;
- break;
- }
- }
- void keyPressed() {
- switch(stage) {
- case 0:
- if (key == ' ') {
- stage = 1;
- }
- break;
- case 1:
- if (key == CODED) {
- if (keyCode == DOWN) {
- roterendeklik.play();
- selection++;
- roterendeklik.rewind();
- }
- if (keyCode == UP) {
- roterendeklik.play();
- selection--;
- roterendeklik.rewind();
- }
- if (keyCode == RIGHT) {
- klik.play();
- switch( selection ) {
- case 0:
- rads[0]+=10;
- break;
- case 1:
- rads[1]+=10;
- break;
- case 2:
- rads[2]+=10;
- break;
- case 3:
- rads[3]+=10;
- break;
- case 4:
- rads[4]+=10;
- break;
- }
- klik.rewind();
- }
- else if (keyCode == LEFT) {
- klik.play();
- rads[selection]-=10;
- klik.rewind();
- }
- }
- if (key == ENTER || key == RETURN) {
- if (rads[0] == 0 && rads[1] == 0 && rads[2] == 0 && rads[3] == 0 && rads[4] == 0) {
- stage = 2;
- succes.play();
- delay(2000);
- succes.rewind();
- }
- else {
- verkeerd.play();
- delay(1000);
- verkeerd.rewind();
- }
- }
- break;
- case 3:
- if (key == ENTER || key == RETURN) {
- stage = 4;
- }
- }
- }
- float pangle, angle;
- color bg;
- int px,py;
- void setup(){
- size(200,200);
- bg = rcolor();
- px = int(random( 50,150 ));
- py = int(random( 50,150 ));
- }
- void draw(){
- background(bg);
- line(px,py, mouseX, mouseY);
- angle = int(degrees(HALF_PI+atan2( mouseY-py, mouseX-px )))/10;
- if( angle != pangle ){
- println( angle );
- bg = rcolor();
- pangle = angle;
- }
- }
- color rcolor(){
- return color( random(255),random(255),random(255) );
- }
And I think the color detection part comes first..
But I can't really find a color detection code that detects TWO colors..
The only ones out there find ONE color..
And if someone out here does find a code that detects two colors, it'll be very helpful BUT what I really need to know then is how to combine it with my current code..
So.. any help is appreciated.
I really hope anyone out there can help me out with this.
My deadline is Thursday and there has been some unexpected situations that slowed me down on this, this week.
Thanks alot all!
1