Skip to content

Commit 81a9c55

Browse files
authored
Add label_font_path parameter to change the font of the label (#100)
Add label_font_path parameter to change the font of the label Co-authored-by: [email protected] <7p=e763wN3A6k+[C>
1 parent 5915995 commit 81a9c55

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

PPOCRLabel.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@
3838
QPointF,
3939
QProcess,
4040
)
41-
from PyQt5.QtGui import QImage, QCursor, QPixmap, QImageReader, QColor, QIcon
41+
from PyQt5.QtGui import (
42+
QImage,
43+
QCursor,
44+
QPixmap,
45+
QImageReader,
46+
QColor,
47+
QIcon,
48+
QFontDatabase,
49+
)
4250
from PyQt5.QtWidgets import (
4351
QMainWindow,
4452
QListWidget,
@@ -141,6 +149,7 @@ def __init__(
141149
rec_model_dir=None,
142150
rec_char_dict_path=None,
143151
cls_model_dir=None,
152+
label_font_path=None,
144153
):
145154
super(MainWindow, self).__init__()
146155
self.setWindowTitle(__appname__)
@@ -1190,6 +1199,15 @@ def getStr(strId):
11901199
if self.filePath and os.path.isdir(self.filePath):
11911200
self.openDirDialog(dirpath=self.filePath, silent=True)
11921201

1202+
# load font
1203+
self.label_font_family = None
1204+
if label_font_path is not None:
1205+
label_font_id = QFontDatabase.addApplicationFont(label_font_path)
1206+
if label_font_id >= 0:
1207+
self.label_font_family = QFontDatabase.applicationFontFamilies(
1208+
label_font_id
1209+
)[0]
1210+
11931211
def menu(self, title, actions=None):
11941212
menu = self.menuBar().addMenu(title)
11951213
if actions:
@@ -1640,7 +1658,12 @@ def loadLabels(self, shapes):
16401658
s = []
16411659
shape_index = 0
16421660
for label, points, line_color, key_cls, difficult in shapes:
1643-
shape = Shape(label=label, line_color=line_color, key_cls=key_cls)
1661+
shape = Shape(
1662+
label=label,
1663+
line_color=line_color,
1664+
key_cls=key_cls,
1665+
font_family=self.label_font_family,
1666+
)
16441667
for x, y in points:
16451668
# Ensure the labels are within the bounds of the image. If not, fix them.
16461669
x, y, snapped = self.canvas.snapPointToCanvas(x, y)
@@ -3574,6 +3597,7 @@ def get_main_app(argv=[]):
35743597
arg_parser.add_argument(
35753598
"--bbox_auto_zoom_center", type=str2bool, default=False, nargs="?"
35763599
)
3600+
arg_parser.add_argument("--label_font_path", type=str, default=None, nargs="?")
35773601

35783602
args = arg_parser.parse_args(argv[1:])
35793603

@@ -3588,6 +3612,7 @@ def get_main_app(argv=[]):
35883612
rec_char_dict_path=args.rec_char_dict_path,
35893613
cls_model_dir=args.cls_model_dir,
35903614
bbox_auto_zoom_center=args.bbox_auto_zoom_center,
3615+
label_font_path=args.label_font_path,
35913616
)
35923617
win.show()
35933618
return app, win

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PPOCRLabelv2 is a semi-automatic graphic annotation tool suitable for OCR field,
1616

1717
### Recent Update
1818

19+
- 2024.11:
20+
- Add `label_font_path` parameter to change the font of the label.
1921
- 2024.09:
2022
- Added `Re-recognition` and `Auto Save Unsaved changes` features. For usage details, please refer to the "11. Additional Feature Description" in the "2.1 Operational Steps" section below.
2123
- Added the parameter `--img_list_natural_sort`, which defaults to natural sorting for the left image list. After configuring this parameter, character sorting will be used to easily locate images based on character order.

README_ch.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具,内置P
1616

1717
#### 近期更新
1818

19+
- 2024.11:
20+
- 新增`label_font_path`参数,用来改变标签字体
1921
- 2024.09:
2022
- 新增`自动重新识别``自动保存未提交变更`功能,使用方法详见下方`2.1 操作步骤``11. 补充功能说明`
2123
- 新增`--img_list_natural_sort`参数,默认左侧图片列表使用自然排序,配置该参数后,将使用字符排序,方便根据字符顺序定位图片。

libs/shape.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(
5555
key_cls="None",
5656
paintLabel=False,
5757
paintIdx=False,
58+
font_family=None,
5859
):
5960
self.label = label
6061
self.idx = None # bbox order, only for table annotation
@@ -78,7 +79,7 @@ def __init__(
7879
self.fontsize = 8
7980

8081
self._closed = False
81-
82+
self.font_family = font_family
8283
if line_color is not None:
8384
# Override the class line_color attribute
8485
# with an object attribute. Currently this
@@ -173,6 +174,8 @@ def paint(self, painter):
173174
min_y = min(min_y, point.y())
174175
if min_x != sys.maxsize and min_y != sys.maxsize:
175176
font = QFont()
177+
if self.font_family is not None:
178+
font.setFamily(self.font_family)
176179
font.setPointSize(self.fontsize)
177180
font.setBold(True)
178181
painter.setFont(font)

0 commit comments

Comments
 (0)