Exemple #1
0
// tesseract ./.samples/png/sample000.png out -l eng ./.samples/option/digest001.txt
func Test_Must_WithDigest(t *testing.T) {
	params := map[string]string{
		"src": "./.samples/png/sample001.png",
	}
	Expect(t, gosseract.Must(params)).ToBe("03:41:26\n\n")

	// add optional digest
	params["digest"] = "./.samples/option/digest001.txt"
	Expect(t, gosseract.Must(params)).ToBe("O   I  \n\n")
}
Exemple #2
0
func ProcessImage(path string) (int, int, int, int) {
	loadDictionary("/usr/share/dict/words")

	text := gosseract.Must(gosseract.Params{Src: path})
	totalCharacters := len(text)
	text = strings.TrimSpace(text)
	words := strings.Fields(text)

	util.Log(fmt.Sprintf("Text: %s\n", text))

	alpha, nonAlpha := analyzeWords(words)
	hasMoreAlpha := hasMoreAlphaCharacters(alpha, nonAlpha)

	if hasMoreAlpha {
		util.Log("hasMoreAlphaCharacters")
	}

	dictionaryWords := 0
	for _, word := range words {
		if hasMoreAlpha {
			if isInDictionary(word) {
				dictionaryWords++
				util.Log(fmt.Sprintf("isInDictionary: %s\n", word))
			}
		}
	}

	return alpha, nonAlpha, totalCharacters, dictionaryWords
}
Exemple #3
0
func (this *MainController) Post() {
	fileName := strconv.Itoa(int(time.Now().UnixNano()))
	filePath := imagePath + fileName //无扩展名的文件路径
	//log.Println(filePath)

	err := this.SaveToFile("image", filePath)
	if err != nil {
		log.Println(err)
	}

	cmd := exec.Command("gm", "convert", filePath, filePath+".jpg")
	cmd.Run()
	if err != nil {
		log.Println(err)
	}

	out := gosseract.Must(gosseract.Params{
		Src:       filePath + ".jpg",
		Languages: "eng",
	})
	out = strings.Replace(out, "\n", "", -1)
	out = strings.Replace(out, "\r", "", -1)
	out = strings.Replace(out, " ", "", -1)
	this.Data["json"] = map[string]string{"Result": out, "Id": fileName}
	this.ServeJSON()
}
func BenchmarkMust(b *testing.B) {
	for i := 0; i < b.N; i++ {
		gosseract.Must(gosseract.Params{
			Src:       "./.samples/png/sample000.png",
			Languages: "eng",
		})
	}
}
Exemple #5
0
// Base64 ...
func Base64(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	var body = new(struct {
		Base64    string `json:"base64"`
		Trim      string `json:"trim"`
		Languages string `json:"languages"`
		Whitelist string `json:"whitelist"`
	})

	err := json.NewDecoder(r.Body).Decode(body)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}

	tempfile, err := ioutil.TempFile("", config.AppName()+"-")
	if err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}
	defer func() {
		tempfile.Close()
		os.Remove(tempfile.Name())
	}()

	if len(body.Base64) == 0 {
		render.JSON(http.StatusBadRequest, fmt.Errorf("base64 string required"))
		return
	}
	body.Base64 = regexp.MustCompile("data:image\\/png;base64,").ReplaceAllString(body.Base64, "")
	b, err := base64.StdEncoding.DecodeString(body.Base64)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	tempfile.Write(b)

	// TODO: refactor gosseract
	body.Languages = "eng"

	result := gosseract.Must(gosseract.Params{
		Src:       tempfile.Name(),
		Languages: body.Languages,
		Whitelist: body.Whitelist,
	})
	render.JSON(http.StatusOK, map[string]interface{}{
		"result":  strings.Trim(result, body.Trim),
		"version": config.Version(),
	})
}
Exemple #6
0
func ConvertImage(r io.Reader) (string, map[string]string, error) {
	f, err := NewLocalFile(r, "/tmp", "sajari-convert-")
	if err != nil {
		return "", nil, fmt.Errorf("error creating local file: %v", err)
	}
	defer f.Done()

	meta := make(map[string]string)
	out := make(chan string, 1)
	go func(file *LocalFile) {
		languages.RLock()
		body := gosseract.Must(gosseract.Params{Src: file.Name(), Languages: languages.lang})
		languages.RUnlock()
		out <- string(body)
	}(f)

	return <-out, meta, nil
}
Exemple #7
0
// FileUpload ...
func FileUpload(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	whitelist := r.FormValue("whitelist")
	trim := r.FormValue("trim")
	// Get uploaded file
	r.ParseMultipartForm(32 << 20)
	// upload, h, err := r.FormFile("file")
	upload, _, err := r.FormFile("file")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer upload.Close()

	// Create physical file
	tempfile, err := ioutil.TempFile("", config.AppName()+"-")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer func() {
		tempfile.Close()
		os.Remove(tempfile.Name())
	}()

	// Make uploaded physical
	if _, err := io.Copy(tempfile, upload); err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}

	result := gosseract.Must(gosseract.Params{
		Src:       tempfile.Name(),
		Languages: "eng",
		Whitelist: whitelist,
	})

	render.JSON(http.StatusOK, map[string]interface{}{
		"result":  strings.Trim(result, trim),
		"version": config.Version(),
	})
}
Exemple #8
0
func main() {
	// Decode the JPEG data. If reading from file, create a reader with

	episodesData := []episode{}

	season := "data/frames/16/"
	// names of folders of episodes
	episodeDirs, _ := ioutil.ReadDir(season)

	// each episode
	for e := range episodeDirs {
		if episodeDirs[e].IsDir() {
			// names of files
			episodeName := episodeDirs[e].Name()
			episodeDir, _ := ioutil.ReadDir(season + episodeName)

			ed := episode{
				title: episodeName,
			}
			for i := range episodeDir {
				m, err := loadImage(season + episodeName + "/" + episodeDir[i].Name())
				if err != nil {
					log.Fatal("Error loading image.")
				}

				blackPixels := blackPixelPercent(m)
				if blackPixels > thresh {
					out := gosseract.Must(gosseract.Params{Src: season + episodeName + "/" + episodeDir[i].Name(), Languages: "eng"})

					if out != "" {
						// fmt.Println("candidate frame", episodeDir[i].Name())
						ed.cards = append(ed.cards, out)
						// fmt.Println(out)
					}
				}
			}
			episodesData = append(episodesData, ed)
		}
	}
	s, _ := json.Marshal(map[string][]episode{"episodes": episodesData})
	fmt.Println(string(s))
}
Exemple #9
0
func ExampleMust(t *testing.T) {
	// TODO: it panics! error handling in *Client.accept
	out := gosseract.Must(gosseract.Params{Src: "./.samples/png/sample002.png"})
	fmt.Println(out)
}
Exemple #10
0
func ExampleMust(t *testing.T) {
	// TODO: it panics! error handling in *Client.accept
	out := gosseract.Must(map[string]string{"src": "./.samples/png/sample002.png"})
	fmt.Println(out)
}
Exemple #11
0
func Test_Must(t *testing.T) {
	params := map[string]string{
		"src": "./.samples/png/sample000.png",
	}
	Expect(t, gosseract.Must(params)).ToBe("01:37:58\n\n")
}