我正在尝试获取图像上特定文本的 x 和 y 坐标,例如 。在这张图片上,我试图检测
X:input Y:input
所在的位置,这可能是未来图像上的任何地方。在这种情况下,我希望它在714, 164, 125, 32
(x,y,宽度高度)左右。
我尝试使用 Tesseract 和 Jimp
const worker = await Tesseract.createWorker();
await worker.loadLanguage("eng");
await worker.initialize("eng");
const convertedImage = await image
.grayscale()
.getBufferAsync(Jimp.MIME_PNG);
await worker.setParameters({ tessedit_char_whitelist: "XY012345678" });
const { data } = await worker.recognize(convertedImage);
但我不确定
data
中的任何内容是否能让我得到想要的结果。我不知道其他可能对我有帮助的图书馆
您需要从图像中裁剪出文字。
图像太嘈杂,即使将其转换为灰度。
此外,您可以将
tessedit_pageseg_mode
设置为PSM.SINGLE_LINE
。
import path from "path";
import Jimp from "jimp";
import { createWorker, PSM } from "tesseract.js";
const __dirname = path.resolve();
const main = async () => {
const position = await getPosition(
path.join(__dirname, "image.png"),
700,
160,
150,
40
);
console.log(position); // { x: 323, y: 528 }
};
const getPosition = async (imagePath, xOffset, yOffset, width, height) => {
const worker = await createWorker({
logger: (m) => {
// console.log(m);
},
});
await worker.loadLanguage("eng");
await worker.initialize("eng");
await worker.setParameters({
tessedit_char_whitelist: "XY012345678: ",
tessedit_pageseg_mode: PSM.SINGLE_LINE,
});
const image = await Jimp.read(imagePath);
const convertedImage = image
.grayscale()
.contrast(0.3)
.crop(
xOffset ?? 0,
yOffset ?? 0,
width ?? image.bitmap.width,
height ?? image.bitmap.height
)
.write("greyscale.jpg");
const base64 = await convertedImage.getBase64Async(Jimp.AUTO);
const {
data: { text },
} = await worker.recognize(base64);
let [x, y] = text
.match(/X:(\d+)Y:(\d+)/)
?.slice(1)
?.map((v) => parseInt(v, 10)) || [-1, -1];
await worker.terminate();
return { x, y };
};
(async () => {
await main();
})();