OpenCV Java Smile Detection -


i've tried create smile detector source code i've found on internet. detects face , works pretty well. uses haar classifiers, i've found haar classifiers smile recognition , tried it, doesn't work. i've tried use in same way used recognize face. tried same eye classifier - , worked. classifiers i've found in opencv/data folder, give me tip, more given code?

import java.io.file; import org.opencv.core.core; import org.opencv.core.mat; import org.opencv.core.matofrect; import org.opencv.core.point; import org.opencv.core.rect; import org.opencv.core.scalar; import org.opencv.highgui.highgui; import org.opencv.objdetect.cascadeclassifier;  public class smiledetector {  public void detectsmile(string filename) {     system.loadlibrary(core.native_library_name);     system.out.println("\nrunning smiledetector");      cascadeclassifier facedetector = new cascadeclassifier(new file(             "src/main/resources/haarcascade_frontalface_alt.xml").getabsolutepath());     cascadeclassifier smiledetector = new cascadeclassifier(             new file("src/main/resources/haarcascade_smile.xml").getabsolutepath());     mat image = highgui.imread(filename);     matofrect facedetections = new matofrect();     matofrect smiledetections = new matofrect();     facedetector.detectmultiscale(image, facedetections);      system.out.println(string.format("detected %s faces", facedetections.toarray().length));      (rect rect : facedetections.toarray()) {         core.rectangle(image, new point(rect.x, rect.y), new point(rect.x + rect.width, rect.y + rect.height),                 new scalar(0, 255, 0));     }     mat face = image.submat(facedetections.toarray()[0]);     smiledetector.detectmultiscale(face, smiledetections);      (rect rect : smiledetections.toarray()) {         core.rectangle(face, new point(rect.x, rect.y), new point(rect.x + rect.width, rect.y + rect.height),                 new scalar(0, 255, 0));     }      string outputfilename = "ouput.png";     system.out.println(string.format("writing %s", outputfilename));     highgui.imwrite(outputfilename, image);     highgui.imwrite("ee.png", face); } } 

to answer vi matviichuk comment: yes partially able fix problem. i've used mouth classifier instead of smile, name of mouth classifier opencv samples haarcascade_mcs_mouth.xml ; faces, crop them , mouths on faces. give lot of mouths, have filter them by:

/**  * detects face(s) , each detects , crops mouth  *   * @param filename path file on smile(s) detected  * @return list of mat objects cropped mouth pictures.  */ private arraylist<mat> detectmouth(string filename) {     int = 0;     arraylist<mat> mouths = new arraylist<mat>();     // reading image in grayscale given path     image = highgui.imread(filename, highgui.cv_load_image_grayscale);     matofrect facedetections = new matofrect();     // detecting face(s) on given image , saving them matofrect object     facedetector.detectmultiscale(image, facedetections);     system.out.println(string.format("detected %s faces", facedetections.toarray().length));     matofrect mouthdetections = new matofrect();     // detecting mouth(s) on given image , saving them matofrect object     mouthdetector.detectmultiscale(image, mouthdetections);     system.out.println(string.format("detected %s mouths", mouthdetections.toarray().length));     (rect face : facedetections.toarray()) {         mat outface = image.submat(face);         // saving cropped face picture         highgui.imwrite("face" + + ".png", outface);         (rect mouth : mouthdetections.toarray()) {             // trying find right mouth             // if mouth in lower 2/5 of face             // , lower edge of mouth above of face             // , horizontal center of mouth enter of face             if (mouth.y > face.y + face.height * 3 / 5 && mouth.y + mouth.height < face.y + face.height                     && math.abs((mouth.x + mouth.width / 2)) - (face.x + face.width / 2) < face.width / 10) {                 mat outmouth = image.submat(mouth);                 // resizing mouth unified size of trainsize                 imgproc.resize(outmouth, outmouth, trainsize);                 mouths.add(outmouth);                 // saving mouth picture                  highgui.imwrite("mouth" + + ".png", outmouth);                 i++;             }         }     }     return mouths; } 

then have find smile, tried svm training machine, hadn't got enough samples wasn't perfect. however, whole code got can found here: https://bitbucket.org/cybuch/smile-detector/src/ac8a309454c3467ffd8bc1c34ad95879cb059328/src/main/java/org/cybuch/smiledetector/smiledetector.java?at=master


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -