java+opencv+intellij idea實現Face Service

來源:互聯網
上載者:User

標籤:gui   ssi   catch   流處理   demo   edit   函數   org   name   

首先當然是需要安裝opencv了,我用的是opencv2.4.13。下載完之後就可以直接安裝了,安裝過程也很簡單,直接下一步下一步就好,我就不了。

接下來在opencv下找到jar包,比如我直接安裝在c盤,我的jar包在C:\opencv\build\java中。

然後將jar包拷貝到lib目錄中,並且在idea中配置

接著在opencv的路徑下找到lbpcascade_frontalface.xml。比如我的就是C:\opencv\sources\data\lbpcascades。然後將其拷貝到src目錄下。

這樣該有的環境就已經搭建好了,就可以開始寫代碼了。

直接上代碼

    public static void main(String[] args) {        // Load the native library.        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);        String url ="G:\\web\\uploadPicture\\src\\main\\resources\\assets\\4.jpg";        new DetectFaceDemo().go(url,"G:\\1.jpg");    }

  

  

    public void go(String srcFileName,String newPath) {        Mat image = null;        CascadeClassifier faceDetector = null;        String xmlfilePath = DetectFaceDemo.class.getClassLoader().getResource("lbpcascade_frontalface.xml").getPath().substring(1);        try {            faceDetector = new CascadeClassifier(xmlfilePath);            image = Highgui.imread(srcFileName);        }catch (Exception e){            e.printStackTrace();        }        // Detect faces in the image.        // MatOfRect is a special container class for Rect.        MatOfRect faceDetections = new MatOfRect();        faceDetector.detectMultiScale(image, faceDetections);        System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));        // Draw a bounding box around each face.        for (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));        }//         Save the visualized detection.        System.out.println(String.format("Writing %s", newPath));        Highgui.imwrite(newPath, image);    }

  這樣你就可以直接運行呢,但是會報錯。

      這時你可以開啟edit,

修改vm option -Djava.library.path=C:\opencv\build\java\x64

接著再運行就可以了。

如果要部署到伺服器上的話opencv的jar包一定要放在lib下,我之前在lib下建立了個檔案夾吧jar包放在裡面,一直報classnotfound的異常。

然後在tomcat的vm options中添加路徑即可

 

但是這樣有個致命的問題,他必須是一個檔案路徑,opencv沒有提供對於流處理的封裝,這不符合java的思想,也不滿足項目的需求,尤其是現在許多的圖片都是base64位的流。所以這又需要用stormcv這個jar包了了,再次感謝Apache。

這樣我們就可以把識別的函數改為。

public void run(String imgStr) {    BASE64Decoder decoder = new BASE64Decoder();    Mat image = null;    CascadeClassifier faceDetector = null;    String xmlfilePath = DetectFaceDemo.class.getClassLoader().getResource("lbpcascade_frontalface.xml").getPath().substring(1);    try {        faceDetector = new CascadeClassifier(xmlfilePath);        byte[] b = decoder.decodeBuffer(imgStr); //將base64位流解碼為二進位檔案        image = ImageUtils.bytes2Mat(b);      //將二進位檔案轉化為mat    }catch (Exception e){        e.printStackTrace();    }    // Detect faces in the image.    // MatOfRect is a special container class for Rect.    MatOfRect faceDetections = new MatOfRect();    faceDetector.detectMultiScale(image, faceDetections);    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));}

  

  好了,大功告成

java+opencv+intellij idea實現Face Service

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.