diff --git a/PPOCRLabel.py b/PPOCRLabel.py index d2519b6..f5ac99c 100644 --- a/PPOCRLabel.py +++ b/PPOCRLabel.py @@ -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 diff --git a/README.md b/README.md index 4f5e6d6..a040744 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/README_ch.md b/README_ch.md index fc34b6b..7c91f10 100644 --- a/README_ch.md +++ b/README_ch.md @@ -16,6 +16,8 @@ PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具,内置P #### 近期更新 +- 2024.11: + - 新增`label_font_path`参数,用来改变标签字体 - 2024.09: - 新增`自动重新识别`和`自动保存未提交变更`功能,使用方法详见下方`2.1 操作步骤`的`11. 补充功能说明`。 - 新增`--img_list_natural_sort`参数,默认左侧图片列表使用自然排序,配置该参数后,将使用字符排序,方便根据字符顺序定位图片。 diff --git a/libs/shape.py b/libs/shape.py index 9bd3d95..3a119bf 100644 --- a/libs/shape.py +++ b/libs/shape.py @@ -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)