[ Video ]
[ About ]
OpenCVで動画から顔を識別、テキスチャと顔の座標・サイズをシェーダに送信して加工
Face detection by OpenCV. Send texture and face coordinate / size to shader for processing.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#pragma once #include "ofMain.h" #include <opencv2/opencv.hpp> class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; cv::Size cap_size; cv::VideoCapture cap; cv::Mat frame; ofImage image; cv::CascadeClassifier face_cascade; int number_of_targets; vector<glm::vec4> targets; vector<glm::vec4> sizes; ofShader shader; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); this->cap.open("video.mp4"); this->cap_size = cv::Size(1280, 720); this->image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); this->frame = cv::Mat(cv::Size(this->image.getWidth(), this->image.getHeight()), CV_MAKETYPE(CV_8UC3, this->image.getPixels().getNumChannels()), this->image.getPixels().getData(), 0); this->number_of_targets = 2; for (int i = 0; i < this->number_of_targets; i++) { this->targets.push_back(glm::vec4(0.f, 0.f, 0.f, 0.f)); this->sizes.push_back(glm::vec4(0.f, 0.f, 0.f, 0.f)); } this->face_cascade.load("opencv-3.4.1\\build\\install\\etc\\haarcascades\\haarcascade_frontalface_alt2.xml"); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->number_of_targets; i++) { this->targets[i] = glm::vec4(0.f, 0.f, 0.f, 0.f); this->sizes[i] = glm::vec4(0.f, 0.f, 0.f, 0.f); } cv::Mat src; this->cap >> src; if (src.empty()) { this->cap.set(CV_CAP_PROP_POS_FRAMES, 1); return; } else { cv::resize(src, this->frame, this->cap_size); cv::cvtColor(this->frame, this->frame, CV_BGR2RGB); this->image.update(); } cv::Mat gray_frame, small_frame; cv::cvtColor(this->frame, gray_frame, CV_RGB2GRAY); cv::resize(gray_frame, small_frame, cv::Size(this->cap_size.width * 0.5, this->cap_size.height * 0.5)); vector<cv::Rect> faces; this->face_cascade.detectMultiScale(small_frame, faces); int face_count = 0; for (cv::Rect r : faces) { this->targets[face_count] = glm::vec4(r.x * 2.f + r.width, r.y * 2.f + r.height, 0.f, 0.f); float value = r.width < r.height ? r.width : r.height; this->sizes[face_count] = glm::vec4(value, value, 0.f, 0.f); face_count++; if (face_count >= this->number_of_targets) { break; } } } //-------------------------------------------------------------- void ofApp::draw() { this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniformTexture("tex", this->image, 1); this->shader.setUniform4fv("targets", &this->targets[0].x, this->number_of_targets); this->shader.setUniform4fv("sizes", &this->sizes[0].x, this->number_of_targets); ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight()); this->shader.end(); } //-------------------------------------------------------------- int main() { ofGLWindowSettings settings; settings.setGLVersion(3, 2); settings.setSize(1280, 720); ofCreateWindow(settings); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
#version 150 uniform mat4 modelViewProjectionMatrix; in vec4 position; in vec2 texcoord; out vec2 texCoordVarying; void main(){ texCoordVarying = texcoord; gl_Position = modelViewProjectionMatrix * position; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#version 150 const int number_of_targets = 2; uniform float time; uniform vec2 resolution; uniform sampler2DRect tex; uniform vec4 targets[number_of_targets]; uniform vec4 sizes[number_of_targets]; in vec2 texCoordVarying; out vec4 outputColor; void main() { vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); for(int i = 0; i < number_of_targets; i++){ vec2 t = vec2(targets[i].x, -targets[i].y) / min(resolution.x, resolution.y) * 2.0; t.xy += vec2(-resolution.x, resolution.y) / min(resolution.x, resolution.y); vec2 sz = vec2(sizes[i].x, -sizes[i].y) / min(resolution.x, resolution.y) * 2.0; vec2 q = p - t; float d = length(q); if(d < length(sz)){ float a = atan(q.y, q.x); float x = t.x + cos(a + time * 5 + floor(d * 10) * 0.5) * d; float y = t.y + sin(a + time * 5 + floor(d * 10) * 0.5) * d; p = vec2(x, y); } } vec4 color = texture(tex, vec2((p.x * min(resolution.x, resolution.y) + resolution.x) * 0.5, resolution.y - (p.y * min(resolution.x, resolution.y) + resolution.y) * 0.5)); outputColor = color; } |