欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV Java實(shí)現(xiàn)人臉識別和裁剪功能

 更新時間:2021年11月17日 11:02:59   作者:蛇皮皮蛋  
這篇文章主要為大家詳細(xì)介紹了OpenCV Java實(shí)現(xiàn)人臉識別和裁剪功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了OpenCV Java實(shí)現(xiàn)人臉識別和裁剪的具體代碼,供大家參考,具體內(nèi)容如下

安裝及配置

1.首先安裝OpenCV,地址

這里我下載的是Windows版的3.4.5

然后安裝即可……

2.Eclipse配置OpenCV

Window->Preferences->Java->User Libraries

New輸入你的Libraries名

這里我的安裝目錄是D:\OpenCV,所以是:

然后引入dll,我是64位機(jī)子,所以是:

Ok,下面創(chuàng)建Java項(xiàng)目做Java與OpenCV的人臉識別。

人臉識別

創(chuàng)建項(xiàng)目后首先右擊選擇Properties

然后引入即可。

引入haarcascade_frontalface_alt.xml這個xml文件:

我的pom文件如下:

 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 
 
 <dependency>
 <groupId>org.bytedeco.javacpp-presets</groupId>
 <artifactId>ffmpeg</artifactId>
 <version>3.1.2-1.2</version>
 </dependency>
 <dependency>
 <groupId>org.bytedeco</groupId>
 <artifactId>javacv</artifactId>
 <version>1.4.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg-platform -->
 <dependency>
 <groupId>org.bytedeco.javacpp-presets</groupId>
 <artifactId>ffmpeg-platform</artifactId>
 <version>3.4.2-1.4.1</version>
 </dependency>
 <dependency>
 <groupId>commons-io</groupId>
 <artifactId>commons-io</artifactId>
 <version>2.4</version>
 </dependency>
 
 
 <!-- 視頻攝像頭 -->
 <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
 <dependency>
 <groupId>org.bytedeco</groupId>
 <artifactId>javacv-platform</artifactId>
 <version>1.4.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv-platform -->
 <dependency>
 <groupId>org.bytedeco.javacpp-presets</groupId>
 <artifactId>opencv-platform</artifactId>
 <version>3.4.1-1.4.1</version>
 </dependency>
 
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <scope>test</scope>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>

修改我的端口號:

server.port=8889

最后代碼如下:

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.io.FileUtils;
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.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
/* 
* @author zzf 
* @date 2019年1月17日 下午12:04:45 
*/
@RestController
public class OpenCVController {
 
 @Value("classpath:haarcascade_frontalface_alt.xml")
 private Resource xml;
 
 @PostMapping("/face")
 public void FaceDetector(HttpServletResponse response, MultipartFile file) throws IOException {
 // D:\workspace-sts-3.9.2.RELEASE\OpenCV\src\main\resources
 // String opencvpath = System.getProperty("user.dir") +
 // "\\src\\main\\resources\\";
 // String opencvDllName = opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll";
 // System.load(opencvDllName);
 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 System.out.println("人臉檢測開始……");
 
 // 創(chuàng)建臨時文件,因?yàn)閎oot打包后無法讀取文件內(nèi)的內(nèi)容
 File targetXmlFile = new File("src/" + xml.getFilename() + "");
 FileUtils.copyInputStreamToFile(xml.getInputStream(), targetXmlFile);
 CascadeClassifier faceDetector = new CascadeClassifier(targetXmlFile.toString());
 if (faceDetector.empty()) {
 System.out.println("請引入文件……");
 return;
 }
 // 創(chuàng)建圖片tempFile
 File tempFile = new File("src/" + file.getOriginalFilename() + "");
 FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
 
 // 讀取創(chuàng)建的圖片tempFile
 Mat image = Imgcodecs.imread(tempFile.toString());
 MatOfRect faceDetections = new MatOfRect();
 // 進(jìn)行人臉檢測
 faceDetector.detectMultiScale(image, faceDetections);
 System.out.println(String.format("檢測到人臉: %s", faceDetections.toArray().length));
 Integer i = 1;
 // 制圖將圖填充到image中
 for (Rect rect : faceDetections.toArray()) {
 Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
  new Scalar(0, 255, 0), 3);
 imageCut(tempFile.toString(), i+".jpg", rect.x, rect.y, rect.width, rect.height);// 進(jìn)行圖片裁剪
 i++;
 }
 // 下面部分是返回給頁面
 String filename = file.getOriginalFilename();
 Imgcodecs.imwrite(filename, image);
 File imgFile = new File(filename);
 if (imgFile.exists()) {
 response.getOutputStream().write(toByteArray(imgFile));
 response.getOutputStream().close();
 }
 
 // 刪除臨時文件
 if (targetXmlFile.exists() && targetXmlFile.isFile()) {
 if (targetXmlFile.delete()) {
 System.out.println("刪除臨時文件" + targetXmlFile + "成功!");
 }
 }
 if (imgFile.exists() && imgFile.isFile()) {
 if (imgFile.delete()) {
 System.out.println("刪除臨時文件" + imgFile + "成功!");
 }
 }
 if (tempFile.exists() && tempFile.isFile()) {
 if (tempFile.delete()) {
 System.out.println("刪除臨時文件" + tempFile + "成功!");
 }
 }
 }
 
 public static void imageCut(String imagePath, String outFile, int posX, int posY, int width, int height) {
 // 原始圖像
 Mat image = Imgcodecs.imread(imagePath);
 // 截取的區(qū)域:參數(shù),坐標(biāo)X,坐標(biāo)Y,截圖寬度,截圖長度
 Rect rect = new Rect(posX, posY, width, height);
 // 兩句效果一樣
 Mat sub = image.submat(rect); // Mat sub = new Mat(image,rect);
 Mat mat = new Mat();
 Size size = new Size(width, height);
 Imgproc.resize(sub, mat, size);// 將人臉進(jìn)行截圖并保存
 Imgcodecs.imwrite(outFile, mat);
 System.out.println(String.format("圖片裁切成功,裁切后圖片文件為: %s", outFile));
 
 }
 
 public static byte[] toByteArray(File file) throws IOException {
 File f = file;
 if (!f.exists()) {
 throw new FileNotFoundException("file not exists");
 }
 ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
 BufferedInputStream in = null;
 try {
 in = new BufferedInputStream(new FileInputStream(f));
 int buf_size = 1024;
 byte[] buffer = new byte[buf_size];
 int len = 0;
 while (-1 != (len = in.read(buffer, 0, buf_size))) {
 bos.write(buffer, 0, len);
 }
 return bos.toByteArray();
 } catch (IOException e) {
 e.printStackTrace();
 throw e;
 } finally {
 try {
 in.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 bos.close();
 }
 }
 
}

下面來一張我男神們的合照

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MySQL?MyBatis?默認(rèn)插入當(dāng)前時間方式

    MySQL?MyBatis?默認(rèn)插入當(dāng)前時間方式

    這篇文章主要介紹了MySQL?MyBatis?默認(rèn)插入當(dāng)前時間方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 淺談在Java中使用Callable、Future進(jìn)行并行編程

    淺談在Java中使用Callable、Future進(jìn)行并行編程

    這篇文章主要介紹了淺談在Java中使用Callable、Future進(jìn)行并行編程,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Java中的對象和引用詳解

    Java中的對象和引用詳解

    這篇文章主要介紹了Java中的對象和引用詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • java泛型基本知識及通用方法

    java泛型基本知識及通用方法

    這篇文章主要介紹了java泛型基礎(chǔ)知識及通用方法,從以下幾個方面介紹一下java的泛型: 基礎(chǔ), 泛型關(guān)鍵字, 泛型方法, 泛型類和接口,感興趣的可以了解一下
    2019-04-04
  • 關(guān)于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題

    關(guān)于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題

    這篇文章主要介紹了關(guān)于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題,如果idea和數(shù)據(jù)庫沒有建立鏈接,idea不識別表的信息,就會出現(xiàn)SQL語句的警告,需要的朋友可以參考下
    2023-05-05
  • Servlet3.0實(shí)現(xiàn)文件上傳的方法

    Servlet3.0實(shí)現(xiàn)文件上傳的方法

    本篇文章主要介紹了Servlet實(shí)現(xiàn)文件上傳的方法,所謂文件上傳就是將本地的文件發(fā)送到服務(wù)器中保存。有興趣的可以了解一下。
    2017-03-03
  • Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼

    Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼

    本文主要介紹了Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java AWT中常用的三種布局管理器詳解

    Java AWT中常用的三種布局管理器詳解

    這篇文章主要介紹了Java AWT中常用的三種布局管理器詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Windows下Java環(huán)境變量配置詳解

    Windows下Java環(huán)境變量配置詳解

    這篇文中給大家介紹的是關(guān)于Windows下JAVA環(huán)境變量JAVA_HOME、CLASSPATH、PATH設(shè)置的相關(guān)資料,文中介紹的還是相對比較詳細(xì)的,對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • 如何使用會話Cookie和Java實(shí)現(xiàn)JWT身份驗(yàn)證

    如何使用會話Cookie和Java實(shí)現(xiàn)JWT身份驗(yàn)證

    這篇文章主要介紹了如何使用會話Cookie和Java實(shí)現(xiàn)JWT身份驗(yàn)證,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-03-03

最新評論