feat(kamite): support updated ocr_image command

This commit is contained in:
Piotr Grabowski 2022-07-12 12:18:16 +02:00
parent 6c0616868b
commit 83ccc74230

View file

@ -19,22 +19,23 @@
package gomicsv package gomicsv
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings"
"github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/gdk"
) )
var ( const (
kamiteCMDEndpointBaseTpl = "http://localhost:%d/cmd/"
kamiteOCRImageEndpoint = "ocr/image"
kamiteOCRManualBlockEndpoint = "ocr/manual-block"
kamiteRecognizeImageSnipDimensionPx = 500 kamiteRecognizeImageSnipDimensionPx = 500
kamiteCMDEndpointBaseTpl = "http://localhost:%d/cmd/"
kamiteOCRImageEndpoint = "ocr/image"
kamiteOCRManualBlockEndpoint = "ocr/manual-block"
bytesPerPixel = 3
) )
func (app *App) kamiteRecognizeManualBlock() { func (app *App) kamiteRecognizeManualBlock() {
@ -106,7 +107,7 @@ func (app *App) kamiteRecognizeImageUnderCursorBlock() {
// 4. Grab area around the cursor // 4. Grab area around the cursor
snipDim := kamiteRecognizeImageSnipDimensionPx snipDim := kamiteRecognizeImageSnipDimensionPx
snipSourceX0, snipSourceY0 := targetX-(snipDim/2), targetY-(snipDim/2) snipSourceX0, snipSourceY0 := targetX-(snipDim/2), targetY-(snipDim/2)
snipBytes := make([]int, snipDim*snipDim) snipBytes := make([]byte, snipDim*snipDim*bytesPerPixel)
for y := 0; y < snipDim; y++ { for y := 0; y < snipDim; y++ {
for x := 0; x < snipDim; x++ { for x := 0; x < snipDim; x++ {
srcX, srcY := snipSourceX0+x, snipSourceY0+y srcX, srcY := snipSourceX0+x, snipSourceY0+y
@ -118,7 +119,10 @@ func (app *App) kamiteRecognizeImageUnderCursorBlock() {
idx := srcY*srcRowstride + srcX*srcNChannels idx := srcY*srcRowstride + srcX*srcNChannels
r, g, b = srcPixels[idx], srcPixels[idx+1], srcPixels[idx+2] r, g, b = srcPixels[idx], srcPixels[idx+1], srcPixels[idx+2]
} }
snipBytes[y*snipDim+x] = int(r)<<16 | int(g)<<8 | int(b) idx := ((y * snipDim) + x) * bytesPerPixel
snipBytes[idx] = r
snipBytes[idx+1] = g
snipBytes[idx+2] = b
} }
} }
@ -132,23 +136,16 @@ func (app *App) kamiteRecognizeImageUnderCursorBlock() {
} }
type KamiteOCRImageCommandParams struct { type KamiteOCRImageCommandParams struct {
Pixels string `json:"pixels"` BytesB64 string `json:"bytesB64"`
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
} }
func kamiteSendOCRImageCommand(port int, imgBytes []int, w, h int) { func kamiteSendOCRImageCommand(port int, imgBytes []byte, w, h int) {
stringBytes := []string{}
for i := range imgBytes {
s := strconv.Itoa(imgBytes[i])
stringBytes = append(stringBytes, s)
}
bytesString := strings.Join(stringBytes, ",")
paramsJSON, err := json.Marshal(KamiteOCRImageCommandParams{ paramsJSON, err := json.Marshal(KamiteOCRImageCommandParams{
Pixels: bytesString, Width: w,
Width: w, Height: h,
Height: h, BytesB64: base64.StdEncoding.EncodeToString(imgBytes),
}) })
if err != nil { if err != nil {
log.Printf("Error encoding Kamite command params: %v", err) log.Printf("Error encoding Kamite command params: %v", err)