├── .github └── workflows │ └── go.yml ├── Dockerfile ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── main.go ├── screenshot.png └── static └── index.html /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Go 5 | 6 | on: 7 | push: 8 | branches: [ "master" ] 9 | pull_request: 10 | branches: [ "master" ] 11 | 12 | jobs: 13 | 14 | build: 15 | permissions: write-all 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - name: Install Package 21 | # You may pin to the exact commit or the version. 22 | # uses: ConorMacBride/install-package@3e7ad059e07782ee54fa35f827df52aae0626f30 23 | uses: ConorMacBride/install-package@v1.1.0 24 | with: 25 | # Packages to install with apt on Linux 26 | apt: tesseract-ocr libtesseract-dev 27 | 28 | - name: Set up Go 29 | uses: actions/setup-go@v3 30 | with: 31 | go-version: 1.19 32 | 33 | - name: Build 34 | run: go build -v ./... 35 | 36 | - name: Upload a Build Artifact 37 | uses: actions/upload-artifact@v3.1.2 38 | with: 39 | path: ocrmyimage 40 | # The desired behavior if no files are found using the provided path. 41 | 42 | - name: 'Get Previous tag' 43 | id: previous_tag 44 | uses: "WyriHaximus/github-action-get-previous-tag@v1" 45 | with: 46 | fallback: v1.0 47 | 48 | - name: Release with Notes 49 | uses: softprops/action-gh-release@v1 50 | with: 51 | tag_name: ${{steps.previous_tag.outputs.tag}} 52 | files: ocrmyimage 53 | env: 54 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 55 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Start from the official Go image 2 | FROM golang:latest 3 | 4 | # Set the working directory inside the container 5 | WORKDIR /app 6 | 7 | # Copy the Go web application source code into the container 8 | COPY . . 9 | 10 | # Install tesseract-ocr dependencies 11 | RUN apt-get update && \ 12 | apt-get install -y tesseract-ocr libleptonica-dev libtesseract-dev 13 | 14 | # Install the tesseract-mar language package 15 | RUN apt-get install -y tesseract-ocr-mar tesseract-ocr-eng tesseract-ocr-san tesseract-ocr-hin 16 | 17 | # Build the Go web application 18 | RUN go build -o webapp 19 | 20 | # Expose the port on which the Go web application runs 21 | EXPOSE 8080 22 | 23 | # Set the command to run the Go web application 24 | CMD ["./webapp"] 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 quaintdev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OCR My Image 2 | 3 | OCR My Image is a web frontend for tesseract OCR 4 | 5 |  6 | 7 | It has dependency on Tesseract ocr and it must be installed first before installing OCR My Image server. 8 | 9 | On fedora, install below dependencies. 10 | 11 | ```bash 12 | sudo dnf install tesseract tesseract-devl 13 | ``` 14 | 15 | On Ubuntu or debian 16 | ```bash 17 | sudo apt install tesseract-ocr libtesseract-dev -y 18 | ``` 19 | 20 | You must install tesseract language pack for the language you want to perform ocr. For example, if you want to perform OCR on Marathi text, use 21 | 22 | ```bash 23 | sudo apt install tesseract-ocr-mar 24 | ``` 25 | 26 | You can view installed languages using `tesseract --list-langs` 27 | 28 | ## Install OCR My Image 29 | 30 | Use `go install github.com/quaintdev/ocrmyimage@latest` to install the web server on your platform. 31 | 32 | If you are on Linux (amd64) you can use pre-built binary from release pages and execute it. 33 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/quaintdev/ocrmyimage 2 | 3 | go 1.20 4 | 5 | require github.com/otiai10/gosseract/v2 v2.4.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= 2 | github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= 3 | github.com/otiai10/gosseract/v2 v2.4.0 h1:gYd3mx6FuMtIlxL4sYb9JLCFEDzg09VgNSZRNbqpiGM= 4 | github.com/otiai10/gosseract/v2 v2.4.0/go.mod h1:fhbIDRh29bj13vni6RT3gtWKjKCAeqDYI4C1dxeJuek= 5 | github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= 6 | github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI= 7 | github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= 8 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "io" 6 | "io/ioutil" 7 | "log" 8 | "net/http" 9 | "os" 10 | "strings" 11 | 12 | "github.com/otiai10/gosseract/v2" 13 | ) 14 | 15 | func main() { 16 | // Serve static files 17 | fs := http.FileServer(http.Dir("static")) 18 | http.Handle("/", fs) 19 | 20 | http.HandleFunc("/upload", uploadHandler) 21 | log.Println("Starting server on port 8080") 22 | log.Fatal(http.ListenAndServe(":8080", nil)) 23 | } 24 | 25 | func uploadHandler(w http.ResponseWriter, r *http.Request) { 26 | log.Println("ocr request receieved") 27 | err := r.ParseMultipartForm(32 << 20) // Limit file size to 32MB 28 | if err != nil { 29 | http.Error(w, "Failed to parse multipart form", http.StatusBadRequest) 30 | return 31 | } 32 | 33 | files := r.MultipartForm.File["images[]"] 34 | if len(files) == 0 { 35 | http.Error(w, "No files uploaded", http.StatusBadRequest) 36 | return 37 | } 38 | 39 | var results []string 40 | 41 | for _, fileHeader := range files { 42 | file, err := fileHeader.Open() 43 | if err != nil { 44 | http.Error(w, "Failed to open the file", http.StatusInternalServerError) 45 | return 46 | } 47 | defer file.Close() 48 | 49 | // Create a temporary file to store the uploaded image 50 | tempFile, err := ioutil.TempFile("", "upload-*.png") 51 | if err != nil { 52 | http.Error(w, "Failed to create a temporary file", http.StatusInternalServerError) 53 | return 54 | } 55 | defer os.Remove(tempFile.Name()) 56 | 57 | // Write the uploaded image to the temporary file 58 | _, err = io.Copy(tempFile, file) 59 | if err != nil { 60 | http.Error(w, "Failed to write the image to the temporary file", http.StatusInternalServerError) 61 | return 62 | } 63 | 64 | log.Println("file uploaded complete. running OCR...") 65 | 66 | client := gosseract.NewClient() 67 | client.SetLanguage(r.FormValue("language")) 68 | defer client.Close() 69 | client.SetImage(tempFile.Name()) 70 | text, _ := client.Text() 71 | 72 | text = strings.ReplaceAll(text, "\n", " ") 73 | results = append(results, text) 74 | } 75 | 76 | // Create a struct to hold the OCR results 77 | type OCRResult struct { 78 | Texts []string `json:"texts"` 79 | } 80 | 81 | // Parse the OCR results into the struct 82 | ocrResult := OCRResult{Texts: results} 83 | log.Println(ocrResult) 84 | // Write the OCR results as JSON to the response 85 | w.Header().Set("Content-Type", "application/json") 86 | err = json.NewEncoder(w).Encode(ocrResult) 87 | if err != nil { 88 | http.Error(w, "Failed to write the OCR results to the response", http.StatusInternalServerError) 89 | return 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quaintdev/ocrmyimage/7335c5331fa7fec3d0f9d688d51be49fac6ce0e9/screenshot.png -------------------------------------------------------------------------------- /static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |