OCR摩拉log (#539)

* update repo.json

* OCR摩拉log

OCR识别摩拉输出到log

* update repo.json

---------

Co-authored-by: JJMdzh <205532257+JJMdzh@users.noreply.github.com>
This commit is contained in:
JJMdzh
2025-04-08 19:49:42 +08:00
committed by GitHub
parent f6a00c8c96
commit af111da778
5 changed files with 176 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
{
"time": "20250408175531",
"time": "20250408194152",
"url": "https://github.com/babalae/bettergi-scripts-list/archive/refs/heads/main.zip",
"file": "repo.json",
"indexes": [
@@ -31781,6 +31781,15 @@
"description": "切换下一队~|~用于切换到下一队",
"tags": []
},
{
"name": "OCR摩拉log",
"type": "directory",
"hash": "823852e0542c74066794f2125532f9bb1927d94c",
"version": "1.0",
"author": "吉吉喵",
"description": "OCR摩拉log~|~OCR识别摩拉输出到log",
"tags": []
},
{
"name": "OCR购买食材",
"type": "directory",

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 B

View File

@@ -0,0 +1,146 @@
// 定义替换映射表
const replacementMap = {
"监": "盐",
"卵": "卯"
};
// 定义所有图标的图像识别对象,每个图片都有自己的识别区域
let CharacterMenuRo = RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/CharacterMenu.png"), 60, 991, 38, 38);
// 定义一个函数用于识别图像
async function recognizeImage(recognitionObject, timeout = 5000) {
let startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
// 尝试识别图像
let imageResult = captureGameRegion().find(recognitionObject);
if (imageResult) {
// log.info(`成功识别图像,坐标: x=${imageResult.x}, y=${imageResult.y}`);
// log.info(`图像尺寸: width=${imageResult.width}, height=${imageResult.height}`);
return { success: true, x: imageResult.x, y: imageResult.y };
}
} catch (error) {
log.error(`识别图像时发生异常: ${error.message}`);
}
await sleep(500); // 短暂延迟,避免过快循环
}
log.warn(`经过多次尝试,仍然无法识别图像`);
return { success: false };
}
// 定义一个函数用于识别文字并点击
async function recognizeTextAndClick(targetText, ocrRegion, timeout = 5000) {
let startTime = Date.now();
let retryCount = 0; // 重试计数
while (Date.now() - startTime < timeout) {
try {
// 尝试 OCR 识别
let resList = captureGameRegion().findMulti(RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height)); // 指定识别区域
// 遍历识别结果,检查是否找到目标文本
for (let res of resList) {
// 后处理:根据替换映射表检查和替换错误识别的字符
let correctedText = res.text;
for (let [wrongChar, correctChar] of Object.entries(replacementMap)) {
correctedText = correctedText.replace(new RegExp(wrongChar, 'g'), correctChar);
}
if (correctedText.includes(targetText)) {
// 如果找到目标文本,计算并点击文字的中心坐标
let centerX = res.x + res.width / 2;
let centerY = res.y + res.height / 2;
await click(centerX, centerY);
await sleep(500); // 确保点击后有足够的时间等待
return { success: true, x: centerX, y: centerY };
}
}
} catch (error) {
retryCount++; // 增加重试计数
log.warn(`页面标志识别失败,正在进行第 ${retryCount} 次重试...`);
}
await sleep(1000); // 短暂延迟,避免过快循环
}
log.warn(`经过多次尝试,仍然无法识别文字: ${targetText}`);
return { success: false };
}
// 定义一个独立的函数用于在指定区域进行 OCR 识别并输出识别内容
async function recognizeTextInRegion(ocrRegion, timeout = 5000) {
let startTime = Date.now();
let retryCount = 0; // 重试计数
while (Date.now() - startTime < timeout) {
try {
// 在指定区域进行 OCR 识别
let ocrResult = captureGameRegion().find(RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height));
if (ocrResult) {
// 后处理:根据替换映射表检查和替换错误识别的字符
let correctedText = ocrResult.text;
for (let [wrongChar, correctChar] of Object.entries(replacementMap)) {
correctedText = correctedText.replace(new RegExp(wrongChar, 'g'), correctChar);
}
return correctedText; // 返回识别到的内容
} else {
log.warn(`OCR 识别区域未找到内容`);
return null; // 如果 OCR 未识别到内容,返回 null
}
} catch (error) {
retryCount++; // 增加重试计数
log.warn(`OCR 摩拉数识别失败,正在进行第 ${retryCount} 次重试...`);
}
await sleep(500); // 短暂延迟,避免过快循环
}
log.warn(`经过多次尝试,仍然无法在指定区域识别到文字`);
return null; // 如果未识别到文字,返回 null
}
(async function () {
setGameMetrics(1920, 1080, 1);
await genshin.returnMainUi();
const notification = settings.notification || false;
// 按下 C 键
keyPress("C");
await sleep(1500);
let recognized = false;
// 识别“角色菜单”图标或“天赋”文字
let startTime = Date.now();
while (Date.now() - startTime < 5000) {
// 尝试识别“角色菜单”图标
let characterMenuResult = await recognizeImage(CharacterMenuRo, 5000);
if (characterMenuResult.success) {
await click(177, 433);
await sleep(500);
recognized = true;
break;
}
// 尝试识别“天赋”文字
let targetText = "天赋";
let ocrRegion = { x: 133, y: 395, width: 115, height: 70 }; // 设置对应的识别区域
let talentResult = await recognizeTextAndClick(targetText, ocrRegion);
if (talentResult.success) {
log.info(`点击天赋文字,坐标: x=${talentResult.x}, y=${talentResult.y}`);
recognized = true;
break;
}
await sleep(1000); // 短暂延迟,避免过快循环
}
// 如果识别到了“角色菜单”或“天赋”,则识别“摩拉数值”
if (recognized) {
let ocrRegionMora = { x: 1620, y: 25, width: 152, height: 46 }; // 设置对应的识别区域
let recognizedText = await recognizeTextInRegion(ocrRegionMora);
if (recognizedText) {
log.info(`成功识别到摩拉数值: ${recognizedText}`);
if (notification) {
notification.Send(`摩拉数值: ${recognizedText}`);
}
} else {
log.warn("未能识别到摩拉数值。");
}
} else {
log.warn("未能识别到角色菜单或天赋,跳过摩拉数值识别。");
}
})();

View File

@@ -0,0 +1,13 @@
{
"manifest_version": 1,
"name": "OCR摩拉log",
"version": "1.0",
"description": "OCR识别摩拉输出到log",
"authors": [
{
"name": "吉吉喵",
}
],
"settings_ui": "settings.json",
"main": "main.js",
}

View File

@@ -0,0 +1,7 @@
[
{
"name": "notification",
"type": "checkbox",
"label": "通知"
}
]