[ Video ]
[ About ]
openFrameworksで手書きした絵を、Yolo v2で判別しています。
Yolo v2 classifies handwritten pictures written by openFrameworks.
[ 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include "ofMain.h" // OpenCV 3.3.1 #include <opencv2/dnn.hpp> #include <opencv2/dnn/shape_utils.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace cv; using namespace cv::dnn; class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void detection(); 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) {}; vector<ofPolyline> lines; int line_index; ofImage dog_image; cv::Mat frame; ofImage frame_image; cv::VideoCapture cap; const size_t network_width = 416; const size_t network_height = 416; String modelConfiguration = "yolo.cfg"; String modelBinary = "yolo.weights"; dnn::Net net; cv::Mat resized, inputBlob, detectionMat; const vector<string> class_names = { "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" }; }; |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(255); ofSetWindowTitle("Insta"); ofNoFill(); this->frame_image.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR); this->frame = cv::Mat(this->frame_image.getHeight(), this->frame_image.getWidth(), CV_MAKETYPE(CV_8UC3, this->frame_image.getPixels().getNumChannels()), this->frame_image.getPixels().getData(), 0); this->dog_image.load("dog.jpg"); this->net = readNetFromDarknet(modelConfiguration, modelBinary); if (net.empty()) { cout << "dnn Net is empty" << endl; } this->line_index = 0; } //-------------------------------------------------------------- void ofApp::update() { if (ofGetMousePressed()) { this->lines[this->line_index].addVertex(ofGetMouseX(), ofGetMouseY()); } } //-------------------------------------------------------------- void ofApp::draw() { ofSetColor(0); for (int i = 0; i < this->lines.size(); i++) { this->lines[i].draw(); } ofSetColor(255); this->dog_image.draw(0, ofGetHeight() / 2 - this->dog_image.getHeight() / 2); this->frame_image.grabScreen(0, 0, ofGetWidth(), ofGetHeight()); cv::cvtColor(this->frame, this->frame, CV_BGR2RGB); if (!this->frame.empty()) { this->detection(); } cv::imshow("frame", this->frame); cv::waitKey(1); } //-------------------------------------------------------------- void ofApp::detection() { cv::resize(this->frame, this->resized, cv::Size(this->network_width, this->network_height)); this->inputBlob = blobFromImage(this->resized, 1 / 255.F); //Convert Mat to batch of images this->net.setInput(this->inputBlob, "data"); //set the network input this->detectionMat = this->net.forward("detection_out"); //compute output float confidenceThreshold = 0.15; for (int i = 0; i < detectionMat.rows; i++) { const int probability_index = 5; const int probability_size = detectionMat.cols - probability_index; float *prob_array_ptr = &detectionMat.at<float>(i, probability_index); size_t objectClass = std::max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr; float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index); if (confidence > confidenceThreshold) { float x = detectionMat.at<float>(i, 0); float y = detectionMat.at<float>(i, 1); float width = detectionMat.at<float>(i, 2); float height = detectionMat.at<float>(i, 3); float xLeftBottom = (x - width / 2) * frame.cols; float yLeftBottom = (y - height / 2) * frame.rows; float xRightTop = (x + width / 2) * frame.cols; float yRightTop = (y + height / 2) * frame.rows; cv::Rect object( (int)xLeftBottom, (int)yLeftBottom, (int)(xRightTop - xLeftBottom), (int)(yRightTop - yLeftBottom)); cv::Rect title( (int)xLeftBottom, (int)yLeftBottom - 15, 100, 15); std::string object_name = ""; if (objectClass < class_names.size()) { object_name = class_names[objectClass]; } cv::rectangle(frame, title, Scalar(0, 255, 0), -1); cv::putText(frame, object_name, cv::Point((int)xLeftBottom, (int)yLeftBottom - 2), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255), 2); cv::rectangle(frame, object, Scalar(0, 255, 0)); } } } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ this->lines.push_back(ofPolyline()); } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button) { this->line_index += 1; } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |