Processing调用OpenCV库实现人脸识别和图片切换
Posted 村头陈师傅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Processing调用OpenCV库实现人脸识别和图片切换相关的知识,希望对你有一定的参考价值。
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); //测试用尺寸
//fullScreen(); //全屏显示
background(255); //背景白色
for (int i = 0; i < images1.length; i ++ ) //循环images1.length次,将图片文件添加进数组
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
以上是关于Processing调用OpenCV库实现人脸识别和图片切换的主要内容,如果未能解决你的问题,请参考以下文章