Skip to content

Commit

Permalink
add GoogleMLKit support
Browse files Browse the repository at this point in the history
  • Loading branch information
sec-an committed Sep 9, 2022
1 parent aed7c85 commit 3992dad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
71 changes: 60 additions & 11 deletions 0.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var meiri = TTXS_PRO_CONFIG.get("meiri", true);
var meizhou = TTXS_PRO_CONFIG.get("meizhou", 0);
var zhuanxiang = TTXS_PRO_CONFIG.get("zhuanxiang", 0);
var tiaozhan = TTXS_PRO_CONFIG.get("tiaozhan", true);
var ocr_plugin = TTXS_PRO_CONFIG.get("ocr_plugin", false);
var ocr_choice = TTXS_PRO_CONFIG.get("ocr_choice", 0);
var ocr_maxtime = TTXS_PRO_CONFIG.get("ocr_maxtime", "5000");
var duizhan_mode = TTXS_PRO_CONFIG.get("duizhan_mode", 0);
var jisu = TTXS_PRO_CONFIG.get("jisu", "0");
Expand All @@ -31,6 +31,46 @@ var yl_on = TTXS_PRO_CONFIG.get("yl_on", true);
var yinliang = TTXS_PRO_CONFIG.get("yinliang", "0");
var zhanghao = TTXS_PRO_CONFIG.get("zhanghao", "");

function google_ocr_api(img) {
console.log('GoogleMLKit文字识别中');
let list = JSON.parse(JSON.stringify(gmlkit.ocr(img,"zh").toArray(3))); // 识别文字,并得到results
let eps = 30; // 坐标误差
for (
var i = 0; i < list.length; i++ // 选择排序对上下排序,复杂度O(N²)但一般list的长度较短只需几十次运算
) {
for (var j = i + 1; j < list.length; j++) {
if (list[i]['bounds']['bottom'] > list[j]['bounds']['bottom']) {
var tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
}

for (
var i = 0; i < list.length; i++ // 在上下排序完成后,进行左右排序
) {
for (var j = i + 1; j < list.length; j++) {
// 由于上下坐标并不绝对,采用误差eps
if (
Math.abs(list[i]['bounds']['bottom'] - list[j]['bounds']['bottom']) <
eps &&
list[i]['bounds']['left'] > list[j]['bounds']['left']
) {
var tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
}
let res = '';
for (var i = 0; i < list.length; i++) {
res += list[i]['text'];
}
list = null;
return res;
}

function paddle_ocr_api() {
console.log('PaddleOCR文字识别中');
let list = JSON.parse(JSON.stringify(paddle.ocr(arguments[0]))); // 识别文字,并得到results
Expand Down Expand Up @@ -66,7 +106,7 @@ function paddle_ocr_api() {
}
let res = '';
for (var i = 0; i < list.length; i++) {
res += list[i]['words'];
res += list[i]['text'];
}
list = null;
return res;
Expand Down Expand Up @@ -97,7 +137,7 @@ fInfo("天天向上Pro"+newest_version+"脚本初始化");
var [device_w, device_h] = init_wh();
// log("fina:", device_w, device_h);
// OCR初始化,重写内置OCR module
if (ocr_plugin) {
if (ocr_choice == 2) {
fInfo("初始化第三方ocr插件");
try {
ocr = plugins.load("com.hraps.ocr");
Expand Down Expand Up @@ -980,7 +1020,9 @@ function do_duizhan1(renshu) {
// 为了适配OCR插件改为下面这句
console.time('题目识别');

if (!ocr_plugin) {
if (ocr_choice == 0) {
var que_txt = google_ocr_api(que_img).replace(/[^\u4e00-\u9fa5\d]|\d{1,2}\./g, "");
} else if (ocr_choice == 1) {
var que_txt = paddle_ocr_api(que_img).replace(/[^\u4e00-\u9fa5\d]|\d{1,2}\./g, "");
} else {
var que_txt = ocr.recognizeText(que_img).replace(/[^\u4e00-\u9fa5\d]|\d{1,2}\./g, "");
Expand Down Expand Up @@ -1091,12 +1133,15 @@ function do_duizhan1(renshu) {
//images.save(allx_img, '/sdcard/1/x_img' + num + '.png');
let xuan_txt_list = [];
let allx_txt = "";
if (!ocr_plugin) {
if (ocr_choice == 0) {
// 排序顺序
// console.time('选项识别1');
let x_results = JSON.parse(JSON.stringify(paddle.ocr(img)));
let x_results = JSON.parse(JSON.stringify(gmlkit.ocr(img,"zh").toArray(3)));
allx_txt = ocr_rslt_to_txt(x_results).replace(/\s+/g, "");
// console.timeEnd('选项识别1');
} else if (ocr_choice == 1) {
let x_results = JSON.parse(JSON.stringify(paddle.ocr(img)));
allx_txt = ocr_rslt_to_txt(x_results).replace(/\s+/g, "");
} else {
// // 直接识别
// console.time('选项识别2');
Expand Down Expand Up @@ -1669,7 +1714,9 @@ function get_ans_by_ocr1() {
// let resp = ocr.recognize(img).results;
// ans = ocr_rslt_to_txt(resp);
// 为适配第三方OCR改动
if (!ocr_plugin) {
if (ocr_choice == 0) {
ans = google_ocr_api(img);
} else if (ocr_choice == 1) {
ans = paddle_ocr_api(img);
} else {
ans = ocr.recognizeText(img);
Expand Down Expand Up @@ -1898,13 +1945,13 @@ function ocr_rslt_to_txt(result) {
if (top == 0) {top = result[idx].bounds.top;}
if (previous_left == 0) {previous_left = result[idx].bounds.left;}
if (result[idx].bounds.top >= top-10 && result[idx].bounds.top <= top+10) {
if (result[idx].bounds.left > previous_left) {txt = txt + " " + result[idx].words;}
else {txt = result[idx].words + " " + txt;}
if (result[idx].bounds.left > previous_left) {txt = txt + " " + result[idx].text;}
else {txt = result[idx].text + " " + txt;}
}
else {
top = result[idx].bounds.top;
txt_list.push(txt);
txt = result[idx].words;
txt = result[idx].text;
}
if (idx == result.length-1) {txt_list.push(txt);}
previous_left = result[idx].bounds.left;
Expand Down Expand Up @@ -2033,7 +2080,9 @@ function ocr_test() {
//console.time("OCR识别结束");
let begin=new Date();

if (!ocr_plugin) {
if (ocr_choice == 0) {
let test_txt = google_ocr_api(img_test);
} else if (ocr_choice == 1){
let test_txt = paddle_ocr_api(img_test);
} else {
let test_txt = ocr.recognizeText(img_test);
Expand Down
16 changes: 7 additions & 9 deletions UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,9 @@ ui.layout(
<horizontal gravity="center_vertical" padding="5 5" >
<View bg="#00BFFF" h="*" w="10" ></View>
<vertical padding="10 8" h="auto" w="0" layout_weight="1">
<text w="auto" textColor="#222222" textSize="15sp" text="是否启用第三方OCR插件" />
<text w="auto" textColor="#999999" textSize="12sp" text="内置ocr很慢的可以试试(并没有内置的好)" />
<text w="auto" textColor="#999999" textSize="12sp" text="需要提前安装,未安装会跳转浏览器下载" />
</vertical>
<checkbox id="ttxs_pro_ocr_plugin" marginLeft="4" marginRight="6" checked="false" />
<text w="auto" textColor="#222222" textSize="15sp" text="OCR选择" />
<spinner id="ttxs_pro_ocr_choice" marginLeft="4" marginRight="6" entries="GoogleMLKit|PaddleOCR|第三方插件" />
</vertical>
</horizontal>
<horizontal gravity="center_vertical" padding="5 5" >
<View bg="#00BFFF" h="*" w="10" ></View>
Expand Down Expand Up @@ -627,7 +625,7 @@ ui.ttxs_pro_save.click(function () {
TTXS_PRO_CONFIG.put("meizhou", ui.ttxs_pro_meizhou.getSelectedItemPosition());
TTXS_PRO_CONFIG.put("zhuanxiang", ui.ttxs_pro_zhuanxiang.getSelectedItemPosition());
TTXS_PRO_CONFIG.put("tiaozhan", ui.ttxs_pro_tiaozhan.isChecked());
TTXS_PRO_CONFIG.put("ocr_plugin", ui.ttxs_pro_ocr_plugin.isChecked());
TTXS_PRO_CONFIG.put("ocr_choice", ui.ttxs_pro_ocr_choice.getSelectedItemPosition());
TTXS_PRO_CONFIG.put("ocr_maxtime", ui.ttxs_pro_ocr_maxtime.getText()+"");
TTXS_PRO_CONFIG.put("duizhan_mode", ui.ttxs_pro_duizhan_mode.getSelectedItemPosition());
TTXS_PRO_CONFIG.put("jisu", ui.ttxs_pro_jisu.getText()+"");
Expand Down Expand Up @@ -671,8 +669,8 @@ ui.ttxs_pro_reset.click(function () {
ui.ttxs_pro_zhuanxiang.setSelection(TTXS_PRO_CONFIG.get("zhuanxiang"));
TTXS_PRO_CONFIG.put("tiaozhan", true);
ui.ttxs_pro_tiaozhan.setChecked(TTXS_PRO_CONFIG.get("tiaozhan"));
TTXS_PRO_CONFIG.put("ocr_plugin", false);
ui.ttxs_pro_ocr_plugin.setChecked(TTXS_PRO_CONFIG.get("ocr_plugin"));
TTXS_PRO_CONFIG.put("ocr_choice", 0);
ui.ttxs_pro_ocr_choice.setSelection(TTXS_PRO_CONFIG.get("ocr_choice"));
TTXS_PRO_CONFIG.put("ocr_maxtime", "5000");
ui.ttxs_pro_ocr_maxtime.setText(TTXS_PRO_CONFIG.get("ocr_maxtime"));
TTXS_PRO_CONFIG.put("duizhan_mode", 0);
Expand Down Expand Up @@ -814,7 +812,7 @@ function Initialize() {
ui.ttxs_pro_meizhou.setSelection(TTXS_PRO_CONFIG.get("meizhou", 0));
ui.ttxs_pro_zhuanxiang.setSelection(TTXS_PRO_CONFIG.get("zhuanxiang", 0));
ui.ttxs_pro_tiaozhan.setChecked(TTXS_PRO_CONFIG.get("tiaozhan", true));
ui.ttxs_pro_ocr_plugin.setChecked(TTXS_PRO_CONFIG.get("ocr_plugin", false));
ui.ttxs_pro_ocr_choice.setSelection(TTXS_PRO_CONFIG.get("ocr_choice", 0));
ui.ttxs_pro_ocr_maxtime.setText(TTXS_PRO_CONFIG.get("ocr_maxtime", "5000"));
ui.ttxs_pro_duizhan_mode.setSelection(TTXS_PRO_CONFIG.get("duizhan_mode", 0));
ui.ttxs_pro_jisu.setText(TTXS_PRO_CONFIG.get("jisu", "0"));
Expand Down

0 comments on commit 3992dad

Please sign in to comment.