[ Video ]
[ About ]
GUCCI JAPANさんのツイートを見て面白いなと思いやってみました、その2です。
OpenCVで目の座標を検知して一定サイズだけ移動させるという方法で、目をずらしています。目線で移動したら面白いなと思ったのですが、大変そうなので断念。また、楽にやれる方法を思いついたらやってみます。
(機械学習で目の画像とベクトルデータを学習させたら行けそう?)
[ 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 |
#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) {}; ofFbo fbo; cv::VideoCapture cap; cv::Size cap_size; cv::Mat frame; ofImage image; cv::CascadeClassifier eye_cascade; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofBackground(0); ofSetWindowTitle("Insta"); this->cap.open(1); this->cap_size = cv::Size(1280 * 0.5, 720 * 0.5); ofSetFrameRate(this->cap.get(CV_CAP_PROP_FPS)); this->eye_cascade.load("opencv-3.3.1\\build\\install\\etc\\haarcascades\\haarcascade_eye.xml"); this->image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); this->frame = cv::Mat(this->image.getHeight(), this->image.getWidth(), CV_MAKETYPE(CV_8UC3, this->image.getPixels().getNumChannels()), this->image.getPixels().getData(), 0); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat cap_frame, src, blend_frame; this->cap >> cap_frame; if (cap_frame.empty()) { cap.set(CV_CAP_PROP_POS_FRAMES, 1); return; } cv::resize(cap_frame, src, this->cap_size); cv::flip(src, src, 1); cv::cvtColor(src, src, CV_RGB2BGR); this->frame = this->frame.zeros(this->frame.size(), this->frame.type()); vector<cv::Rect> eyes; this->eye_cascade.detectMultiScale(src, eyes); for (int x = 0; x < src.cols; x++) { for (int y = src.rows - 1; y > -1; y--) { ofVec2f point(x, y); for (cv::Rect eye : eyes) { ofVec2f eye_point = ofVec2f(eye.x + eye.size().width / 2, eye.y + eye.size().height / 2); if (eye_point.distance(point) < 30) { ofVec2f center = ofVec2f(src.cols / 2, src.rows / 2); point = point + ofVec2f(0, 100 * ofNoise((eye.x, ofGetFrameNum() * 0.05))); } } int tmp_x = (int)point.x; int tmp_y = (int)point.y; if (tmp_x >= 0 && tmp_x < src.cols && tmp_y >= 0 && tmp_y < src.rows) { this->frame.at<cv::Vec3b>(tmp_y, tmp_x) = src.at<cv::Vec3b>(y, x); } } } this->image.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->image.draw(0, 0); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1280 * 0.5, 720 * 0.5, OF_WINDOW); ofRunApp(new ofApp()); } |