screen: update tresholds on text
This commit is contained in:
36
screen.go
36
screen.go
@@ -67,7 +67,6 @@ func (d *Device) GetScreenResolution() (int, int, error) {
|
|||||||
return d.Screenx, d.Screeny, nil
|
return d.Screenx, d.Screeny, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the text on screen in a certain area using gotesseract
|
|
||||||
func (d *Device) GetScreenText(xStart int, xEnd int, yStart int, yEnd int, whitelist string, multiline bool) (string, error) {
|
func (d *Device) GetScreenText(xStart int, xEnd int, yStart int, yEnd int, whitelist string, multiline bool) (string, error) {
|
||||||
// Take screenshot in memory and get bytes
|
// Take screenshot in memory and get bytes
|
||||||
data, err := d.RunCommand("exec-out screencap -p")
|
data, err := d.RunCommand("exec-out screencap -p")
|
||||||
@@ -90,16 +89,37 @@ func (d *Device) GetScreenText(xStart int, xEnd int, yStart int, yEnd int, white
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to grayscale and apply threshold
|
// Convert to grayscale and compute average brightness
|
||||||
threshold := uint8(160)
|
gray := image.NewGray(rect)
|
||||||
|
var totalBrightness int
|
||||||
|
for y := 0; y < rect.Dy(); y++ {
|
||||||
|
for x := 0; x < rect.Dx(); x++ {
|
||||||
|
grayVal := color.GrayModel.Convert(cropped.At(x, y)).(color.Gray)
|
||||||
|
gray.Set(x, y, grayVal)
|
||||||
|
totalBrightness += int(grayVal.Y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
avgBrightness := totalBrightness / (rect.Dx() * rect.Dy())
|
||||||
|
|
||||||
|
// Binarize based on brightness polarity
|
||||||
binarized := image.NewGray(rect)
|
binarized := image.NewGray(rect)
|
||||||
for y := 0; y < rect.Dy(); y++ {
|
for y := 0; y < rect.Dy(); y++ {
|
||||||
for x := 0; x < rect.Dx(); x++ {
|
for x := 0; x < rect.Dx(); x++ {
|
||||||
gray := color.GrayModel.Convert(cropped.At(x, y)).(color.Gray)
|
grayVal := gray.GrayAt(x, y).Y
|
||||||
if gray.Y > threshold {
|
if avgBrightness < 128 {
|
||||||
binarized.SetGray(x, y, color.Gray{Y: 255})
|
// Dark background, light text
|
||||||
|
if int(grayVal) > avgBrightness+10 {
|
||||||
|
binarized.SetGray(x, y, color.Gray{Y: 0})
|
||||||
|
} else {
|
||||||
|
binarized.SetGray(x, y, color.Gray{Y: 255})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
binarized.SetGray(x, y, color.Gray{Y: 0})
|
// Light background, dark text
|
||||||
|
if int(grayVal) < avgBrightness-10 {
|
||||||
|
binarized.SetGray(x, y, color.Gray{Y: 0})
|
||||||
|
} else {
|
||||||
|
binarized.SetGray(x, y, color.Gray{Y: 255})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,7 +130,7 @@ func (d *Device) GetScreenText(xStart int, xEnd int, yStart int, yEnd int, white
|
|||||||
return "", fmt.Errorf("failed to encode image: %w", err)
|
return "", fmt.Errorf("failed to encode image: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call tesseract
|
// Call Tesseract
|
||||||
client := gosseract.NewClient()
|
client := gosseract.NewClient()
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
client.SetImageFromBytes(buf.Bytes())
|
client.SetImageFromBytes(buf.Bytes())
|
||||||
|
|||||||
Reference in New Issue
Block a user