Skip to content

Instantly share code, notes, and snippets.

@mseptiaan
Created February 10, 2022 07:46
Show Gist options
  • Save mseptiaan/614de57248f7652a0b6001fcb37fd8e3 to your computer and use it in GitHub Desktop.
Save mseptiaan/614de57248f7652a0b6001fcb37fd8e3 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/base64"
"image"
"log"
"net/http"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/segment"
"github.com/gofiber/fiber/v2"
"github.com/tidwall/gjson"
)
func main() {
app := fiber.New()
app.Post("/ocr", func(c *fiber.Ctx) error {
request := string(c.Body())
if gjson.Valid(request) {
// Get base64 from json request
base64image := gjson.Get(request, "img").String()
// Decode base64 to byte
sDec, err := base64.StdEncoding.DecodeString(base64image)
if err != nil {
log.Fatal(err)
}
// Decode byte to image struct
img, _, err := image.Decode(bytes.NewReader(sDec))
if err != nil {
log.Fatalln(err)
}
// Convert Image to grayscale
grayscale := effect.Grayscale(img)
// Convert Image to threshold segment
threshold := segment.Threshold(grayscale, 128)
......
return c.JSON(&fiber.Map{"status": "OK"})
}
return c.Status(http.StatusNotAcceptable).JSON(&fiber.Map{"status": "Request not JSON"})
})
log.Fatal(app.Listen(":8000"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment