Processing调用OpenCV库实现人脸识别和图片切换
公众号:STCode
效果描述:
本作品调用OpenCV计算机视觉库,通过调取摄像头,利用Processing编程实现当检测到人脸离开后更换图片的效果,更换图片的形式为逐渐消失和逐渐显示。
Processing调用OpenCV库实现人脸识别和图片切换-一个人从出生到年老的转身过程
源代码:
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
int maxImages = 9;
int pic_width = 893;
int pic_height = 516;
PImage[] images1 = new PImage[maxImages];
Capture video;
OpenCV opencv;
int flag1 = 0;
boolean max = true;
int check_flag = 0;
int pic_num = 0;
int i = 255;
int[] check_face = new int[5];
int cfn = 0;
void setup()
{
size(893,516);
background(255);
for (int i = 0; i < images1.length; i ++ )
{
images1[i] = loadImage( "p" + i + ".png" );
}
video = new Capture(this, 1600/2, 800/2);
opencv = new OpenCV(this, 1600/2, 800/2);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
frameRate(60);
}
void draw()
{
println(i);
background(255);
tint(255,i);
image(images1[pic_num],0,0);
if(i == 255)
{
println("check");
opencv.loadImage(video);
Rectangle[] faces = opencv.detect();
println(faces.length);
check_face[cfn] = faces.length;
cfn++;
if(cfn == 5)
{
cfn = 0;
}
if(check_face[0] == 1 && check_face[1] == 1 && check_face[2] == 1 && check_face[3] == 1 && check_face[4] == 1 && flag1 == 0)
{
flag1 = 1;
check_flag = 0;
}
if(check_face[0] == 0 && check_face[1] == 0 && check_face[2] == 0 && check_face[3] == 0 && check_face[4] == 0 && flag1 == 1)
{
flag1 = 0;
check_flag = 1;
}
}
if(check_flag == 1)
{
if(max == false)
{
if(i<=255)
{
i=i+2;
}
else
{
i = 255;
max = true;
check_flag = 0;
}
}
else
{
if(i>0)
{
i=i-2;
}
else
{
i = 0;
max = false;
pic_num++;
if(pic_num == 9)
{
pic_num = 0;
}
}
}
}
}
void captureEvent(Capture c)
{
c.read();
}
图片素材获取请关注公众号:STCode
|