go and python version. Both works fine
9
goVersion/errors.go
Normal 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
|
After Width: | Height: | Size: 129 B |
BIN
goVersion/images/dot.png
Normal file
|
After Width: | Height: | Size: 138 B |
BIN
goVersion/images/dotndot.png
Normal file
|
After Width: | Height: | Size: 140 B |
BIN
goVersion/images/numbers/0.1.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/0.2.png
Normal file
|
After Width: | Height: | Size: 175 B |
BIN
goVersion/images/numbers/0.3.png
Normal file
|
After Width: | Height: | Size: 178 B |
BIN
goVersion/images/numbers/0.4.png
Normal file
|
After Width: | Height: | Size: 156 B |
BIN
goVersion/images/numbers/0.5.png
Normal file
|
After Width: | Height: | Size: 167 B |
BIN
goVersion/images/numbers/0.6.png
Normal file
|
After Width: | Height: | Size: 172 B |
BIN
goVersion/images/numbers/0.7.png
Normal file
|
After Width: | Height: | Size: 165 B |
BIN
goVersion/images/numbers/0.8.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/0.9.png
Normal file
|
After Width: | Height: | Size: 167 B |
BIN
goVersion/images/numbers/1.1.png
Normal file
|
After Width: | Height: | Size: 141 B |
BIN
goVersion/images/numbers/1.2.png
Normal file
|
After Width: | Height: | Size: 135 B |
BIN
goVersion/images/numbers/1.3.png
Normal file
|
After Width: | Height: | Size: 136 B |
BIN
goVersion/images/numbers/1.4.png
Normal file
|
After Width: | Height: | Size: 150 B |
BIN
goVersion/images/numbers/1.5.png
Normal file
|
After Width: | Height: | Size: 147 B |
BIN
goVersion/images/numbers/1.6.png
Normal file
|
After Width: | Height: | Size: 156 B |
BIN
goVersion/images/numbers/1.7.png
Normal file
|
After Width: | Height: | Size: 156 B |
BIN
goVersion/images/numbers/1.8.png
Normal file
|
After Width: | Height: | Size: 154 B |
BIN
goVersion/images/numbers/1.9.png
Normal file
|
After Width: | Height: | Size: 171 B |
BIN
goVersion/images/numbers/2.1.png
Normal file
|
After Width: | Height: | Size: 177 B |
BIN
goVersion/images/numbers/2.2.png
Normal file
|
After Width: | Height: | Size: 178 B |
BIN
goVersion/images/numbers/2.3.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/2.4.png
Normal file
|
After Width: | Height: | Size: 175 B |
BIN
goVersion/images/numbers/2.5.png
Normal file
|
After Width: | Height: | Size: 173 B |
BIN
goVersion/images/numbers/2.6.png
Normal file
|
After Width: | Height: | Size: 175 B |
BIN
goVersion/images/numbers/2.7.png
Normal file
|
After Width: | Height: | Size: 175 B |
BIN
goVersion/images/numbers/2.8.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/2.9.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/3.1.png
Normal file
|
After Width: | Height: | Size: 166 B |
BIN
goVersion/images/numbers/3.2.png
Normal file
|
After Width: | Height: | Size: 160 B |
BIN
goVersion/images/numbers/3.3.png
Normal file
|
After Width: | Height: | Size: 165 B |
BIN
goVersion/images/numbers/3.4.png
Normal file
|
After Width: | Height: | Size: 166 B |
BIN
goVersion/images/numbers/3.5.png
Normal file
|
After Width: | Height: | Size: 174 B |
BIN
goVersion/images/numbers/3.6.png
Normal file
|
After Width: | Height: | Size: 175 B |
BIN
goVersion/images/numbers/3.7.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/3.8.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/3.9.png
Normal file
|
After Width: | Height: | Size: 173 B |
BIN
goVersion/images/numbers/4.1.png
Normal file
|
After Width: | Height: | Size: 152 B |
BIN
goVersion/images/numbers/4.2.png
Normal file
|
After Width: | Height: | Size: 156 B |
BIN
goVersion/images/numbers/4.3.png
Normal file
|
After Width: | Height: | Size: 159 B |
BIN
goVersion/images/numbers/4.4.png
Normal file
|
After Width: | Height: | Size: 157 B |
BIN
goVersion/images/numbers/4.5.png
Normal file
|
After Width: | Height: | Size: 150 B |
BIN
goVersion/images/numbers/4.6.png
Normal file
|
After Width: | Height: | Size: 154 B |
BIN
goVersion/images/numbers/4.7.png
Normal file
|
After Width: | Height: | Size: 151 B |
BIN
goVersion/images/numbers/4.8.png
Normal file
|
After Width: | Height: | Size: 151 B |
BIN
goVersion/images/numbers/4.9.png
Normal file
|
After Width: | Height: | Size: 155 B |
BIN
goVersion/images/numbers/5.1.png
Normal file
|
After Width: | Height: | Size: 169 B |
BIN
goVersion/images/numbers/5.2.png
Normal file
|
After Width: | Height: | Size: 158 B |
BIN
goVersion/images/numbers/5.3.png
Normal file
|
After Width: | Height: | Size: 174 B |
BIN
goVersion/images/numbers/5.4.png
Normal file
|
After Width: | Height: | Size: 171 B |
BIN
goVersion/images/numbers/5.5.png
Normal file
|
After Width: | Height: | Size: 167 B |
BIN
goVersion/images/numbers/5.6.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/5.7.png
Normal file
|
After Width: | Height: | Size: 159 B |
BIN
goVersion/images/numbers/5.8.png
Normal file
|
After Width: | Height: | Size: 158 B |
BIN
goVersion/images/numbers/5.9.png
Normal file
|
After Width: | Height: | Size: 169 B |
BIN
goVersion/images/numbers/6.1.png
Normal file
|
After Width: | Height: | Size: 180 B |
BIN
goVersion/images/numbers/6.2.png
Normal file
|
After Width: | Height: | Size: 175 B |
BIN
goVersion/images/numbers/6.3.png
Normal file
|
After Width: | Height: | Size: 180 B |
BIN
goVersion/images/numbers/6.4.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/6.5.png
Normal file
|
After Width: | Height: | Size: 178 B |
BIN
goVersion/images/numbers/6.6.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
goVersion/images/numbers/6.7.png
Normal file
|
After Width: | Height: | Size: 174 B |
BIN
goVersion/images/numbers/6.8.png
Normal file
|
After Width: | Height: | Size: 170 B |
BIN
goVersion/images/numbers/6.9.png
Normal file
|
After Width: | Height: | Size: 170 B |
BIN
goVersion/images/numbers/7.1.png
Normal file
|
After Width: | Height: | Size: 158 B |
BIN
goVersion/images/numbers/7.2.png
Normal file
|
After Width: | Height: | Size: 165 B |
BIN
goVersion/images/numbers/7.3.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/7.4.png
Normal file
|
After Width: | Height: | Size: 163 B |
BIN
goVersion/images/numbers/7.5.png
Normal file
|
After Width: | Height: | Size: 164 B |
BIN
goVersion/images/numbers/7.6.png
Normal file
|
After Width: | Height: | Size: 163 B |
BIN
goVersion/images/numbers/7.7.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/7.8.png
Normal file
|
After Width: | Height: | Size: 162 B |
BIN
goVersion/images/numbers/7.9.png
Normal file
|
After Width: | Height: | Size: 163 B |
BIN
goVersion/images/numbers/8.1.png
Normal file
|
After Width: | Height: | Size: 151 B |
BIN
goVersion/images/numbers/8.2.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/8.3.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/8.4.png
Normal file
|
After Width: | Height: | Size: 177 B |
BIN
goVersion/images/numbers/8.5.png
Normal file
|
After Width: | Height: | Size: 153 B |
BIN
goVersion/images/numbers/8.6.png
Normal file
|
After Width: | Height: | Size: 153 B |
BIN
goVersion/images/numbers/8.7.png
Normal file
|
After Width: | Height: | Size: 148 B |
BIN
goVersion/images/numbers/8.8.png
Normal file
|
After Width: | Height: | Size: 150 B |
BIN
goVersion/images/numbers/8.9.png
Normal file
|
After Width: | Height: | Size: 169 B |
BIN
goVersion/images/numbers/9.1.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
goVersion/images/numbers/9.2.png
Normal file
|
After Width: | Height: | Size: 158 B |
BIN
goVersion/images/numbers/9.3.png
Normal file
|
After Width: | Height: | Size: 160 B |
BIN
goVersion/images/numbers/9.4.png
Normal file
|
After Width: | Height: | Size: 165 B |
BIN
goVersion/images/numbers/9.5.png
Normal file
|
After Width: | Height: | Size: 172 B |
BIN
goVersion/images/numbers/9.6.png
Normal file
|
After Width: | Height: | Size: 163 B |
BIN
goVersion/images/numbers/9.7.png
Normal file
|
After Width: | Height: | Size: 165 B |
BIN
goVersion/images/numbers/9.8.png
Normal file
|
After Width: | Height: | Size: 172 B |
BIN
goVersion/images/numbers/9.9.png
Normal file
|
After Width: | Height: | Size: 174 B |
BIN
goVersion/images/sentdex.png
Normal file
|
After Width: | Height: | Size: 5.4 KiB |
BIN
goVersion/images/test.png
Normal file
|
After Width: | Height: | Size: 161 B |
35
goVersion/imgOperations.go
Normal 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
@@ -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)
|
||||
|
||||
}
|
||||
29
goVersion/printSortedMapStringInt.go
Normal 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
@@ -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
|
||||
|
||||
}
|
||||