go and python version. Both works fine

This commit is contained in:
arnaucode
2017-06-11 23:26:29 +02:00
parent 14d4a5f81c
commit 641349cd48
202 changed files with 317 additions and 0 deletions

9
goVersion/errors.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import "fmt"
func check(err error) {
if err != nil {
fmt.Println(err)
}
}

BIN
goVersion/images/blank.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

BIN
goVersion/images/dot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
goVersion/images/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 B

View File

@@ -0,0 +1,35 @@
package main
import "fmt"
func comparePixels(p []int, q []int) int {
numCoincidences := 0
for i := 0; i < len(p); i++ {
if p[i] == q[i] {
numCoincidences++
}
}
return numCoincidences
}
func compareImgs(a imgRGBA, b imgRGBA) int {
numCoincidences := 0
for i := 0; i < len(a); i++ {
numCoincidences = numCoincidences + comparePixels(a[i], b[i])
}
return numCoincidences
}
func analyzeImg(examplesImg map[string][]imgRGBA, timg imgRGBA) map[string]int {
numCoincidences := make(map[string]int)
for k, imgs := range examplesImg {
numCoincidences[k] = 0
fmt.Println(k)
for _, img := range imgs {
numCoincidences[k] = numCoincidences[k] + compareImgs(img, timg)
}
}
return numCoincidences
}

15
goVersion/main.go Normal file
View File

@@ -0,0 +1,15 @@
package main
func main() {
//first, read the example images
examplesImg := readExamples()
//fmt.Println(examplesImg)
//now, read the target image
timg := readImage("images/test.png")
//fmt.Println(timg)
numCoincidences := analyzeImg(examplesImg, timg)
_ = printSortedMapStringInt(numCoincidences, 0)
}

View File

@@ -0,0 +1,29 @@
package main
import (
"fmt"
"sort"
"strconv"
)
func printSortedMapStringInt(m map[string]int, threshold int) map[int][]string {
total := len(m)
fmt.Println("total: " + strconv.Itoa(total))
n := map[int][]string{}
var a []int
for k, v := range m {
if v > threshold {
n[v] = append(n[v], k)
}
}
for k := range n {
a = append(a, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(a)))
for _, k := range a {
for _, s := range n[k] {
fmt.Println(strconv.Itoa(k) + " - " + s)
}
}
return n
}

52
goVersion/readExamples.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import (
"fmt"
"image"
_ "image/png"
"io/ioutil"
"os"
"strings"
)
type imgRGBA [][]int
type CollectionImgs []imgRGBA
type ExamplesImg map[string]CollectionImgs
func readImage(path string) imgRGBA {
//open image, and generate the histogram, made from r,g,b,a components
reader, err := os.Open(path)
check(err)
defer reader.Close()
m, _, err := image.Decode(reader)
check(err)
bounds := m.Bounds()
//generate the histogram
var histogram imgRGBA
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := m.At(x, y).RGBA()
var pixel []int
pixel = append(pixel, int(r), int(g), int(b), int(a))
histogram = append(histogram, pixel)
}
}
return histogram
}
func readExamples() map[string][]imgRGBA {
//var examplesImg ExamplesImg
examplesImg := make(map[string][]imgRGBA)
//for each image in the directory of example images, read the image and generate the histogram
files, _ := ioutil.ReadDir("./images/numbers/")
for _, f := range files {
fmt.Println("images/numbers/" + f.Name())
file := readImage("images/numbers/" + f.Name())
numberName := strings.Split(f.Name(), ".")[0]
fmt.Println(numberName)
examplesImg[numberName] = append(examplesImg[numberName], file)
}
return examplesImg
}

Some files were not shown because too many files have changed in this diff Show More