Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
liumingye committed Aug 30, 2024
1 parent 3be60e0 commit 3274d1f
Show file tree
Hide file tree
Showing 28 changed files with 1,412 additions and 334 deletions.
45 changes: 45 additions & 0 deletions .yarnclean
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# test directories
__tests__
test
tests
powered-test

# asset directories
docs
doc
website
images
#assets

# examples
example
examples

# code coverage directories
coverage
.nyc_output

# build scripts
Makefile
Gulpfile.js
Gruntfile.js

# configs
appveyor.yml
circle.yml
codeship-services.yml
codeship-steps.yml
wercker.yml
.tern-project
.gitattributes
.editorconfig
.*ignore
.eslintrc
.jshintrc
.flowconfig
.documentup.json
.yarn-metadata.json
.travis.yml

# misc
*.md
Binary file modified build/logo.ico
Binary file not shown.
27 changes: 22 additions & 5 deletions electron-builder.json5
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@
linux: {
artifactName: "${productName}_${version}_${arch}.${ext}",
target: ["deb"],
icon: "build/logo.ico",
desktop: {
Icon: "build/logo.ico",
},
icon: "build/logo.png",
description: "my-ocr",
category: "Application;AudioVideo;Graphics;",
extraFiles: [
{
from: "./release/ocr_server/",
to: "ocr_server",
},
{
from: "./build/logo.png",
to: "resources",
},
],
desktop:{
Icon: "/opt/MY-OCR/resources/logo.png",
Category: "Application;AudioVideo;Graphics;"
}
},
mac: {
target: [
Expand All @@ -26,6 +39,10 @@
],
artifactName: "${productName}_${version}_${arch}.${ext}",
target: ["dmg", "zip"],
extraFiles: {
from: "./release/ocr_server/",
to: "ocr_server",
},
},
win: {
target: [
Expand Down Expand Up @@ -55,7 +72,7 @@
},
publish: {
provider: "generic",
url: "http://127.0.0.1:5500/",
url: "http://136.33.32.127/my-ocr/",
channel: "latest",
updaterCacheDirName: "my-ocr-updater",
},
Expand Down
27 changes: 22 additions & 5 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function createWindow() {
});

if (process.platform === "darwin") {
app.dock.setIcon(path.join(__dirname, "logo.png"));
app.dock.setIcon(path.join(process.env.VITE_PUBLIC, "logo.png"));
}

// and load the index.html of the app.
Expand All @@ -116,11 +116,28 @@ function createWindow() {
console.log(...args);
},
});

let lastVisualStatus = true

screenshots.on("startCapture", (_e) => {
if (!mainWindow) return;
lastVisualStatus = mainWindow.isVisible()
mainWindow.hide();
});

screenshots.on("endCapture", (_e) => {
if (!mainWindow) return;
if(lastVisualStatus){
mainWindow.show();
mainWindow.focus();
}
});

screenshots.on("ok", (_e, buffer) => {
mainWindow?.webContents.send("SCREENSHOTS:ok", buffer);
mainWindow?.show();
mainWindow?.focus();
if (!mainWindow) return;
mainWindow.webContents.send("SCREENSHOTS:ok", buffer);
mainWindow.show();
mainWindow.focus();
});

ipcMain.on("startCapture", () => {
Expand All @@ -141,7 +158,7 @@ app
.whenReady()
.then(() => {
let mainWindow = createWindow();
tray = new Tray(path.join(process.env.VITE_PUBLIC, "favicon.ico"));
tray = new Tray(path.join(process.env.VITE_PUBLIC, "logo.png"));
tray.setToolTip("MY-OCR");
const menu = getTrayMenu(mainWindow);
if (isMac) {
Expand Down
10 changes: 10 additions & 0 deletions electron/main/screenshots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export default class Screenshots extends Events {
* 开始截图
*/
public async startCapture(): Promise<void> {
const event = new Event();
this.emit("startCapture", event);

await new Promise<void>((resolve) => {
setTimeout(() => resolve(), 100);
}),

this.logger("startCapture");

const display = getDisplay();
Expand All @@ -102,6 +109,9 @@ export default class Screenshots extends Events {
* 结束截图
*/
public async endCapture(): Promise<void> {
const event = new Event();
this.emit("endCapture", event);

this.logger("endCapture");
await this.reset();

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "my-ocr",
"productName": "MY-OCR",
"version": "1.0.0",
"version": "1.0.3",
"private": true,
"main": "dist-electron/main/index.js",
"type": "module",
"homepage": ".",
"author": "liumingye <[email protected]>",
"debug": {
"env": {
"VITE_DEV_SERVER_URL": "http://127.0.0.1:3000/"
Expand Down Expand Up @@ -59,5 +60,6 @@
},
"resolutions": {
"jackspeak": "2.1.1"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Binary file modified public/favicon.ico
Binary file not shown.
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 79 additions & 48 deletions py-service/ocr_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import cv2
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol
from thrift.server import TServer
from thrift_ocr_py.ocr import OcrService
import paddleocr
from tbpu import getParser
import gc
from utils.timer import Timer
from rectify.rectify import correct_image_base64, preProcess

# 内存回收时间
gc_time = 10
gc_time = 60
RUN_PATH = os.path.split(os.path.realpath(__file__))[0]


Expand Down Expand Up @@ -51,61 +52,91 @@ def initOcr():
# 是否使用方向分类器
use_angle_cls=True,
ocr_version="PP-OCRv4",
enable_mkldnn=True,
# enable_mkldnn=True,
)

def readBase64Image(b64):
img_data = re.sub("^data:image/.+;base64,", "", b64)
im = cv2.imdecode(
np.frombuffer(base64.b64decode(img_data), np.uint8), -1
)
if im.shape[2] == 4:
im = cv2.cvtColor(im, cv2.COLOR_BGRA2BGR)
return im

class Ocr_handler:
# def four_point_transform(self, img_path):
# res = {
# "code": 100
# }
# im = readBase64Image(img_path)
# screenCnt, ratio = preProcess(im)
# print('four_point_transform', screenCnt)
# res["data"] = screenCnt
# return json.dumps(res)

def rectify(self, img_path):
res = {
"code": 100
}
im = readBase64Image(img_path)
result = correct_image_base64(im)
print('rectify',result[0:999])
res["data"] = result
return json.dumps(res)

def ocr(self, nid, img_path, config):
timer.cancel()
initOcr()
# 设置config默认值
default_config = {"tbpu": "none"}
config = {**default_config, **config}
res = {}
print(f"start ocr {nid} {img_path}")
res = {
"code": 100
}
# print(f"start ocr {nid} {img_path}")
print(f"start ocr {nid}")
print(f"{nid} start ocr")
try:
if img_path.startswith("data:"):
img_data = re.sub("^data:image/.+;base64,", "", img_path)
im = cv2.imdecode(
np.frombuffer(base64.b64decode(img_data), np.uint8), -1
)
if im.shape[2] == 4:
im = cv2.cvtColor(im, cv2.COLOR_BGRA2BGR)
result = ppocr.ocr(im, cls=True)
else:
result = ppocr.ocr(img_path, cls=True)

if result:
tbs = result[0]
res["code"] = 100

# 处理数据
res["data"] = transform_data(tbs)

# 计算平均置信度
score, num = 0, 0
for r in res["data"]:
score += r["score"]
num += 1
if num > 0:
score /= num
res["score"] = score

# 执行 tbpu
res["data"] = getParser(config["tbpu"]).run(res["data"])
# 如果忽略区域等处理将所有文本删除,则结束tbpu
if not res["data"]:
res["code"] = 101
res["data"] = ""
else:
res["code"] = 901
res["data"] = f"{nid} ocr error"
except Exception as e:
print(f"{nid} ocr error {e}")
res["code"] = 901
res["data"] = f"{nid} ocr error {e}"
# try:
if img_path.startswith("data:"):
im = readBase64Image(img_path)
# im = correct_image(im)
# content = cv2.imencode('.jpg', im)[1]
# base64_str = str(base64.b64encode(content))[2:-1]
# res['image'] = base64_str
result = ppocr.ocr(im, cls=True)
else:
result = ppocr.ocr(img_path, cls=True)

tbs = result[0]

if tbs:
# 处理数据
res["data"] = transform_data(tbs)

# 计算平均置信度
score, num = 0, 0
for r in res["data"]:
score += r["score"]
num += 1
if num > 0:
score /= num
res["score"] = score

# 执行 tbpu
res["data"] = getParser(config["tbpu"]).run(res["data"])

# 如果忽略区域等处理将所有文本删除,则结束tbpu
if not res["data"]:
res["code"] = 101
res["data"] = ""
else:
res["code"] = 100
res["data"] = ""
# except Exception as e:
# print(f"{nid} ocr error {e}")
# res["code"] = 901
# res["data"] = f"{nid} ocr error {e}"
print("done")
timer.reset()
return json.dumps(res)
Expand Down Expand Up @@ -141,9 +172,9 @@ def transform_data(data):
# 选择传输层
tfactory = TTransport.TBufferedTransportFactory()
# 选择传输协议
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
pfactory = TCompactProtocol.TCompactProtocolFactory()
# 创建服务端
server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
print(f"start server on {port}")
server.serve()
print("Done")
1 change: 1 addition & 0 deletions py-service/rectify/___init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['transform','rectify','imgEnhance']
35 changes: 35 additions & 0 deletions py-service/rectify/imgEnhance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from PIL import ImageEnhance
import numpy as np
import cv2

class Enhancer:
def bright(self, image, brightness):
enh_bri = ImageEnhance.Brightness(image)
brightness = brightness
imageBrightend = enh_bri.enhance(brightness)
return imageBrightend

def color(self, image, color):
enh_col = ImageEnhance.Color(image)
color = color
imageColored = enh_col.enhance(color)
return imageColored

def contrast(self, image, contrast):
enh_con = ImageEnhance.Contrast(image)
contrast = contrast
image_contrasted = enh_con.enhance(contrast)
return image_contrasted

def sharp(self, image, sharpness):
enh_sha = ImageEnhance.Sharpness(image)
sharpness = sharpness
image_sharped = enh_sha.enhance(sharpness)
return image_sharped

def gamma(self, image, gamma):
# gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
# gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
# return cv2.LUT(image, gamma_table)
gamma_image = np.power(image / float(np.max(image)), gamma)
return gamma_image
Loading

0 comments on commit 3274d1f

Please sign in to comment.