Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. // FaceDetectFromImage is a funtion to detect human face from 2 picture
  2. func FaceDetectFromImage() {
  3. if len(os.Args) < 4 {
  4. fmt.Println("Please sent these argument to us.")
  5. fmt.Println("How to run:\n\t FaceDetectFromImage [path/to/file/image1.jpg] [path/to/file/image2.jpg] [classifier XML file]")
  6. return
  7. }
  8. // parse args
  9. imageFile1 := os.Args[1]
  10. imageFile2 := os.Args[2]
  11. xmlFile := os.Args[3]
  12.  
  13. // prepare image matrix
  14. img1 := gocv.IMRead(imageFile1, gocv.IMReadAnyColor)
  15. img2 := gocv.IMRead(imageFile2, gocv.IMReadAnyColor)
  16. if img1.Empty() {
  17. log.Panic("Can not read Image file : ", imageFile1)
  18. return
  19. }
  20. if img2.Empty() {
  21. log.Panic("Can not read Image file : ", imageFile2)
  22. return
  23. }
  24. defer img1.Close()
  25. defer img2.Close()
  26.  
  27. // load classifier to recognize faces
  28. classifier := gocv.NewCascadeClassifier()
  29. defer classifier.Close()
  30.  
  31. if !classifier.Load(xmlFile) {
  32. fmt.Printf("Error reading cascade file: %v\n", xmlFile)
  33. return
  34. }
  35.  
  36. // color for the rect when faces detected
  37. // blue := color.RGBA{0, 0, 255, 0}
  38.  
  39. // detect faces
  40. rects1 := classifier.DetectMultiScale(img1)
  41. fmt.Printf("found %d faces\n", len(rects1))
  42. rects2 := classifier.DetectMultiScale(img2)
  43. fmt.Printf("found %d faces\n", len(rects2))
  44.  
  45. // open display window
  46. window1 := gocv.NewWindow("Face Detect 1")
  47. defer window1.Close()
  48.  
  49. // open display window
  50. window2 := gocv.NewWindow("Face Detect 2")
  51. defer window2.Close()
  52.  
  53. // draw a rectangle around each face on the original image,
  54. // along with text identifying as "Human"
  55. var faceCrop1 gocv.Mat
  56. var faceCrop2 gocv.Mat
  57. images := make([]gocv.Mat, 2)
  58. for _, r := range rects1 {
  59. // gocv.Rectangle(&img1, r, blue, 3)
  60. faceCrop1 = img1.Region(r)
  61. // size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
  62. // pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
  63. // gocv.PutText(&img1, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
  64. }
  65.  
  66. for _, r2 := range rects2 {
  67. // gocv.Rectangle(&img2, r2, blue, 3)
  68. faceCrop2 = img2.Region(r2)
  69. // size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
  70. // pt := image.Pt(r2.Min.X+(r2.Min.X/2)-(size.X/2), r2.Min.Y-2)
  71. // gocv.PutText(&img2, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
  72. }
  73.  
  74. images[0] = faceCrop1
  75. images[1] = faceCrop2
  76. FaceCompare(images)
  77. // for {
  78. // // show the image in the window, and wait 1 millisecond
  79. // window1.IMShow(faceCrop1)
  80. // window2.IMShow(faceCrop2)
  81. // if window1.WaitKey(1) >= 0 || window2.WaitKey(1) >= 0 {
  82. // break
  83. // }
  84.  
  85. // }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement