文章目錄
- OpenCV
- Github
- 官網
- 安裝
- 環境變量
- Go的OpenCV綁定庫
- Github
- 文檔
- 安裝
- 搜索視頻設備ID
- 顯示視頻
- 檢測人臉
OpenCV
Github
- https://github.com/opencv/opencv/
官網
- https://opencv.org/
安裝
brew install opencv
brew upgrade opencv
- 安裝目錄
cd /usr/local/opt/opencv@4
環境變量
vim ~/.zshrc
export PKG_CONFIG_PATH="/usr/local/opt/opencv@4/lib/pkgconfig:$PKG_CONFIG_PATH"
source ~/.zshrc
- 驗證是否安裝成功
pkg-config --cflags --libs opencv4
Go的OpenCV綁定庫
Github
- https://github.com/hybridgroup/gocv
文檔
- https://gocv.io/getting-started/macos/
安裝
go get -u -d gocv.io/x/gocv
搜索視頻設備ID
brew install ffmpeg
# 列出視頻的設備ID
ffmpeg -f avfoundation -list_devices true -i ""
注意: [0] FaceTime HD Camera,deviceID := 0 是電腦攝像頭。
顯示視頻
此示例使用設備“0”打開視頻捕獲設備,讀取幀,并在GUI窗口中顯示視頻。
# 初始化
go mod init demo
# 安裝庫
go get -u -d gocv.io/x/gocv
package mainimport ("fmt""github.com/kbinani/screenshot""gocv.io/x/gocv""image"
)func main() {deviceID := 0webcam, _ := gocv.OpenVideoCapture(deviceID)window := gocv.NewWindow("Hello")defer window.Close()// 調整窗口大小winWidth, winHeight := 400, 400window.ResizeWindow(winWidth, winHeight)// 獲取屏幕分辨率width, height, _ := getScreenResolution()winX := (width - winWidth) / 2winY := (height - winHeight) / 2window.MoveWindow(winX, winY-100)img := gocv.NewMat()defer img.Close()for {webcam.Read(&img)// 調整圖像大小size := image.Point{X: winWidth, Y: winHeight}gocv.Resize(img, &img, size, 0, 0, gocv.InterpolationDefault)window.IMShow(img)window.WaitKey(1)}
}func getScreenResolution() (int, int, error) {// 獲取活動顯示器的數量numDisplays := screenshot.NumActiveDisplays()if numDisplays <= 0 {panic("no active display found")}// 獲取第一個顯示器的分辨率bounds := screenshot.GetDisplayBounds(0)width := bounds.Dx()height := bounds.Dy()fmt.Printf("Primary display resolution: %d x %d\n", width, height)return width, height, nil
}
檢測人臉
這是一個使用設備“0”打開視頻捕獲設備的更完整示例。它還使用CascadeClassifier類來加載包含分類器數據的外部數據文件。該程序從視頻中獲取每一幀,然后使用分類器來檢測人臉。如果找到任何人臉,它會在每個人臉周圍繪制一個藍色矩形,然后在輸出窗口中顯示視頻。
haarcascade_frontalface_default.xml 是一個預訓練的 Haar 特征分類器,用于人臉檢測。它是由 OpenCV 提供的一個經典工具,基于 Viola-Jones 對象檢測框架。該文件包含了一系列經過訓練的特征,可以快速有效地檢測圖像中的人臉。
Haar 特征分類器是一種基于機器學習的方法,用于圖像中對象的檢測。以下是其主要特點:
-
簡單矩形特征:Haar 特征由簡單的矩形區域組成,這些區域被分成亮和暗部分。通過比較這些區域的亮度差異,可以確定特征的存在。
-
積分圖像:為了快速計算矩形特征,使用積分圖像(Integral Image)來加速特征計算。這使得檢測過程非常高效。
-
級聯分類器:級聯分類器將多個弱分類器(簡單的矩形特征)串聯起來,以形成一個強大的分類器。通過逐級過濾非目標區域,逐步縮小檢測范圍,提高檢測速度。
- 下載 haarcascade_frontalface_default.xml 文件
package mainimport ("fmt""github.com/kbinani/screenshot""gocv.io/x/gocv""image""image/color"
)func main() {// set to use a video capture device 0deviceID := 0// open webcamwebcam, err := gocv.OpenVideoCapture(deviceID)if err != nil {fmt.Println(err)return}defer webcam.Close()// open display windowwindow := gocv.NewWindow("Face Detect")defer window.Close()// 調整窗口大小winWidth, winHeight := 400, 400window.ResizeWindow(winWidth, winHeight)// 獲取屏幕分辨率width, height, _ := getScreenResolution()winX := (width - winWidth) / 2winY := (height - winHeight) / 2window.MoveWindow(winX, winY-100)// prepare image matriximg := gocv.NewMat()defer img.Close()// color for the rect when faces detectedblue := color.RGBA{0, 0, 255, 0}// load classifier to recognize facesclassifier := gocv.NewCascadeClassifier()defer classifier.Close()if !classifier.Load("./haarcascade_frontalface_default.xml") {fmt.Println("Error reading cascade file: ./haarcascade_frontalface_default.xml")return}fmt.Printf("start reading camera device: %v\n", deviceID)for {if ok := webcam.Read(&img); !ok {fmt.Printf("cannot read device %v\n", deviceID)return}if img.Empty() {continue}// 調整圖像大小size := image.Point{X: winWidth, Y: winHeight}gocv.Resize(img, &img, size, 0, 0, gocv.InterpolationDefault)// detect facesrects := classifier.DetectMultiScale(img)fmt.Printf("found %d faces\n", len(rects))// draw a rectangle around each face on the original imagefor _, r := range rects {gocv.Rectangle(&img, r, blue, 3)}// show the image in the window, and wait 1 millisecondwindow.IMShow(img)window.WaitKey(1)}
}func getScreenResolution() (int, int, error) {// 獲取活動顯示器的數量numDisplays := screenshot.NumActiveDisplays()if numDisplays <= 0 {panic("no active display found")}// 獲取第一個顯示器的分辨率bounds := screenshot.GetDisplayBounds(0)width := bounds.Dx()height := bounds.Dy()fmt.Printf("Primary display resolution: %d x %d\n", width, height)return width, height, nil
}