Skip to content

Commit

Permalink
finished
Browse files Browse the repository at this point in the history
  • Loading branch information
PyMarcus authored Jul 24, 2023
1 parent b3ae108 commit 81b2c87
Show file tree
Hide file tree
Showing 5 changed files with 24,436 additions and 0 deletions.
Binary file added face.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/PyMarcus/go_imagerecognizer

go 1.20

require gocv.io/x/gocv v0.33.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
gocv.io/x/gocv v0.33.0 h1:WDtaBrq92AKrhepYzEktydDzNSm3t5k7ciawZK4rns8=
gocv.io/x/gocv v0.33.0/go.mod h1:oc6FvfYqfBp99p+yOEzs9tbYF9gOrAQSeL/dyIPefJU=
77 changes: 77 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"image"
"image/color"
"log"

"gocv.io/x/gocv"
)

/*
INSTALATION:
1)go install gocv.io/x/gocv
2)export CGO_CPPFLAGS="-I/usr/local/include"
3)export CGO_LDFLAGS="-L/usr/local/lib -lopencv_core -lopencv_face -lopencv_videoio -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_objdetect -lopencv_features2d -lopencv_video -lopencv_dnn -lopencv_xfeatures2d"
4)sudo pacman -S opencv python-opencv
*/
func applyGrayScale(img *gocv.Mat) *gocv.Mat{
log.Println("Applyng gray scale...")
grayImg := gocv.NewMat()
gocv.CvtColor(*img, &grayImg, gocv.ColorBGRToGray)
return &grayImg
}

func faceRecognition(img *gocv.Mat){
log.Println("Performing face detection...")

trainedXMLfile := "./trained_model/frontal.xml"

classifier := gocv.NewCascadeClassifier()
defer classifier.Close()

if !classifier.Load(trainedXMLfile){
log.Fatalln("Error to load classifier")
}

rects := classifier.DetectMultiScale(*img)
drawRectanglesAroundDetectedFaces(rects, img)

}

func drawRectanglesAroundDetectedFaces(rect []image.Rectangle, img *gocv.Mat){
for _, r := range rect{
gocv.Rectangle(img, r, color.RGBA{255, 0, 0, 0}, 2)
}
}

func showImageDetected(img *gocv.Mat){
window := gocv.NewWindow("Face detection")
defer window.Close()
window.IMShow(*img)

// wait for a press key
window.WaitKey(0)
}

func main(){
log.Println("starting recognition...")
img := gocv.IMRead("./face.jpg", gocv.IMReadColor)
if img.Empty() {
log.Fatalln("error to open image")
}
defer img.Close()

log.Println("Image has been found!")

defer applyGrayScale(&img).Close()

faceRecognition(&img)

showImageDetected(&img)

}

Loading

0 comments on commit 81b2c87

Please sign in to comment.