This commit is contained in:
恍兮惚兮 2024-05-05 11:48:28 +08:00
parent ce45fdcabb
commit 88c08dd822
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,47 @@
from ocrengines.baseocrclass import baseocr
import base64
class OCR(baseocr):
def ocr(self, imgfile):
# https://github.com/dmotz/thing-translator/blob/d1fec3f38d24e973af49766669f9ee00bd9e98a8/src/effects/snap.js
# https://cloud.google.com/vision/docs/ocr?hl=zh-cn
# https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageResponse#EntityAnnotation
self.checkempty(["googlecloudvision"])
ocr_url = (
"https://vision.googleapis.com/v1/images:annotate?key=" + self.config["key"]
)
with open(imgfile, "rb") as f:
data = f.read()
encodestr = str(base64.b64encode(data), "utf-8")
data = {
"requests": [
{
"image": {"content": encodestr},
"features": [{"type": "TEXT_DETECTION"}],
}
]
}
response = self.session.post(ocr_url, json=data)
try:
boxs = []
texts = []
for anno in response.json()["responses"][0]["textAnnotations"]:
texts.append(anno["description"])
boxs.append(
[
anno["boundingPoly"]["vertices"][0]["x"],
anno["boundingPoly"]["vertices"][0]["y"],
anno["boundingPoly"]["vertices"][1]["x"],
anno["boundingPoly"]["vertices"][1]["y"],
anno["boundingPoly"]["vertices"][2]["x"],
anno["boundingPoly"]["vertices"][2]["y"],
anno["boundingPoly"]["vertices"][3]["x"],
anno["boundingPoly"]["vertices"][3]["y"],
]
)
return self.common_solve_text_orientation(boxs, texts)
except:
raise Exception(response.text)

View File

@ -787,6 +787,10 @@
"use": false, "use": false,
"name": "docsumo" "name": "docsumo"
}, },
"googlecloudvision": {
"use": false,
"name": "googlecloudvision"
},
"youdaocr": { "youdaocr": {
"use": false, "use": false,
"name": "有道OCR" "name": "有道OCR"

View File

@ -212,6 +212,11 @@
} }
} }
}, },
"googlecloudvision": {
"args": {
"key": ""
}
},
"mangaocr": { "mangaocr": {
"args": { "args": {
"项目仓库": "https://github.com/kha-white/manga-ocr", "项目仓库": "https://github.com/kha-white/manga-ocr",