Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "fmt"
  6. "strings"
  7.  
  8. "gocv.io/x/gocv"
  9. "gocv.io/x/gocv/contrib"
  10. )
  11.  
  12. var (
  13. useAll = flag.Bool("all", false, "Compute all hashes")
  14. usePHash = flag.Bool("phash", false, "Compute PHash")
  15. useAverage = flag.Bool("average", false, "Compute AverageHash")
  16. useBlockMean0 = flag.Bool("blockmean0", false, "Compute BlockMeanHash mode 0")
  17. useBlockMean1 = flag.Bool("blockmean1", false, "Compute BlockMeanHash mode 1")
  18. useColorMoment = flag.Bool("colormoment", false, "Compute ColorMomentHash")
  19. useMarrHildreth = flag.Bool("marrhildreth", false, "Compute MarrHildrethHash")
  20. useRadialVariance = flag.Bool("radialvariance", false, "Compute RadialVarianceHash")
  21. )
  22.  
  23. func setupHashes() []contrib.ImgHashBase {
  24. var hashes []contrib.ImgHashBase
  25.  
  26. if *usePHash || *useAll {
  27. hashes = append(hashes, contrib.PHash{})
  28. }
  29. if *useAverage || *useAll {
  30. hashes = append(hashes, contrib.AverageHash{})
  31. }
  32. if *useBlockMean0 || *useAll {
  33. hashes = append(hashes, contrib.BlockMeanHash{})
  34. }
  35. if *useBlockMean1 || *useAll {
  36. hashes = append(hashes, contrib.BlockMeanHash{Mode: contrib.BlockMeanHashMode1})
  37. }
  38. if *useColorMoment || *useAll {
  39. hashes = append(hashes, contrib.ColorMomentHash{})
  40. }
  41. if *useMarrHildreth || *useAll {
  42. // MarrHildreth has default parameters for alpha/scale
  43. hashes = append(hashes, contrib.NewMarrHildrethHash())
  44. }
  45. if *useRadialVariance || *useAll {
  46. // RadialVariance has default parameters too
  47. hashes = append(hashes, contrib.NewRadialVarianceHash())
  48. }
  49.  
  50. // If no hashes were selected, behave as if all hashes were selected
  51. if len(hashes) == 0 {
  52. *useAll = true
  53. return setupHashes()
  54. }
  55.  
  56. return hashes
  57. }
  58.  
  59. // FaceCompare is a function to compare 2 face
  60. func FaceCompare(images []gocv.Mat) {
  61. // var isSamePerson bool
  62. printHashes := flag.Bool("print", false, "print hash values")
  63. // construct all of the hash types in a list. normally, you'd only use one of these.
  64. hashes := setupHashes()
  65. // compute and compare the images for each hash type
  66. for _, hash := range hashes {
  67. results := make([]gocv.Mat, len(images))
  68.  
  69. for i, img := range images {
  70. results[i] = gocv.NewMat()
  71. defer results[i].Close()
  72. hash.Compute(img, &results[i])
  73. if results[i].Empty() {
  74. fmt.Println("error computing hash for %s")
  75. return
  76. }
  77. }
  78.  
  79. // compare for similarity; this returns a float64, but the meaning of values is
  80. // unique to each algorithm.
  81. similar := hash.Compare(results[0], results[1])
  82.  
  83. // make a pretty name for the hash
  84. name := strings.TrimPrefix(fmt.Sprintf("%T", hash), "contrib.")
  85. fmt.Printf("%s: similarity %g\n", name, similar)
  86.  
  87. if *printHashes {
  88. // print hash result for each image
  89. for i, _ := range images {
  90. fmt.Printf("%x\n", results[i].ToBytes())
  91. }
  92. }
  93. }
  94. // return isSamePerson
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement