Skip to content

Commit

Permalink
feat(orc): support doc2x and simpletxt
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed May 20, 2024
1 parent 9101057 commit 4034a93
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 66 deletions.
3 changes: 0 additions & 3 deletions packages/ohmymn/src/jsExtension/handleReceivedEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ export default defineEventHandlers<
},
async onPopupMenuOnNote(sender) {
if (self.window !== MN.currentWindow) return
// MN.log(MN.studyController.view.frame)
// MN.log(MN.studyController.view.bounds)
// alert(JSON.stringify(MN.studyController.view.frame))
if (MN.studyController.studyMode !== StudyMode.study) return
MN.log("Popup menu on note open", "event")
if (isModuleON("gesture") || isModuleON("toolbar")) {
Expand Down
8 changes: 4 additions & 4 deletions packages/ohmymn/src/modules/addon/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default i18n({
intro: `当前版本:${Addon.version}`,
double_link: "双击打开链接",
use_markdown: {
label: "使用 Markdown 评论",
help: "所有通过 OhMyMN 生成的评论将直接使用 Markdown "
label: "默认使用 Markdown",
help: "所有通过 OhMyMN 生成的评论将使用 Markdown。摘录内容目前不行。"
},
profile: {
$option5: [
Expand Down Expand Up @@ -135,8 +135,8 @@ export default i18n({
label: "Auto Backup Profile"
},
use_markdown: {
label: "Use Markdown Comment",
help: "All comments generated by OhMyMN will use Markdown directly."
label: "Use Markdown by Default",
help: "All comments generated by OhMyMN will use Markdown. Excerpts cannot be used at present."
},
backup_ID: {
help: "Enter the backup card link, please make sure that the card has subcards, otherwise it cannot be written. The more subcards, the better. ",
Expand Down
64 changes: 53 additions & 11 deletions packages/ohmymn/src/modules/autoocr/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { CellViewType } from "~/typings"
import { showHUD } from "marginnote"
import { HUDController, showHUD } from "marginnote"
import { defineConfig } from "~/profile"
import lang from "./lang"
import {
baiduFormulaOCR,
baiduHandWrittingOCR,
doc2xOCR,
mainOCR,
mathpixOCR,
QRCodeOCR
QRCodeOCR,
simpleTexOCR
} from "./utils"
import { doc } from "~/utils"

Expand Down Expand Up @@ -52,6 +54,13 @@ const config = defineConfig({
label: lang.markdown.label,
help: lang.markdown.help
},
{
key: "simpleTexModel",
type: CellViewType.Select,
option: lang.simpletex_model.$option2,
label: lang.simpletex_model.label,
bind: ["formulaOCRProviders", 2]
},
{
key: "showKey",
type: CellViewType.Expland,
Expand All @@ -67,7 +76,7 @@ const config = defineConfig({
{
key: "baiduSecretKey",
type: CellViewType.Input,
help: lang.baidu_secret_key,
help: lang.baidu_secret_key.help,
bind: ["showKey", true]
},
{
Expand All @@ -79,6 +88,26 @@ const config = defineConfig({
["showKey", true],
["formulaOCRProviders", 1]
]
},
{
key: "simpleTexApiKey",
type: CellViewType.Input,
help: lang.simpletex_api_key.help,
link: lang.simpletex_api_key.link,
bind: [
["showKey", true],
["formulaOCRProviders", 2]
]
},
{
key: "doc2xApiKey",
type: CellViewType.Input,
help: lang.doc2x_api_key.help,
link: lang.doc2x_api_key.link,
bind: [
["showKey", true],
["formulaOCRProviders", 3]
]
}
],
actions4text: [
Expand All @@ -87,16 +116,29 @@ const config = defineConfig({
key: "formulaOCR",
label: lang.formula_ocr.label,
option: lang.formula_ocr.$option3,
help: lang.formula_ocr.help,
method: async ({ imgBase64, option }) => {
method: async ({ option, img, imgBase64 }) => {
try {
const res = (
self.globalProfile.autoocr.formulaOCRProviders[0] === 0
? await baiduFormulaOCR(imgBase64)
: await mathpixOCR(imgBase64)
).trim()
return [res, `$${res}$`, `$$\n${res}\n$$`][option]
const fn = (function () {
switch (self.globalProfile.autoocr.formulaOCRProviders[0]) {
case 1:
return mathpixOCR
case 2:
return simpleTexOCR
case 3:
return doc2xOCR
default:
return baiduFormulaOCR
}
})()
HUDController.show("正在识别中...")
let res = await fn(imgBase64, img)
HUDController.hidden()
if (res) {
res = res.trim()
return [res, `$${res}$`, `$$\n${res}\n$$`][option]
}
} catch (err) {
HUDController.hidden()
showHUD(String(err), 2)
}
}
Expand Down
87 changes: 62 additions & 25 deletions packages/ohmymn/src/modules/autoocr/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { doc } from "~/utils"

export default i18n({
zh: {
intro: "使用百度 OCR 服务在摘录时进行小语种的在线矫正。 ",
intro:
"使用百度 OCR 服务在摘录时进行小语种的在线矫正。使用 SimpleTex 等服务进行公式识别。",
on: {
label: "摘录时自动执行",
help: "【仅当前文档】"
Expand Down Expand Up @@ -37,27 +38,49 @@ export default i18n({
},
formula_ocr: {
label: "公式识别",
$option3: ["Pure Latex", "$ Latex $", "$$ Latex $$"] as StringTuple<3>,
help: `"Markdown" 插件请选择 Pure Latex`
$option3: ["Pure Latex", "$ Latex $", "$$ Latex $$"] as StringTuple<3>
},
formula_ocr_providers: {
label: "公式识别提供商",
$option2: ["百度", "Mathpix"] as StringTuple<2>,
help: "公式识别不支持摘录时自动识别,只能在 MagicAtion for Text 中手动进行公式识别。"
$option2: ["百度", "Mathpix", "SimpleTex", "Doc2X"] as StringTuple<4>,
help: "公式识别不支持摘录时自动识别,只能在 MagicAtion for Text 中手动进行公式识别。点击查看使用技巧。",
link: doc("autoocr")
},
markdown: {
label: "使用的 Markdown 插件",
help: "用于显示公式,推荐使用 Milkdown 。",
$option3: ["Markdown", "myMarkDown", "Milkdown"] as StringTuple<3>
},
simpletex_model: {
label: "SimpleTex 模型",
$option2: ["轻量", "标准"] as StringTuple<2>
},
baidu_api_key: {
help: "百度 Api Key,点击查看如何获取。",
link: doc("autoocr", "百度-ocr")
link: doc("autoocr", "百度-ocr"),
no_baidu_api_key: "没有设置百度 OCR Api Key"
},
baidu_secret_key: {
help: "百度 Secret Key",
no_baidu_secret_key: "没有设置百度 OCR Secret Key"
},
baidu_secret_key: "百度 Secret Key",
mathpix_app_key: {
help: "Mathpix App Key,点击查看如何获取。",
link: doc("autoocr", "mathpix")
link: doc("autoocr", "mathpix"),
no_mathpix_key: "没有输入 Mathpix key!",
mathpix_key_error: "Mathpix key 输入错误!"
},
doc2x_api_key: {
help: "Doc2X Api Key,点击查看如何获取。",
link: doc("autoocr", "doc2x"),
no_doc2x_key: "没有输入 Doc2X API key!",
doc2x_key_error: "Doc2X API key 输入错误!"
},
simpletex_api_key: {
help: "SimpleTex Api Key,点击查看如何获取。",
link: doc("autoocr", "simpletex"),
no_simpletex_key: "没有输入 SimpleTex API key!",
simpletex_key_error: "SimpleTex API key 输入错误!"
},
$show_key2: [
"点击查看密钥,不要让其他人看到",
Expand All @@ -70,15 +93,11 @@ export default i18n({
detect_link:
"结果已复制到剪贴板上,但检测到识别结果中含有链接,是否直接在浏览器中打开?",
baidu_token_error: "百度 OCR Api Key 或 Secret Key 输入错误!",
no_mathpix_key: "没有输入 Mathpix key!",
mathpix_key_error: "Mathpix key 输入错误!",
success_clipboard: "结果已复制到剪贴板上,快去粘贴吧!",
no_baidu_api_key: "没有设置百度 OCR Api Key",
no_baidu_secret_key: "没有设置百度 OCR Secret Key"
success_clipboard: "结果已复制到剪贴板上,快去粘贴吧!"
},
en: {
intro:
"Use the Baidu OCR service to correct the online small language when excerpting. ",
"Use Baidu OCR service to correct small languages online when excerpting. Use services such as SimpleTex for formula recognition.",
on: {
label: "Auto Run When Excerpting",
help: "[Only Current Document]"
Expand Down Expand Up @@ -112,8 +131,9 @@ export default i18n({
},
formula_ocr_providers: {
label: "Formula OCR Providers",
$option2: ["Baidu", "Mathpix"],
help: "Formula OCR is not supported in auto mode, only in MagicAtion for Text."
$option2: ["Baidu", "Mathpix", "SimpleTex", "Doc2X"],
help: "Formula OCR is not supported in auto mode, only in MagicAtion for Text. Click to see the usage.",
link: doc("autoocr")
},
markdown: {
label: "Markdown Addon Used",
Expand All @@ -122,17 +142,38 @@ export default i18n({
},
formula_ocr: {
label: "Formula OCR",
help: 'For "Markdown" Addon, please choose Pure Latex',
$option3: ["Pure Latex", "$ Latex $", "$$ Latex $$"]
},
baidu_api_key: {
link: doc("autoocr", "baidu-ocr"),
help: "Baidu Api Key, click to see how to get it."
help: "Baidu Api Key, click to see how to get it.",
no_baidu_api_key: "No Baidu OCR Api Key"
},
baidu_secret_key: {
help: "Baidu Secret Key",
no_baidu_secret_key: "No Baidu OCR Secret Key"
},
baidu_secret_key: "Baidu Secret Key",
mathpix_app_key: {
link: doc("autoocr", "mathpix"),
help: "Mathpix App Key, click to see how to get it."
help: "Mathpix App Key, click to see how to get it.",
no_mathpix_key: "No Mathpix key!",
mathpix_key_error: "Mathpix key is wrong!"
},
doc2x_api_key: {
link: doc("autoocr", "doc2x"),
help: "Doc2X API Key, click to see how to get it.",
doc2x_key_error: "Doc2X API key is wrong!",
no_doc2x_key: "No Doc2X API key!"
},
simpletex_api_key: {
link: doc("autoocr", "simpletex"),
help: "SimpleTex API Key, click to see how to get it.",
no_simpletex_key: "No SimpleTex API key!",
simpletex_key_error: "SimpleTex API key is wrong!"
},
simpletex_model: {
label: "SimpleTex Model",
$option2: ["Light", "Standard"]
},
$show_key2: ["Click to show secret key", "Click to hide secret key"],
text_ocr: "Text OCR",
Expand All @@ -142,11 +183,7 @@ export default i18n({
detect_link:
"The result has been copied to the clipboard, but a link is detected in the recognition result, is it opened directly?",
baidu_token_error: "Api Key or Secret Key is wrong!",
no_mathpix_key: "No Mathpix key!",
mathpix_key_error: "Mathpix key is wrong!",
success_clipboard:
"The results have been copied to the clipboard, go ahead and paste them!",
no_baidu_api_key: "No Baidu OCR Api Key",
no_baidu_secret_key: "No Baidu OCR Secret Key"
"The results have been copied to the clipboard, go ahead and paste them!"
}
})
Loading

0 comments on commit 4034a93

Please sign in to comment.