Browse Source

better output

master
arnaucode 7 years ago
parent
commit
6eb4541fba
8 changed files with 108 additions and 17 deletions
  1. +2
    -0
      .gitignore
  2. +57
    -0
      goVersion/color.go
  3. BIN
      goVersion/images/test.png
  4. +1
    -3
      goVersion/imgOperations.go
  5. +6
    -2
      goVersion/main.go
  6. +8
    -9
      goVersion/printSortedMapStringInt.go
  7. +2
    -3
      goVersion/readExamples.go
  8. +32
    -0
      goVersion/server.go

+ 2
- 0
.gitignore

@ -0,0 +1,2 @@
media
webapp

+ 57
- 0
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
}

BIN
goVersion/images/test.png

Before After
Width: 8  |  Height: 8  |  Size: 161 B Width: 8  |  Height: 8  |  Size: 161 B

+ 1
- 3
goVersion/imgOperations.go

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

+ 6
- 2
goVersion/main.go

@ -1,5 +1,7 @@
package main package main
import "fmt"
func main() { func main() {
//first, read the example images //first, read the example images
examplesImg := readExamples() examplesImg := readExamples()
@ -10,6 +12,8 @@ func main() {
//fmt.Println(timg) //fmt.Println(timg)
numCoincidences := analyzeImg(examplesImg, timg) numCoincidences := analyzeImg(examplesImg, timg)
_ = printSortedMapStringInt(numCoincidences, 0)
_, result := printSortedMapStringInt(numCoincidences, 0)
fmt.Print("The image contains the number: ")
c.Green(result)
//runServer()
} }

+ 8
- 9
goVersion/printSortedMapStringInt.go

@ -1,14 +1,12 @@
package main package main
import ( import (
"fmt"
"sort" "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{} n := map[int][]string{}
var a []int var a []int
for k, v := range m { for k, v := range m {
@ -20,10 +18,11 @@ func printSortedMapStringInt(m map[string]int, threshold int) map[int][]string {
a = append(a, k) a = append(a, k)
} }
sort.Sort(sort.Reverse(sort.IntSlice(a))) 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] { for _, s := range n[k] {
fmt.Println(strconv.Itoa(k) + " - " + s)
fmt.Println(strconv.Itoa(k) + " - " + s)
} }
}
return n
}*/
return n, r
} }

+ 2
- 3
goVersion/readExamples.go

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"image" "image"
_ "image/png" _ "image/png"
"io/ioutil" "io/ioutil"
@ -41,10 +40,10 @@ func readExamples() map[string][]imgRGBA {
//for each image in the directory of example images, read the image and generate the histogram //for each image in the directory of example images, read the image and generate the histogram
files, _ := ioutil.ReadDir("./images/numbers/") files, _ := ioutil.ReadDir("./images/numbers/")
for _, f := range files { for _, f := range files {
fmt.Println("images/numbers/" + f.Name())
//fmt.Println("images/numbers/" + f.Name())
file := readImage("images/numbers/" + f.Name()) file := readImage("images/numbers/" + f.Name())
numberName := strings.Split(f.Name(), ".")[0] numberName := strings.Split(f.Name(), ".")[0]
fmt.Println(numberName)
//fmt.Println(numberName)
examplesImg[numberName] = append(examplesImg[numberName], file) examplesImg[numberName] = append(examplesImg[numberName], file)
} }
return examplesImg return examplesImg

+ 32
- 0
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")
}*/

Loading…
Cancel
Save