Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增label_font_path参数,用来改变标签字体 #100

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions PPOCRLabel.py
Original file line number Diff line number Diff line change
@@ -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,
@@ -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__)
@@ -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:
@@ -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)
@@ -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:])

@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions README_ch.md
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@ PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具,内置P

#### 近期更新

- 2024.11:
- 新增`label_font_path`参数,用来改变标签字体
- 2024.09:
- 新增`自动重新识别`和`自动保存未提交变更`功能,使用方法详见下方`2.1 操作步骤`的`11. 补充功能说明`。
- 新增`--img_list_natural_sort`参数,默认左侧图片列表使用自然排序,配置该参数后,将使用字符排序,方便根据字符顺序定位图片。
5 changes: 4 additions & 1 deletion libs/shape.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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)