---
# System prepended metadata

title: Mac opencv安裝&臉辯
tags: [opencv]

---

---
title: 'Mac opencv安裝&臉辯'
tags: opencv
description: Mac opencv安裝&臉辯
---

# Mac opencv安裝

---

[TOC]

---

## 使用的技術&工具

- Springboot
- Opencv4
- Zulu JDK 8(因為OpenJDK 8沒有Arm架構)

---

## Zulu JDK 8 install

安裝指令

``` Terminal
brew install homebrew/cask-versions/zulu8
``` 

顯示Mac上所有安裝的Java

``` Terminal
/usr/libexec/java_home -V
``` 

![](https://i.imgur.com/wNQf75U.png)


---

## Opencv4 install

``` Terminal
// xcode
xcode-select --install

//brew 更新
brew update

// ant安裝
brew install ant

// python3 install
brew install python3

//比如要更新的話請下
brew upgrade python3

// opencv install
brew install opencv

``` 

請修改opencv設定，改為ON
``` Terminal
// opencv edit config
brew edit opencv
``` 

![](https://i.imgur.com/2zkbI0J.png)


以下如果JDK版本有問題的話或者沒有JDK是Build不出java的Library
``` Terminal
brew install --build-from-source opencv
// 比如要重跑請下 brew reinstall --build-from-source opencv
``` 

安裝&Build完後，請確認一下以下路徑是否存不存在
``` Terminal
ls /opt/homebrew/Cellar/opencv/4.6.0_1/share/java
``` 

---
## 實際測試

請在POM裡面寫opencv的依賴
``` xml
    <dependency>
        <groupId>org.openpnp</groupId>
        <artifactId>opencv</artifactId>
        <version>4.6.0-0</version>
    </dependency>
``` 

寫一支Java

``` java

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.objdetect.Objdetect;

import java.io.ByteArrayInputStream;

public class FaceDetection {

    public static Mat loadImage(String imagePath) {
        Imgcodecs imageCodecs = new Imgcodecs();
        return imageCodecs.imread(imagePath);
    }

    public static void saveImage(Mat imageMatrix, String targetPath) {
        Imgcodecs imgcodecs = new Imgcodecs();
        imgcodecs.imwrite(targetPath, imageMatrix);
    }

    public static void detectFace(String sourceImagePath, String targetImagePath) {
        Mat loadedImage = loadImage(sourceImagePath);
        MatOfRect facesDetected = new MatOfRect();
        CascadeClassifier cascadeClassifier = new CascadeClassifier();
        int minFaceSize = Math.round(loadedImage.rows() * 0.1f);
        cascadeClassifier.load("/opt/homebrew/Cellar/opencv/4.6.0_1/share/opencv4/haarcascades/haarcascade_frontalface_alt.xml");
        cascadeClassifier.detectMultiScale(loadedImage,
                facesDetected,
                1.1,
                3,
                Objdetect.CASCADE_SCALE_IMAGE,
                new Size(minFaceSize, minFaceSize),
                new Size()
        );
        Rect[] facesArray =  facesDetected.toArray();
        for(Rect face : facesArray) {
            Imgproc.rectangle(loadedImage, face.tl(), face.br(), new Scalar(0, 0, 255), 3 );
        }
        saveImage(loadedImage, targetImagePath);
    }

}

```
java exec
``` java

import org.opencv.core.Core;
import web.component.FaceDetection;

public class Main {

    public static void main(String[] args) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        FaceDetection.detectFace("/Users/gn715364/Documents/server/abc.jpg","/Users/gn715364/Documents/server/abc-targ.jpg");

    }

}

```

---




