Skip to content

Commit

Permalink
add example opencv
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglei.w committed Nov 19, 2020
1 parent 5b1e7d8 commit 5029339
Show file tree
Hide file tree
Showing 8 changed files with 33,451 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ func TestExampleReadFrameAsJpeg(t *testing.T) {
func TestExampleShowProgress(t *testing.T) {
ExampleShowProgress("./sample_data/in1.mp4", "./sample_data/out2.mp4")
}

func TestExampleOpenCvFaceDetect(t *testing.T) {
ExampleFaceDetection("./sample_data/head-pose-face-detection-male-short.mp4",
"./sample_data/haarcascade_frontalface_default.xml")
}
101 changes: 101 additions & 0 deletions examples/opencv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package examples

import (
"fmt"
"image"
"image/color"
"io"
"log"

ffmpeg "github.com/u2takey/ffmpeg-go"
"gocv.io/x/gocv"
)

func readProcess(infileName string, writer io.WriteCloser) <-chan error {
log.Println("Starting ffmpeg process1")
done := make(chan error)
go func() {
err := ffmpeg.Input(infileName).
Output("pipe:",
ffmpeg.KwArgs{
"format": "rawvideo", "pix_fmt": "rgb24",
}).
WithOutput(writer).
Run()
log.Println("ffmpeg process1 done")
_ = writer.Close()
done <- err
close(done)
}()
return done
}

func openCvProcess(xmlFile string, reader io.ReadCloser, w, h int) {
// open display window
window := gocv.NewWindow("Face Detect")
defer window.Close()

// color for the rect when faces detected
blue := color.RGBA{0, 0, 255, 0}

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

if !classifier.Load(xmlFile) {
fmt.Printf("Error reading cascade file: %v\n", xmlFile)
return
}

frameSize := w * h * 3
buf := make([]byte, frameSize, frameSize)
for {
n, err := io.ReadFull(reader, buf)
if n == 0 || err == io.EOF {
return
} else if n != frameSize || err != nil {
panic(fmt.Sprintf("read error: %d, %s", n, err))
}
img, err := gocv.NewMatFromBytes(h, w, gocv.MatTypeCV8UC3, buf)
if err != nil {
fmt.Println("decode fail", err)
}
if img.Empty() {
continue
}

// detect faces
rects := classifier.DetectMultiScale(img)
fmt.Printf("found %d faces\n", len(rects))

// draw a rectangle around each face on the original image,
// along with text identifing as "Human"
for _, r := range rects {
gocv.Rectangle(&img, r, blue, 3)

size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(&img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}

// show the image in the window, and wait 1 millisecond
window.IMShow(img)
if window.WaitKey(10) >= 0 {
break
}
}
return
}

func ExampleFaceDetection(inputFile, xmlFile string) {
w, h := getVideoSize(inputFile)
log.Println(w, h)

pr1, pw1 := io.Pipe()
done1 := readProcess(inputFile, pw1)
openCvProcess(xmlFile, pr1, w, h)
err := <-done1
if err != nil {
panic(err)
}
log.Println("Done")
}
Loading

0 comments on commit 5029339

Please sign in to comment.