OSDN Git Service

new version
[ahegaoman/ahegaoman.git] / AhegaoMan.pde
1 import hypermedia.video.*;
2 import java.awt.Rectangle;
3 import processing.video.*;
4
5 boolean rec = true;  // true: recording
6 int fps = 30;
7 int w = 1280;
8 int h = 720;
9 int angle = 0;
10
11 OpenCV opencv;
12 MovieMaker mm;
13
14 PImage img_back;
15 PImage img_ahegao;
16 PImage[] img_rotated = new PImage[360];
17
18 void setup() {
19   size( w, h );
20
21   String moviePath = selectInput();
22
23   opencv = new OpenCV( this );
24   opencv.movie( moviePath, width, height );
25   opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
26
27   img_back = loadImage("ahegao_back.png");  
28   MakeRotatedImage();
29
30   img_ahegao = loadImage("ahegao.png");
31   imageMode(CORNER);
32
33   if(rec){
34     mm = new MovieMaker(this, w, h, "ahegao.mov", fps, MovieMaker.VIDEO, MovieMaker.LOSSLESS); 
35     frameRate(fps);
36   }
37 }
38
39 void MakeRotatedImage(){
40   float theta = 0;
41   img_back.loadPixels();
42
43   int cx = (int)(img_back.width/2);
44   int cy = (int)(img_back.height/2);
45
46   for(int i =0; i<360; i++){
47     img_rotated[i] = createImage(img_back.width, img_back.height, ARGB);    
48   }
49
50   for(int i =0; i<360; i ++){
51     theta = 2*PI*i/360;
52     int tmp_sin = (int)(sin(theta)*1024);
53     int tmp_cos = (int)(cos(theta)*1024);
54
55     for(int y2 = 0; y2 < img_back.height; y2++){
56       for(int x2 = 0; x2 < img_back.width; x2++){
57         //          int x1 = (int)((x2-cx)*cos(theta) - (y2-cy)*sin(theta) +cx);
58         //          int y1 = (int)((x2-cx)*sin(theta) + (y2-cy)*cos(theta) +cy);
59         int x1 = (((x2-cx)*tmp_cos - (y2-cy)*tmp_sin) >> 10) +cx;
60         int y1 = (((x2-cx)*tmp_sin + (y2-cy)*tmp_cos) >> 10) +cy;
61         if(x1 >=0 && x1 < img_back.width && y1 >= 0 && y1 < img_back.height){
62           img_rotated[i].pixels[x2+y2*img_back.width] = img_back.pixels[x1+y1*img_back.width];
63         }
64       }
65     }
66   }
67 }
68
69 void draw() {
70   opencv.read();
71   image( opencv.image(), 0, 0 );
72   Rectangle[] faces = opencv.detect();
73
74   for( int i=0; i<faces.length; i++){
75     image(img_rotated[angle], faces[i].x-faces[i].width*0.2, faces[i].y-faces[i].height*0.2, faces[i].width*1.4, faces[i].height*1.4);
76     image(img_ahegao, faces[i].x+faces[i].width*0.1, faces[i].y+faces[i].height*0.1, faces[i].width*0.8, faces[i].height*0.8);
77   }
78
79   if(rec){
80     mm.addFrame(); 
81   }
82
83   angle +=3;
84   if(angle > 359){
85     angle =0;
86   }
87 }
88
89 void keyPressed() { 
90   if (key == ' ') { 
91     if(rec){
92       mm.finish(); 
93     }
94   exit();
95   }
96 }
97