Skip to content

Commit

Permalink
dev3 优化忽略区域:1.计算缩放系数时减少一次除法,提高浮点数精度。
Browse files Browse the repository at this point in the history
2.绘制UI方框增加xy轴2像素偏移量,使显示效果更精准。
  • Loading branch information
hiroi-sora committed Nov 13, 2022
1 parent 94aa220 commit 41da8b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from utils.config import Umi
from ui.win_main import MainWin

Umi.ver = '1.3.2 dev'
Umi.ver = '1.3.2 dev 3'
Umi.pname = 'Umi-OCR'
Umi.name = f'{Umi.pname} v{Umi.ver}'
Umi.website = 'https://github.com/hiroi-sora/Umi-OCR'
Expand Down
8 changes: 5 additions & 3 deletions ui/win_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ def offAllOutput(e): # 关闭全部输出
wid = ttk.Checkbutton(fr1, text='图片中不含文字时,不输出信息',
variable=Config.getTK('isIgnoreNoText'),)
wid.grid(column=0, row=10, columnspan=9, sticky='w')
self.lockWidget.append(wid)

tk.Label(fOutput, fg='gray',
text="下面两项为空时,默认输出到第一张图片所在的文件夹"
Expand Down Expand Up @@ -933,13 +934,14 @@ def closeSelectArea(self): # 关闭选择区域,获取选择区域数据
self.ignoreLabel["text"] = f"生效分辨率:\n{area['size'][0]}\n{area['size'][1]}"
self.canvas.delete(tk.ALL) # 清除画布
scale = self.canvasHeight / area['size'][1] # 显示缩放比例
width = int(self.canvasHeight * (area['size'][0] / area['size'][1]))
width = round(self.canvasHeight * (area['size'][0] / area['size'][1]))
self.canvas['width'] = width
areaColor = ["red", "green", "darkorange"]
tran = 2 # 绘制偏移量
for i in range(3): # 绘制新图
for a in area['area'][i]:
x0, y0 = a[0][0]*scale, a[0][1]*scale,
x1, y1 = a[1][0]*scale, a[1][1]*scale,
x0, y0 = a[0][0]*scale+tran, a[0][1]*scale+tran,
x1, y1 = a[1][0]*scale+tran, a[1][1]*scale+tran,
self.canvas.create_rectangle(
x0, y0, x1, y1, fill=areaColor[i])
self.ignoreBtn.pack_forget() # 隐藏按钮
Expand Down
25 changes: 14 additions & 11 deletions ui/win_select_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, closeSendData=None, defaultPath=""):
self.balloon = Config.main.balloon # 气泡框
self.cW = 960 # 画板尺寸
self.cH = 540
self.tran = 2 # 绘制偏移量
self.areaColor = ["red", "green", # 各个矩形区域的标志颜色
"darkorange", "white"]

Expand Down Expand Up @@ -193,12 +194,13 @@ def loadImage(self, path): # 载入新图片
self.imgSize = img.size
# 计算 按宽和高 分别缩放的比例
sw, sh = self.cW/img.size[0], self.cH/img.size[1]
if sw > sh: # 测试,按宽、还是高缩放,刚好填满画布
# 测试,按宽、还是高缩放,刚好填满画布
if sw > sh: # 按高
self.imgReSize = (round(img.size[0]*sh), self.cH)
else:
self.imgScale = sh
else: # 按宽
self.imgReSize = (self.cW, round(img.size[1]*sw))
# 记录缩放系数,原分辨率*系数=绘制分辨率
self.imgScale = self.imgReSize[0]/self.imgSize[0]
self.imgScale = sw
self.imgSizeText.set(f'{self.imgSize[0]}x{self.imgSize[1]}')
elif not self.imgSize == img.size: # 尺寸不符合
tk.messagebox.showwarning("图片尺寸错误!",
Expand Down Expand Up @@ -233,10 +235,10 @@ def runOCR():
data['data'], s = tbpu.run(data['data'], imgInfo)
for o in data["data"]: # 绘制矩形框
# 提取左上角、右下角的坐标
p1x = round(o['box'][0][0]*self.imgScale)
p1y = round(o['box'][0][1]*self.imgScale)
p2x = round(o['box'][2][0]*self.imgScale)
p2y = round(o['box'][2][1]*self.imgScale)
p1x = round(o['box'][0][0]*self.imgScale)+self.tran
p1y = round(o['box'][0][1]*self.imgScale)+self.tran
p2x = round(o['box'][2][0]*self.imgScale)+self.tran
p2y = round(o['box'][2][1]*self.imgScale)+self.tran
r1 = self.canvas.create_rectangle(
p1x, p1y, p2x, p2y, outline='white', width=2) # 绘制白实线基底
r2 = self.canvas.create_rectangle(
Expand Down Expand Up @@ -319,8 +321,8 @@ def mouseDown(self, event): # 鼠标按下,生成新矩形
elif y < 0:
y = 0
c = self.areaColor[self.areaType]
id = self.canvas.create_rectangle(
x, y, x, y, width=2, activefill=c, outline=c) # 绘制新图
id = self.canvas.create_rectangle( # 绘制新图
x+self.tran, y+self.tran, x+self.tran, y+self.tran, width=2, activefill=c, outline=c)
self.area[self.areaType].append([(x, y), (x, y)]) # 向对应列表中增加1个矩形
self.areaHistory.append({"id": id, "type": self.areaType}) # 载入历史记录

Expand All @@ -338,5 +340,6 @@ def mouseMove(self, event): # 鼠标拖动,刷新最后的矩形
y = 0
x0, y0 = self.area[self.areaType][-1][0][0], self.area[self.areaType][-1][0][1]
# 刷新历史记录中最后的图形的坐标
self.canvas.coords(self.areaHistory[-1]["id"], x0, y0, x, y)
self.canvas.coords(
self.areaHistory[-1]["id"], x0+self.tran, y0+self.tran, x+self.tran, y+self.tran)
self.area[self.areaType][-1][1] = (x, y) # 刷新右下角点

0 comments on commit 41da8b9

Please sign in to comment.