arnaucube
arnaucube/numberImgRecognitionPublic Self-hosted repository
better output
arnaucode idoctnef@openmailbox.org
Mon, 12 Jun 2017 11:24:16 +0200
commit

6eb4541fba2230ce80b70f15b0803f13075af5ef

parent

641349cd485df9fc58280b9b48bd2886ad97add1

A .gitignore

@@ -0,0 +1,2 @@

+media +webapp
A goVersion/color.go

@@ -0,0 +1,57 @@

+package main + +import "fmt" + +//Color struct, defines the color +type Color struct{} + +var c Color + +//DarkGray color +func (c Color) DarkGray(t string) { + fmt.Print("\x1b[30;1m") //dark gray + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Red color +func (c Color) Red(t string) { + fmt.Print("\x1b[31;1m") //red + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Green color +func (c Color) Green(t string) { + fmt.Print("\x1b[32;1m") //green + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Yellow color +func (c Color) Yellow(t string) { + fmt.Print("\x1b[33;1m") //yellow + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Blue color +func (c Color) Blue(t string) { + fmt.Print("\x1b[34;1m") //blue + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Purple color +func (c Color) Purple(t string) { + fmt.Print("\x1b[35;1m") //purple + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Cyan color +func (c Color) Cyan(t string) { + fmt.Print("\x1b[36;1m") //cyan + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +}
M goVersion/imgOperations.go → goVersion/imgOperations.go

@@ -1,7 +1,5 @@

package main -import "fmt" - func comparePixels(p []int, q []int) int { numCoincidences := 0 for i := 0; i < len(p); i++ {

@@ -26,7 +24,7 @@ numCoincidences := make(map[string]int)

for k, imgs := range examplesImg { numCoincidences[k] = 0 - fmt.Println(k) + //fmt.Println(k) for _, img := range imgs { numCoincidences[k] = numCoincidences[k] + compareImgs(img, timg) }
M goVersion/main.go → goVersion/main.go

@@ -1,5 +1,7 @@

package main +import "fmt" + func main() { //first, read the example images examplesImg := readExamples()

@@ -10,6 +12,8 @@ timg := readImage("images/test.png")

//fmt.Println(timg) numCoincidences := analyzeImg(examplesImg, timg) - _ = printSortedMapStringInt(numCoincidences, 0) - + _, result := printSortedMapStringInt(numCoincidences, 0) + fmt.Print("The image contains the number: ") + c.Green(result) + //runServer() }
M goVersion/printSortedMapStringInt.go → goVersion/printSortedMapStringInt.go

@@ -1,14 +1,12 @@

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)) +func printSortedMapStringInt(m map[string]int, threshold int) (map[int][]string, string) { + /*total := len(m) + fmt.Println("total: " + strconv.Itoa(total))*/ n := map[int][]string{} var a []int for k, v := range m {

@@ -20,10 +18,11 @@ for k := range n {

a = append(a, k) } sort.Sort(sort.Reverse(sort.IntSlice(a))) - for _, k := range a { + r := n[a[0]][0] + /*for _, k := range a { for _, s := range n[k] { - fmt.Println(strconv.Itoa(k) + " - " + s) + fmt.Println(strconv.Itoa(k) + " - " + s) } - } - return n + }*/ + return n, r }
M goVersion/readExamples.go → goVersion/readExamples.go

@@ -1,7 +1,6 @@

package main import ( - "fmt" "image" _ "image/png" "io/ioutil"

@@ -41,10 +40,10 @@ 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()) + //fmt.Println("images/numbers/" + f.Name()) file := readImage("images/numbers/" + f.Name()) numberName := strings.Split(f.Name(), ".")[0] - fmt.Println(numberName) + //fmt.Println(numberName) examplesImg[numberName] = append(examplesImg[numberName], file) } return examplesImg
A goVersion/server.go

@@ -0,0 +1,32 @@

+package main + +/* +type NumberImg struct { + File string `json: "file"` + Text string `json: "text"` +} + +func runServer() { + router := mux.NewRouter().StrictSlash(true) + router.HandleFunc("/", Index).Methods("GET") + router.HandleFunc("/img", ImgToNumber).Methods("POST") + log.Fatal(http.ListenAndServe(":3020", router)) +} +func Index(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.Path) + fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) +} +func ImgToNumber(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.Path) + decoder := json.NewDecoder(r.Body) + + var t NumberImg + err := decoder.Decode(&t) + + if err != nil { + panic(err) + } + + fmt.Println(t.Text) + fmt.Fprintf(w, "data") +}*/