Skip to content

Commit

Permalink
Add label_font_path parameter to change the font of the label (#100)
Browse files Browse the repository at this point in the history
Add label_font_path parameter to change the font of the label

Co-authored-by: [email protected] <7p=e763wN3A6k+[C>
  • Loading branch information
yes-github authored Oct 30, 2024
1 parent 5915995 commit 81a9c55
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
29 changes: 27 additions & 2 deletions PPOCRLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
QPointF,
QProcess,
)
from PyQt5.QtGui import QImage, QCursor, QPixmap, QImageReader, QColor, QIcon
from PyQt5.QtGui import (
QImage,
QCursor,
QPixmap,
QImageReader,
QColor,
QIcon,
QFontDatabase,
)
from PyQt5.QtWidgets import (
QMainWindow,
QListWidget,
Expand Down Expand Up @@ -141,6 +149,7 @@ def __init__(
rec_model_dir=None,
rec_char_dict_path=None,
cls_model_dir=None,
label_font_path=None,
):
super(MainWindow, self).__init__()
self.setWindowTitle(__appname__)
Expand Down Expand Up @@ -1190,6 +1199,15 @@ def getStr(strId):
if self.filePath and os.path.isdir(self.filePath):
self.openDirDialog(dirpath=self.filePath, silent=True)

# load font
self.label_font_family = None
if label_font_path is not None:
label_font_id = QFontDatabase.addApplicationFont(label_font_path)
if label_font_id >= 0:
self.label_font_family = QFontDatabase.applicationFontFamilies(
label_font_id
)[0]

def menu(self, title, actions=None):
menu = self.menuBar().addMenu(title)
if actions:
Expand Down Expand Up @@ -1640,7 +1658,12 @@ def loadLabels(self, shapes):
s = []
shape_index = 0
for label, points, line_color, key_cls, difficult in shapes:
shape = Shape(label=label, line_color=line_color, key_cls=key_cls)
shape = Shape(
label=label,
line_color=line_color,
key_cls=key_cls,
font_family=self.label_font_family,
)
for x, y in points:
# Ensure the labels are within the bounds of the image. If not, fix them.
x, y, snapped = self.canvas.snapPointToCanvas(x, y)
Expand Down Expand Up @@ -3574,6 +3597,7 @@ def get_main_app(argv=[]):
arg_parser.add_argument(
"--bbox_auto_zoom_center", type=str2bool, default=False, nargs="?"
)
arg_parser.add_argument("--label_font_path", type=str, default=None, nargs="?")

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

Expand All @@ -3588,6 +3612,7 @@ def get_main_app(argv=[]):
rec_char_dict_path=args.rec_char_dict_path,
cls_model_dir=args.cls_model_dir,
bbox_auto_zoom_center=args.bbox_auto_zoom_center,
label_font_path=args.label_font_path,
)
win.show()
return app, win
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ PPOCRLabelv2 is a semi-automatic graphic annotation tool suitable for OCR field,

### Recent Update

- 2024.11:
- Add `label_font_path` parameter to change the font of the label.
- 2024.09:
- 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.
- 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.
Expand Down
2 changes: 2 additions & 0 deletions README_ch.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具,内置P

#### 近期更新

- 2024.11:
- 新增`label_font_path`参数,用来改变标签字体
- 2024.09:
- 新增`自动重新识别``自动保存未提交变更`功能,使用方法详见下方`2.1 操作步骤``11. 补充功能说明`
- 新增`--img_list_natural_sort`参数,默认左侧图片列表使用自然排序,配置该参数后,将使用字符排序,方便根据字符顺序定位图片。
Expand Down
5 changes: 4 additions & 1 deletion libs/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(
key_cls="None",
paintLabel=False,
paintIdx=False,
font_family=None,
):
self.label = label
self.idx = None # bbox order, only for table annotation
Expand All @@ -78,7 +79,7 @@ def __init__(
self.fontsize = 8

self._closed = False

self.font_family = font_family
if line_color is not None:
# Override the class line_color attribute
# with an object attribute. Currently this
Expand Down Expand Up @@ -173,6 +174,8 @@ def paint(self, painter):
min_y = min(min_y, point.y())
if min_x != sys.maxsize and min_y != sys.maxsize:
font = QFont()
if self.font_family is not None:
font.setFamily(self.font_family)
font.setPointSize(self.fontsize)
font.setBold(True)
painter.setFont(font)
Expand Down

0 comments on commit 81a9c55

Please sign in to comment.