Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
wiki:tutoriels:kinect_dessiner_avec_la_main:kinect_dessiner_avec_la_main [2020/11/26 18:09] damien.muti |
— (Version actuelle) | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Dessiner avec la main avec la kinect ====== | ||
- | * Licence : libre ! | ||
- | * Fichiers : http:// | ||
- | * Modifié par Damien MUTI DESGROUAS le 17/12/2018 | ||
- | * Fichiers : | ||
- | * {{: | ||
- | * {{: | ||
- | * {{: | ||
- | * [[wiki: | ||
- | |||
- | |||
- | ===== Exemple de la librairie OpenKinect for Processing ===== | ||
- | Daniel Shifmann propose un tutoriel très pratique pour l' | ||
- | * [[https:// | ||
- | * [[https:// | ||
- | |||
- | L' | ||
- | |||
- | Le programme commenté en français est le suivant : A COMPLETER PAR FLORA WAN DER POORT | ||
- | |||
- | |||
- | ===== Exemple 1 : Dessiner une ligne brisée ===== | ||
- | A partir de l' | ||
- | ==== Code principal ==== | ||
- | Le code est le suivant : {{ : | ||
- | |||
- | < | ||
- | // Daniel Shiffman | ||
- | // Tracking the average location beyond a given depth threshold | ||
- | // Thanks to Dan O' | ||
- | |||
- | // https:// | ||
- | // http:// | ||
- | |||
- | import org.openkinect.freenect.*; | ||
- | import org.openkinect.processing.*; | ||
- | |||
- | // The kinect stuff is happening in another class | ||
- | KinectTracker tracker; | ||
- | Kinect kinect; | ||
- | |||
- | // dessin | ||
- | PVector anciennePosition; | ||
- | int compteurFrames = 0; | ||
- | |||
- | |||
- | void setup() { | ||
- | size(640, 520); | ||
- | // initialisation var. Glob. | ||
- | kinect = new Kinect(this); | ||
- | tracker = new KinectTracker(); | ||
- | anciennePosition = new PVector(0, 0); | ||
- | } | ||
- | |||
- | void draw() { | ||
- | // | ||
- | |||
- | // Run the tracking analysis | ||
- | tracker.track(); | ||
- | // Show the image | ||
- | // | ||
- | |||
- | // Rond Rouge | ||
- | // Let's draw the raw location RED | ||
- | PVector v1 = tracker.getPos(); | ||
- | fill(# | ||
- | noStroke(); | ||
- | ellipse(v1.x, | ||
- | |||
- | // dessin d'une ligne brisée | ||
- | if (compteurFrames > 3 ) { // | ||
- | strokeWeight(2); | ||
- | stroke(255, 255, 0); | ||
- | line(anciennePosition.x, | ||
- | // mémorisation de la nouvelle position | ||
- | anciennePosition = v1; | ||
- | } | ||
- | |||
- | |||
- | // Info Profondeur | ||
- | // Display some info | ||
- | int t = tracker.getThreshold(); | ||
- | fill(0); | ||
- | text(" | ||
- | "UP increase threshold, DOWN decrease threshold", | ||
- | | ||
- | // incrémentation du compteur de frames | ||
- | compteurFrames++; | ||
- | println(" | ||
- | } | ||
- | |||
- | // Adjust the threshold with key presses | ||
- | // réglage profondeur de champ | ||
- | void keyPressed() { | ||
- | int t = tracker.getThreshold(); | ||
- | if (key == CODED) { | ||
- | if (keyCode == UP) { | ||
- | t+=5; | ||
- | tracker.setThreshold(t); | ||
- | } else if (keyCode == DOWN) { | ||
- | t-=5; | ||
- | tracker.setThreshold(t); | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Classe KinectTracker ==== | ||
- | La classe KinectTracker est la suivante : | ||
- | < | ||
- | // Daniel Shiffman | ||
- | // Tracking the average location beyond a given depth threshold | ||
- | // Thanks to Dan O' | ||
- | |||
- | // https:// | ||
- | // http:// | ||
- | |||
- | class KinectTracker { | ||
- | |||
- | // Depth threshold | ||
- | int threshold = 745; | ||
- | |||
- | // Raw location | ||
- | PVector loc; | ||
- | |||
- | // Interpolated location | ||
- | PVector lerpedLoc; | ||
- | |||
- | // Depth data | ||
- | int[] depth; | ||
- | | ||
- | // What we'll show the user | ||
- | PImage display; | ||
- | |||
- | KinectTracker() { | ||
- | // This is an awkard use of a global variable here | ||
- | // But doing it this way for simplicity | ||
- | kinect.initDepth(); | ||
- | kinect.enableMirror(true); | ||
- | // Make a blank image | ||
- | display = createImage(kinect.width, | ||
- | // Set up the vectors | ||
- | loc = new PVector(0, 0); | ||
- | lerpedLoc = new PVector(0, 0); | ||
- | } | ||
- | |||
- | void track() { | ||
- | // Get the raw depth as array of integers | ||
- | depth = kinect.getRawDepth(); | ||
- | |||
- | // Being overly cautious here | ||
- | if (depth == null) return; | ||
- | |||
- | float sumX = 0; | ||
- | float sumY = 0; | ||
- | float count = 0; | ||
- | |||
- | for (int x = 0; x < kinect.width; | ||
- | for (int y = 0; y < kinect.height; | ||
- | | ||
- | int offset = x + y*kinect.width; | ||
- | // Grabbing the raw depth | ||
- | int rawDepth = depth[offset]; | ||
- | |||
- | // Testing against threshold | ||
- | if (rawDepth < threshold) { // détection des points inférieurs au seuil | ||
- | sumX += x; | ||
- | sumY += y; | ||
- | count++; | ||
- | } | ||
- | } | ||
- | } | ||
- | // As long as we found something | ||
- | if (count != 0) { | ||
- | loc = new PVector(sumX/ | ||
- | } | ||
- | |||
- | // Interpolating the location, doing it arbitrarily for now | ||
- | lerpedLoc.x = PApplet.lerp(lerpedLoc.x, | ||
- | lerpedLoc.y = PApplet.lerp(lerpedLoc.y, | ||
- | } | ||
- | |||
- | PVector getLerpedPos() { | ||
- | return lerpedLoc; | ||
- | } | ||
- | |||
- | PVector getPos() { | ||
- | return loc; | ||
- | } | ||
- | |||
- | void display() { | ||
- | PImage img = kinect.getDepthImage(); | ||
- | |||
- | // Being overly cautious here | ||
- | if (depth == null || img == null) return; | ||
- | |||
- | // Going to rewrite the depth image to show which pixels are in threshold | ||
- | // A lot of this is redundant, but this is just for demonstration purposes | ||
- | display.loadPixels(); | ||
- | for (int x = 0; x < kinect.width; | ||
- | for (int y = 0; y < kinect.height; | ||
- | |||
- | int offset = x + y * kinect.width; | ||
- | // Raw depth | ||
- | int rawDepth = depth[offset]; | ||
- | int pix = x + y * display.width; | ||
- | if (rawDepth < threshold) { | ||
- | | ||
- | // A red color instead | ||
- | // couleur contraste profondeur | ||
- | // display.pixels[pix] = color(150, 50, 50); | ||
- | display.pixels[pix] = color(# | ||
- | } else { | ||
- | display.pixels[pix] = img.pixels[offset]; | ||
- | } | ||
- | } | ||
- | } | ||
- | display.updatePixels(); | ||
- | |||
- | // Draw the image | ||
- | // Affichage vidéo | ||
- | image(display, | ||
- | } | ||
- | |||
- | int getThreshold() { | ||
- | return threshold; | ||
- | } | ||
- | |||
- | void setThreshold(int t) { | ||
- | threshold = t; | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ===== Exemple 2 : Dessiner une courbe ===== | ||
- | On modifie l' | ||
- | |||
- | Le code est le suivant : {{ : | ||
- | |||
- | < | ||
- | // Daniel Shiffman | ||
- | // Tracking the average location beyond a given depth threshold | ||
- | // Thanks to Dan O' | ||
- | |||
- | // https:// | ||
- | // http:// | ||
- | |||
- | import org.openkinect.freenect.*; | ||
- | import org.openkinect.processing.*; | ||
- | |||
- | // The kinect stuff is happening in another class | ||
- | KinectTracker tracker; | ||
- | Kinect kinect; | ||
- | |||
- | // dessin | ||
- | PVector anciennePosition; | ||
- | int compteurFrames = 0; | ||
- | |||
- | ArrayList< | ||
- | |||
- | |||
- | void setup() { | ||
- | size(640, 520); | ||
- | // initialisation var. Glob. | ||
- | kinect = new Kinect(this); | ||
- | tracker = new KinectTracker(); | ||
- | anciennePosition = new PVector(0, 0); | ||
- | listePoints = new ArrayList< | ||
- | } | ||
- | |||
- | void draw() { | ||
- | background(255); | ||
- | |||
- | // Run the tracking analysis | ||
- | tracker.track(); | ||
- | // Show the image | ||
- | // | ||
- | |||
- | // Rond Rouge | ||
- | // Let's draw the raw location RED | ||
- | PVector v1 = tracker.getPos(); | ||
- | fill(# | ||
- | noStroke(); | ||
- | ellipse(v1.x, | ||
- | |||
- | // | ||
- | listePoints.add(v1); | ||
- | | ||
- | // dessin d'une ligne brisée | ||
- | //for( int i=1; i< | ||
- | // line(listePoints.get(i-1).x, | ||
- | //} | ||
- | |||
- | | ||
- | // dessin d'une courbe | ||
- | noFill(); | ||
- | beginShape(); | ||
- | for( int i=0; i< | ||
- | curveVertex(listePoints.get(i).x, | ||
- | } | ||
- | endShape(); | ||
- | | ||
- | // Info Profondeur | ||
- | // Display some info | ||
- | int t = tracker.getThreshold(); | ||
- | fill(0); | ||
- | text(" | ||
- | "UP increase threshold, DOWN decrease threshold", | ||
- | | ||
- | // incrémentation du compteur de frames | ||
- | compteurFrames++; | ||
- | println(" | ||
- | } | ||
- | |||
- | // Adjust the threshold with key presses | ||
- | // réglage profondeur de champ | ||
- | void keyPressed() { | ||
- | int t = tracker.getThreshold(); | ||
- | if (key == CODED) { | ||
- | if (keyCode == UP) { | ||
- | t+=5; | ||
- | tracker.setThreshold(t); | ||
- | } else if (keyCode == DOWN) { | ||
- | t-=5; | ||
- | tracker.setThreshold(t); | ||
- | } | ||
- | } | ||
- | }// Daniel Shiffman | ||
- | // Tracking the average location beyond a given depth threshold | ||
- | // Thanks to Dan O' | ||
- | |||
- | // https:// | ||
- | // http:// | ||
- | |||
- | import org.openkinect.freenect.*; | ||
- | import org.openkinect.processing.*; | ||
- | |||
- | // The kinect stuff is happening in another class | ||
- | KinectTracker tracker; | ||
- | Kinect kinect; | ||
- | |||
- | // dessin | ||
- | PVector anciennePosition; | ||
- | int compteurFrames = 0; | ||
- | |||
- | ArrayList< | ||
- | |||
- | |||
- | void setup() { | ||
- | size(640, 520); | ||
- | // initialisation var. Glob. | ||
- | kinect = new Kinect(this); | ||
- | tracker = new KinectTracker(); | ||
- | anciennePosition = new PVector(0, 0); | ||
- | listePoints = new ArrayList< | ||
- | } | ||
- | |||
- | void draw() { | ||
- | background(255); | ||
- | |||
- | // Run the tracking analysis | ||
- | tracker.track(); | ||
- | // Show the image | ||
- | // | ||
- | |||
- | // Rond Rouge | ||
- | // Let's draw the raw location RED | ||
- | PVector v1 = tracker.getPos(); | ||
- | fill(# | ||
- | noStroke(); | ||
- | ellipse(v1.x, | ||
- | |||
- | // | ||
- | listePoints.add(v1); | ||
- | | ||
- | // dessin d'une ligne brisée | ||
- | //for( int i=1; i< | ||
- | // line(listePoints.get(i-1).x, | ||
- | //} | ||
- | |||
- | | ||
- | // dessin d'une courbe | ||
- | noFill(); | ||
- | beginShape(); | ||
- | for( int i=0; i< | ||
- | curveVertex(listePoints.get(i).x, | ||
- | } | ||
- | endShape(); | ||
- | | ||
- | // Info Profondeur | ||
- | // Display some info | ||
- | int t = tracker.getThreshold(); | ||
- | fill(0); | ||
- | text(" | ||
- | "UP increase threshold, DOWN decrease threshold", | ||
- | | ||
- | // incrémentation du compteur de frames | ||
- | compteurFrames++; | ||
- | println(" | ||
- | } | ||
- | |||
- | // Adjust the threshold with key presses | ||
- | // réglage profondeur de champ | ||
- | void keyPressed() { | ||
- | int t = tracker.getThreshold(); | ||
- | if (key == CODED) { | ||
- | if (keyCode == UP) { | ||
- | t+=5; | ||
- | tracker.setThreshold(t); | ||
- | } else if (keyCode == DOWN) { | ||
- | t-=5; | ||
- | tracker.setThreshold(t); | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | La classe KinectTracker est la suivante : [[wiki: | ||
- | |||
- | |||
- | |||