From 4c4cad8bd53251b355e26de01c7cd3ce43ffd2f1 Mon Sep 17 00:00:00 2001 From: Leif <4603009@qq.com> Date: Fri, 11 Dec 2020 19:23:27 +0800 Subject: [PATCH] Add PPOCRLabel --- PPOCRLabel/Makefile | 35 + PPOCRLabel/PPOCRLabel.py | 2042 +++ PPOCRLabel/README.md | 99 + PPOCRLabel/README_en.md | 121 + PPOCRLabel/__init__.py | 0 PPOCRLabel/combobox.py | 46 + PPOCRLabel/data/gif/steps.gif | Bin 0 -> 2491358 bytes PPOCRLabel/data/paddle.png | Bin 0 -> 6599 bytes PPOCRLabel/data/predefined_classes.txt | 0 PPOCRLabel/libs/__init__.py | 2 + PPOCRLabel/libs/autoDialog.py | 151 + PPOCRLabel/libs/canvas.py | 798 ++ PPOCRLabel/libs/colorDialog.py | 49 + PPOCRLabel/libs/constants.py | 31 + PPOCRLabel/libs/create_ml_io.py | 143 + PPOCRLabel/libs/hashableQListWidgetItem.py | 40 + PPOCRLabel/libs/labelDialog.py | 107 + PPOCRLabel/libs/labelFile.py | 152 + PPOCRLabel/libs/pascal_voc_io.py | 183 + PPOCRLabel/libs/resources.py | 11236 ++++++++++++++++ PPOCRLabel/libs/settings.py | 60 + PPOCRLabel/libs/shape.py | 217 + PPOCRLabel/libs/stringBundle.py | 86 + PPOCRLabel/libs/toolBar.py | 51 + PPOCRLabel/libs/ustr.py | 29 + PPOCRLabel/libs/utils.py | 182 + PPOCRLabel/libs/yolo_io.py | 146 + PPOCRLabel/libs/zoomWidget.py | 38 + .../requirements-linux-python3.txt | 2 + PPOCRLabel/resources.qrc | 40 + PPOCRLabel/resources/icons/Auto.png | Bin 0 -> 471 bytes PPOCRLabel/resources/icons/app.icns | Bin 0 -> 8 bytes PPOCRLabel/resources/icons/app.png | Bin 0 -> 59236 bytes PPOCRLabel/resources/icons/app.svg | 27 + PPOCRLabel/resources/icons/cancel.png | Bin 0 -> 2136 bytes PPOCRLabel/resources/icons/close.png | Bin 0 -> 3111 bytes PPOCRLabel/resources/icons/color.png | Bin 0 -> 1461 bytes PPOCRLabel/resources/icons/color_line.png | Bin 0 -> 2368 bytes PPOCRLabel/resources/icons/copy.png | Bin 0 -> 646 bytes PPOCRLabel/resources/icons/delete.png | Bin 0 -> 1486 bytes PPOCRLabel/resources/icons/done.png | Bin 0 -> 2198 bytes PPOCRLabel/resources/icons/done.svg | 400 + PPOCRLabel/resources/icons/edit.png | Bin 0 -> 1092 bytes PPOCRLabel/resources/icons/expert1.png | Bin 0 -> 278 bytes PPOCRLabel/resources/icons/expert2.png | Bin 0 -> 335 bytes PPOCRLabel/resources/icons/eye.png | Bin 0 -> 1264 bytes PPOCRLabel/resources/icons/feBlend-icon.png | Bin 0 -> 8059 bytes PPOCRLabel/resources/icons/file.png | Bin 0 -> 765 bytes PPOCRLabel/resources/icons/fit-width.png | Bin 0 -> 1365 bytes PPOCRLabel/resources/icons/fit-window.png | Bin 0 -> 1102 bytes PPOCRLabel/resources/icons/fit.png | Bin 0 -> 2262 bytes .../resources/icons/format_createml.png | Bin 0 -> 4156 bytes PPOCRLabel/resources/icons/format_voc.png | Bin 0 -> 786 bytes PPOCRLabel/resources/icons/format_yolo.png | Bin 0 -> 675 bytes PPOCRLabel/resources/icons/help.png | Bin 0 -> 1587 bytes PPOCRLabel/resources/icons/labels.png | Bin 0 -> 2381 bytes PPOCRLabel/resources/icons/labels.svg | 819 ++ PPOCRLabel/resources/icons/new.png | Bin 0 -> 977 bytes PPOCRLabel/resources/icons/next.png | Bin 0 -> 30288 bytes PPOCRLabel/resources/icons/objects.png | Bin 0 -> 1103 bytes PPOCRLabel/resources/icons/open.png | Bin 0 -> 2073 bytes PPOCRLabel/resources/icons/open.svg | 577 + PPOCRLabel/resources/icons/prev.png | Bin 0 -> 30665 bytes PPOCRLabel/resources/icons/quit.png | Bin 0 -> 1915 bytes PPOCRLabel/resources/icons/reRec.png | Bin 0 -> 1205 bytes PPOCRLabel/resources/icons/resetall.png | Bin 0 -> 3637 bytes PPOCRLabel/resources/icons/save-as.png | Bin 0 -> 2811 bytes PPOCRLabel/resources/icons/save-as.svg | 1358 ++ PPOCRLabel/resources/icons/save.png | Bin 0 -> 1187 bytes PPOCRLabel/resources/icons/save.svg | 679 + PPOCRLabel/resources/icons/undo-cross.png | Bin 0 -> 2004 bytes PPOCRLabel/resources/icons/undo.png | Bin 0 -> 2018 bytes PPOCRLabel/resources/icons/verify.png | Bin 0 -> 3041 bytes PPOCRLabel/resources/icons/zoom-in.png | Bin 0 -> 1099 bytes PPOCRLabel/resources/icons/zoom-out.png | Bin 0 -> 1074 bytes PPOCRLabel/resources/icons/zoom.png | Bin 0 -> 1139 bytes .../strings/strings-zh-CN.properties | 96 + .../strings/strings-zh-TW.properties | 70 + .../resources/strings/strings.properties | 96 + PPOCRLabel/setup.cfg | 8 + PPOCRLabel/setup.py | 139 + 81 files changed, 20355 insertions(+) create mode 100644 PPOCRLabel/Makefile create mode 100644 PPOCRLabel/PPOCRLabel.py create mode 100644 PPOCRLabel/README.md create mode 100644 PPOCRLabel/README_en.md create mode 100644 PPOCRLabel/__init__.py create mode 100644 PPOCRLabel/combobox.py create mode 100644 PPOCRLabel/data/gif/steps.gif create mode 100644 PPOCRLabel/data/paddle.png create mode 100644 PPOCRLabel/data/predefined_classes.txt create mode 100644 PPOCRLabel/libs/__init__.py create mode 100644 PPOCRLabel/libs/autoDialog.py create mode 100644 PPOCRLabel/libs/canvas.py create mode 100644 PPOCRLabel/libs/colorDialog.py create mode 100644 PPOCRLabel/libs/constants.py create mode 100644 PPOCRLabel/libs/create_ml_io.py create mode 100644 PPOCRLabel/libs/hashableQListWidgetItem.py create mode 100644 PPOCRLabel/libs/labelDialog.py create mode 100644 PPOCRLabel/libs/labelFile.py create mode 100644 PPOCRLabel/libs/pascal_voc_io.py create mode 100644 PPOCRLabel/libs/resources.py create mode 100644 PPOCRLabel/libs/settings.py create mode 100644 PPOCRLabel/libs/shape.py create mode 100644 PPOCRLabel/libs/stringBundle.py create mode 100644 PPOCRLabel/libs/toolBar.py create mode 100644 PPOCRLabel/libs/ustr.py create mode 100644 PPOCRLabel/libs/utils.py create mode 100644 PPOCRLabel/libs/yolo_io.py create mode 100644 PPOCRLabel/libs/zoomWidget.py create mode 100644 PPOCRLabel/requirements/requirements-linux-python3.txt create mode 100644 PPOCRLabel/resources.qrc create mode 100644 PPOCRLabel/resources/icons/Auto.png create mode 100644 PPOCRLabel/resources/icons/app.icns create mode 100644 PPOCRLabel/resources/icons/app.png create mode 100644 PPOCRLabel/resources/icons/app.svg create mode 100644 PPOCRLabel/resources/icons/cancel.png create mode 100644 PPOCRLabel/resources/icons/close.png create mode 100644 PPOCRLabel/resources/icons/color.png create mode 100644 PPOCRLabel/resources/icons/color_line.png create mode 100644 PPOCRLabel/resources/icons/copy.png create mode 100644 PPOCRLabel/resources/icons/delete.png create mode 100644 PPOCRLabel/resources/icons/done.png create mode 100644 PPOCRLabel/resources/icons/done.svg create mode 100644 PPOCRLabel/resources/icons/edit.png create mode 100644 PPOCRLabel/resources/icons/expert1.png create mode 100644 PPOCRLabel/resources/icons/expert2.png create mode 100644 PPOCRLabel/resources/icons/eye.png create mode 100644 PPOCRLabel/resources/icons/feBlend-icon.png create mode 100644 PPOCRLabel/resources/icons/file.png create mode 100644 PPOCRLabel/resources/icons/fit-width.png create mode 100644 PPOCRLabel/resources/icons/fit-window.png create mode 100644 PPOCRLabel/resources/icons/fit.png create mode 100644 PPOCRLabel/resources/icons/format_createml.png create mode 100644 PPOCRLabel/resources/icons/format_voc.png create mode 100644 PPOCRLabel/resources/icons/format_yolo.png create mode 100644 PPOCRLabel/resources/icons/help.png create mode 100644 PPOCRLabel/resources/icons/labels.png create mode 100644 PPOCRLabel/resources/icons/labels.svg create mode 100644 PPOCRLabel/resources/icons/new.png create mode 100644 PPOCRLabel/resources/icons/next.png create mode 100644 PPOCRLabel/resources/icons/objects.png create mode 100644 PPOCRLabel/resources/icons/open.png create mode 100644 PPOCRLabel/resources/icons/open.svg create mode 100644 PPOCRLabel/resources/icons/prev.png create mode 100644 PPOCRLabel/resources/icons/quit.png create mode 100644 PPOCRLabel/resources/icons/reRec.png create mode 100644 PPOCRLabel/resources/icons/resetall.png create mode 100644 PPOCRLabel/resources/icons/save-as.png create mode 100644 PPOCRLabel/resources/icons/save-as.svg create mode 100644 PPOCRLabel/resources/icons/save.png create mode 100644 PPOCRLabel/resources/icons/save.svg create mode 100644 PPOCRLabel/resources/icons/undo-cross.png create mode 100644 PPOCRLabel/resources/icons/undo.png create mode 100644 PPOCRLabel/resources/icons/verify.png create mode 100644 PPOCRLabel/resources/icons/zoom-in.png create mode 100644 PPOCRLabel/resources/icons/zoom-out.png create mode 100644 PPOCRLabel/resources/icons/zoom.png create mode 100644 PPOCRLabel/resources/strings/strings-zh-CN.properties create mode 100644 PPOCRLabel/resources/strings/strings-zh-TW.properties create mode 100644 PPOCRLabel/resources/strings/strings.properties create mode 100644 PPOCRLabel/setup.cfg create mode 100644 PPOCRLabel/setup.py diff --git a/PPOCRLabel/Makefile b/PPOCRLabel/Makefile new file mode 100644 index 00000000..7d72a890 --- /dev/null +++ b/PPOCRLabel/Makefile @@ -0,0 +1,35 @@ +# ex: set ts=8 noet: + +all: qt5 test + +test: testpy3 + +testpy2: + python -m unittest discover tests + +testpy3: + python3 -m unittest discover tests + +qt4: qt4py2 + +qt5: qt5py3 + +qt4py2: + pyrcc4 -py2 -o libs/resources.py resources.qrc + +qt4py3: + pyrcc4 -py3 -o libs/resources.py resources.qrc + +qt5py3: + pyrcc5 -o libs/resources.py resources.qrc + +clean: + rm -rf ~/.labelImgSettings.pkl *.pyc dist labelImg.egg-info __pycache__ build + +pip_upload: + python3 setup.py upload + +long_description: + restview --long-description + +.PHONY: all diff --git a/PPOCRLabel/PPOCRLabel.py b/PPOCRLabel/PPOCRLabel.py new file mode 100644 index 00000000..278bc511 --- /dev/null +++ b/PPOCRLabel/PPOCRLabel.py @@ -0,0 +1,2042 @@ +# Copyright (c) <2015-Present> Tzutalin +# Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba, +# William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +# associated documentation files (the "Software"), to deal in the Software without restriction, including without +# limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of +# the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# pyrcc5 -o libs/resources.py resources.qrc +import argparse +import ast +import codecs +import os.path +import platform +import subprocess +import sys +from functools import partial +from collections import defaultdict +import json + + +__dir__ = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(__dir__) +sys.path.append(os.path.abspath(os.path.join(__dir__, '../..'))) +sys.path.append("..") + +from paddleocr import PaddleOCR + +try: + from PyQt5 import QtCore, QtGui, QtWidgets + from PyQt5.QtGui import * + from PyQt5.QtCore import * + from PyQt5.QtWidgets import * +except ImportError: + # needed for py3+qt4 + # Ref: + # http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html + # http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string + if sys.version_info.major >= 3: + import sip + + sip.setapi('QVariant', 2) + from PyQt4.QtGui import * + from PyQt4.QtCore import * + +from combobox import ComboBox +from libs.constants import * +from libs.utils import * +from libs.settings import Settings +from libs.shape import Shape, DEFAULT_LINE_COLOR, DEFAULT_FILL_COLOR +from libs.stringBundle import StringBundle +from libs.canvas import Canvas +from libs.zoomWidget import ZoomWidget +from libs.autoDialog import AutoDialog +from libs.labelDialog import LabelDialog +from libs.colorDialog import ColorDialog +from libs.labelFile import LabelFile, LabelFileError +from libs.toolBar import ToolBar +from libs.ustr import ustr +from libs.hashableQListWidgetItem import HashableQListWidgetItem + +__appname__ = 'PPOCRLabel' + + +class WindowMixin(object): + + def menu(self, title, actions=None): + menu = self.menuBar().addMenu(title) + if actions: + addActions(menu, actions) + return menu + + def toolbar(self, title, actions=None): + toolbar = ToolBar(title) + toolbar.setObjectName(u'%sToolBar' % title) + # toolbar.setOrientation(Qt.Vertical) + toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) + if actions: + addActions(toolbar, actions) + self.addToolBar(Qt.LeftToolBarArea, toolbar) + return toolbar + + +class MainWindow(QMainWindow, WindowMixin): + FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = list(range(3)) + + def __init__(self, lang="ch", defaultFilename=None, defaultPrefdefClassFile=None, defaultSaveDir=None): + super(MainWindow, self).__init__() + self.setWindowTitle(__appname__) + + # Load setting in the main thread + self.settings = Settings() + self.settings.load() + settings = self.settings + self.lang = lang + # Load string bundle for i18n + if lang not in ['ch', 'en']: + lang = 'en' + self.stringBundle = StringBundle.getBundle(localeStr='zh-CN' if lang=='ch' else 'en') # 'en' + getStr = lambda strId: self.stringBundle.getString(strId) + + self.defaultSaveDir = defaultSaveDir + self.ocr = PaddleOCR(use_pdserving=False, use_angle_cls=True, det=True, cls=True, use_gpu=True, lang=lang) + + if os.path.exists('./data/paddle.png'): + result = self.ocr.ocr('./data/paddle.png', cls=True, det=True) + + # For loading all image under a directory + self.mImgList = [] + self.mImgList5 = [] + self.dirname = None + self.labelHist = [] + self.lastOpenDir = None + self.result_dic = [] + + self.changeFileFolder = False + self.haveAutoReced = False + self.labelFile = None + self.currIndex = 0 + + + # Whether we need to save or not. + self.dirty = False + + self._noSelectionSlot = False + self._beginner = True + self.screencastViewer = self.getAvailableScreencastViewer() + self.screencast = "https://github.com/PaddlePaddle/PaddleOCR" + + # Load predefined classes to the list + self.loadPredefinedClasses(defaultPrefdefClassFile) + + # Main widgets and related state. + self.labelDialog = LabelDialog(parent=self, listItem=self.labelHist) + self.autoDialog = AutoDialog(parent=self) + + self.itemsToShapes = {} + self.shapesToItems = {} + self.itemsToShapesbox = {} + self.shapesToItemsbox = {} + self.prevLabelText = getStr('tempLabel') + self.model = 'paddle' + self.PPreader = None + self.autoSaveNum = 10 + + ################# file list ############### + self.fileListWidget = QListWidget() + self.fileListWidget.itemClicked.connect(self.fileitemDoubleClicked) + self.fileListWidget.setIconSize(QSize(25, 25)) + filelistLayout = QVBoxLayout() + filelistLayout.setContentsMargins(0, 0, 0, 0) + filelistLayout.addWidget(self.fileListWidget) + + self.AutoRecognition = QToolButton() + self.AutoRecognition.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + self.AutoRecognition.setIcon(newIcon('Auto')) + autoRecLayout = QHBoxLayout() + autoRecLayout.setContentsMargins(0, 0, 0, 0) + autoRecLayout.addWidget(self.AutoRecognition) + autoRecContainer = QWidget() + autoRecContainer.setLayout(autoRecLayout) + filelistLayout.addWidget(autoRecContainer) + + fileListContainer = QWidget() + fileListContainer.setLayout(filelistLayout) + self.filedock = QDockWidget(getStr('fileList'), self) + self.filedock.setObjectName(getStr('files')) + self.filedock.setWidget(fileListContainer) + self.addDockWidget(Qt.LeftDockWidgetArea, self.filedock) + + ######## Right area ########## + listLayout = QVBoxLayout() + listLayout.setContentsMargins(0, 0, 0, 0) + + self.editButton = QToolButton() + self.reRecogButton = QToolButton() + self.reRecogButton.setIcon(newIcon('reRec', 30)) + self.reRecogButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + + self.newButton = QToolButton() + self.newButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + self.SaveButton = QToolButton() + self.SaveButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + self.DelButton = QToolButton() + self.DelButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + + + lefttoptoolbox = QHBoxLayout() + lefttoptoolbox.addWidget(self.newButton) + lefttoptoolbox.addWidget(self.reRecogButton) + lefttoptoolboxcontainer = QWidget() + lefttoptoolboxcontainer.setLayout(lefttoptoolbox) + listLayout.addWidget(lefttoptoolboxcontainer) + + + ################## label list #################### + # Create and add a widget for showing current label items + self.labelList = QListWidget() + labelListContainer = QWidget() + labelListContainer.setLayout(listLayout) + self.labelList.itemActivated.connect(self.labelSelectionChanged) + self.labelList.itemSelectionChanged.connect(self.labelSelectionChanged) + self.labelList.itemDoubleClicked.connect(self.editLabel) + # Connect to itemChanged to detect checkbox changes. + self.labelList.itemChanged.connect(self.labelItemChanged) + self.labelListDock = QDockWidget(getStr('recognitionResult'),self) + self.labelListDock.setWidget(self.labelList) + self.labelListDock.setFeatures(QDockWidget.NoDockWidgetFeatures) + listLayout.addWidget(self.labelListDock) + + ################## detection box #################### + self.BoxList = QListWidget() + + self.BoxList.itemActivated.connect(self.boxSelectionChanged) + self.BoxList.itemSelectionChanged.connect(self.boxSelectionChanged) + self.BoxList.itemDoubleClicked.connect(self.editBox) + # Connect to itemChanged to detect checkbox changes. + self.BoxList.itemChanged.connect(self.boxItemChanged) + self.BoxListDock = QDockWidget(getStr('detectionBoxposition'), self) + self.BoxListDock.setWidget(self.BoxList) + self.BoxListDock.setFeatures(QDockWidget.NoDockWidgetFeatures) + listLayout.addWidget(self.BoxListDock) + + ############ lower right area ############ + leftbtmtoolbox = QHBoxLayout() + leftbtmtoolbox.addWidget(self.SaveButton) + leftbtmtoolbox.addWidget(self.DelButton) + leftbtmtoolboxcontainer = QWidget() + leftbtmtoolboxcontainer.setLayout(leftbtmtoolbox) + listLayout.addWidget(leftbtmtoolboxcontainer) + + self.dock = QDockWidget(getStr('boxLabelText'), self) + self.dock.setObjectName(getStr('labels')) + self.dock.setWidget(labelListContainer) + + + ########## zoom bar ######### + self.imgsplider = QSlider(Qt.Horizontal) + self.imgsplider.valueChanged.connect(self.CanvasSizeChange) + self.imgsplider.setMinimum(-150) + self.imgsplider.setMaximum(150) + self.imgsplider.setSingleStep(1) + self.imgsplider.setTickPosition(QSlider.TicksBelow) + self.imgsplider.setTickInterval(1) + op = QGraphicsOpacityEffect() + op.setOpacity(0.2) + self.imgsplider.setGraphicsEffect(op) + # self.imgsplider.setAttribute(Qt.WA_TranslucentBackground) + self.imgsplider.setStyleSheet("background-color:transparent") + self.imgsliderDock = QDockWidget(getStr('ImageResize'), self) + self.imgsliderDock.setObjectName(getStr('IR')) + self.imgsliderDock.setWidget(self.imgsplider) + self.imgsliderDock.setFeatures(QDockWidget.DockWidgetFloatable) + # op = QGraphicsOpacityEffect() + # op.setOpacity(0.2) + # self.imgsliderDock.setGraphicsEffect(op) + self.imgsliderDock.setAttribute(Qt.WA_TranslucentBackground) + self.addDockWidget(Qt.RightDockWidgetArea, self.imgsliderDock) + + self.zoomWidget = ZoomWidget() + self.colorDialog = ColorDialog(parent=self) + self.zoomWidgetValue = self.zoomWidget.value() + + ########## thumbnail ######### + hlayout = QHBoxLayout() + m = (0, 0, 0, 0) + hlayout.setSpacing(0) + hlayout.setContentsMargins(*m) + self.preButton = QToolButton() + self.preButton.setIcon(newIcon("prev",40)) + self.preButton.setIconSize(QSize(40, 100)) + self.preButton.clicked.connect(self.openPrevImg) + self.preButton.setStyleSheet('border: none;') + self.iconlist = QListWidget() + self.iconlist.setViewMode(QListView.IconMode) + self.iconlist.setFlow(QListView.TopToBottom) + self.iconlist.setSpacing(10) + self.iconlist.setIconSize(QSize(50, 50)) + self.iconlist.setMovement(False) + self.iconlist.setResizeMode(QListView.Adjust) + self.iconlist.itemClicked.connect(self.iconitemDoubleClicked) + self.iconlist.setStyleSheet("background-color:transparent; border: none;") + self.iconlist.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + self.nextButton = QToolButton() + self.nextButton.setIcon(newIcon("next", 40)) + self.nextButton.setIconSize(QSize(40, 100)) + self.nextButton.setStyleSheet('border: none;') + self.nextButton.clicked.connect(self.openNextImg) + + hlayout.addWidget(self.preButton) + hlayout.addWidget(self.iconlist) + hlayout.addWidget(self.nextButton) + + # self.setLayout(hlayout) + + iconListContainer = QWidget() + iconListContainer.setLayout(hlayout) + iconListContainer.setFixedHeight(100) + # iconListContainer.setFixedWidth(530) + # op = QGraphicsOpacityEffect() + # op.setOpacity(0.5) + # iconListContainer.setGraphicsEffect(op) + + ########### Canvas ########### + self.canvas = Canvas(parent=self) + self.canvas.zoomRequest.connect(self.zoomRequest) + self.canvas.setDrawingShapeToSquare(settings.get(SETTING_DRAW_SQUARE, False)) + + scroll = QScrollArea() + scroll.setWidget(self.canvas) + scroll.setWidgetResizable(True) + self.scrollBars = { + Qt.Vertical: scroll.verticalScrollBar(), + Qt.Horizontal: scroll.horizontalScrollBar() + } + self.scrollArea = scroll + self.canvas.scrollRequest.connect(self.scrollRequest) + + self.canvas.newShape.connect(self.newShape) + self.canvas.shapeMoved.connect(self.updateBoxlist) # self.setDirty + self.canvas.selectionChanged.connect(self.shapeSelectionChanged) + self.canvas.drawingPolygon.connect(self.toggleDrawingSensitive) + + centerLayout = QVBoxLayout() + centerLayout.setContentsMargins(0, 0, 0, 0) + centerLayout.addWidget(scroll) + #centerLayout.addWidget(self.icondock) + centerLayout.addWidget(iconListContainer,0,Qt.AlignCenter) + centercontainer = QWidget() + centercontainer.setLayout(centerLayout) + + # self.scrolldock = QDockWidget('WorkSpace',self) + # self.scrolldock.setObjectName('WorkSpace') + # self.scrolldock.setWidget(centercontainer) + # self.scrolldock.setFeatures(QDockWidget.NoDockWidgetFeatures) + # orititle = self.scrolldock.titleBarWidget() + # tmpwidget = QWidget() + # self.scrolldock.setTitleBarWidget(tmpwidget) + # del orititle + self.setCentralWidget(centercontainer) #self.scrolldock + self.addDockWidget(Qt.RightDockWidgetArea, self.dock) + + + # self.filedock.setFeatures(QDockWidget.DockWidgetFloatable) + self.filedock.setFeatures(self.filedock.features() ^ QDockWidget.DockWidgetFloatable) + + self.dockFeatures = QDockWidget.DockWidgetClosable | QDockWidget.DockWidgetFloatable + self.dock.setFeatures(self.dock.features() ^ self.dockFeatures) + + self.filedock.setFeatures(QDockWidget.NoDockWidgetFeatures) + + ###### Actions ####### + action = partial(newAction, self) + quit = action(getStr('quit'), self.close, + 'Ctrl+Q', 'quit', getStr('quitApp')) + + open = action(getStr('openFile'), self.openFile, + 'Ctrl+O', 'open', getStr('openFileDetail')) + + opendir = action(getStr('openDir'), self.openDirDialog, + 'Ctrl+u', 'open', getStr('openDir')) + + openNextImg = action(getStr('nextImg'), self.openNextImg, + 'd', 'next', getStr('nextImgDetail')) + + openPrevImg = action(getStr('prevImg'), self.openPrevImg, + 'a', 'prev', getStr('prevImgDetail')) + + verify = action(getStr('verifyImg'), self.verifyImg, + 'space', 'verify', getStr('verifyImgDetail')) + + save = action(getStr('save'), self.saveFile, + 'Ctrl+S', 'save', getStr('saveDetail'), enabled=False) + + alcm = action(getStr('choosemodel'), self.autolcm, + 'Ctrl+M', 'next', getStr('tipchoosemodel')) + + deleteImg = action(getStr('deleteImg'), self.deleteImg, 'Ctrl+D', 'close', getStr('deleteImgDetail'), + enabled=True) + + resetAll = action(getStr('resetAll'), self.resetAll, None, 'resetall', getStr('resetAllDetail')) + + color1 = action(getStr('boxLineColor'), self.chooseColor1, + 'Ctrl+L', 'color_line', getStr('boxLineColorDetail')) + + createMode = action(getStr('crtBox'), self.setCreateMode, + 'w', 'new', getStr('crtBoxDetail'), enabled=False) + editMode = action('&Edit\nRectBox', self.setEditMode, + 'Ctrl+J', 'edit', u'Move and edit Boxs', enabled=False) + + create = action(getStr('crtBox'), self.createShape, + 'w', 'new', getStr('crtBoxDetail'), enabled=False) + + delete = action(getStr('delBox'), self.deleteSelectedShape, + 'Delete', 'delete', getStr('delBoxDetail'), enabled=False) + copy = action(getStr('dupBox'), self.copySelectedShape, + 'Ctrl+D', 'copy', getStr('dupBoxDetail'), + enabled=False) + + hideAll = action(getStr('hideBox'), partial(self.togglePolygons, False), + 'Ctrl+H', 'hide', getStr('hideAllBoxDetail'), + enabled=False) + showAll = action(getStr('showBox'), partial(self.togglePolygons, True), + 'Ctrl+A', 'hide', getStr('showAllBoxDetail'), + enabled=False) + + help = action(getStr('tutorial'), self.showTutorialDialog, None, 'help', getStr('tutorialDetail')) + showInfo = action(getStr('info'), self.showInfoDialog, None, 'help', getStr('info')) + showSteps = action(getStr('steps'), self.showStepsDialog, None, 'help', getStr('steps')) + + zoom = QWidgetAction(self) + zoom.setDefaultWidget(self.zoomWidget) + self.zoomWidget.setWhatsThis( + u"Zoom in or out of the image. Also accessible with" + " %s and %s from the canvas." % (fmtShortcut("Ctrl+[-+]"), + fmtShortcut("Ctrl+Wheel"))) + self.zoomWidget.setEnabled(False) + + zoomIn = action(getStr('zoomin'), partial(self.addZoom, 10), + 'Ctrl++', 'zoom-in', getStr('zoominDetail'), enabled=False) + zoomOut = action(getStr('zoomout'), partial(self.addZoom, -10), + 'Ctrl+-', 'zoom-out', getStr('zoomoutDetail'), enabled=False) + zoomOrg = action(getStr('originalsize'), partial(self.setZoom, 100), + 'Ctrl+=', 'zoom', getStr('originalsizeDetail'), enabled=False) + fitWindow = action(getStr('fitWin'), self.setFitWindow, + 'Ctrl+F', 'fit-window', getStr('fitWinDetail'), + checkable=True, enabled=False) + fitWidth = action(getStr('fitWidth'), self.setFitWidth, + 'Ctrl+Shift+F', 'fit-width', getStr('fitWidthDetail'), + checkable=True, enabled=False) + # Group zoom controls into a list for easier toggling. + zoomActions = (self.zoomWidget, zoomIn, zoomOut, + zoomOrg, fitWindow, fitWidth) + self.zoomMode = self.MANUAL_ZOOM + self.scalers = { + self.FIT_WINDOW: self.scaleFitWindow, + self.FIT_WIDTH: self.scaleFitWidth, + # Set to one to scale to 100% when loading files. + self.MANUAL_ZOOM: lambda: 1, + } + + edit = action(getStr('editLabel'), self.editLabel, + 'Ctrl+E', 'edit', getStr('editLabelDetail'), + enabled=False) + + ######## New actions ####### + AutoRec = action(getStr('autoRecognition'), self.autoRecognition, + 'Ctrl+Shift+A', 'Auto', getStr('autoRecognition'), enabled=False) + + reRec = action(getStr('reRecognition'), self.reRecognition, + 'Ctrl+Shift+R', 'reRec', getStr('reRecognition'), enabled=False) + + createpoly = action(getStr('creatPolygon'), self.createPolygon, + 'p', 'new', 'Creat Polygon', enabled=True) + + saveRec = action(getStr('saveRec'), self.saveRecResult, + '', 'saveRec', getStr('saveRec'), enabled=False) + + self.editButton.setDefaultAction(edit) + self.newButton.setDefaultAction(create) + self.DelButton.setDefaultAction(deleteImg) + self.SaveButton.setDefaultAction(save) + self.AutoRecognition.setDefaultAction(AutoRec) + self.reRecogButton.setDefaultAction(reRec) + # self.preButton.setDefaultAction(openPrevImg) + # self.nextButton.setDefaultAction(openNextImg) + + ############# Zoom layout ############## + zoomLayout = QHBoxLayout() + zoomLayout.addStretch() + self.zoominButton = QToolButton() + self.zoominButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + self.zoominButton.setDefaultAction(zoomIn) + self.zoomoutButton = QToolButton() + self.zoomoutButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + self.zoomoutButton.setDefaultAction(zoomOut) + self.zoomorgButton = QToolButton() + self.zoomorgButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) + self.zoomorgButton.setDefaultAction(zoomOrg) + zoomLayout.addWidget(self.zoominButton) + zoomLayout.addWidget(self.zoomorgButton) + zoomLayout.addWidget(self.zoomoutButton) + + zoomContainer = QWidget() + zoomContainer.setLayout(zoomLayout) + zoomContainer.setGeometry(0, 0, 30, 150) + + shapeLineColor = action(getStr('shapeLineColor'), self.chshapeLineColor, + icon='color_line', tip=getStr('shapeLineColorDetail'), + enabled=False) + shapeFillColor = action(getStr('shapeFillColor'), self.chshapeFillColor, + icon='color', tip=getStr('shapeFillColorDetail'), + enabled=False) + + # labels = self.dock.toggleViewAction() + # labels.setText(getStr('showHide')) + # labels.setShortcut('Ctrl+Shift+L') + + # Label list context menu. + labelMenu = QMenu() + addActions(labelMenu, (edit, delete)) + + self.labelList.setContextMenuPolicy(Qt.CustomContextMenu) + self.labelList.customContextMenuRequested.connect( + self.popLabelListMenu) + + # Draw squares/rectangles + self.drawSquaresOption = QAction(getStr('drawSquares'), self) + self.drawSquaresOption.setShortcut('Ctrl+Shift+R') + self.drawSquaresOption.setCheckable(True) + self.drawSquaresOption.setChecked(settings.get(SETTING_DRAW_SQUARE, False)) + self.drawSquaresOption.triggered.connect(self.toogleDrawSquare) + + # Store actions for further handling. + self.actions = struct(save=save, open=open, resetAll=resetAll, deleteImg=deleteImg, + lineColor=color1, create=create, delete=delete, edit=edit, copy=copy, + saveRec=saveRec, + createMode=createMode, editMode=editMode, + shapeLineColor=shapeLineColor, shapeFillColor=shapeFillColor, + zoom=zoom, zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg, + fitWindow=fitWindow, fitWidth=fitWidth, + zoomActions=zoomActions, + fileMenuActions=( + open, opendir, save, resetAll, quit), + beginner=(), advanced=(), + editMenu=(createpoly, edit, copy, delete, + None, color1, self.drawSquaresOption), + beginnerContext=(create, edit, copy, delete), + advancedContext=(createMode, editMode, edit, copy, + delete, shapeLineColor, shapeFillColor), + onLoadActive=( + create, createMode, editMode), + onShapesPresent=(hideAll, showAll)) + + # menus + self.menus = struct( + file=self.menu('&'+getStr('mfile')), + edit=self.menu('&'+getStr('medit')), + view=self.menu('&'+getStr('mview')), + autolabel=self.menu('&PaddleOCR'), + help=self.menu('&'+getStr('mhelp')), + recentFiles=QMenu('Open &Recent'), + labelList=labelMenu) + + + # Sync single class mode from PR#106 + self.singleClassMode = QAction(getStr('singleClsMode'), self) + self.singleClassMode.setShortcut("Ctrl+Shift+S") + self.singleClassMode.setCheckable(True) + self.singleClassMode.setChecked(settings.get(SETTING_SINGLE_CLASS, False)) + self.lastLabel = None + # Add option to enable/disable labels being displayed at the top of bounding boxes + self.displayLabelOption = QAction(getStr('displayLabel'), self) + self.displayLabelOption.setShortcut("Ctrl+Shift+P") + self.displayLabelOption.setCheckable(True) + self.displayLabelOption.setChecked(settings.get(SETTING_PAINT_LABEL, False)) + self.displayLabelOption.triggered.connect(self.togglePaintLabelsOption) + + addActions(self.menus.file, + (opendir, None, save, resetAll, deleteImg, quit)) + + addActions(self.menus.help, (showSteps, showInfo)) + addActions(self.menus.view, ( + self.displayLabelOption, # labels, + None, + hideAll, showAll, None, + zoomIn, zoomOut, zoomOrg, None, + fitWindow, fitWidth)) + + addActions(self.menus.autolabel, (alcm, saveRec, None, help)) # + + self.menus.file.aboutToShow.connect(self.updateFileMenu) + + # Custom context menu for the canvas widget: + addActions(self.canvas.menus[0], self.actions.beginnerContext) + addActions(self.canvas.menus[1], ( + action('&Copy here', self.copyShape), + action('&Move here', self.moveShape))) + + # self.tools = self.toolbar('Tools') + + self.actions.beginner = ( + open, opendir, openNextImg, openPrevImg, verify, save, None, create, copy, delete, None, + zoomIn, zoom, zoomOut, fitWindow, fitWidth) + + self.actions.advanced = ( + open, opendir, openNextImg, openPrevImg, save, None, + createMode, editMode, None, + hideAll, showAll) + + self.statusBar().showMessage('%s started.' % __appname__) + self.statusBar().show() + + # Application state. + self.image = QImage() + self.filePath = ustr(defaultFilename) + self.lastOpenDir = None + self.recentFiles = [] + self.maxRecent = 7 + self.lineColor = None + self.fillColor = None + self.zoom_level = 100 + self.fit_window = False + # Add Chris + self.difficult = False + + ## Fix the compatible issue for qt4 and qt5. Convert the QStringList to python list + if settings.get(SETTING_RECENT_FILES): + if have_qstring(): + recentFileQStringList = settings.get(SETTING_RECENT_FILES) + self.recentFiles = [ustr(i) for i in recentFileQStringList] + else: + self.recentFiles = recentFileQStringList = settings.get(SETTING_RECENT_FILES) + + size = settings.get(SETTING_WIN_SIZE, QSize(1200, 800)) + + position = QPoint(0, 0) + saved_position = settings.get(SETTING_WIN_POSE, position) + # Fix the multiple monitors issue + for i in range(QApplication.desktop().screenCount()): + if QApplication.desktop().availableGeometry(i).contains(saved_position): + position = saved_position + break + self.resize(size) + self.move(position) + saveDir = ustr(settings.get(SETTING_SAVE_DIR, None)) + self.lastOpenDir = ustr(settings.get(SETTING_LAST_OPEN_DIR, None)) + + self.restoreState(settings.get(SETTING_WIN_STATE, QByteArray())) + Shape.line_color = self.lineColor = QColor(settings.get(SETTING_LINE_COLOR, DEFAULT_LINE_COLOR)) + Shape.fill_color = self.fillColor = QColor(settings.get(SETTING_FILL_COLOR, DEFAULT_FILL_COLOR)) + self.canvas.setDrawingColor(self.lineColor) + # Add chris + Shape.difficult = self.difficult + + + # ADD: + # Populate the File menu dynamically. + self.updateFileMenu() + + # Since loading the file may take some time, make sure it runs in the background. + if self.filePath and os.path.isdir(self.filePath): + self.queueEvent(partial(self.importDirImages, self.filePath or "")) + elif self.filePath: + self.queueEvent(partial(self.loadFile, self.filePath or "")) + + # Callbacks: + self.zoomWidget.valueChanged.connect(self.paintCanvas) + + self.populateModeActions() + + # Display cursor coordinates at the right of status bar + self.labelCoordinates = QLabel('') + self.statusBar().addPermanentWidget(self.labelCoordinates) + + # Open Dir if deafult file + if self.filePath and os.path.isdir(self.filePath): + self.openDirDialog(dirpath=self.filePath, silent=True) + + def keyReleaseEvent(self, event): + if event.key() == Qt.Key_Control: + self.canvas.setDrawingShapeToSquare(False) + + def keyPressEvent(self, event): + if event.key() == Qt.Key_Control: + # Draw rectangle if Ctrl is pressed + self.canvas.setDrawingShapeToSquare(True) + + + def noShapes(self): + return not self.itemsToShapes + + + def populateModeActions(self): + self.canvas.menus[0].clear() + addActions(self.canvas.menus[0], self.actions.beginnerContext) + self.menus.edit.clear() + actions = (self.actions.create,) # if self.beginner() else (self.actions.createMode, self.actions.editMode) + addActions(self.menus.edit, actions + self.actions.editMenu) + + + def setDirty(self): + self.dirty = True + self.actions.save.setEnabled(True) + + def setClean(self): + self.dirty = False + self.actions.save.setEnabled(False) + self.actions.create.setEnabled(True) + + def toggleActions(self, value=True): + """Enable/Disable widgets which depend on an opened image.""" + for z in self.actions.zoomActions: + z.setEnabled(value) + for action in self.actions.onLoadActive: + action.setEnabled(value) + + def queueEvent(self, function): + QTimer.singleShot(0, function) + + def status(self, message, delay=5000): + self.statusBar().showMessage(message, delay) + + def resetState(self): + self.itemsToShapes.clear() + self.shapesToItems.clear() + self.itemsToShapesbox.clear() # ADD + self.shapesToItemsbox.clear() + self.labelList.clear() + self.BoxList.clear() + self.filePath = None + self.imageData = None + self.labelFile = None + self.canvas.resetState() + self.labelCoordinates.clear() + # self.comboBox.cb.clear() + self.result_dic = [] + + def currentItem(self): + items = self.labelList.selectedItems() + if items: + return items[0] + return None + + def currentBox(self): + items = self.BoxList.selectedItems() + if items: + return items[0] + return None + + def addRecentFile(self, filePath): + if filePath in self.recentFiles: + self.recentFiles.remove(filePath) + elif len(self.recentFiles) >= self.maxRecent: + self.recentFiles.pop() + self.recentFiles.insert(0, filePath) + + def beginner(self): + return self._beginner + + def advanced(self): + return not self.beginner() + + def getAvailableScreencastViewer(self): + osName = platform.system() + + if osName == 'Windows': + return ['C:\\Program Files\\Internet Explorer\\iexplore.exe'] + elif osName == 'Linux': + return ['xdg-open'] + elif osName == 'Darwin': + return ['open'] + + ## Callbacks ## + def showTutorialDialog(self): + subprocess.Popen(self.screencastViewer + [self.screencast]) + + def showInfoDialog(self): + from libs.__init__ import __version__ + msg = u'Name:{0} \nApp Version:{1} \n{2} '.format(__appname__, __version__, sys.version_info) + QMessageBox.information(self, u'Information', msg) + + def showStepsDialog(self): + msg = stepsInfo(self.lang) + QMessageBox.information(self, u'Information', msg) + + def createShape(self): + assert self.beginner() + self.canvas.setEditing(False) + self.actions.create.setEnabled(False) + self.canvas.fourpoint = False + + def createPolygon(self): + assert self.beginner() + self.canvas.setEditing(False) + self.canvas.fourpoint = True + self.actions.create.setEnabled(False) + + def toggleDrawingSensitive(self, drawing=True): + """In the middle of drawing, toggling between modes should be disabled.""" + self.actions.editMode.setEnabled(not drawing) + if not drawing and self.beginner(): + # Cancel creation. + print('Cancel creation.') + self.canvas.setEditing(True) + self.canvas.restoreCursor() + self.actions.create.setEnabled(True) + + def toggleDrawMode(self, edit=True): + self.canvas.setEditing(edit) + self.actions.createMode.setEnabled(edit) + self.actions.editMode.setEnabled(not edit) + + def setCreateMode(self): + assert self.advanced() + self.toggleDrawMode(False) + + def setEditMode(self): + assert self.advanced() + self.toggleDrawMode(True) + self.labelSelectionChanged() + + def updateFileMenu(self): + currFilePath = self.filePath + + def exists(filename): + return os.path.exists(filename) + + menu = self.menus.recentFiles + menu.clear() + files = [f for f in self.recentFiles if f != + currFilePath and exists(f)] + for i, f in enumerate(files): + icon = newIcon('labels') + action = QAction( + icon, '&%d %s' % (i + 1, QFileInfo(f).fileName()), self) + action.triggered.connect(partial(self.loadRecent, f)) + menu.addAction(action) + + def popLabelListMenu(self, point): + self.menus.labelList.exec_(self.labelList.mapToGlobal(point)) + + def editLabel(self): + if not self.canvas.editing(): + return + item = self.currentItem() + if not item: + return + text = self.labelDialog.popUp(item.text()) + if text is not None: + item.setText(text) + # item.setBackground(generateColorByText(text)) + self.setDirty() + self.updateComboBox() + + ######## detection box related functions ####### + + def boxItemChanged(self, item): + shape = self.itemsToShapesbox[item] + + box = ast.literal_eval(item.text()) + # print('shape in labelItemChanged is',shape.points) + if box != [(p.x(), p.y()) for p in shape.points]: + # shape.points = box + shape.points = [QPointF(p[0], p[1]) for p in box] + + # QPointF(x,y) + # shape.line_color = generateColorByText(shape.label) + self.setDirty() + else: # User probably changed item visibility + self.canvas.setShapeVisible(shape, True)#item.checkState() == Qt.Checked + + def editBox(self): # ADD + if not self.canvas.editing(): + return + item = self.currentBox() + if not item: + return + text = self.labelDialog.popUp(item.text()) + + imageSize = str(self.image.size()) + width, height = self.image.width(), self.image.height() + if text: + try: + text_list = eval(text) + except: + msg_box = QMessageBox(QMessageBox.Warning, 'Warning', 'Please enter the correct format') + msg_box.exec_() + return + if len(text_list) < 4: + msg_box = QMessageBox(QMessageBox.Warning, 'Warning', 'Please enter the coordinates of 4 points') + msg_box.exec_() + return + for box in text_list: + if box[0] > width or box[0] < 0 or box[1] > height or box[1] < 0: + msg_box = QMessageBox(QMessageBox.Warning, 'Warning', 'Out of picture size') + msg_box.exec_() + return + + item.setText(text) + # item.setBackground(generateColorByText(text)) + self.setDirty() + self.updateComboBox() + + def updateBoxlist(self): + shape = self.canvas.selectedShape + item = self.shapesToItemsbox[shape] # listitem + text = [(int(p.x()), int(p.y())) for p in shape.points] + item.setText(str(text)) + self.setDirty() + + def indexTo5Files(self, currIndex): + if currIndex < 2: + return self.mImgList[:5] + elif currIndex > len(self.mImgList)-3: + return self.mImgList[-5:] + else: + return self.mImgList[currIndex - 2 : currIndex + 3] + + + # Tzutalin 20160906 : Add file list and dock to move faster + def fileitemDoubleClicked(self, item=None): + self.currIndex = self.mImgList.index(ustr(os.path.join(os.path.abspath(self.dirname), item.text()))) + filename = self.mImgList[self.currIndex] + if filename: + self.mImgList5 = self.indexTo5Files(self.currIndex) + # self.additems5(None) + self.loadFile(filename) + + def iconitemDoubleClicked(self, item=None): + self.currIndex = self.mImgList.index(ustr(os.path.join(item.toolTip()))) + filename = self.mImgList[self.currIndex] + if filename: + self.mImgList5 = self.indexTo5Files(self.currIndex) + # self.additems5(None) + self.loadFile(filename) + + def CanvasSizeChange(self): + if len(self.mImgList) > 0: + self.zoomWidget.setValue(self.zoomWidgetValue + self.imgsplider.value()) + + # React to canvas signals. + def shapeSelectionChanged(self, selected=False): + if self._noSelectionSlot: + self._noSelectionSlot = False + else: + shape = self.canvas.selectedShape + if shape: + self.shapesToItems[shape].setSelected(True) + self.shapesToItemsbox[shape].setSelected(True) # ADD + else: + self.labelList.clearSelection() + self.actions.delete.setEnabled(selected) + self.actions.copy.setEnabled(selected) + self.actions.edit.setEnabled(selected) + self.actions.shapeLineColor.setEnabled(selected) + self.actions.shapeFillColor.setEnabled(selected) + + def addLabel(self, shape): + shape.paintLabel = self.displayLabelOption.isChecked() + item = HashableQListWidgetItem(shape.label) + item.setFlags(item.flags() | Qt.ItemIsUserCheckable) + item.setCheckState(Qt.Unchecked) if shape.difficult else item.setCheckState(Qt.Checked) + # Checked means difficult is False + # item.setBackground(generateColorByText(shape.label)) + self.itemsToShapes[item] = shape + self.shapesToItems[shape] = item + self.labelList.addItem(item) + # print('item in add label is ',[(p.x(), p.y()) for p in shape.points], shape.label) + + # ADD for box + item = HashableQListWidgetItem(str([(int(p.x()), int(p.y())) for p in shape.points])) + self.itemsToShapesbox[item] = shape + self.shapesToItemsbox[shape] = item + self.BoxList.addItem(item) + for action in self.actions.onShapesPresent: + action.setEnabled(True) + self.updateComboBox() + + def remLabel(self, shape): + if shape is None: + # print('rm empty label') + return + item = self.shapesToItems[shape] + self.labelList.takeItem(self.labelList.row(item)) + del self.shapesToItems[shape] + del self.itemsToShapes[item] + self.updateComboBox() + + # ADD: + item = self.shapesToItemsbox[shape] + self.BoxList.takeItem(self.BoxList.row(item)) + del self.shapesToItemsbox[shape] + del self.itemsToShapesbox[item] + self.updateComboBox() + + def loadLabels(self, shapes): + s = [] + for label, points, line_color, fill_color, difficult in shapes: + shape = Shape(label=label) + 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) + if snapped: + self.setDirty() + + shape.addPoint(QPointF(x, y)) + shape.difficult = difficult + shape.close() + s.append(shape) + + # if line_color: + # shape.line_color = QColor(*line_color) + # else: + # shape.line_color = generateColorByText(label) + # + # if fill_color: + # shape.fill_color = QColor(*fill_color) + # else: + # shape.fill_color = generateColorByText(label) + + self.addLabel(shape) + self.updateComboBox() + self.canvas.loadShapes(s) + + def updateComboBox(self): + # Get the unique labels and add them to the Combobox. + itemsTextList = [str(self.labelList.item(i).text()) for i in range(self.labelList.count())] + + uniqueTextList = list(set(itemsTextList)) + # Add a null row for showing all the labels + uniqueTextList.append("") + uniqueTextList.sort() + + # self.comboBox.update_items(uniqueTextList) + + def saveLabels(self, annotationFilePath, mode='Auto'): + # Mode is Auto means that labels will be loaded from self.result_dic totally, which is the output of ocr model + annotationFilePath = ustr(annotationFilePath) + if self.labelFile is None: + self.labelFile = LabelFile() + self.labelFile.verified = self.canvas.verified + + def format_shape(s): + # print('s in saveLabels is ',s) + return dict(label=s.label, # str + line_color=s.line_color.getRgb(), + fill_color=s.fill_color.getRgb(), + points=[(p.x(), p.y()) for p in s.points], # QPonitF + # add chris + difficult=s.difficult) # bool + + shapes = [] if mode == 'Auto' else \ + [format_shape(shape) for shape in self.canvas.shapes] + # Can add differrent annotation formats here + + for box in self.result_dic: + trans_dic = {"label": box[1][0], "points": box[0], 'difficult': False} + if trans_dic["label"] is "" and mode == 'Auto': + continue + shapes.append(trans_dic) + + try: + trans_dic = [] + for box in shapes: + trans_dic.append({"transcription": box['label'], "points": box['points'], 'difficult': box['difficult']}) + self.PPlabel[annotationFilePath] = trans_dic + + if mode == 'Auto': + self.Cachelabel[annotationFilePath] = trans_dic + + # else: + # self.labelFile.save(annotationFilePath, shapes, self.filePath, self.imageData, + # self.lineColor.getRgb(), self.fillColor.getRgb()) + # print('Image:{0} -> Annotation:{1}'.format(self.filePath, annotationFilePath)) + return True + except LabelFileError as e: + self.errorMessage(u'Error saving label data', u'%s' % e) + return False + + def copySelectedShape(self): + self.addLabel(self.canvas.copySelectedShape()) + # fix copy and delete + self.shapeSelectionChanged(True) + + + def labelSelectionChanged(self): + item = self.currentItem() + if item and self.canvas.editing(): + self._noSelectionSlot = True + self.canvas.selectShape(self.itemsToShapes[item]) + shape = self.itemsToShapes[item] + + def boxSelectionChanged(self): + item = self.currentBox() + if item and self.canvas.editing(): + self._noSelectionSlot = True + self.canvas.selectShape(self.itemsToShapesbox[item]) + shape = self.itemsToShapesbox[item] + + def labelItemChanged(self, item): + shape = self.itemsToShapes[item] + label = item.text() + if label != shape.label: + shape.label = item.text() + # shape.line_color = generateColorByText(shape.label) + self.setDirty() + elif not ((item.checkState()== Qt.Unchecked) ^ (not shape.difficult)): + shape.difficult = True if item.checkState() == Qt.Unchecked else False + self.setDirty() + else: # User probably changed item visibility + self.canvas.setShapeVisible(shape, True) # item.checkState() == Qt.Checked + # self.actions.save.setEnabled(True) + + # Callback functions: + def newShape(self): + """Pop-up and give focus to the label editor. + + position MUST be in global coordinates. + """ + if len(self.labelHist) > 0: + self.labelDialog = LabelDialog( + parent=self, listItem=self.labelHist) + + # Sync single class mode from PR#106 + if self.singleClassMode.isChecked() and self.lastLabel: + text = self.lastLabel + else: + text = self.labelDialog.popUp(text=self.prevLabelText) + self.lastLabel = text + + if text is not None: + self.prevLabelText = self.stringBundle.getString('tempLabel') + # generate_color = generateColorByText(text) + shape = self.canvas.setLastLabel(text, None, None)#generate_color, generate_color + self.addLabel(shape) + if self.beginner(): # Switch to edit mode. + self.canvas.setEditing(True) + self.actions.create.setEnabled(True) + else: + self.actions.editMode.setEnabled(True) + self.setDirty() + + else: + # self.canvas.undoLastLine() + self.canvas.resetAllLines() + + def scrollRequest(self, delta, orientation): + units = - delta / (8 * 15) + bar = self.scrollBars[orientation] + bar.setValue(bar.value() + bar.singleStep() * units) + + def setZoom(self, value): + self.actions.fitWidth.setChecked(False) + self.actions.fitWindow.setChecked(False) + self.zoomMode = self.MANUAL_ZOOM + self.zoomWidget.setValue(value) + + def addZoom(self, increment=10): + self.setZoom(self.zoomWidget.value() + increment) + + def zoomRequest(self, delta): + # get the current scrollbar positions + # calculate the percentages ~ coordinates + h_bar = self.scrollBars[Qt.Horizontal] + v_bar = self.scrollBars[Qt.Vertical] + + # get the current maximum, to know the difference after zooming + h_bar_max = h_bar.maximum() + v_bar_max = v_bar.maximum() + + # get the cursor position and canvas size + # calculate the desired movement from 0 to 1 + # where 0 = move left + # 1 = move right + # up and down analogous + cursor = QCursor() + pos = cursor.pos() + relative_pos = QWidget.mapFromGlobal(self, pos) + + cursor_x = relative_pos.x() + cursor_y = relative_pos.y() + + w = self.scrollArea.width() + h = self.scrollArea.height() + + # the scaling from 0 to 1 has some padding + # you don't have to hit the very leftmost pixel for a maximum-left movement + margin = 0.1 + move_x = (cursor_x - margin * w) / (w - 2 * margin * w) + move_y = (cursor_y - margin * h) / (h - 2 * margin * h) + + # clamp the values from 0 to 1 + move_x = min(max(move_x, 0), 1) + move_y = min(max(move_y, 0), 1) + + # zoom in + units = delta / (8 * 15) + scale = 10 + self.addZoom(scale * units) + + # get the difference in scrollbar values + # this is how far we can move + d_h_bar_max = h_bar.maximum() - h_bar_max + d_v_bar_max = v_bar.maximum() - v_bar_max + + # get the new scrollbar values + new_h_bar_value = h_bar.value() + move_x * d_h_bar_max + new_v_bar_value = v_bar.value() + move_y * d_v_bar_max + + h_bar.setValue(new_h_bar_value) + v_bar.setValue(new_v_bar_value) + + def setFitWindow(self, value=True): + if value: + self.actions.fitWidth.setChecked(False) + self.zoomMode = self.FIT_WINDOW if value else self.MANUAL_ZOOM + self.adjustScale() + + def setFitWidth(self, value=True): + if value: + self.actions.fitWindow.setChecked(False) + self.zoomMode = self.FIT_WIDTH if value else self.MANUAL_ZOOM + self.adjustScale() + + def togglePolygons(self, value): + for item, shape in self.itemsToShapes.items(): + self.canvas.setShapeVisible(shape, value) + + def loadFile(self, filePath=None): + """Load the specified file, or the last opened file if None.""" + self.resetState() + self.canvas.setEnabled(False) + if filePath is None: + filePath = self.settings.get(SETTING_FILENAME) + + # Make sure that filePath is a regular python string, rather than QString + filePath = ustr(filePath) + # Fix bug: An index error after select a directory when open a new file. + unicodeFilePath = ustr(filePath) + # unicodeFilePath = os.path.abspath(unicodeFilePath) + # Tzutalin 20160906 : Add file list and dock to move faster + # Highlight the file item + if unicodeFilePath and self.fileListWidget.count() > 0: + if unicodeFilePath in self.mImgList: + index = self.mImgList.index(unicodeFilePath) + fileWidgetItem = self.fileListWidget.item(index) + print('unicodeFilePath is', unicodeFilePath) + fileWidgetItem.setSelected(True) + ### + self.iconlist.clear() + self.additems5(None) + for i in range(5): + item_tooltip = self.iconlist.item(i).toolTip() + # print(i,"---",item_tooltip) + if item_tooltip == ustr(filePath): + titem = self.iconlist.item(i) + titem.setSelected(True) + self.iconlist.scrollToItem(titem) + break + else: + self.fileListWidget.clear() + self.mImgList.clear() + self.iconlist.clear() + + # if unicodeFilePath and self.iconList.count() > 0: + # if unicodeFilePath in self.mImgList: + + if unicodeFilePath and os.path.exists(unicodeFilePath): + if LabelFile.isLabelFile(unicodeFilePath): + try: + self.labelFile = LabelFile(unicodeFilePath) + except LabelFileError as e: + self.errorMessage(u'Error opening file', + (u"
%s
" + u"Make sure %s is a valid label file.") + % (e, unicodeFilePath)) + self.status("Error reading %s" % unicodeFilePath) + return False + self.imageData = self.labelFile.imageData + self.lineColor = QColor(*self.labelFile.lineColor) + self.fillColor = QColor(*self.labelFile.fillColor) + self.canvas.verified = self.labelFile.verified + else: + # Load image: + # read data first and store for saving into label file. + self.imageData = read(unicodeFilePath, None) + self.labelFile = None + self.canvas.verified = False + + image = QImage.fromData(self.imageData) + if image.isNull(): + self.errorMessage(u'Error opening file', + u"
Make sure %s is a valid image file." % unicodeFilePath) + self.status("Error reading %s" % unicodeFilePath) + return False + self.status("Loaded %s" % os.path.basename(unicodeFilePath)) + self.image = image + self.filePath = unicodeFilePath + self.canvas.loadPixmap(QPixmap.fromImage(image)) + if self.labelFile: + self.loadLabels(self.labelFile.shapes) + if self.validFilestate(filePath) is True: + self.setClean() + else: + self.dirty = False + self.actions.save.setEnabled(True) + + self.canvas.setEnabled(True) + self.adjustScale(initial=True) + self.paintCanvas() + self.addRecentFile(self.filePath) + self.toggleActions(True) + self.showBoundingBoxFromPPlabel(filePath) + + self.setWindowTitle(__appname__ + ' ' + filePath) + + # Default : select last item if there is at least one item + if self.labelList.count(): + self.labelList.setCurrentItem(self.labelList.item(self.labelList.count() - 1)) + self.labelList.item(self.labelList.count() - 1).setSelected(True) + + self.canvas.setFocus(True) + return True + return False + + + def showBoundingBoxFromPPlabel(self, filePath): + imgidx = self.getImglabelidx(filePath) + if imgidx not in self.PPlabel.keys(): + return + shapes = [] + for box in self.PPlabel[imgidx]: + shapes.append((box['transcription'], box['points'], None, None, box['difficult'])) + + print(shapes) + self.loadLabels(shapes) + self.canvas.verified = False + + + def validFilestate(self, filePath): + if filePath not in self.fileStatedict.keys(): + return None + elif self.fileStatedict[filePath] == 1: + return True + else: + return False + + def resizeEvent(self, event): + if self.canvas and not self.image.isNull() \ + and self.zoomMode != self.MANUAL_ZOOM: + self.adjustScale() + super(MainWindow, self).resizeEvent(event) + + def paintCanvas(self): + assert not self.image.isNull(), "cannot paint null image" + self.canvas.scale = 0.01 * self.zoomWidget.value() + self.canvas.adjustSize() + self.canvas.update() + + def adjustScale(self, initial=False): + value = self.scalers[self.FIT_WINDOW if initial else self.zoomMode]() + self.zoomWidget.setValue(int(100 * value)) + + def scaleFitWindow(self): + """Figure out the size of the pixmap in order to fit the main widget.""" + e = 2.0 # So that no scrollbars are generated. + w1 = self.centralWidget().width() - e + h1 = self.centralWidget().height() - e -110 + a1 = w1 / h1 + # Calculate a new scale value based on the pixmap's aspect ratio. + w2 = self.canvas.pixmap.width() - 0.0 + h2 = self.canvas.pixmap.height() - 0.0 + a2 = w2 / h2 + return w1 / w2 if a2 >= a1 else h1 / h2 + + def scaleFitWidth(self): + # The epsilon does not seem to work too well here. + w = self.centralWidget().width() - 2.0 + return w / self.canvas.pixmap.width() + + def closeEvent(self, event): + if not self.mayContinue(): + event.ignore() + else: + settings = self.settings + # If it loads images from dir, don't load it at the begining + if self.dirname is None: + settings[SETTING_FILENAME] = self.filePath if self.filePath else '' + else: + settings[SETTING_FILENAME] = '' + + settings[SETTING_WIN_SIZE] = self.size() + settings[SETTING_WIN_POSE] = self.pos() + settings[SETTING_WIN_STATE] = self.saveState() + settings[SETTING_LINE_COLOR] = self.lineColor + settings[SETTING_FILL_COLOR] = self.fillColor + settings[SETTING_RECENT_FILES] = self.recentFiles + settings[SETTING_ADVANCE_MODE] = not self._beginner + if self.defaultSaveDir and os.path.exists(self.defaultSaveDir): + settings[SETTING_SAVE_DIR] = ustr(self.defaultSaveDir) + else: + settings[SETTING_SAVE_DIR] = '' + + if self.lastOpenDir and os.path.exists(self.lastOpenDir): + settings[SETTING_LAST_OPEN_DIR] = self.lastOpenDir + else: + settings[SETTING_LAST_OPEN_DIR] = '' + + settings[SETTING_SINGLE_CLASS] = self.singleClassMode.isChecked() + settings[SETTING_PAINT_LABEL] = self.displayLabelOption.isChecked() + settings[SETTING_DRAW_SQUARE] = self.drawSquaresOption.isChecked() + settings.save() + try: + self.saveFilestate() + self.savePPlabel() + except: + pass + + def loadRecent(self, filename): + if self.mayContinue(): + self.loadFile(filename) + + def scanAllImages(self, folderPath): + extensions = ['.%s' % fmt.data().decode("ascii").lower() for fmt in QImageReader.supportedImageFormats()] + images = [] + + for file in os.listdir(folderPath): + if file.lower().endswith(tuple(extensions)): + relativePath = os.path.join(folderPath, file) + path = ustr(os.path.abspath(relativePath)) + images.append(path) + natural_sort(images, key=lambda x: x.lower()) + return images + + + + def openDirDialog(self, _value=False, dirpath=None, silent=False): + if not self.mayContinue(): + return + + defaultOpenDirPath = dirpath if dirpath else '.' + if self.lastOpenDir and os.path.exists(self.lastOpenDir): + defaultOpenDirPath = self.lastOpenDir + else: + defaultOpenDirPath = os.path.dirname(self.filePath) if self.filePath else '.' + if silent != True: + targetDirPath = ustr(QFileDialog.getExistingDirectory(self, + '%s - Open Directory' % __appname__, + defaultOpenDirPath, + QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)) + else: + targetDirPath = ustr(defaultOpenDirPath) + self.lastOpenDir = targetDirPath + self.importDirImages(targetDirPath) + + def importDirImages(self, dirpath, isDelete = False): + if not self.mayContinue() or not dirpath: + return + if self.defaultSaveDir and self.defaultSaveDir != dirpath: + self.saveFilestate() + self.savePPlabel() + + if not isDelete: + self.loadFilestate(dirpath) + self.PPlabelpath = dirpath+ '/Label.txt' + self.PPlabel = self.loadLabelFile(self.PPlabelpath) + self.Cachelabelpath = dirpath + '/Cache.cach' + self.Cachelabel = self.loadLabelFile(self.Cachelabelpath) + if self.Cachelabel: + self.PPlabel = dict(self.Cachelabel, **self.PPlabel) + self.lastOpenDir = dirpath + self.dirname = dirpath + + + self.defaultSaveDir = dirpath + self.statusBar().showMessage('%s started. Annotation will be saved to %s' % + (__appname__, self.defaultSaveDir)) + self.statusBar().show() + + self.filePath = None + self.fileListWidget.clear() + self.mImgList = self.scanAllImages(dirpath) + self.mImgList5 = self.mImgList[:5] + self.openNextImg() + doneicon = newIcon('done') + closeicon = newIcon('close') + for imgPath in self.mImgList: + filename = os.path.basename(imgPath) + if self.validFilestate(imgPath) is True: + item = QListWidgetItem(doneicon, filename) + else: + item = QListWidgetItem(closeicon, filename) + self.fileListWidget.addItem(item) + + print('dirPath in importDirImages is', dirpath) + self.iconlist.clear() + self.additems5(dirpath) + self.changeFileFolder = True + self.haveAutoReced = False + self.AutoRecognition.setEnabled(True) + self.reRecogButton.setEnabled(True) + + def verifyImg(self, _value=False): + # Proceding next image without dialog if having any label + if self.filePath is not None: + try: + self.labelFile.toggleVerify() + except AttributeError: + # If the labelling file does not exist yet, create if and + # re-save it with the verified attribute. + self.saveFile() + if self.labelFile != None: + self.labelFile.toggleVerify() + else: + return + + self.canvas.verified = self.labelFile.verified + self.paintCanvas() + self.saveFile() + + def openPrevImg(self, _value=False): + if len(self.mImgList) <= 0: + return + + if self.filePath is None: + return + + currIndex = self.mImgList.index(self.filePath) + self.mImgList5 = self.mImgList[:5] + if currIndex - 1 >= 0: + filename = self.mImgList[currIndex - 1] + self.mImgList5 = self.indexTo5Files(currIndex - 1) + if filename: + self.loadFile(filename) + + def openNextImg(self, _value=False): + if not self.mayContinue(): + return + + if len(self.mImgList) <= 0: + return + + filename = None + if self.filePath is None: + filename = self.mImgList[0] + self.mImgList5 = self.mImgList[:5] + else: + currIndex = self.mImgList.index(self.filePath) + if currIndex + 1 < len(self.mImgList): + filename = self.mImgList[currIndex + 1] + self.mImgList5 = self.indexTo5Files(currIndex + 1) + else: + self.mImgList5 = self.indexTo5Files(currIndex) + if filename: + print('file name in openNext is ',filename) + self.loadFile(filename) + + def openFile(self, _value=False): + if not self.mayContinue(): + return + path = os.path.dirname(ustr(self.filePath)) if self.filePath else '.' + formats = ['*.%s' % fmt.data().decode("ascii").lower() for fmt in QImageReader.supportedImageFormats()] + filters = "Image & Label files (%s)" % ' '.join(formats + ['*%s' % LabelFile.suffix]) + filename = QFileDialog.getOpenFileName(self, '%s - Choose Image or Label file' % __appname__, path, filters) + if filename: + if isinstance(filename, (tuple, list)): + filename = filename[0] + self.loadFile(filename) + # print('filename in openfile is ', self.filePath) + self.filePath = None + self.fileListWidget.clear() + self.iconlist.clear() + self.mImgList = [filename] + self.openNextImg() + if self.validFilestate(filename) is True: + item = QListWidgetItem(newIcon('done'), filename) + self.setClean() + elif self.validFilestate(filename) is None: + item = QListWidgetItem(newIcon('close'), filename) + else: + item = QListWidgetItem(newIcon('close'), filename) + self.setDirty() + self.fileListWidget.addItem(filename) + self.additems5(None) + print('opened image is', filename) + + def updateFileListIcon(self, filename): + pass + + def saveFile(self, _value=False, mode='Manual'): + # Manual mode is used for users click "Save" manually,which will change the state of the image + if self.defaultSaveDir is not None and len(ustr(self.defaultSaveDir)): + if self.filePath: + imgidx = self.getImglabelidx(self.filePath) + self._saveFile(imgidx, mode=mode) + + else: + imgFileDir = os.path.dirname(self.filePath) + imgFileName = os.path.basename(self.filePath) + savedFileName = os.path.splitext(imgFileName)[0] + savedPath = os.path.join(imgFileDir, savedFileName) + self._saveFile(savedPath if self.labelFile + else self.saveFileDialog(removeExt=False), mode=mode) + + def saveFileAs(self, _value=False): + assert not self.image.isNull(), "cannot save empty image" + self._saveFile(self.saveFileDialog()) + + def saveFileDialog(self, removeExt=True): + caption = '%s - Choose File' % __appname__ + filters = 'File (*%s)' % LabelFile.suffix + openDialogPath = self.currentPath() + dlg = QFileDialog(self, caption, openDialogPath, filters) + dlg.setDefaultSuffix(LabelFile.suffix[1:]) + dlg.setAcceptMode(QFileDialog.AcceptSave) + filenameWithoutExtension = os.path.splitext(self.filePath)[0] + dlg.selectFile(filenameWithoutExtension) + dlg.setOption(QFileDialog.DontUseNativeDialog, False) + if dlg.exec_(): + fullFilePath = ustr(dlg.selectedFiles()[0]) + if removeExt: + return os.path.splitext(fullFilePath)[0] # Return file path without the extension. + else: + return fullFilePath + return '' + + def _saveFile(self, annotationFilePath, mode='Manual'): + if mode == 'Manual': + if annotationFilePath and self.saveLabels(annotationFilePath, mode=mode): + self.setClean() + self.statusBar().showMessage('Saved to %s' % annotationFilePath) + self.statusBar().show() + currIndex = self.mImgList.index(self.filePath) + item = self.fileListWidget.item(currIndex) + item.setIcon(newIcon('done')) + + self.fileStatedict[self.filePath] = 1 + if len(self.fileStatedict)%self.autoSaveNum ==0: + self.saveFilestate() + self.savePPlabel(mode='Auto') + + self.fileListWidget.insertItem(int(currIndex), item) + self.openNextImg() + self.actions.saveRec.setEnabled(True) + + elif mode == 'Auto': + if annotationFilePath and self.saveLabels(annotationFilePath, mode=mode): + + self.setClean() + self.statusBar().showMessage('Saved to %s' % annotationFilePath) + self.statusBar().show() + + def closeFile(self, _value=False): + if not self.mayContinue(): + return + self.resetState() + self.setClean() + self.toggleActions(False) + self.canvas.setEnabled(False) + self.actions.saveAs.setEnabled(False) + + def deleteImg(self): + deletePath = self.filePath + if deletePath is not None: + deleteInfo = self.deleteImgDialog() + if deleteInfo == QMessageBox.Yes: + if platform.system() == 'Windows': + from win32com.shell import shell, shellcon + shell.SHFileOperation((0, shellcon.FO_DELETE, deletePath, None, + shellcon.FOF_SILENT | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION, + None, None)) + # linux + elif platform.system() == 'Linux': + cmd = 'trash ' + deletePath + os.system(cmd) + # macOS + elif platform.system() == 'Darwin': + import subprocess + absPath = os.path.abspath(deletePath).replace('\\', '\\\\').replace('"', '\\"') + cmd = ['osascript', '-e', + 'tell app "Finder" to move {the POSIX file "' + absPath + '"} to trash'] + print(cmd) + subprocess.call(cmd, stdout=open(os.devnull, 'w')) + + if self.filePath in self.fileStatedict.keys(): + self.fileStatedict.pop(self.filePath) + imgidx = self.getImglabelidx(self.filePath) + if imgidx in self.PPlabel.keys(): + self.PPlabel.pop(imgidx) + self.openNextImg() + self.importDirImages(self.lastOpenDir, isDelete=True) + + def deleteImgDialog(self): + yes, cancel = QMessageBox.Yes, QMessageBox.Cancel + msg = u'The image will be deleted to the recycle bin' + return QMessageBox.warning(self, u'Attention', msg, yes | cancel) + + def resetAll(self): + self.settings.reset() + self.close() + proc = QProcess() + proc.startDetached(os.path.abspath(__file__)) + + def mayContinue(self): # + if not self.dirty: + return True + else: + discardChanges = self.discardChangesDialog() + if discardChanges == QMessageBox.No: + return True + elif discardChanges == QMessageBox.Yes: + self.saveFile() + return True + else: + return False + + def discardChangesDialog(self): + yes, no, cancel = QMessageBox.Yes, QMessageBox.No, QMessageBox.Cancel + msg = u'You have unsaved changes, would you like to save them and proceed?\nClick "No" to undo all changes.' + return QMessageBox.warning(self, u'Attention', msg, yes | no | cancel) + + def errorMessage(self, title, message): + return QMessageBox.critical(self, title, + '
%s
%s' % (title, message)) + + def currentPath(self): + return os.path.dirname(self.filePath) if self.filePath else '.' + + def chooseColor1(self): + color = self.colorDialog.getColor(self.lineColor, u'Choose line color', + default=DEFAULT_LINE_COLOR) + if color: + self.lineColor = color + Shape.line_color = color + self.canvas.setDrawingColor(color) + self.canvas.update() + self.setDirty() + + def deleteSelectedShape(self): + self.remLabel(self.canvas.deleteSelected()) + self.setDirty() + if self.noShapes(): + for action in self.actions.onShapesPresent: + action.setEnabled(False) + + def chshapeLineColor(self): + color = self.colorDialog.getColor(self.lineColor, u'Choose line color', + default=DEFAULT_LINE_COLOR) + if color: + self.canvas.selectedShape.line_color = color + self.canvas.update() + self.setDirty() + + def chshapeFillColor(self): + color = self.colorDialog.getColor(self.fillColor, u'Choose fill color', + default=DEFAULT_FILL_COLOR) + if color: + self.canvas.selectedShape.fill_color = color + self.canvas.update() + self.setDirty() + + def copyShape(self): + self.canvas.endMove(copy=True) + self.addLabel(self.canvas.selectedShape) + self.setDirty() + + def moveShape(self): + self.canvas.endMove(copy=False) + self.setDirty() + + def loadPredefinedClasses(self, predefClassesFile): + if os.path.exists(predefClassesFile) is True: + with codecs.open(predefClassesFile, 'r', 'utf8') as f: + for line in f: + line = line.strip() + if self.labelHist is None: + self.labelHist = [line] + else: + self.labelHist.append(line) + + + def togglePaintLabelsOption(self): + for shape in self.canvas.shapes: + shape.paintLabel = self.displayLabelOption.isChecked() + + def toogleDrawSquare(self): + self.canvas.setDrawingShapeToSquare(self.drawSquaresOption.isChecked()) + + def additems(self, dirpath): + for file in self.mImgList: + pix = QPixmap(file) + _, filename = os.path.split(file) + filename, _ = os.path.splitext(filename) + item = QListWidgetItem(QIcon(pix.scaled(100, 100, Qt.IgnoreAspectRatio, Qt.FastTransformation)), + filename[:10]) + item.setToolTip(file) + self.iconlist.addItem(item) + + def additems5(self, dirpath): + for file in self.mImgList5: + pix = QPixmap(file) + _, filename = os.path.split(file) + filename, _ = os.path.splitext(filename) + pfilename = filename[:10] + if len(pfilename) < 10: + lentoken = 12 - len(pfilename) + prelen = lentoken // 2 + bfilename = prelen * " " + pfilename + (lentoken - prelen) * " " + # item = QListWidgetItem(QIcon(pix.scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation)),filename[:10]) + item = QListWidgetItem(QIcon(pix.scaled(100, 100, Qt.IgnoreAspectRatio, Qt.FastTransformation)),pfilename) + # item.setForeground(QBrush(Qt.white)) + item.setToolTip(file) + self.iconlist.addItem(item) + owidth = 0 + for index in range(len(self.mImgList5)): + item = self.iconlist.item(index) + itemwidget = self.iconlist.visualItemRect(item) + owidth += itemwidget.width() + self.iconlist.setMinimumWidth(owidth + 50) + + def getImglabelidx(self, filePath): + if platform.system()=='Windows': + spliter = '\\' + else: + spliter = '/' + filepathsplit = filePath.split(spliter)[-2:] + return filepathsplit[0] + '/' + filepathsplit[1] + + def autoRecognition(self): + assert self.mImgList is not None + print('Using model from ', self.model) + + uncheckedList = [i for i in self.mImgList if i not in self.fileStatedict.keys()] + self.autoDialog = AutoDialog(parent=self, ocr=self.ocr, mImgList=uncheckedList, lenbar=len(uncheckedList)) + self.autoDialog.popUp() + self.currIndex=len(self.mImgList) + self.loadFile(self.filePath) # ADD + self.haveAutoReced = True + self.AutoRecognition.setEnabled(False) + self.setDirty() + self.saveCacheLabel() + + + def reRecognition(self): + img = cv2.imread(self.filePath) + # org_box = [dic['points'] for dic in self.PPlabel[self.getImglabelidx(self.filePath)]] + if self.canvas.shapes: + self.result_dic = [] + rec_flag = 0 + for shape in self.canvas.shapes: + box = [[int(p.x()), int(p.y())] for p in shape.points] + assert len(box) == 4 + img_crop = get_rotate_crop_image(img, np.array(box, np.float32)) + if img_crop is None: + msg = 'Can not recognise the detection box in ' + self.filePath + '. Please change manually' + QMessageBox.information(self, "Information", msg) + return + result = self.ocr.ocr(img_crop, cls=True, det=False) + if result[0][0] is not '': + result.insert(0, box) + print('result in reRec is ', result) + self.result_dic.append(result) + if result[1][0] == shape.label: + print('label no change') + else: + rec_flag += 1 + + if len(self.result_dic) > 0 and rec_flag > 0: + self.saveFile(mode='Auto') + self.loadFile(self.filePath) + self.setDirty() + elif len(self.result_dic) == len(self.canvas.shapes) and rec_flag == 0: + QMessageBox.information(self, "Information", "The recognition result remains unchanged!") + else: + print('Can not recgonise in ', self.filePath) + else: + QMessageBox.information(self, "Information", "Draw a box!") + + + def autolcm(self): + vbox = QVBoxLayout() + hbox = QHBoxLayout() + self.panel = QLabel() + self.panel.setText(self.stringBundle.getString('choseModelLg')) + self.panel.setAlignment(Qt.AlignLeft) + self.comboBox = QComboBox() + self.comboBox.setObjectName("comboBox") + self.comboBox.addItems(['Chinese & English', 'English', 'French', 'German', 'Korean', 'Japanese']) + vbox.addWidget(self.panel) + vbox.addWidget(self.comboBox) + self.dialog = QDialog() + self.dialog.resize(300, 100) + self.okBtn = QPushButton(self.stringBundle.getString('ok')) + self.cancelBtn = QPushButton(self.stringBundle.getString('cancel')) + + self.okBtn.clicked.connect(self.modelChoose) + self.cancelBtn.clicked.connect(self.cancel) + self.dialog.setWindowTitle(self.stringBundle.getString('choseModelLg')) + + hbox.addWidget(self.okBtn) + hbox.addWidget(self.cancelBtn) + + vbox.addWidget(self.panel) + vbox.addLayout(hbox) + self.dialog.setLayout(vbox) + self.dialog.setWindowModality(Qt.ApplicationModal) + self.dialog.exec_() + if self.filePath: + self.AutoRecognition.setEnabled(True) + + + def modelChoose(self): + print(self.comboBox.currentText()) + lg_idx = {'Chinese & English': 'ch', 'English': 'en', 'French': 'french', 'German': 'german', + 'Korean': 'korean', 'Japanese': 'japan'} + del self.ocr + self.ocr = PaddleOCR(use_pdserving=False, use_angle_cls=True, det=True, cls=True, use_gpu=False, + lang=lg_idx[self.comboBox.currentText()]) + self.dialog.close() + + def cancel(self): + self.dialog.close() + + def loadFilestate(self, saveDir): + self.fileStatepath = saveDir + '/fileState.txt' + self.fileStatedict = {} + if not os.path.exists(self.fileStatepath): + f = open(self.fileStatepath, 'w', encoding='utf-8') + else: + with open(self.fileStatepath, 'r', encoding='utf-8') as f: + states = f.readlines() + for each in states: + file, state = each.split('\t') + self.fileStatedict[file] = 1 + + + def saveFilestate(self): + with open(self.fileStatepath, 'w', encoding='utf-8') as f: + for key in self.fileStatedict: + f.write(key + '\t') + f.write(str(self.fileStatedict[key]) + '\n') + + + def loadLabelFile(self, labelpath): + labeldict = {} + if not os.path.exists(labelpath): + f = open(labelpath, 'w', encoding='utf-8') + + else: + with open(labelpath, 'r', encoding='utf-8') as f: + data = f.readlines() + for each in data: + file, label = each.split('\t') + if label: + label = label.replace('false', 'False') + label = label.replace('true', 'True') + labeldict[file] = eval(label) + else: + labeldict[file] = [] + return labeldict + + + def savePPlabel(self,mode='Manual'): + savedfile = [self.getImglabelidx(i) for i in self.fileStatedict.keys()] + with open(self.PPlabelpath, 'w', encoding='utf-8') as f: + for key in self.PPlabel: + if key in savedfile and self.PPlabel[key] != []: + f.write(key + '\t') + f.write(json.dumps(self.PPlabel[key], ensure_ascii=False) + '\n') + + if mode=='Manual': + msg = 'Images that have been checked are saved in '+ self.PPlabelpath + QMessageBox.information(self, "Information", msg) + + def saveCacheLabel(self): + with open(self.Cachelabelpath, 'w', encoding='utf-8') as f: + for key in self.Cachelabel: + f.write(key + '\t') + f.write(json.dumps(self.Cachelabel[key], ensure_ascii=False) + '\n') + + def saveRecResult(self): + if None in [self.PPlabelpath, self.PPlabel, self.fileStatedict]: + QMessageBox.information(self, "Information", "Save file first") + return + + rec_gt_dir = os.path.dirname(self.PPlabelpath) + '/rec_gt.txt' + crop_img_dir = os.path.dirname(self.PPlabelpath) + '/crop_img/' + if not os.path.exists(crop_img_dir): + os.mkdir(crop_img_dir) + + with open(rec_gt_dir, 'w', encoding='utf-8') as f: + for key in self.fileStatedict: + idx = self.getImglabelidx(key) + for i, label in enumerate(self.PPlabel[idx]): + if label['difficult']: continue + img = cv2.imread(key) + img_crop = get_rotate_crop_image(img, np.array(label['points'], np.float32)) + img_name = os.path.splitext(os.path.basename(idx))[0] + '_crop_'+str(i)+'.jpg' + cv2.imwrite(crop_img_dir+img_name, img_crop) + f.write('crop_img/'+ img_name + '\t') + f.write(label['transcription'] + '\n') + + QMessageBox.information(self, "Information", "Cropped images has been saved in "+str(crop_img_dir)) + +def inverted(color): + return QColor(*[255 - v for v in color.getRgb()]) + + +def read(filename, default=None): + try: + with open(filename, 'rb') as f: + return f.read() + except: + return default + + +def get_main_app(argv=[]): + """ + Standard boilerplate Qt application code. + Do everything but app.exec_() -- so that we can test the application in one thread + """ + app = QApplication(argv) + app.setApplicationName(__appname__) + app.setWindowIcon(newIcon("app")) + # Tzutalin 201705+: Accept extra agruments to change predefined class file + argparser = argparse.ArgumentParser() + argparser.add_argument("--lang", default='ch', nargs="?") + argparser.add_argument("--predefined_classes_file", + default=os.path.join(os.path.dirname(__file__), "data", "predefined_classes.txt"), + nargs="?") + args = argparser.parse_args(argv[1:]) + # Usage : labelImg.py image predefClassFile saveDir + win = MainWindow(lang=args.lang, + defaultPrefdefClassFile=args.predefined_classes_file) + win.show() + return app, win + + +def main(): + '''construct main app and run it''' + app, _win = get_main_app(sys.argv) + return app.exec_() + + +if __name__ == '__main__': + + resource_file = './libs/resources.py' + if not os.path.exists(resource_file): + output = os.system('pyrcc5 -o libs/resources.py resources.qrc') + assert output is 0, "operate the cmd have some problems ,please check whether there is a in the lib " \ + "directory resources.py " + import libs.resources + sys.exit(main()) diff --git a/PPOCRLabel/README.md b/PPOCRLabel/README.md new file mode 100644 index 00000000..eb22a3a0 --- /dev/null +++ b/PPOCRLabel/README.md @@ -0,0 +1,99 @@ +# PPOCRLabel + +PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具,使用python3和pyqt5编写,支持矩形框标注和四点标注模式,导出格式可直接用于PPOCR检测和识别模型的训练。 + + + +## 安装 + +### 1. 安装PaddleOCR +参考[PaddleOCR安装文档](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/installation.md)准备好PaddleOCR + +### 2. 安装PPOCRLabel +#### Windows + Anaconda + +下载安装[Anaconda](https://www.anaconda.com/download/#download) (Python 3+) + +``` +conda install pyqt=5 +cd ./PPOCRLabel # 将目录切换到PPOCRLabel文件夹下 +pyrcc5 -o libs/resources.py resources.qrc +python PPOCRLabel.py +``` + +#### Ubuntu Linux + +``` +pip3 install pyqt5 +pip3 install trash-cli +cd ./PPOCRLabel # 将目录切换到PPOCRLabel文件夹下 +python3 PPOCRLabel.py +``` + +#### macOS +``` +pip3 install pyqt5 +pip3 uninstall opencv-python # 由于mac版本的opencv与pyqt有冲突,需先手动卸载opencv +pip3 install opencv-contrib-python-headless # 安装headless版本的open-cv +cd ./PPOCRLabel # 将目录切换到PPOCRLabel文件夹下 +python3 PPOCRLabel.py +``` + +## 使用 + +### 操作步骤 + +1. 安装与运行:使用上述命令安装与运行程序。 +2. 打开文件夹:在菜单栏点击 “文件” - "打开目录" 选择待标记图片的文件夹[1]. +3. 自动标注:点击 ”自动标注“,使用PPOCR超轻量模型对图片文件名前图片状态[2]为 “X” 的图片进行自动标注。 +4. 手动标注:点击 “矩形标注”(推荐直接在英文模式下点击键盘中的 “W”),用户可对当前图片中模型未检出的部分进行手动绘制标记框。点击键盘P,则使用四点标注模式(或点击“编辑” - “四点标注”),用户依次点击4个点后,双击左键表示标注完成。 +5. 标记框绘制完成后,用户点击 “确认”,检测框会先被预分配一个 “待识别” 标签。 +6. 重新识别:将图片中的所有检测画绘制/调整完成后,点击 “重新识别”,PPOCR模型会对当前图片中的**所有检测框**重新识别[3]。 +7. 内容更改:双击识别结果,对不准确的识别结果进行手动更改。 +8. 确认结果:点击 “确认”,图片状态切换为 “√”,跳转至下一张。 +9. 删除:点击 “删除图像”,图片将会被删除至回收站。 +10. 保存标注结果:关闭应用程序或切换文件路径后,手动确认过的标签将会被存放在所打开图片文件夹下的*Label.txt*中。在菜单栏点击 “PaddleOCR” - "保存识别结果"后,会将此类图片的识别训练数据保存在*crop_img*文件夹下,识别标签保存在*rec_gt.txt*中[4]。 + +### 注意 + +[1] PPOCRLabel以文件夹为基本标记单位,打开待标记的图片文件夹后,不会在窗口栏中显示图片,而是在点击 "选择文件夹" 之后直接将文件夹下的图片导入到程序中。 + +[2] 图片状态表示本张图片用户是否手动保存过,未手动保存过即为 “X”,手动保存过为 “√”。点击 “自动标注”按钮后,PPOCRLabel不会对状态为 “√” 的图片重新标注。 + +[3] 点击“重新识别”后,模型会对图片中的识别结果进行覆盖。因此如果在此之前手动更改过识别结果,有可能在重新识别后产生变动。 + +[4] PPOCRLabel产生的文件均在标记图片的文件夹中,包括一下几种,请勿手动更改其中内容,否则会引起程序出现异常。 + +| 文件名 | 说明 | +| :-----------: | :----------------------------------------------------------: | +| Label.txt | 检测标签,可直接用于PPOCR检测模型训练。用户每保存10张检测结果后,程序会进行自动写入。当用户关闭应用程序或切换文件路径后同样会进行写入。 | +| fileState.txt | 图片状态标记文件,保存当前文件夹下已经被用户手动确认过的图片名称。 | +| Cache.cach | 缓存文件,保存模型自动识别的结果。 | +| rec_gt.txt | 识别标签。可直接用于PPOCR识别模型训练。需用户手动点击菜单栏“PaddleOCR” - "保存识别结果"后产生。 | +| crop_img | 识别数据。按照检测框切割后的图片。与rec_gt.txt同时产生。 | + +## 说明 +### 内置模型 + + - 默认模型:PPOCRLabel默认使用PaddleOCR中的中英文超轻量OCR模型,支持中英文与数字识别,多种语言检测。 + + - 模型语言切换:用户可通过菜单栏中 "PaddleOCR" - "选择模型" 切换内置模型语言,目前支持的语言包括法文、德文、韩文、日文。具体模型下载链接可参考[PaddleOCR模型列表](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/models_list.md). + + - 自定义模型:用户可根据[自定义模型代码使用](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/whl.md#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9E%8B),通过修改PPOCRLabel.py中针对[PaddleOCR类的实例化](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/PPOCRLabel/PPOCRLabel.py#L110)替换成自己训练的模型。 + +### 导出部分识别结果 + +针对部分难以识别的数据,通过在识别结果的复选框中**取消勾选**相应的标记,其识别结果不会被导出。 + +*注意:识别结果中的复选框状态仍需用户手动点击保存后才能保留* + +### 错误提示 +- 如果同时使用whl包安装了paddleocr,其优先级大于通过paddleocr.py调用PaddleOCR类,whl包未更新时会导致程序异常。 +- PPOCRLabel**不支持对中文文件名**的图片进行自动标注。 +- 如果您在打开软件过程中出现**objc[XXXXX]**开头的错误,证明您的opencv版本太高,建议安装4.2版本: + ``` + pip install opencv-python==4.2.0.32 + ``` +### 参考资料 + +1.[Tzutalin. LabelImg. Git code (2015)](https://github.com/tzutalin/labelImg) diff --git a/PPOCRLabel/README_en.md b/PPOCRLabel/README_en.md new file mode 100644 index 00000000..668ca200 --- /dev/null +++ b/PPOCRLabel/README_en.md @@ -0,0 +1,121 @@ +# PPOCRLabel + +PPOCRLabel is a semi-automatic graphic annotation tool suitable for OCR field. It is written in python3 and pyqt5, supporting rectangular box annotation and four-point annotation modes. Annotations can be directly used for the training of PPOCR detection and recognition models. + + + +## Installation + +### 1. Install PaddleOCR + +Refer to [PaddleOCR installation document](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/installation.md) to prepare PaddleOCR + +### 2. Install PPOCRLabel + +#### Windows + Anaconda + +Download and install [Anaconda](https://www.anaconda.com/download/#download) (Python 3+) + +``` +conda install pyqt=5 +cd ./PPOCRLabel # Change the directory to the PPOCRLabel folder +pyrcc5 -o libs/resources.py resources.qrc +python PPOCRLabel.py --lang en +``` + +#### Ubuntu Linux + +``` +pip3 install pyqt5 +pip3 install trash-cli +cd ./PPOCRLabel # Change the directory to the PPOCRLabel folder +python3 PPOCRLabel.py --lang en +``` + +#### macOS +``` +pip3 install pyqt5 +pip3 uninstall opencv-python # Uninstall opencv manually as it conflicts with pyqt +pip3 install opencv-contrib-python-headless # Install the headless version of opencv +cd ./PPOCRLabel # Change the directory to the PPOCRLabel folder +python3 PPOCRLabel.py --lang en +``` + +## Usage + +### Steps + +1. Build and launch using the instructions above. + +2. Click 'Open Dir' in Menu/File to select the folder of the picture.[1] + +3. Click 'Auto recognition', use PPOCR model to automatically annotate images which marked with 'X' [2]before the file name. + +4. Create Box: + + 4.1 Click 'Create RectBox' or press 'W' in English keyboard mode to draw a new rectangle detection box. Click and release left mouse to select a region to annotate the text area. + + 4.2 Press 'P' to enter four-point labeling mode which enables you to create any four-point shape by clicking four points with the left mouse button in succession and DOUBLE CLICK the left mouse as the signal of labeling completion. + +5. After the marking frame is drawn, the user clicks "OK", and the detection frame will be pre-assigned a "TEMPORARY" label. + +6. Click 're-Recognition', model will rewrite ALL recognition results in ALL detection box[3]. + +7. Double click the result in 'recognition result' list to manually change inaccurate recognition results. + +8. Click "Save", the image status will switch to "√",then the program automatically jump to the next. + +9. Click "Delete Image" and the image will be deleted to the recycle bin. + +10. Labeling result: After closing the application or switching the file path, the manually saved label will be stored in *Label.txt* under the opened picture folder. + Click "PaddleOCR"-"Save Recognition Results" in the menu bar, the recognition training data of such pictures will be saved in the *crop_img* folder, and the recognition label will be saved in *rec_gt.txt*[4]. + +### Note + +[1] PPOCRLabel uses the opened folder as the project. After opening the image folder, the picture will not be displayed in the dialog. Instead, the pictures under the folder will be directly imported into the program after clicking "Open Dir". + +[2] The image status indicates whether the user has saved the image manually. If it has not been saved manually it is "X", otherwise it is "√", PPOCRLabel will not relabel pictures with a status of "√". + +[3] After clicking "Re-recognize", the model will overwrite ALL recognition results in the picture. +Therefore, if the recognition result has been manually changed before, it may change after re-recognition. + +[4] The files produced by PPOCRLabel include the following, please do not manually change the contents, otherwise it will cause the program to be abnormal. + +| File name | Description | +| :-----------: | :----------------------------------------------------------: | +| Label.txt | The detection label file can be directly used for PPOCR detection model training. After the user saves 10 label results, the file will be automatically saved. It will also be written when the user closes the application or changes the file folder. | +| fileState.txt | The picture status file save the image in the current folder that has been manually confirmed by the user. | +| Cache.cach | Cache files to save the results of model recognition. | +| rec_gt.txt | The recognition label file, which can be directly used for PPOCR identification model training, is generated after the user clicks on the menu bar "PaddleOCR"-"Save recognition result". | +| crop_img | The recognition data, generated at the same time with *rec_gt.txt* | + +## Explanation + +### Built-in Model + +- Default model: PPOCRLabel uses the Chinese and English ultra-lightweight OCR model in PaddleOCR by default, supports Chinese, English and number recognition, and multiple language detection. + +- Model language switching: Changing the built-in model language is supportable by clicking "PaddleOCR"-"Choose OCR Model" in the menu bar. Currently supported languagesinclude French, German, Korean, and Japanese. + For specific model download links, please refer to [PaddleOCR Model List](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_en/models_list_en.md#multilingual-recognition-modelupdating) + +- Custom model: The model trained by users can be replaced by modifying PPOCRLabel.py in [PaddleOCR class instantiation](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/PPOCRLabel/PPOCRLabel.py#L110) referring [Custom Model Code](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_en/whl_en.md#use-custom-model) + +### Export partial recognition results + +For some data that are difficult to recognize, the recognition results will not be exported by **unchecking** the corresponding tags in the recognition results checkbox. + +*Note: The status of the checkboxes in the recognition results still needs to be saved manually by clicking Save Button.* + +### Error message + +- If paddleocr is installed with whl, it has a higher priority than calling PaddleOCR class with paddleocr.py, which may cause an exception if whl package is not updated. + +- If you get an error starting with **objc[XXXXX]** when opening the software, it proves that your opencv version is too high. It is recommended to install version 4.2: + + ``` + pip install opencv-python==4.2.0.32 + ``` + +### Related + +1.[Tzutalin. LabelImg. Git code (2015)](https://github.com/tzutalin/labelImg) diff --git a/PPOCRLabel/__init__.py b/PPOCRLabel/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/PPOCRLabel/combobox.py b/PPOCRLabel/combobox.py new file mode 100644 index 00000000..8526b453 --- /dev/null +++ b/PPOCRLabel/combobox.py @@ -0,0 +1,46 @@ +# Copyright (c) <2015-Present> Tzutalin +# Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba, +# William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +# associated documentation files (the "Software"), to deal in the Software without restriction, including without +# limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of +# the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import sys +try: + from PyQt5.QtWidgets import QWidget, QHBoxLayout, QComboBox +except ImportError: + # needed for py3+qt4 + # Ref: + # http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html + # http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string + if sys.version_info.major >= 3: + import sip + sip.setapi('QVariant', 2) + from PyQt4.QtGui import QWidget, QHBoxLayout, QComboBox + + +class ComboBox(QWidget): + def __init__(self, parent=None, items=[]): + super(ComboBox, self).__init__(parent) + + layout = QHBoxLayout() + self.cb = QComboBox() + self.items = items + self.cb.addItems(self.items) + + self.cb.currentIndexChanged.connect(parent.comboSelectionChanged) + + layout.addWidget(self.cb) + self.setLayout(layout) + + def update_items(self, items): + self.items = items + + self.cb.clear() + self.cb.addItems(self.items) diff --git a/PPOCRLabel/data/gif/steps.gif b/PPOCRLabel/data/gif/steps.gif new file mode 100644 index 0000000000000000000000000000000000000000..6ea998500b4ce5c2f3c3861ce5c9a71c99ae83cb GIT binary patch literal 2491358 zcmW*RWmpv7+XwJb0cnsj2#KXrl#m9ArE}@-SQW4Xkv;euBc**EWYSsj4;k9V~sT4Xk(5z?x K)*Qw5Srt_WWG-oc^3D0@DGoJLs=RNINPj2Ru
zpZe@4Is-~jfEv`F`$VWf<*CquHuRtag(yQK`p}9}^r92Z=szv0(S%}jqaXEXM;8jx
zkvcS_Bo%2%^Vtxs1dKN?on{B6Ia8S~j4ZO^XgR~D!k(`1r#|&*J6iY`AEHbFn^?^M
z%y?L`N+#wpD9ci0Y)F_%9Me+Jdx};4LPw)jjcZ=aUV|pZL4sZJITXrX;EXdN-1Mw;Tmq@1}@?*
zuHx!#;WlpMJ}%=
^v-+v8x_dH}r^~v2-*@}*x1-NRrR(~o^ZKqM%$ei*`8;;85A+FpxQe^@
zhQD~SA3KXL`?D*1v^P7mm$