PPOCRLabel.py 115.5 KB
Newer Older
qq_25193841's avatar
qq_25193841 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# 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.
13
# !/usr/bin/env python
qq_25193841's avatar
qq_25193841 已提交
14 15 16 17 18
# -*- coding: utf-8 -*-
# pyrcc5 -o libs/resources.py resources.qrc
import argparse
import ast
import codecs
19
import json
qq_25193841's avatar
qq_25193841 已提交
20 21 22 23
import os.path
import platform
import subprocess
import sys
W
whjdark 已提交
24
from tkinter.tix import Tree
25
import xlrd
qq_25193841's avatar
qq_25193841 已提交
26 27
from functools import partial

HinGwenWoong's avatar
HinGwenWoong 已提交
28 29 30
from PyQt5.QtCore import QSize, Qt, QPoint, QByteArray, QTimer, QFileInfo, QPointF, QProcess
from PyQt5.QtGui import QImage, QCursor, QPixmap, QImageReader
from PyQt5.QtWidgets import QMainWindow, QListWidget, QVBoxLayout, QToolButton, QHBoxLayout, QDockWidget, QWidget, \
W
new  
whj_dark 已提交
31
    QSlider, QGraphicsOpacityEffect, QMessageBox, QListView, QScrollArea, QWidgetAction, QApplication, QLabel, QGridLayout, \
W
whjdark 已提交
32
    QFileDialog, QListWidgetItem, QComboBox, QDialog, QAbstractItemView
33

qq_25193841's avatar
qq_25193841 已提交
34
__dir__ = os.path.dirname(os.path.abspath(__file__))
35

qq_25193841's avatar
qq_25193841 已提交
36 37
sys.path.append(__dir__)
sys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))
38
sys.path.append(os.path.abspath(os.path.join(__dir__, '../PaddleOCR')))
qq_25193841's avatar
qq_25193841 已提交
39 40
sys.path.append("..")

W
new  
whj_dark 已提交
41
from paddleocr import PaddleOCR, PPStructure
qq_25193841's avatar
qq_25193841 已提交
42 43
from libs.constants import *
from libs.utils import *
44
from libs.labelColor import label_colormap
qq_25193841's avatar
qq_25193841 已提交
45
from libs.settings import Settings
46
from libs.shape import Shape, DEFAULT_LINE_COLOR, DEFAULT_FILL_COLOR, DEFAULT_LOCK_COLOR
qq_25193841's avatar
qq_25193841 已提交
47 48 49 50 51 52 53 54
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.ustr import ustr
from libs.hashableQListWidgetItem import HashableQListWidgetItem
qq_25193841's avatar
qq_25193841 已提交
55
from libs.editinlist import EditInList
56 57
from libs.unique_label_qlist_widget import UniqueLabelQListWidget
from libs.keyDialog import KeyDialog
qq_25193841's avatar
qq_25193841 已提交
58 59 60

__appname__ = 'PPOCRLabel'

61 62
LABEL_COLORMAP = label_colormap()

qq_25193841's avatar
qq_25193841 已提交
63

64
class MainWindow(QMainWindow):
qq_25193841's avatar
qq_25193841 已提交
65 66
    FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = list(range(3))

67 68 69
    def __init__(self,
                 lang="ch",
                 gpu=False,
70
                 kie_mode=False,
71
                 default_filename=None,
H
HinGwenWoong 已提交
72
                 default_predefined_class_file=None,
73
                 default_save_dir=None):
qq_25193841's avatar
qq_25193841 已提交
74 75
        super(MainWindow, self).__init__()
        self.setWindowTitle(__appname__)
76 77
        self.setWindowState(Qt.WindowMaximized)  # set window max
        self.activateWindow()  # PPOCRLabel goes to the front when activate
qq_25193841's avatar
qq_25193841 已提交
78 79 80

        # Load setting in the main thread
        self.settings = Settings()
81
        self.settings.load()
qq_25193841's avatar
qq_25193841 已提交
82 83
        settings = self.settings
        self.lang = lang
HinGwenWoong's avatar
HinGwenWoong 已提交
84

qq_25193841's avatar
qq_25193841 已提交
85 86 87
        # Load string bundle for i18n
        if lang not in ['ch', 'en']:
            lang = 'en'
88
        self.stringBundle = StringBundle.getBundle(localeStr='zh-CN' if lang == 'ch' else 'en')  # 'en'
qq_25193841's avatar
qq_25193841 已提交
89 90
        getStr = lambda strId: self.stringBundle.getString(strId)

HinGwenWoong's avatar
HinGwenWoong 已提交
91 92 93 94 95 96
        # KIE setting
        self.kie_mode = kie_mode
        self.key_previous_text = ""
        self.existed_key_cls_set = set()
        self.key_dialog_tip = getStr('keyDialogTip')

97 98 99 100 101 102 103 104
        self.defaultSaveDir = default_save_dir
        self.ocr = PaddleOCR(use_pdserving=False,
                             use_angle_cls=True,
                             det=True,
                             cls=True,
                             use_gpu=gpu,
                             lang=lang,
                             show_log=False)
W
new  
whj_dark 已提交
105 106 107 108 109
        self.table_ocr = PPStructure(use_pdserving=False,
                                     use_gpu=gpu,
                                     lang=lang,
                                     layout=False,
                                     show_log=False)
qq_25193841's avatar
qq_25193841 已提交
110 111 112

        if os.path.exists('./data/paddle.png'):
            result = self.ocr.ocr('./data/paddle.png', cls=True, det=True)
W
new  
whj_dark 已提交
113
            result = self.table_ocr('./data/paddle.png', return_ocr_result_in_table=True)
qq_25193841's avatar
qq_25193841 已提交
114 115 116 117 118 119 120 121

        # For loading all image under a directory
        self.mImgList = []
        self.mImgList5 = []
        self.dirname = None
        self.labelHist = []
        self.lastOpenDir = None
        self.result_dic = []
R
redearly123/PaddleOCR 已提交
122
        self.result_dic_locked = []
qq_25193841's avatar
qq_25193841 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
        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
H
HinGwenWoong 已提交
137
        self.loadPredefinedClasses(default_predefined_class_file)
qq_25193841's avatar
qq_25193841 已提交
138 139 140 141 142 143 144 145 146 147

        # 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')
qq_25193841's avatar
qq_25193841 已提交
148
        self.noLabelText = getStr('nullLabel')
qq_25193841's avatar
qq_25193841 已提交
149 150
        self.model = 'paddle'
        self.PPreader = None
151
        self.autoSaveNum = 5
qq_25193841's avatar
qq_25193841 已提交
152

153
        #  ================== File List  ==================
HinGwenWoong's avatar
HinGwenWoong 已提交
154 155 156 157

        filelistLayout = QVBoxLayout()
        filelistLayout.setContentsMargins(0, 0, 0, 0)

qq_25193841's avatar
qq_25193841 已提交
158 159 160 161
        self.fileListWidget = QListWidget()
        self.fileListWidget.itemClicked.connect(self.fileitemDoubleClicked)
        self.fileListWidget.setIconSize(QSize(25, 25))
        filelistLayout.addWidget(self.fileListWidget)
162

qq_25193841's avatar
qq_25193841 已提交
163 164
        fileListContainer = QWidget()
        fileListContainer.setLayout(filelistLayout)
165
        self.fileListName = getStr('fileList')
H
HinGwenWoong 已提交
166 167 168 169
        self.fileDock = QDockWidget(self.fileListName, self)
        self.fileDock.setObjectName(getStr('files'))
        self.fileDock.setWidget(fileListContainer)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.fileDock)
170

HinGwenWoong's avatar
HinGwenWoong 已提交
171 172
        #  ================== Key List  ==================
        if self.kie_mode:
173
            self.keyList = UniqueLabelQListWidget()
174 175 176 177 178 179 180

            # set key list height
            key_list_height = int(QApplication.desktop().height() // 4)
            if key_list_height < 50:
                key_list_height = 50
            self.keyList.setMaximumHeight(key_list_height)

HinGwenWoong's avatar
HinGwenWoong 已提交
181 182 183 184 185 186
            self.keyListDockName = getStr('keyListTitle')
            self.keyListDock = QDockWidget(self.keyListDockName, self)
            self.keyListDock.setWidget(self.keyList)
            self.keyListDock.setFeatures(QDockWidget.NoDockWidgetFeatures)
            filelistLayout.addWidget(self.keyListDock)

187 188 189 190 191 192 193 194 195 196
        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)

197
        #  ================== Right Area  ==================
qq_25193841's avatar
qq_25193841 已提交
198 199 200
        listLayout = QVBoxLayout()
        listLayout.setContentsMargins(0, 0, 0, 0)

HinGwenWoong's avatar
HinGwenWoong 已提交
201
        # Buttons
qq_25193841's avatar
qq_25193841 已提交
202 203 204 205 206
        self.editButton = QToolButton()
        self.reRecogButton = QToolButton()
        self.reRecogButton.setIcon(newIcon('reRec', 30))
        self.reRecogButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

W
new  
whj_dark 已提交
207 208 209
        self.tableRecButton = QToolButton()
        self.tableRecButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

qq_25193841's avatar
qq_25193841 已提交
210 211
        self.newButton = QToolButton()
        self.newButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
W
new  
whj_dark 已提交
212 213 214
        self.createpolyButton = QToolButton()
        self.createpolyButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

qq_25193841's avatar
qq_25193841 已提交
215 216 217 218 219
        self.SaveButton = QToolButton()
        self.SaveButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
        self.DelButton = QToolButton()
        self.DelButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

W
new  
whj_dark 已提交
220 221 222
        leftTopToolBox = QGridLayout()
        leftTopToolBox.addWidget(self.newButton, 0, 0, 1, 1)
        leftTopToolBox.addWidget(self.createpolyButton, 0, 1, 1, 1)
W
new  
whj_dark 已提交
223 224 225
        leftTopToolBox.addWidget(self.reRecogButton, 1, 0, 1, 1)
        leftTopToolBox.addWidget(self.tableRecButton, 1, 1, 1, 1)

HinGwenWoong's avatar
HinGwenWoong 已提交
226 227 228
        leftTopToolBoxContainer = QWidget()
        leftTopToolBoxContainer.setLayout(leftTopToolBox)
        listLayout.addWidget(leftTopToolBoxContainer)
qq_25193841's avatar
qq_25193841 已提交
229

230
        #  ================== Label List  ==================
qq_25193841's avatar
qq_25193841 已提交
231
        # Create and add a widget for showing current label items
qq_25193841's avatar
qq_25193841 已提交
232
        self.labelList = EditInList()
qq_25193841's avatar
qq_25193841 已提交
233 234 235
        labelListContainer = QWidget()
        labelListContainer.setLayout(listLayout)
        self.labelList.itemSelectionChanged.connect(self.labelSelectionChanged)
qq_25193841's avatar
qq_25193841 已提交
236
        self.labelList.clicked.connect(self.labelList.item_clicked)
237

qq_25193841's avatar
qq_25193841 已提交
238 239
        # Connect to itemChanged to detect checkbox changes.
        self.labelList.itemChanged.connect(self.labelItemChanged)
240 241
        self.labelListDockName = getStr('recognitionResult')
        self.labelListDock = QDockWidget(self.labelListDockName, self)
qq_25193841's avatar
qq_25193841 已提交
242 243 244 245
        self.labelListDock.setWidget(self.labelList)
        self.labelListDock.setFeatures(QDockWidget.NoDockWidgetFeatures)
        listLayout.addWidget(self.labelListDock)

W
whjdark 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259
        # enable labelList drag_drop to adjust bbox order
        # 设置选择模式为单选  
        self.labelList.setSelectionMode(QAbstractItemView.SingleSelection)
        # 启用拖拽
        self.labelList.setDragEnabled(True)
        # 设置接受拖放
        self.labelList.viewport().setAcceptDrops(True)
        # 设置显示将要被放置的位置
        self.labelList.setDropIndicatorShown(True)
        # 设置拖放模式为移动项目,如果不设置,默认为复制项目
        self.labelList.setDragDropMode(QAbstractItemView.InternalMove) 
        # 触发放置
        self.labelList.model().rowsMoved.connect(self.drag_drop_happened)

260
        #  ================== Detection Box  ==================
qq_25193841's avatar
qq_25193841 已提交
261 262
        self.BoxList = QListWidget()

263
        # self.BoxList.itemActivated.connect(self.boxSelectionChanged)
qq_25193841's avatar
qq_25193841 已提交
264 265 266 267
        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)
268 269
        self.BoxListDockName = getStr('detectionBoxposition')
        self.BoxListDock = QDockWidget(self.BoxListDockName, self)
qq_25193841's avatar
qq_25193841 已提交
270 271 272 273
        self.BoxListDock.setWidget(self.BoxList)
        self.BoxListDock.setFeatures(QDockWidget.NoDockWidgetFeatures)
        listLayout.addWidget(self.BoxListDock)

274
        #  ================== Lower Right Area  ==================
qq_25193841's avatar
qq_25193841 已提交
275 276 277 278 279 280 281 282 283 284 285
        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)

286
        #  ================== Zoom Bar  ==================
287 288 289 290 291 292 293 294
        self.imageSlider = QSlider(Qt.Horizontal)
        self.imageSlider.valueChanged.connect(self.CanvasSizeChange)
        self.imageSlider.setMinimum(-9)
        self.imageSlider.setMaximum(510)
        self.imageSlider.setSingleStep(1)
        self.imageSlider.setTickPosition(QSlider.TicksBelow)
        self.imageSlider.setTickInterval(1)

qq_25193841's avatar
qq_25193841 已提交
295 296
        op = QGraphicsOpacityEffect()
        op.setOpacity(0.2)
297 298 299 300 301 302 303 304 305
        self.imageSlider.setGraphicsEffect(op)

        self.imageSlider.setStyleSheet("background-color:transparent")
        self.imageSliderDock = QDockWidget(getStr('ImageResize'), self)
        self.imageSliderDock.setObjectName(getStr('IR'))
        self.imageSliderDock.setWidget(self.imageSlider)
        self.imageSliderDock.setFeatures(QDockWidget.DockWidgetFloatable)
        self.imageSliderDock.setAttribute(Qt.WA_TranslucentBackground)
        self.addDockWidget(Qt.RightDockWidgetArea, self.imageSliderDock)
qq_25193841's avatar
qq_25193841 已提交
306 307 308 309

        self.zoomWidget = ZoomWidget()
        self.colorDialog = ColorDialog(parent=self)
        self.zoomWidgetValue = self.zoomWidget.value()
310 311 312

        self.msgBox = QMessageBox()

313
        #  ================== Thumbnail ==================
qq_25193841's avatar
qq_25193841 已提交
314 315 316 317 318
        hlayout = QHBoxLayout()
        m = (0, 0, 0, 0)
        hlayout.setSpacing(0)
        hlayout.setContentsMargins(*m)
        self.preButton = QToolButton()
319
        self.preButton.setIcon(newIcon("prev", 40))
qq_25193841's avatar
qq_25193841 已提交
320 321 322
        self.preButton.setIconSize(QSize(40, 100))
        self.preButton.clicked.connect(self.openPrevImg)
        self.preButton.setStyleSheet('border: none;')
323
        self.preButton.setShortcut('a')
qq_25193841's avatar
qq_25193841 已提交
324 325 326 327 328
        self.iconlist = QListWidget()
        self.iconlist.setViewMode(QListView.IconMode)
        self.iconlist.setFlow(QListView.TopToBottom)
        self.iconlist.setSpacing(10)
        self.iconlist.setIconSize(QSize(50, 50))
329
        self.iconlist.setMovement(QListView.Static)
qq_25193841's avatar
qq_25193841 已提交
330 331
        self.iconlist.setResizeMode(QListView.Adjust)
        self.iconlist.itemClicked.connect(self.iconitemDoubleClicked)
332
        self.iconlist.setStyleSheet("QListWidget{ background-color:transparent; border: none;}")
qq_25193841's avatar
qq_25193841 已提交
333 334 335 336 337 338
        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)
339
        self.nextButton.setShortcut('d')
340

qq_25193841's avatar
qq_25193841 已提交
341 342 343 344 345 346 347
        hlayout.addWidget(self.preButton)
        hlayout.addWidget(self.iconlist)
        hlayout.addWidget(self.nextButton)

        iconListContainer = QWidget()
        iconListContainer.setLayout(hlayout)
        iconListContainer.setFixedHeight(100)
348

349
        #  ================== Canvas ==================
qq_25193841's avatar
qq_25193841 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363
        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)

qq_25193841's avatar
qq_25193841 已提交
364
        self.canvas.newShape.connect(partial(self.newShape, False))
qq_25193841's avatar
qq_25193841 已提交
365 366 367 368 369 370 371
        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)
372 373 374
        centerLayout.addWidget(iconListContainer, 0, Qt.AlignCenter)
        centerContainer = QWidget()
        centerContainer.setLayout(centerLayout)
qq_25193841's avatar
qq_25193841 已提交
375

376 377
        self.setCentralWidget(centerContainer)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock)
qq_25193841's avatar
qq_25193841 已提交
378

379
        self.dock.setFeatures(QDockWidget.DockWidgetClosable | QDockWidget.DockWidgetFloatable)
H
HinGwenWoong 已提交
380
        self.fileDock.setFeatures(QDockWidget.NoDockWidgetFeatures)
qq_25193841's avatar
qq_25193841 已提交
381

382
        #  ================== Actions ==================
qq_25193841's avatar
qq_25193841 已提交
383 384 385 386 387 388 389
        action = partial(newAction, self)
        quit = action(getStr('quit'), self.close,
                      'Ctrl+Q', 'quit', getStr('quitApp'))

        opendir = action(getStr('openDir'), self.openDirDialog,
                         'Ctrl+u', 'open', getStr('openDir'))

390
        open_dataset_dir = action(getStr('openDatasetDir'), self.openDatasetDirDialog,
391
                                  'Ctrl+p', 'open', getStr('openDatasetDir'), enabled=False)
392

qq_25193841's avatar
qq_25193841 已提交
393
        save = action(getStr('save'), self.saveFile,
qq_25193841's avatar
qq_25193841 已提交
394
                      'Ctrl+V', 'verify', getStr('saveDetail'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
395 396

        alcm = action(getStr('choosemodel'), self.autolcm,
397
                      'Ctrl+M', 'next', getStr('tipchoosemodel'))
qq_25193841's avatar
qq_25193841 已提交
398

399
        deleteImg = action(getStr('deleteImg'), self.deleteImg, 'Ctrl+Shift+D', 'close', getStr('deleteImgDetail'),
qq_25193841's avatar
qq_25193841 已提交
400 401 402 403
                           enabled=True)

        resetAll = action(getStr('resetAll'), self.resetAll, None, 'resetall', getStr('resetAllDetail'))

404
        color1 = action(getStr('boxLineColor'), self.chooseColor,
qq_25193841's avatar
qq_25193841 已提交
405 406 407 408 409 410 411 412
                        '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,
413
                        'w', 'objects', getStr('crtBoxDetail'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
414 415

        delete = action(getStr('delBox'), self.deleteSelectedShape,
qq_25193841's avatar
qq_25193841 已提交
416
                        'backspace', 'delete', getStr('delBoxDetail'), enabled=False)
417

qq_25193841's avatar
qq_25193841 已提交
418
        copy = action(getStr('dupBox'), self.copySelectedShape,
qq_25193841's avatar
qq_25193841 已提交
419
                      'Ctrl+C', 'copy', getStr('dupBoxDetail'),
qq_25193841's avatar
qq_25193841 已提交
420 421 422 423 424 425 426 427 428 429 430 431
                      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'))
赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
432
        showKeys = action(getStr('keys'), self.showKeysDialog, None, 'help', getStr('keys'))
qq_25193841's avatar
qq_25193841 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

        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,
        }

465 466
        #  ================== New Actions ==================

qq_25193841's avatar
qq_25193841 已提交
467
        edit = action(getStr('editLabel'), self.editLabel,
468
                      'Ctrl+E', 'edit', getStr('editLabelDetail'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
469 470

        AutoRec = action(getStr('autoRecognition'), self.autoRecognition,
471
                         '', 'Auto', getStr('autoRecognition'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
472

473
        reRec = action(getStr('reRecognition'), self.reRecognition,
474
                       'Ctrl+Shift+R', 'reRec', getStr('reRecognition'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
475

476 477 478
        singleRere = action(getStr('singleRe'), self.singleRerecognition,
                            'Ctrl+R', 'reRec', getStr('singleRe'), enabled=False)

qq_25193841's avatar
qq_25193841 已提交
479
        createpoly = action(getStr('creatPolygon'), self.createPolygon,
W
new  
whj_dark 已提交
480 481 482 483 484 485 486
                            'q', 'new', getStr('creatPolygon'), enabled=False)
        
        tableRec = action(getStr('TableRecognition'), self.TableRecognition,
                        '', 'Auto', getStr('TableRecognition'), enabled=False)

        cellreRec = action(getStr('cellreRecognition'), self.cellreRecognition,
                        '', 'reRec', getStr('cellreRecognition'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
487 488

        saveRec = action(getStr('saveRec'), self.saveRecResult,
489
                         '', 'save', getStr('saveRec'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
490

491 492
        saveLabel = action(getStr('saveLabel'), self.saveLabelFile,  #
                           'Ctrl+S', 'save', getStr('saveLabel'), enabled=False)
W
new  
whj_dark 已提交
493 494 495
        
        exportJSON = action(getStr('exportJSON'), self.exportJSON,
                            '', 'save', getStr('exportJSON'), enabled=False)
qq_25193841's avatar
qq_25193841 已提交
496

497
        undoLastPoint = action(getStr("undoLastPoint"), self.canvas.undoLastPoint,
498
                               'Ctrl+Z', "undo", getStr("undoLastPoint"), enabled=False)
499

500 501
        rotateLeft = action(getStr("rotateLeft"), partial(self.rotateImgAction, 1),
                            'Ctrl+Alt+L', "rotateLeft", getStr("rotateLeft"), enabled=False)
502

503 504
        rotateRight = action(getStr("rotateRight"), partial(self.rotateImgAction, -1),
                             'Ctrl+Alt+R', "rotateRight", getStr("rotateRight"), enabled=False)
505

506
        undo = action(getStr("undo"), self.undoShapeEdit,
507
                      'Ctrl+Z', "undo", getStr("undo"), enabled=False)
508

HinGwenWoong's avatar
HinGwenWoong 已提交
509
        change_cls = action(getStr("keyChange"), self.change_box_key,
510
                            'Ctrl+X', "edit", getStr("keyChange"), enabled=False)
HinGwenWoong's avatar
HinGwenWoong 已提交
511

R
redearly123/PaddleOCR 已提交
512
        lock = action(getStr("lockBox"), self.lockSelectedShape,
513
                      None, "lock", getStr("lockBoxDetail"), enabled=False)
514

qq_25193841's avatar
qq_25193841 已提交
515 516
        self.editButton.setDefaultAction(edit)
        self.newButton.setDefaultAction(create)
W
new  
whj_dark 已提交
517
        self.createpolyButton.setDefaultAction(createpoly)
qq_25193841's avatar
qq_25193841 已提交
518 519 520 521
        self.DelButton.setDefaultAction(deleteImg)
        self.SaveButton.setDefaultAction(save)
        self.AutoRecognition.setDefaultAction(AutoRec)
        self.reRecogButton.setDefaultAction(reRec)
W
new  
whj_dark 已提交
522
        self.tableRecButton.setDefaultAction(tableRec)
qq_25193841's avatar
qq_25193841 已提交
523 524 525
        # self.preButton.setDefaultAction(openPrevImg)
        # self.nextButton.setDefaultAction(openNextImg)

526
        #  ================== Zoom layout ==================
qq_25193841's avatar
qq_25193841 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
        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)

        # Label list context menu.
        labelMenu = QMenu()
        addActions(labelMenu, (edit, delete))

        self.labelList.setContextMenuPolicy(Qt.CustomContextMenu)
558
        self.labelList.customContextMenuRequested.connect(self.popLabelListMenu)
qq_25193841's avatar
qq_25193841 已提交
559 560 561 562 563 564 565 566

        # Draw squares/rectangles
        self.drawSquaresOption = QAction(getStr('drawSquares'), self)
        self.drawSquaresOption.setCheckable(True)
        self.drawSquaresOption.setChecked(settings.get(SETTING_DRAW_SQUARE, False))
        self.drawSquaresOption.triggered.connect(self.toogleDrawSquare)

        # Store actions for further handling.
567
        self.actions = struct(save=save, resetAll=resetAll, deleteImg=deleteImg,
W
new  
whj_dark 已提交
568 569
                              lineColor=color1, create=create, createpoly=createpoly, tableRec=tableRec, delete=delete, edit=edit, copy=copy,
                              saveRec=saveRec, singleRere=singleRere, AutoRec=AutoRec, reRec=reRec, cellreRec=cellreRec,
qq_25193841's avatar
qq_25193841 已提交
570 571 572 573
                              createMode=createMode, editMode=editMode,
                              shapeLineColor=shapeLineColor, shapeFillColor=shapeFillColor,
                              zoom=zoom, zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg,
                              fitWindow=fitWindow, fitWidth=fitWidth,
HinGwenWoong's avatar
HinGwenWoong 已提交
574
                              zoomActions=zoomActions, saveLabel=saveLabel, change_cls=change_cls,
575
                              undo=undo, undoLastPoint=undoLastPoint, open_dataset_dir=open_dataset_dir,
W
new  
whj_dark 已提交
576 577
                              rotateLeft=rotateLeft, rotateRight=rotateRight, lock=lock, exportJSON=exportJSON,
                              fileMenuActions=(opendir, open_dataset_dir, saveLabel, exportJSON, resetAll, quit),
qq_25193841's avatar
qq_25193841 已提交
578
                              beginner=(), advanced=(),
W
new  
whj_dark 已提交
579
                              editMenu=(createpoly, edit, copy, delete, singleRere, cellreRec, None, undo, undoLastPoint,
580 581
                                        None, rotateLeft, rotateRight, None, color1, self.drawSquaresOption, lock,
                                        None, change_cls),
HinGwenWoong's avatar
HinGwenWoong 已提交
582
                              beginnerContext=(
W
new  
whj_dark 已提交
583
                                  create, createpoly, edit, copy, delete, singleRere, cellreRec, rotateLeft, rotateRight, lock, change_cls),
qq_25193841's avatar
qq_25193841 已提交
584 585
                              advancedContext=(createMode, editMode, edit, copy,
                                               delete, shapeLineColor, shapeFillColor),
W
new  
whj_dark 已提交
586
                              onLoadActive=(create, createpoly, createMode, editMode),
qq_25193841's avatar
qq_25193841 已提交
587 588 589 590
                              onShapesPresent=(hideAll, showAll))

        # menus
        self.menus = struct(
591 592 593
            file=self.menu('&' + getStr('mfile')),
            edit=self.menu('&' + getStr('medit')),
            view=self.menu('&' + getStr('mview')),
qq_25193841's avatar
qq_25193841 已提交
594
            autolabel=self.menu('&PaddleOCR'),
595
            help=self.menu('&' + getStr('mhelp')),
qq_25193841's avatar
qq_25193841 已提交
596 597 598 599 600 601 602 603 604 605 606
            recentFiles=QMenu('Open &Recent'),
            labelList=labelMenu)

        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)

W
whjdark 已提交
607 608 609 610 611 612
        # Add option to enable/disable box index being displayed at the top of bounding boxes
        self.displayIndexOption = QAction(getStr('displayIndex'), self)
        self.displayIndexOption.setCheckable(True)
        self.displayIndexOption.setChecked(settings.get(SETTING_PAINT_INDEX, False))
        self.displayIndexOption.triggered.connect(self.togglePaintIndexOption)

qq_25193841's avatar
qq_25193841 已提交
613 614 615 616
        self.labelDialogOption = QAction(getStr('labelDialogOption'), self)
        self.labelDialogOption.setShortcut("Ctrl+Shift+L")
        self.labelDialogOption.setCheckable(True)
        self.labelDialogOption.setChecked(settings.get(SETTING_PAINT_LABEL, False))
W
whjdark 已提交
617
        self.displayIndexOption.setChecked(settings.get(SETTING_PAINT_INDEX, False))
qq_25193841's avatar
qq_25193841 已提交
618 619
        self.labelDialogOption.triggered.connect(self.speedChoose)

620 621 622
        self.autoSaveOption = QAction(getStr('autoSaveMode'), self)
        self.autoSaveOption.setCheckable(True)
        self.autoSaveOption.setChecked(settings.get(SETTING_PAINT_LABEL, False))
W
whjdark 已提交
623
        self.displayIndexOption.setChecked(settings.get(SETTING_PAINT_INDEX, False))
624 625
        self.autoSaveOption.triggered.connect(self.autoSaveFunc)

qq_25193841's avatar
qq_25193841 已提交
626
        addActions(self.menus.file,
W
new  
whj_dark 已提交
627
                   (opendir, open_dataset_dir, None, saveLabel, saveRec, exportJSON, self.autoSaveOption, None, resetAll, deleteImg,
628
                    quit))
qq_25193841's avatar
qq_25193841 已提交
629

630
        addActions(self.menus.help, (showKeys, showSteps, showInfo))
qq_25193841's avatar
qq_25193841 已提交
631
        addActions(self.menus.view, (
W
whjdark 已提交
632
            self.displayLabelOption, self.displayIndexOption, self.labelDialogOption,
633
            None,
qq_25193841's avatar
qq_25193841 已提交
634 635 636 637
            hideAll, showAll, None,
            zoomIn, zoomOut, zoomOrg, None,
            fitWindow, fitWidth))

W
new  
whj_dark 已提交
638
        addActions(self.menus.autolabel, (AutoRec, reRec, cellreRec, alcm, None, help))
qq_25193841's avatar
qq_25193841 已提交
639 640 641 642 643

        self.menus.file.aboutToShow.connect(self.updateFileMenu)

        # Custom context menu for the canvas widget:
        addActions(self.canvas.menus[0], self.actions.beginnerContext)
644

qq_25193841's avatar
qq_25193841 已提交
645 646 647 648 649
        self.statusBar().showMessage('%s started.' % __appname__)
        self.statusBar().show()

        # Application state.
        self.image = QImage()
650
        self.filePath = ustr(default_filename)
qq_25193841's avatar
qq_25193841 已提交
651 652 653 654 655 656 657 658 659 660
        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

661
        # Fix the compatible issue for qt4 and qt5. Convert the QStringList to python list
qq_25193841's avatar
qq_25193841 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
        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 ""))

HinGwenWoong's avatar
HinGwenWoong 已提交
700 701
        self.keyDialog = None

qq_25193841's avatar
qq_25193841 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714
        # 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)

715 716 717 718 719 720
    def menu(self, title, actions=None):
        menu = self.menuBar().addMenu(title)
        if actions:
            addActions(menu, actions)
        return menu

qq_25193841's avatar
qq_25193841 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    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)
W
new  
whj_dark 已提交
748
        self.actions.createpoly.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825

    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)

赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
826 827 828 829
    def showKeysDialog(self):
        msg = keysInfo(self.lang)
        QMessageBox.information(self, u'Information', msg)

qq_25193841's avatar
qq_25193841 已提交
830 831 832 833
    def createShape(self):
        assert self.beginner()
        self.canvas.setEditing(False)
        self.actions.create.setEnabled(False)
W
new  
whj_dark 已提交
834
        self.actions.createpoly.setEnabled(False)
qq_25193841's avatar
qq_25193841 已提交
835 836 837 838 839 840 841
        self.canvas.fourpoint = False

    def createPolygon(self):
        assert self.beginner()
        self.canvas.setEditing(False)
        self.canvas.fourpoint = True
        self.actions.create.setEnabled(False)
W
new  
whj_dark 已提交
842
        self.actions.createpoly.setEnabled(False)
843
        self.actions.undoLastPoint.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
844

845 846 847 848 849 850 851 852 853 854
    def rotateImg(self, filename, k, _value):
        self.actions.rotateRight.setEnabled(_value)
        pix = cv2.imread(filename)
        pix = np.rot90(pix, k)
        cv2.imwrite(filename, pix)
        self.canvas.update()
        self.loadFile(filename)

    def rotateImgWarn(self):
        if self.lang == 'ch':
855
            self.msgBox.warning(self, "提示", "\n 该图片已经有标注框,旋转操作会打乱标注,建议清除标注框后旋转。")
856
        else:
857 858 859
            self.msgBox.warning(self, "Warn", "\n The picture already has a label box, "
                                              "and rotation will disrupt the label. "
                                              "It is recommended to clear the label box and rotate it.")
860

861
    def rotateImgAction(self, k=1, _value=False):
862 863 864 865

        filename = self.mImgList[self.currIndex]

        if os.path.exists(filename):
866 867 868
            if self.itemsToShapesbox:
                self.rotateImgWarn()
            else:
869 870 871
                self.saveFile()
                self.dirty = False
                self.rotateImg(filename=filename, k=k, _value=True)
872
        else:
873
            self.rotateImgWarn()
874
            self.actions.rotateRight.setEnabled(False)
875
            self.actions.rotateLeft.setEnabled(False)
876

qq_25193841's avatar
qq_25193841 已提交
877 878 879 880 881 882 883 884 885
    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)
W
new  
whj_dark 已提交
886
            self.actions.createpoly.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

    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()

935
    # =================== detection box related functions ===================
qq_25193841's avatar
qq_25193841 已提交
936 937 938 939 940
    def boxItemChanged(self, item):
        shape = self.itemsToShapesbox[item]

        box = ast.literal_eval(item.text())
        # print('shape in labelItemChanged is',shape.points)
941
        if box != [(int(p.x()), int(p.y())) for p in shape.points]:
qq_25193841's avatar
qq_25193841 已提交
942 943 944 945 946 947 948
            # 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
949
            self.canvas.setShapeVisible(shape, True)  # item.checkState() == Qt.Checked
qq_25193841's avatar
qq_25193841 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

    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):
赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
984
        self.canvas.selectedShapes_hShape = []
赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
985
        if self.canvas.hShape != None:
赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
986
            self.canvas.selectedShapes_hShape = self.canvas.selectedShapes + [self.canvas.hShape]
赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
987
        else:
赛佬的小迷弟's avatar
赛佬的小迷弟 已提交
988 989
            self.canvas.selectedShapes_hShape = self.canvas.selectedShapes
        for shape in self.canvas.selectedShapes_hShape:
W
whjdark 已提交
990 991 992 993
            if shape in self.shapesToItemsbox.keys():
                item = self.shapesToItemsbox[shape]  # listitem
                text = [(int(p.x()), int(p.y())) for p in shape.points]
                item.setText(str(text))
994
        self.actions.undo.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
995 996 997 998 999
        self.setDirty()

    def indexTo5Files(self, currIndex):
        if currIndex < 2:
            return self.mImgList[:5]
1000
        elif currIndex > len(self.mImgList) - 3:
qq_25193841's avatar
qq_25193841 已提交
1001 1002
            return self.mImgList[-5:]
        else:
1003
            return self.mImgList[currIndex - 2: currIndex + 3]
qq_25193841's avatar
qq_25193841 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

    # 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):
1023 1024
        if len(self.mImgList) > 0 and self.imageSlider.hasFocus():
            self.zoomWidget.setValue(self.imageSlider.value())
qq_25193841's avatar
qq_25193841 已提交
1025

1026 1027
    def shapeSelectionChanged(self, selected_shapes):
        self._noSelectionSlot = True
1028
        for shape in self.canvas.selectedShapes:
1029 1030
            shape.selected = False
        self.labelList.clearSelection()
1031
        self.canvas.selectedShapes = selected_shapes
1032 1033 1034
        for shape in self.canvas.selectedShapes:
            shape.selected = True
            self.shapesToItems[shape].setSelected(True)
1035 1036
            self.shapesToItemsbox[shape].setSelected(True)

1037
        self.labelList.scrollToItem(self.currentItem())  # QAbstractItemView.EnsureVisible
1038
        self.BoxList.scrollToItem(self.currentBox())
1039 1040 1041 1042 1043

        if self.kie_mode:
            if len(self.canvas.selectedShapes) == 1 and self.keyList.count() > 0:
                selected_key_item_row = self.keyList.findItemsByLabel(self.canvas.selectedShapes[0].key_cls,
                                                                      get_row=True)
H
HinGwenWoong 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052
                if isinstance(selected_key_item_row, list) and len(selected_key_item_row) == 0:
                    key_text = self.canvas.selectedShapes[0].key_cls
                    item = self.keyList.createItemFromLabel(key_text)
                    self.keyList.addItem(item)
                    rgb = self._get_rgb_by_label(key_text, self.kie_mode)
                    self.keyList.setItemLabel(item, key_text, rgb)
                    selected_key_item_row = self.keyList.findItemsByLabel(self.canvas.selectedShapes[0].key_cls,
                                                                          get_row=True)

1053
                self.keyList.setCurrentRow(selected_key_item_row)
1054 1055 1056

        self._noSelectionSlot = False
        n_selected = len(selected_shapes)
1057
        self.actions.singleRere.setEnabled(n_selected)
W
new  
whj_dark 已提交
1058
        self.actions.cellreRec.setEnabled(n_selected)
1059 1060 1061
        self.actions.delete.setEnabled(n_selected)
        self.actions.copy.setEnabled(n_selected)
        self.actions.edit.setEnabled(n_selected == 1)
R
redearly123/PaddleOCR 已提交
1062
        self.actions.lock.setEnabled(n_selected)
HinGwenWoong's avatar
HinGwenWoong 已提交
1063
        self.actions.change_cls.setEnabled(n_selected)
qq_25193841's avatar
qq_25193841 已提交
1064 1065 1066

    def addLabel(self, shape):
        shape.paintLabel = self.displayLabelOption.isChecked()
W
whjdark 已提交
1067 1068
        shape.paintIdx = self.displayIndexOption.isChecked()

qq_25193841's avatar
qq_25193841 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
        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()

1088 1089 1090 1091
        # update show counting
        self.BoxListDock.setWindowTitle(self.BoxListDockName + f" ({self.BoxList.count()})")
        self.labelListDock.setWindowTitle(self.labelListDockName + f" ({self.labelList.count()})")

1092 1093
    def remLabels(self, shapes):
        if shapes is None:
qq_25193841's avatar
qq_25193841 已提交
1094 1095
            # print('rm empty label')
            return
1096 1097 1098 1099 1100 1101
        for shape in shapes:
            item = self.shapesToItems[shape]
            self.labelList.takeItem(self.labelList.row(item))
            del self.shapesToItems[shape]
            del self.itemsToShapes[item]
            self.updateComboBox()
qq_25193841's avatar
qq_25193841 已提交
1102

1103 1104 1105 1106 1107 1108
            # ADD:
            item = self.shapesToItemsbox[shape]
            self.BoxList.takeItem(self.BoxList.row(item))
            del self.shapesToItemsbox[shape]
            del self.itemsToShapesbox[item]
            self.updateComboBox()
qq_25193841's avatar
qq_25193841 已提交
1109 1110 1111

    def loadLabels(self, shapes):
        s = []
W
whjdark 已提交
1112
        shape_index = 0
1113
        for label, points, line_color, key_cls, difficult in shapes:
HinGwenWoong's avatar
HinGwenWoong 已提交
1114
            shape = Shape(label=label, line_color=line_color, key_cls=key_cls)
qq_25193841's avatar
qq_25193841 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123
            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
W
whjdark 已提交
1124 1125
            shape.idx = shape_index
            shape_index += 1
1126
            # shape.locked = False
qq_25193841's avatar
qq_25193841 已提交
1127 1128 1129
            shape.close()
            s.append(shape)

HinGwenWoong's avatar
HinGwenWoong 已提交
1130
            self._update_shape_color(shape)
qq_25193841's avatar
qq_25193841 已提交
1131
            self.addLabel(shape)
1132

qq_25193841's avatar
qq_25193841 已提交
1133 1134 1135
        self.updateComboBox()
        self.canvas.loadShapes(s)

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
    def singleLabel(self, shape):
        if shape is None:
            # print('rm empty label')
            return
        item = self.shapesToItems[shape]
        item.setText(shape.label)
        self.updateComboBox()

        # ADD:
        item = self.shapesToItemsbox[shape]
        item.setText(str([(int(p.x()), int(p.y())) for p in shape.points]))
        self.updateComboBox()

qq_25193841's avatar
qq_25193841 已提交
1149
    def updateComboBox(self):
qq_25193841's avatar
qq_25193841 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
        # 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)

        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(),
qq_25193841's avatar
qq_25193841 已提交
1169
                        points=[(int(p.x()), int(p.y())) for p in s.points],  # QPonitF
1170 1171
                        difficult=s.difficult,
                        key_cls=s.key_cls)  # bool
qq_25193841's avatar
qq_25193841 已提交
1172

1173 1174 1175 1176
        if mode == 'Auto':
            shapes = []
        else:
            shapes = [format_shape(shape) for shape in self.canvas.shapes if shape.line_color != DEFAULT_LOCK_COLOR]
qq_25193841's avatar
qq_25193841 已提交
1177
        # Can add differrent annotation formats here
1178
        for box in self.result_dic:
1179 1180
            trans_dic = {"label": box[1][0], "points": box[0], "difficult": False}
            if self.kie_mode:
1181 1182 1183 1184
                if len(box) == 3:
                    trans_dic.update({"key_cls": box[2]})
                else:
                    trans_dic.update({"key_cls": "None"})
1185
            if trans_dic["label"] == "" and mode == 'Auto':
qq_25193841's avatar
qq_25193841 已提交
1186 1187 1188 1189 1190 1191
                continue
            shapes.append(trans_dic)

        try:
            trans_dic = []
            for box in shapes:
1192 1193 1194 1195
                trans_dict = {"transcription": box['label'], "points": box['points'], "difficult": box['difficult']}
                if self.kie_mode:
                    trans_dict.update({"key_cls": box['key_cls']})
                trans_dic.append(trans_dict)
qq_25193841's avatar
qq_25193841 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204
            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
1205
        except:
qq_25193841's avatar
qq_25193841 已提交
1206
            self.errorMessage(u'Error saving label data', u'Error saving label data')
qq_25193841's avatar
qq_25193841 已提交
1207 1208 1209
            return False

    def copySelectedShape(self):
1210 1211
        for shape in self.canvas.copySelectedShape():
            self.addLabel(shape)
qq_25193841's avatar
qq_25193841 已提交
1212
        # fix copy and delete
1213
        # self.shapeSelectionChanged(True)
1214

qq_25193841's avatar
qq_25193841 已提交
1215
    def labelSelectionChanged(self):
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        if self._noSelectionSlot:
            return
        if self.canvas.editing():
            selected_shapes = []
            for item in self.labelList.selectedItems():
                selected_shapes.append(self.itemsToShapes[item])
            if selected_shapes:
                self.canvas.selectShapes(selected_shapes)
            else:
                self.canvas.deSelectShape()

qq_25193841's avatar
qq_25193841 已提交
1227
    def boxSelectionChanged(self):
1228
        if self._noSelectionSlot:
1229
            # self.BoxList.scrollToItem(self.currentBox(), QAbstractItemView.PositionAtCenter)
1230 1231 1232
            return
        if self.canvas.editing():
            selected_shapes = []
1233
            for item in self.BoxList.selectedItems():
1234 1235 1236 1237 1238 1239
                selected_shapes.append(self.itemsToShapesbox[item])
            if selected_shapes:
                self.canvas.selectShapes(selected_shapes)
            else:
                self.canvas.deSelectShape()

qq_25193841's avatar
qq_25193841 已提交
1240
    def labelItemChanged(self, item):
W
whjdark 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
        # avoid accidentally triggering the itemChanged siganl with unhashable item
        # Unknown trigger condition
        if type(item) == HashableQListWidgetItem:
            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)
        else:
            print('enter labelItemChanged slot with unhashable item: ', item, item.text())
    
    def drag_drop_happened(self):
        '''
        label list drag drop signal slot
        '''
        # print('___________________drag_drop_happened_______________')
        # should only select single item
        for item in self.labelList.selectedItems():
            newIndex = self.labelList.indexFromItem(item).row()

        # only support drag_drop one item
        assert len(self.canvas.selectedShapes) > 0
        for shape in self.canvas.selectedShapes:
            selectedShapeIndex = shape.idx
        
        if newIndex == selectedShapeIndex:
            return

        # move corresponding item in shape list
        shape = self.canvas.shapes.pop(selectedShapeIndex)
        self.canvas.shapes.insert(newIndex, shape)
            
        # update bbox index
        self.canvas.updateShapeIndex()

        # boxList update simultaneously
        item = self.BoxList.takeItem(selectedShapeIndex)
        self.BoxList.insertItem(newIndex, item)

        # changes happen
        self.setDirty()
qq_25193841's avatar
qq_25193841 已提交
1289 1290

    # Callback functions:
qq_25193841's avatar
qq_25193841 已提交
1291
    def newShape(self, value=True):
qq_25193841's avatar
qq_25193841 已提交
1292 1293 1294 1295 1296
        """Pop-up and give focus to the label editor.

        position MUST be in global coordinates.
        """
        if len(self.labelHist) > 0:
1297
            self.labelDialog = LabelDialog(parent=self, listItem=self.labelHist)
qq_25193841's avatar
qq_25193841 已提交
1298

qq_25193841's avatar
qq_25193841 已提交
1299
        if value:
qq_25193841's avatar
qq_25193841 已提交
1300 1301
            text = self.labelDialog.popUp(text=self.prevLabelText)
            self.lastLabel = text
qq_25193841's avatar
qq_25193841 已提交
1302 1303
        else:
            text = self.prevLabelText
qq_25193841's avatar
qq_25193841 已提交
1304 1305 1306

        if text is not None:
            self.prevLabelText = self.stringBundle.getString('tempLabel')
1307

1308
            shape = self.canvas.setLastLabel(text, None, None, None)  # generate_color, generate_color
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
            if self.kie_mode:
                key_text, _ = self.keyDialog.popUp(self.key_previous_text)
                if key_text is not None:
                    shape = self.canvas.setLastLabel(text, None, None, key_text)  # generate_color, generate_color
                    self.key_previous_text = key_text
                    if not self.keyList.findItemsByLabel(key_text):
                        item = self.keyList.createItemFromLabel(key_text)
                        self.keyList.addItem(item)
                        rgb = self._get_rgb_by_label(key_text, self.kie_mode)
                        self.keyList.setItemLabel(item, key_text, rgb)
1319 1320

                    self._update_shape_color(shape)
1321 1322
                    self.keyDialog.addLabelHistory(key_text)

qq_25193841's avatar
qq_25193841 已提交
1323 1324 1325 1326
            self.addLabel(shape)
            if self.beginner():  # Switch to edit mode.
                self.canvas.setEditing(True)
                self.actions.create.setEnabled(True)
W
new  
whj_dark 已提交
1327
                self.actions.createpoly.setEnabled(True)
1328 1329
                self.actions.undoLastPoint.setEnabled(False)
                self.actions.undo.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
1330 1331 1332 1333 1334 1335 1336 1337
            else:
                self.actions.editMode.setEnabled(True)
            self.setDirty()

        else:
            # self.canvas.undoLastLine()
            self.canvas.resetAllLines()

1338
    def _update_shape_color(self, shape):
1339
        r, g, b = self._get_rgb_by_label(shape.key_cls, self.kie_mode)
HinGwenWoong's avatar
HinGwenWoong 已提交
1340 1341 1342 1343 1344 1345
        shape.line_color = QColor(r, g, b)
        shape.vertex_fill_color = QColor(r, g, b)
        shape.hvertex_fill_color = QColor(255, 255, 255)
        shape.fill_color = QColor(r, g, b, 128)
        shape.select_line_color = QColor(255, 255, 255)
        shape.select_fill_color = QColor(r, g, b, 155)
1346 1347

    def _get_rgb_by_label(self, label, kie_mode):
1348 1349
        shift_auto_shape_color = 2  # use for random color
        if kie_mode and label != "None":
1350 1351 1352 1353 1354 1355
            item = self.keyList.findItemsByLabel(label)[0]
            label_id = self.keyList.indexFromItem(item).row() + 1
            label_id += shift_auto_shape_color
            return LABEL_COLORMAP[label_id % len(LABEL_COLORMAP)]
        else:
            return (0, 255, 0)
1356

qq_25193841's avatar
qq_25193841 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
    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)
1370
        self.imageSlider.setValue(self.zoomWidget.value() + increment)  # set zoom slider value
qq_25193841's avatar
qq_25193841 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441

    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."""
qq_25193841's avatar
qq_25193841 已提交
1442 1443
        if self.dirty:
            self.mayContinue()
qq_25193841's avatar
qq_25193841 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
        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
1456

qq_25193841's avatar
qq_25193841 已提交
1457 1458 1459 1460 1461 1462 1463 1464
        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)
1465

qq_25193841's avatar
qq_25193841 已提交
1466 1467 1468 1469 1470 1471 1472
                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)
1473
                        break
qq_25193841's avatar
qq_25193841 已提交
1474 1475 1476 1477 1478 1479 1480
            else:
                self.fileListWidget.clear()
                self.mImgList.clear()
                self.iconlist.clear()

        # if unicodeFilePath and self.iconList.count() > 0:
        #     if unicodeFilePath in self.mImgList:
1481

qq_25193841's avatar
qq_25193841 已提交
1482
        if unicodeFilePath and os.path.exists(unicodeFilePath):
1483
            self.canvas.verified = False
1484 1485 1486 1487 1488
            cvimg = cv2.imdecode(np.fromfile(unicodeFilePath, dtype=np.uint8), 1)
            height, width, depth = cvimg.shape
            cvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2RGB)
            image = QImage(cvimg.data, width, height, width * depth, QImage.Format_RGB888)

qq_25193841's avatar
qq_25193841 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497
            if image.isNull():
                self.errorMessage(u'Error opening file',
                                  u"<p>Make sure <i>%s</i> 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))
1498

qq_25193841's avatar
qq_25193841 已提交
1499 1500 1501 1502 1503
            if self.validFilestate(filePath) is True:
                self.setClean()
            else:
                self.dirty = False
                self.actions.save.setEnabled(True)
R
redearly123/PaddleOCR 已提交
1504 1505
            if len(self.canvas.lockedShapes) != 0:
                self.actions.save.setEnabled(True)
1506
                self.setDirty()
qq_25193841's avatar
qq_25193841 已提交
1507 1508 1509 1510 1511
            self.canvas.setEnabled(True)
            self.adjustScale(initial=True)
            self.paintCanvas()
            self.addRecentFile(self.filePath)
            self.toggleActions(True)
R
redearly123/PaddleOCR 已提交
1512

qq_25193841's avatar
qq_25193841 已提交
1513
            self.showBoundingBoxFromPPlabel(filePath)
1514

qq_25193841's avatar
qq_25193841 已提交
1515
            self.setWindowTitle(__appname__ + ' ' + filePath)
1516

qq_25193841's avatar
qq_25193841 已提交
1517 1518 1519 1520 1521
            # 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)

1522 1523 1524
            # show file list image count
            select_indexes = self.fileListWidget.selectedIndexes()
            if len(select_indexes) > 0:
H
HinGwenWoong 已提交
1525
                self.fileDock.setWindowTitle(self.fileListName + f" ({select_indexes[0].row() + 1}"
1526
                                                                 f"/{self.fileListWidget.count()})")
1527 1528 1529
            # update show counting
            self.BoxListDock.setWindowTitle(self.BoxListDockName + f" ({self.BoxList.count()})")
            self.labelListDock.setWindowTitle(self.labelListDockName + f" ({self.labelList.count()})")
1530

qq_25193841's avatar
qq_25193841 已提交
1531 1532 1533 1534 1535
            self.canvas.setFocus(True)
            return True
        return False

    def showBoundingBoxFromPPlabel(self, filePath):
R
redearly123/PaddleOCR 已提交
1536
        width, height = self.image.width(), self.image.height()
qq_25193841's avatar
qq_25193841 已提交
1537
        imgidx = self.getImglabelidx(filePath)
1538 1539
        shapes = []
        # box['ratio'] of the shapes saved in lockedShapes contains the ratio of the
R
redearly123/PaddleOCR 已提交
1540 1541
        # four corner coordinates of the shapes to the height and width of the image
        for box in self.canvas.lockedShapes:
qq_25193841's avatar
qq_25193841 已提交
1542
            key_cls = 'None' if not self.kie_mode else box['key_cls']
R
redearly123/PaddleOCR 已提交
1543
            if self.canvas.isInTheSameImage:
1544
                shapes.append((box['transcription'], [[s[0] * width, s[1] * height] for s in box['ratio']],
1545
                               DEFAULT_LOCK_COLOR, key_cls, box['difficult']))
R
redearly123/PaddleOCR 已提交
1546
            else:
1547
                shapes.append(('锁定框:待检测', [[s[0] * width, s[1] * height] for s in box['ratio']],
1548
                               DEFAULT_LOCK_COLOR, key_cls, box['difficult']))
R
redearly123/PaddleOCR 已提交
1549 1550
        if imgidx in self.PPlabel.keys():
            for box in self.PPlabel[imgidx]:
qq_25193841's avatar
qq_25193841 已提交
1551
                key_cls = 'None' if not self.kie_mode else box.get('key_cls', 'None')
1552
                shapes.append((box['transcription'], box['points'], None, key_cls, box.get('difficult', False)))
1553

qq_25193841's avatar
qq_25193841 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
        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() \
1567
                and self.zoomMode != self.MANUAL_ZOOM:
qq_25193841's avatar
qq_25193841 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
            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))
1580
        self.imageSlider.setValue(self.zoomWidget.value())  # set zoom slider value
qq_25193841's avatar
qq_25193841 已提交
1581 1582 1583 1584 1585

    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
1586
        h1 = self.centralWidget().height() - e - 110
qq_25193841's avatar
qq_25193841 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
        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
1604
            # If it loads images from dir, don't load it at the beginning
qq_25193841's avatar
qq_25193841 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
            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_PAINT_LABEL] = self.displayLabelOption.isChecked()
W
whjdark 已提交
1628
            settings[SETTING_PAINT_INDEX] = self.displayIndexOption.isChecked()
qq_25193841's avatar
qq_25193841 已提交
1629 1630 1631
            settings[SETTING_DRAW_SQUARE] = self.drawSquaresOption.isChecked()
            settings.save()
            try:
qq_25193841's avatar
qq_25193841 已提交
1632
                self.saveLabelFile()
qq_25193841's avatar
qq_25193841 已提交
1633 1634 1635 1636 1637
            except:
                pass

    def loadRecent(self, filename):
        if self.mayContinue():
1638
            print(filename, "======")
qq_25193841's avatar
qq_25193841 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
            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,
1664 1665 1666
                                                                  '%s - Open Directory' % __appname__,
                                                                  defaultOpenDirPath,
                                                                  QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks))
qq_25193841's avatar
qq_25193841 已提交
1667 1668 1669 1670 1671
        else:
            targetDirPath = ustr(defaultOpenDirPath)
        self.lastOpenDir = targetDirPath
        self.importDirImages(targetDirPath)

1672
    def openDatasetDirDialog(self):
1673
        if self.lastOpenDir and os.path.exists(self.lastOpenDir):
1674 1675 1676 1677
            if platform.system() == 'Windows':
                os.startfile(self.lastOpenDir)
            else:
                os.system('open ' + os.path.normpath(self.lastOpenDir))
1678
            defaultOpenDirPath = self.lastOpenDir
1679

1680 1681 1682 1683
        else:
            if self.lang == 'ch':
                self.msgBox.warning(self, "提示", "\n 原文件夹已不存在,请从新选择数据集路径!")
            else:
1684 1685
                self.msgBox.warning(self, "Warn",
                                    "\n The original folder no longer exists, please choose the data set path again!")
1686 1687 1688

            self.actions.open_dataset_dir.setEnabled(False)
            defaultOpenDirPath = os.path.dirname(self.filePath) if self.filePath else '.'
1689

1690 1691 1692 1693 1694 1695 1696
    def init_key_list(self, label_dict):
        if not self.kie_mode:
            return
        # load key_cls
        for image, info in label_dict.items():
            for box in info:
                if "key_cls" not in box:
1697
                    box.update({"key_cls": "None"})
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
                self.existed_key_cls_set.add(box["key_cls"])
        if len(self.existed_key_cls_set) > 0:
            for key_text in self.existed_key_cls_set:
                if not self.keyList.findItemsByLabel(key_text):
                    item = self.keyList.createItemFromLabel(key_text)
                    self.keyList.addItem(item)
                    rgb = self._get_rgb_by_label(key_text, self.kie_mode)
                    self.keyList.setItemLabel(item, key_text, rgb)

        if self.keyDialog is None:
            # key list dialog
            self.keyDialog = KeyDialog(
                text=self.key_dialog_tip,
                parent=self,
                labels=self.existed_key_cls_set,
                sort_labels=True,
                show_text_field=True,
                completion="startswith",
                fit_to_content={'column': True, 'row': False},
                flags=None
            )

1720
    def importDirImages(self, dirpath, isDelete=False):
qq_25193841's avatar
qq_25193841 已提交
1721 1722 1723
        if not self.mayContinue() or not dirpath:
            return
        if self.defaultSaveDir and self.defaultSaveDir != dirpath:
qq_25193841's avatar
qq_25193841 已提交
1724
            self.saveLabelFile()
qq_25193841's avatar
qq_25193841 已提交
1725 1726 1727

        if not isDelete:
            self.loadFilestate(dirpath)
1728
            self.PPlabelpath = dirpath + '/Label.txt'
qq_25193841's avatar
qq_25193841 已提交
1729 1730 1731 1732 1733
            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)
HinGwenWoong's avatar
HinGwenWoong 已提交
1734

1735
            self.init_key_list(self.PPlabel)
HinGwenWoong's avatar
HinGwenWoong 已提交
1736

qq_25193841's avatar
qq_25193841 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
        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)

1760
        print('DirPath in importDirImages is', dirpath)
qq_25193841's avatar
qq_25193841 已提交
1761 1762 1763 1764 1765 1766
        self.iconlist.clear()
        self.additems5(dirpath)
        self.changeFileFolder = True
        self.haveAutoReced = False
        self.AutoRecognition.setEnabled(True)
        self.reRecogButton.setEnabled(True)
W
new  
whj_dark 已提交
1767
        self.tableRecButton.setEnabled(True)
1768 1769
        self.actions.AutoRec.setEnabled(True)
        self.actions.reRec.setEnabled(True)
W
new  
whj_dark 已提交
1770
        self.actions.tableRec.setEnabled(True)
1771
        self.actions.open_dataset_dir.setEnabled(True)
1772
        self.actions.rotateLeft.setEnabled(True)
1773
        self.actions.rotateRight.setEnabled(True)
1774

1775
        self.fileListWidget.setCurrentRow(0)  # set list index to first
H
HinGwenWoong 已提交
1776
        self.fileDock.setWindowTitle(self.fileListName + f" (1/{self.fileListWidget.count()})")  # show image count
qq_25193841's avatar
qq_25193841 已提交
1777 1778 1779 1780 1781 1782 1783

    def openPrevImg(self, _value=False):
        if len(self.mImgList) <= 0:
            return

        if self.filePath is None:
            return
1784

qq_25193841's avatar
qq_25193841 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
        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:
1812
            print('file name in openNext is ', filename)
qq_25193841's avatar
qq_25193841 已提交
1813
            self.loadFile(filename)
1814

qq_25193841's avatar
qq_25193841 已提交
1815 1816 1817 1818 1819
    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
1820 1821 1822
        if self.filePath:
            imgidx = self.getImglabelidx(self.filePath)
            self._saveFile(imgidx, mode=mode)
qq_25193841's avatar
qq_25193841 已提交
1823

R
redearly123/PaddleOCR 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
    def saveLockedShapes(self):
        self.canvas.lockedShapes = []
        self.canvas.selectedShapes = []
        for s in self.canvas.shapes:
            if s.line_color == DEFAULT_LOCK_COLOR:
                self.canvas.selectedShapes.append(s)
        self.lockSelectedShape()
        for s in self.canvas.shapes:
            if s.line_color == DEFAULT_LOCK_COLOR:
                self.canvas.selectedShapes.remove(s)
                self.canvas.shapes.remove(s)
qq_25193841's avatar
qq_25193841 已提交
1835 1836

    def _saveFile(self, annotationFilePath, mode='Manual'):
R
redearly123/PaddleOCR 已提交
1837 1838 1839
        if len(self.canvas.lockedShapes) != 0:
            self.saveLockedShapes()

qq_25193841's avatar
qq_25193841 已提交
1840
        if mode == 'Manual':
1841 1842 1843 1844
            self.result_dic_locked = []
            img = cv2.imread(self.filePath)
            width, height = self.image.width(), self.image.height()
            for shape in self.canvas.lockedShapes:
1845
                box = [[int(p[0] * width), int(p[1] * height)] for p in shape['ratio']]
E
Evezerest 已提交
1846
                # assert len(box) == 4
1847
                result = [(shape['transcription'], 1)]
1848 1849
                result.insert(0, box)
                self.result_dic_locked.append(result)
R
redearly123/PaddleOCR 已提交
1850 1851
            self.result_dic += self.result_dic_locked
            self.result_dic_locked = []
qq_25193841's avatar
qq_25193841 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860
            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
1861
                if len(self.fileStatedict) % self.autoSaveNum == 0:
qq_25193841's avatar
qq_25193841 已提交
1862 1863 1864 1865
                    self.saveFilestate()
                    self.savePPlabel(mode='Auto')

                self.fileListWidget.insertItem(int(currIndex), item)
R
redearly123/PaddleOCR 已提交
1866 1867
                if not self.canvas.isInTheSameImage:
                    self.openNextImg()
qq_25193841's avatar
qq_25193841 已提交
1868
                self.actions.saveRec.setEnabled(True)
1869
                self.actions.saveLabel.setEnabled(True)
W
new  
whj_dark 已提交
1870
                self.actions.exportJSON.setEnabled(True) 
qq_25193841's avatar
qq_25193841 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894

        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,
1895 1896
                                           shellcon.FOF_SILENT | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION,
                                           None, None))
qq_25193841's avatar
qq_25193841 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
                    # 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):  #
1930
        if not self.dirty:
qq_25193841's avatar
qq_25193841 已提交
1931 1932 1933 1934 1935 1936
            return True
        else:
            discardChanges = self.discardChangesDialog()
            if discardChanges == QMessageBox.No:
                return True
            elif discardChanges == QMessageBox.Yes:
R
redearly123/PaddleOCR 已提交
1937
                self.canvas.isInTheSameImage = True
qq_25193841's avatar
qq_25193841 已提交
1938
                self.saveFile()
R
redearly123/PaddleOCR 已提交
1939
                self.canvas.isInTheSameImage = False
qq_25193841's avatar
qq_25193841 已提交
1940 1941 1942 1943 1944 1945
                return True
            else:
                return False

    def discardChangesDialog(self):
        yes, no, cancel = QMessageBox.Yes, QMessageBox.No, QMessageBox.Cancel
1946 1947 1948 1949
        if self.lang == 'ch':
            msg = u'您有未保存的变更, 您想保存再继续吗?\n点击 "No" 丢弃所有未保存的变更.'
        else:
            msg = u'You have unsaved changes, would you like to save them and proceed?\nClick "No" to undo all changes.'
qq_25193841's avatar
qq_25193841 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958
        return QMessageBox.warning(self, u'Attention', msg, yes | no | cancel)

    def errorMessage(self, title, message):
        return QMessageBox.critical(self, title,
                                    '<p><b>%s</b></p>%s' % (title, message))

    def currentPath(self):
        return os.path.dirname(self.filePath) if self.filePath else '.'

1959
    def chooseColor(self):
qq_25193841's avatar
qq_25193841 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
        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):
1970 1971
        self.remLabels(self.canvas.deleteSelected())
        self.actions.undo.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
1972 1973 1974 1975
        self.setDirty()
        if self.noShapes():
            for action in self.actions.onShapesPresent:
                action.setEnabled(False)
1976 1977
        self.BoxListDock.setWindowTitle(self.BoxListDockName + f" ({self.BoxList.count()})")
        self.labelListDock.setWindowTitle(self.labelListDockName + f" ({self.labelList.count()})")
qq_25193841's avatar
qq_25193841 已提交
1978 1979 1980 1981 1982

    def chshapeLineColor(self):
        color = self.colorDialog.getColor(self.lineColor, u'Choose line color',
                                          default=DEFAULT_LINE_COLOR)
        if color:
1983
            for shape in self.canvas.selectedShapes: shape.line_color = color
qq_25193841's avatar
qq_25193841 已提交
1984 1985 1986 1987 1988 1989 1990
            self.canvas.update()
            self.setDirty()

    def chshapeFillColor(self):
        color = self.colorDialog.getColor(self.fillColor, u'Choose fill color',
                                          default=DEFAULT_FILL_COLOR)
        if color:
1991
            for shape in self.canvas.selectedShapes: shape.fill_color = color
qq_25193841's avatar
qq_25193841 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
            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):
W
whjdark 已提交
2015 2016 2017 2018 2019 2020 2021
        self.displayIndexOption.setChecked(False)
        for shape in self.canvas.shapes:
            shape.paintLabel = self.displayLabelOption.isChecked()
            shape.paintIdx = self.displayIndexOption.isChecked()

    def togglePaintIndexOption(self):
        self.displayLabelOption.setChecked(False)
qq_25193841's avatar
qq_25193841 已提交
2022 2023
        for shape in self.canvas.shapes:
            shape.paintLabel = self.displayLabelOption.isChecked()
W
whjdark 已提交
2024
            shape.paintIdx = self.displayIndexOption.isChecked()
qq_25193841's avatar
qq_25193841 已提交
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049

    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])
2050
            item = QListWidgetItem(QIcon(pix.scaled(100, 100, Qt.IgnoreAspectRatio, Qt.FastTransformation)), pfilename)
qq_25193841's avatar
qq_25193841 已提交
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
            # 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)

qq_25193841's avatar
qq_25193841 已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
    def gen_quad_from_poly(self, poly):
        """
        Generate min area quad from poly.
        """
        point_num = poly.shape[0]
        min_area_quad = np.zeros((4, 2), dtype=np.float32)
        rect = cv2.minAreaRect(poly.astype(
            np.int32))  # (center (x,y), (width, height), angle of rotation)
        box = np.array(cv2.boxPoints(rect))

        first_point_idx = 0
        min_dist = 1e4
        for i in range(4):
            dist = np.linalg.norm(box[(i + 0) % 4] - poly[0]) + \
                   np.linalg.norm(box[(i + 1) % 4] - poly[point_num // 2 - 1]) + \
                   np.linalg.norm(box[(i + 2) % 4] - poly[point_num // 2]) + \
                   np.linalg.norm(box[(i + 3) % 4] - poly[-1])
            if dist < min_dist:
                min_dist = dist
                first_point_idx = i
        for i in range(4):
            min_area_quad[i] = box[(first_point_idx + i) % 4]

        bbox_new = min_area_quad.tolist()
        bbox = []

        for box in bbox_new:
            box = list(map(int, box))
            bbox.append(box)

        return bbox

qq_25193841's avatar
qq_25193841 已提交
2093
    def getImglabelidx(self, filePath):
2094
        if platform.system() == 'Windows':
qq_25193841's avatar
qq_25193841 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
            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()
2108
        self.currIndex = len(self.mImgList) - 1
2109
        self.loadFile(self.filePath)  # ADD
qq_25193841's avatar
qq_25193841 已提交
2110 2111
        self.haveAutoReced = True
        self.AutoRecognition.setEnabled(False)
2112
        self.actions.AutoRec.setEnabled(False)
qq_25193841's avatar
qq_25193841 已提交
2113 2114 2115
        self.setDirty()
        self.saveCacheLabel()

2116 2117
        self.init_key_list(self.Cachelabel)

qq_25193841's avatar
qq_25193841 已提交
2118 2119 2120 2121 2122
    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 = []
R
redearly123/PaddleOCR 已提交
2123
            self.result_dic_locked = []  # result_dic_locked stores the ocr result of self.canvas.lockedShapes
qq_25193841's avatar
qq_25193841 已提交
2124 2125 2126
            rec_flag = 0
            for shape in self.canvas.shapes:
                box = [[int(p.x()), int(p.y())] for p in shape.points]
2127
                kie_cls = shape.key_cls
qq_25193841's avatar
qq_25193841 已提交
2128 2129 2130

                if len(box) > 4:
                    box = self.gen_quad_from_poly(np.array(box))
qq_25193841's avatar
qq_25193841 已提交
2131
                assert len(box) == 4
qq_25193841's avatar
qq_25193841 已提交
2132

qq_25193841's avatar
qq_25193841 已提交
2133 2134 2135 2136 2137 2138
                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)
2139
                if result[0][0] != '':
R
redearly123/PaddleOCR 已提交
2140 2141 2142
                    if shape.line_color == DEFAULT_LOCK_COLOR:
                        shape.label = result[0][0]
                        result.insert(0, box)
2143 2144
                        if self.kie_mode:
                            result.append(kie_cls)
R
redearly123/PaddleOCR 已提交
2145 2146 2147
                        self.result_dic_locked.append(result)
                    else:
                        result.insert(0, box)
2148 2149
                        if self.kie_mode:
                            result.append(kie_cls)
R
redearly123/PaddleOCR 已提交
2150
                        self.result_dic.append(result)
qq_25193841's avatar
qq_25193841 已提交
2151 2152
                else:
                    print('Can not recognise the box')
R
redearly123/PaddleOCR 已提交
2153 2154
                    if shape.line_color == DEFAULT_LOCK_COLOR:
                        shape.label = result[0][0]
2155 2156 2157 2158
                        if self.kie_mode:
                            self.result_dic_locked.append([box, (self.noLabelText, 0), kie_cls])
                        else:
                            self.result_dic_locked.append([box, (self.noLabelText, 0)])
R
redearly123/PaddleOCR 已提交
2159
                    else:
2160 2161 2162 2163
                        if self.kie_mode:
                            self.result_dic.append([box, (self.noLabelText, 0), kie_cls])
                        else:
                            self.result_dic.append([box, (self.noLabelText, 0)])
R
redearly123/PaddleOCR 已提交
2164 2165 2166 2167 2168 2169
                try:
                    if self.noLabelText == shape.label or result[1][0] == shape.label:
                        print('label no change')
                    else:
                        rec_flag += 1
                except IndexError as e:
2170 2171 2172
                    print('Can not recognise the box')
            if (len(self.result_dic) > 0 and rec_flag > 0) or self.canvas.lockedShapes:
                self.canvas.isInTheSameImage = True
qq_25193841's avatar
qq_25193841 已提交
2173 2174
                self.saveFile(mode='Auto')
                self.loadFile(self.filePath)
R
redearly123/PaddleOCR 已提交
2175
                self.canvas.isInTheSameImage = False
qq_25193841's avatar
qq_25193841 已提交
2176 2177
                self.setDirty()
            elif len(self.result_dic) == len(self.canvas.shapes) and rec_flag == 0:
2178 2179 2180 2181
                if self.lang == 'ch':
                    QMessageBox.information(self, "Information", "识别结果保持一致!")
                else:
                    QMessageBox.information(self, "Information", "The recognition result remains unchanged!")
qq_25193841's avatar
qq_25193841 已提交
2182 2183 2184 2185 2186
            else:
                print('Can not recgonise in ', self.filePath)
        else:
            QMessageBox.information(self, "Information", "Draw a box!")

2187 2188
    def singleRerecognition(self):
        img = cv2.imread(self.filePath)
2189 2190
        for shape in self.canvas.selectedShapes:
            box = [[int(p.x()), int(p.y())] for p in shape.points]
qq_25193841's avatar
qq_25193841 已提交
2191 2192
            if len(box) > 4:
                box = self.gen_quad_from_poly(np.array(box))
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
            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] != '':
                result.insert(0, box)
                print('result in reRec is ', result)
                if result[1][0] == shape.label:
                    print('label no change')
                else:
                    shape.label = result[1][0]
qq_25193841's avatar
qq_25193841 已提交
2207 2208 2209 2210 2211 2212 2213 2214
            else:
                print('Can not recognise the box')
                if self.noLabelText == shape.label:
                    print('label no change')
                else:
                    shape.label = self.noLabelText
            self.singleLabel(shape)
            self.setDirty()
qq_25193841's avatar
qq_25193841 已提交
2215

W
new  
whj_dark 已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
    def TableRecognition(self):
        '''
            Table Recegnition
        '''
        from paddleocr.ppstructure.table.predict_table import to_excel

        import time

        start = time.time()
        img = cv2.imread(self.filePath)
        res = self.table_ocr(img, return_ocr_result_in_table=True)

        TableRec_excel_dir = self.lastOpenDir + '/tableRec_excel_output/'
        os.makedirs(TableRec_excel_dir, exist_ok=True)
2230
        filename, _ = os.path.splitext(os.path.basename(self.filePath))
E
Evezerest 已提交
2231

W
new  
whj_dark 已提交
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
        excel_path = TableRec_excel_dir + '{}.xlsx'.format(filename)
        
        if res is None:
            msg = 'Can not recognise the table in ' + self.filePath + '. Please change manually'
            QMessageBox.information(self, "Information", msg)
            to_excel('', excel_path) # create an empty excel
            return
        
        # save res
        # ONLY SUPPORT ONE TABLE in one image
        hasTable = False
        for region in res:
            if region['type'] == 'Table':
                if region['res']['boxes'] is None:
                    msg = 'Can not recognise the detection box in ' + self.filePath + '. Please change manually'
                    QMessageBox.information(self, "Information", msg)
                    to_excel('', excel_path) # create an empty excel
                    return
                hasTable = True
                # save table ocr result on PPOCRLabel
                # clear all old annotaions before saving result
                self.itemsToShapes.clear()
                self.shapesToItems.clear()
                self.itemsToShapesbox.clear()  # ADD
                self.shapesToItemsbox.clear()
                self.labelList.clear()
                self.BoxList.clear()
                self.result_dic = []
                self.result_dic_locked = []

                shapes = []
                result_len = len(region['res']['boxes'])
W
whjdark 已提交
2264
                order_index = 0
W
new  
whj_dark 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
                for i in range(result_len):
                    bbox = np.array(region['res']['boxes'][i])
                    rec_text = region['res']['rec_res'][i][0]

                    # polys to rectangles
                    x1, y1 = np.min(bbox[:, 0]), np.min(bbox[:, 1])
                    x2, y2 = np.max(bbox[:, 0]), np.max(bbox[:, 1])
                    rext_bbox = [[x1, y1], [x2, y1], [x2, y2], [x1, y2]]

                    # save bbox to shape
                    shape = Shape(label=rec_text, line_color=DEFAULT_LINE_COLOR, key_cls=None)
                    for point in rext_bbox:
                        x, y = point
                        # Ensure the labels are within the bounds of the image. 
                        # If not, fix them.
                        x, y, snapped = self.canvas.snapPointToCanvas(x, y)
                        shape.addPoint(QPointF(x, y))
                    shape.difficult = False
W
whjdark 已提交
2283 2284
                    shape.idx = order_index
                    order_index += 1
W
new  
whj_dark 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
                    # shape.locked = False
                    shape.close()
                    self.addLabel(shape)
                    shapes.append(shape)
                self.setDirty()
                self.canvas.loadShapes(shapes)
                
                # save HTML result to excel
                try:
                    to_excel(region['res']['html'], excel_path)
                except:
                    print('Can not save excel file, maybe Permission denied (.xlsx is being occupied)')
                break
        
        if not hasTable:
            msg = 'Can not recognise the table in ' + self.filePath + '. Please change manually'
            QMessageBox.information(self, "Information", msg)
            to_excel('', excel_path) # create an empty excel
            return

        # automatically open excel annotation file
2306 2307 2308 2309 2310 2311
        if platform.system() == 'Windows':
            try:
                import win32com.client
            except:
                print("CANNOT OPEN .xlsx. It could be one of the following reasons: " \
                    "Only support Windows | No python win32com")
W
new  
whj_dark 已提交
2312

2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
            try:
                xl = win32com.client.Dispatch("Excel.Application")
                xl.Visible = True
                xl.Workbooks.Open(excel_path)
                # excelEx = "You need to show the excel executable at this point"
                # subprocess.Popen([excelEx, excel_path])

                # os.startfile(excel_path)
            except:
                print("CANNOT OPEN .xlsx. It could be the following reasons: " \
                    ".xlsx is not existed")
        else:
            os.system('open ' + os.path.normpath(excel_path))
W
new  
whj_dark 已提交
2326 2327 2328 2329 2330 2331 2332 2333
                
        print('time cost: ', time.time() - start)

    def cellreRecognition(self):
        '''
            re-recognise text in a cell
        '''
        img = cv2.imread(self.filePath)
W
new  
whj_dark 已提交
2334 2335
        for shape in self.canvas.selectedShapes:
            box = [[int(p.x()), int(p.y())] for p in shape.points]
W
new  
whj_dark 已提交
2336

W
new  
whj_dark 已提交
2337 2338 2339
            if len(box) > 4:
                box = self.gen_quad_from_poly(np.array(box))
            assert len(box) == 4
W
new  
whj_dark 已提交
2340

W
new  
whj_dark 已提交
2341 2342 2343 2344 2345 2346 2347
            # pad around bbox for better text recognition accuracy
            _box = boxPad(box, img.shape, 6)
            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
W
new  
whj_dark 已提交
2348

W
new  
whj_dark 已提交
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
            # merge the text result in the cell
            texts = ''
            probs = 0. # the probability of the cell is avgerage prob of every text box in the cell
            bboxes = self.ocr.ocr(img_crop, det=True, rec=False, cls=False)
            if len(bboxes) > 0:
                bboxes.reverse() # top row text at first
                for _bbox in bboxes:
                    patch = get_rotate_crop_image(img_crop, np.array(_bbox, np.float32))
                    rec_res = self.ocr.ocr(patch, det=False, rec=True, cls=False)
                    text = rec_res[0][0]
                    if text != '':
qq_25193841's avatar
qq_25193841 已提交
2360
                        texts += text + ('' if text[0].isalpha() else ' ') # add space between english word
W
new  
whj_dark 已提交
2361 2362 2363
                        probs += rec_res[0][1]
                probs = probs / len(bboxes)
            result = [(texts.strip(), probs)]
W
new  
whj_dark 已提交
2364

W
new  
whj_dark 已提交
2365 2366 2367 2368 2369
            if result[0][0] != '':
                result.insert(0, box)
                print('result in reRec is ', result)
                if result[1][0] == shape.label:
                    print('label no change')
W
new  
whj_dark 已提交
2370
                else:
W
new  
whj_dark 已提交
2371
                    shape.label = result[1][0]
W
new  
whj_dark 已提交
2372
            else:
W
new  
whj_dark 已提交
2373 2374 2375 2376 2377 2378 2379
                print('Can not recognise the box')
                if self.noLabelText == shape.label:
                    print('label no change')
                else:
                    shape.label = self.noLabelText
            self.singleLabel(shape)
            self.setDirty()
W
new  
whj_dark 已提交
2380 2381 2382 2383 2384 2385

    def exportJSON(self):
        '''
            export PPLabel and CSV to JSON (PubTabNet)
        '''
        import pandas as pd
W
new  
whj_dark 已提交
2386
        from libs.dataPartitionDialog import DataPartitionDialog
W
new  
whj_dark 已提交
2387

qq_25193841's avatar
qq_25193841 已提交
2388 2389 2390 2391 2392
        # data partition user input
        partitionDialog = DataPartitionDialog(parent=self)
        partitionDialog.exec()
        if partitionDialog.getStatus() == False:
            return
W
new  
whj_dark 已提交
2393 2394

        # automatically save annotations
qq_25193841's avatar
qq_25193841 已提交
2395 2396
        self.saveFilestate()
        self.savePPlabel(mode='auto')
W
new  
whj_dark 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415

        # load box annotations
        labeldict = {}
        if not os.path.exists(self.PPlabelpath):
            msg = 'ERROR, Can not find Label.txt'
            QMessageBox.information(self, "Information", msg)
            return
        else:
            with open(self.PPlabelpath, '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] = []

W
new  
whj_dark 已提交
2416 2417
        train_split, val_split, test_split = partitionDialog.getDataPartition()
        # check validate
W
new  
whj_dark 已提交
2418
        if train_split + val_split + test_split > 100:
W
new  
whj_dark 已提交
2419 2420
            msg = "The sum of training, validation and testing data should be less than 100%"
            QMessageBox.information(self, "Information", msg)
W
new  
whj_dark 已提交
2421
            return
W
new  
whj_dark 已提交
2422
        print(train_split, val_split, test_split)
W
new  
whj_dark 已提交
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
        train_split, val_split, test_split = float(train_split) / 100., float(val_split) / 100., float(test_split) / 100.
        train_id = int(len(labeldict) * train_split)
        val_id = int(len(labeldict) * (train_split + val_split))
        print('Data partition: train:', train_id, 
              'validation:',  val_id - train_id,
              'test:', len(labeldict) - val_id)

        TableRec_excel_dir = os.path.join(self.lastOpenDir, 'tableRec_excel_output')
        json_results = []
        imgid = 0
        for image_path in labeldict.keys():
            # load csv annotations
2435
            filename, _ = os.path.splitext(os.path.basename(image_path))
W
new  
whj_dark 已提交
2436 2437
            csv_path = os.path.join(TableRec_excel_dir, filename + '.xlsx')
            if not os.path.exists(csv_path):
qq_25193841's avatar
qq_25193841 已提交
2438
                continue
W
new  
whj_dark 已提交
2439

2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
            excel = xlrd.open_workbook(csv_path)
            sheet0 = excel.sheet_by_index(0)  # only sheet 0
            merged_cells = sheet0.merged_cells # (0,1,1,3) start row, end row, start col, end col

            html_list = [['td'] * sheet0.ncols for i in range(sheet0.nrows)]

            for merged in merged_cells:
                html_list = expand_list(merged, html_list)

            token_list = convert_token(html_list)
W
new  
whj_dark 已提交
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467

            # load box annotations
            cells = []
            for anno in labeldict[image_path]:
                tokens = list(anno['transcription'])
                obb = anno['points']
                hbb = OBB2HBB(np.array(obb)).tolist()
                cells.append({'tokens': tokens, 'bbox': hbb})
            
            # data split
            if imgid < train_id:
                split = 'train'
            elif imgid < val_id:
                split = 'val'
            else:
                split = 'test'

            #  save dict
2468
            html = {'structure': {'tokens': token_list}, 'cell': cells}
qq_25193841's avatar
qq_25193841 已提交
2469
            json_results.append({'filename': os.path.basename(image_path), 'split': split, 'imgid': imgid, 'html': html})
W
new  
whj_dark 已提交
2470 2471 2472
            imgid += 1

        # save json
2473
        with open("{}/annotation.json".format(self.lastOpenDir), "w", encoding='utf-8') as fid:
qq_25193841's avatar
qq_25193841 已提交
2474
            fid.write(json.dumps(json_results, ensure_ascii=False))
W
new  
whj_dark 已提交
2475
        
W
new  
whj_dark 已提交
2476
        msg = 'JSON sucessfully saved in {}/annotation.json'.format(self.lastOpenDir)
W
new  
whj_dark 已提交
2477 2478
        QMessageBox.information(self, "Information", msg)

qq_25193841's avatar
qq_25193841 已提交
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
    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)
2509
            self.actions.AutoRec.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
2510 2511 2512 2513 2514 2515 2516 2517

    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()])
W
new  
whj_dark 已提交
2518 2519 2520 2521 2522 2523
        del self.table_ocr
        self.table_ocr = PPStructure(use_pdserving=False,
                                     use_gpu=False,
                                     lang=lg_idx[self.comboBox.currentText()],
                                     layout=False,
                                     show_log=False)
qq_25193841's avatar
qq_25193841 已提交
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
        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
2540 2541
                self.actions.saveLabel.setEnabled(True)
                self.actions.saveRec.setEnabled(True)
W
new  
whj_dark 已提交
2542
                self.actions.exportJSON.setEnabled(True)
qq_25193841's avatar
qq_25193841 已提交
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567

    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

2568
    def savePPlabel(self, mode='Manual'):
qq_25193841's avatar
qq_25193841 已提交
2569 2570 2571 2572 2573 2574 2575
        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')

2576 2577 2578 2579 2580
        if mode == 'Manual':
            if self.lang == 'ch':
                msg = '已将检查过的图片标签保存在 ' + self.PPlabelpath + " 文件中"
            else:
                msg = 'Images that have been checked are saved in ' + self.PPlabelpath
qq_25193841's avatar
qq_25193841 已提交
2581 2582 2583 2584 2585 2586 2587 2588
            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')

qq_25193841's avatar
qq_25193841 已提交
2589 2590 2591 2592
    def saveLabelFile(self):
        self.saveFilestate()
        self.savePPlabel()

qq_25193841's avatar
qq_25193841 已提交
2593
    def saveRecResult(self):
2594 2595
        if {} in [self.PPlabelpath, self.PPlabel, self.fileStatedict]:
            QMessageBox.information(self, "Information", "Check the image first")
qq_25193841's avatar
qq_25193841 已提交
2596 2597 2598 2599
            return

        rec_gt_dir = os.path.dirname(self.PPlabelpath) + '/rec_gt.txt'
        crop_img_dir = os.path.dirname(self.PPlabelpath) + '/crop_img/'
2600
        ques_img = []
qq_25193841's avatar
qq_25193841 已提交
2601 2602 2603 2604 2605 2606
        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)
2607
                try:
qq_25193841's avatar
qq_25193841 已提交
2608
                    img = cv2.imread(key)
2609
                    for i, label in enumerate(self.PPlabel[idx]):
2610 2611
                        if label['difficult']:
                            continue
2612
                        img_crop = get_rotate_crop_image(img, np.array(label['points'], np.float32))
2613 2614 2615
                        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')
2616 2617 2618
                        f.write(label['transcription'] + '\n')
                except Exception as e:
                    ques_img.append(key)
2619
                    print("Can not read image ", e)
2620
        if ques_img:
2621 2622 2623 2624 2625
            QMessageBox.information(self,
                                    "Information",
                                    "The following images can not be saved, please check the image path and labels.\n"
                                    + "".join(str(i) + '\n' for i in ques_img))
        QMessageBox.information(self, "Information", "Cropped images have been saved in " + str(crop_img_dir))
qq_25193841's avatar
qq_25193841 已提交
2626

qq_25193841's avatar
qq_25193841 已提交
2627 2628 2629 2630 2631 2632 2633 2634 2635
    def speedChoose(self):
        if self.labelDialogOption.isChecked():
            self.canvas.newShape.disconnect()
            self.canvas.newShape.connect(partial(self.newShape, True))

        else:
            self.canvas.newShape.disconnect()
            self.canvas.newShape.connect(partial(self.newShape, False))

2636 2637
    def autoSaveFunc(self):
        if self.autoSaveOption.isChecked():
2638
            self.autoSaveNum = 1  # Real auto_Save
2639 2640 2641 2642
            try:
                self.saveLabelFile()
            except:
                pass
2643 2644
            print('The program will automatically save once after confirming an image')
        else:
2645
            self.autoSaveNum = 5  # Used for backup
2646 2647
            print('The program will automatically save once after confirming 5 images (default)')

HinGwenWoong's avatar
HinGwenWoong 已提交
2648
    def change_box_key(self):
2649 2650
        if not self.kie_mode:
            return
HinGwenWoong's avatar
HinGwenWoong 已提交
2651 2652 2653 2654 2655 2656
        key_text, _ = self.keyDialog.popUp(self.key_previous_text)
        if key_text is None:
            return
        self.key_previous_text = key_text
        for shape in self.canvas.selectedShapes:
            shape.key_cls = key_text
2657 2658 2659 2660 2661 2662
            if not self.keyList.findItemsByLabel(key_text):
                item = self.keyList.createItemFromLabel(key_text)
                self.keyList.addItem(item)
                rgb = self._get_rgb_by_label(key_text, self.kie_mode)
                self.keyList.setItemLabel(item, key_text, rgb)

HinGwenWoong's avatar
HinGwenWoong 已提交
2663
            self._update_shape_color(shape)
2664
            self.keyDialog.addLabelHistory(key_text)
HinGwenWoong's avatar
HinGwenWoong 已提交
2665

2666 2667 2668 2669
    def undoShapeEdit(self):
        self.canvas.restoreShape()
        self.labelList.clear()
        self.BoxList.clear()
2670
        self.loadShapes(self.canvas.shapes)
2671 2672 2673 2674 2675 2676 2677 2678 2679
        self.actions.undo.setEnabled(self.canvas.isShapeRestorable)

    def loadShapes(self, shapes, replace=True):
        self._noSelectionSlot = True
        for shape in shapes:
            self.addLabel(shape)
        self.labelList.clearSelection()
        self._noSelectionSlot = False
        self.canvas.loadShapes(shapes, replace=replace)
2680 2681
        print("loadShapes")  # 1

R
redearly123/PaddleOCR 已提交
2682
    def lockSelectedShape(self):
2683
        """lock the selected shapes.
R
redearly123/PaddleOCR 已提交
2684 2685 2686 2687 2688

        Add self.selectedShapes to lock self.canvas.lockedShapes, 
        which holds the ratio of the four coordinates of the locked shapes
        to the width and height of the image
        """
R
redearly123/PaddleOCR 已提交
2689
        width, height = self.image.width(), self.image.height()
2690

R
redearly123/PaddleOCR 已提交
2691 2692 2693 2694
        def format_shape(s):
            return dict(label=s.label,  # str
                        line_color=s.line_color.getRgb(),
                        fill_color=s.fill_color.getRgb(),
2695
                        ratio=[[int(p.x()) / width, int(p.y()) / height] for p in s.points],  # QPonitF
2696 2697
                        difficult=s.difficult,  # bool
                        key_cls=s.key_cls,  # bool
2698
                        )
2699 2700

        # lock
R
redearly123/PaddleOCR 已提交
2701 2702
        if len(self.canvas.lockedShapes) == 0:
            for s in self.canvas.selectedShapes:
R
redearly123/PaddleOCR 已提交
2703 2704 2705
                s.line_color = DEFAULT_LOCK_COLOR
                s.locked = True
            shapes = [format_shape(shape) for shape in self.canvas.selectedShapes]
R
redearly123/PaddleOCR 已提交
2706 2707
            trans_dic = []
            for box in shapes:
2708 2709 2710 2711
                trans_dict = {"transcription": box['label'], "ratio": box['ratio'], "difficult": box['difficult']}
                if self.kie_mode:
                    trans_dict.update({"key_cls": box["key_cls"]})
                trans_dic.append(trans_dict)
R
redearly123/PaddleOCR 已提交
2712
            self.canvas.lockedShapes = trans_dic
R
redearly123/PaddleOCR 已提交
2713 2714
            self.actions.save.setEnabled(True)

2715
        # unlock
R
redearly123/PaddleOCR 已提交
2716 2717
        else:
            for s in self.canvas.shapes:
R
redearly123/PaddleOCR 已提交
2718 2719 2720 2721 2722
                s.line_color = DEFAULT_LINE_COLOR
            self.canvas.lockedShapes = []
            self.result_dic_locked = []
            self.setDirty()
            self.actions.save.setEnabled(True)
2723

qq_25193841's avatar
qq_25193841 已提交
2724

qq_25193841's avatar
qq_25193841 已提交
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
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

2736

qq_25193841's avatar
qq_25193841 已提交
2737 2738
def str2bool(v):
    return v.lower() in ("true", "t", "1")
qq_25193841's avatar
qq_25193841 已提交
2739

2740

qq_25193841's avatar
qq_25193841 已提交
2741 2742 2743 2744 2745 2746 2747 2748
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"))
2749
    # Tzutalin 201705+: Accept extra arguments to change predefined class file
2750
    arg_parser = argparse.ArgumentParser()
HinGwenWoong's avatar
HinGwenWoong 已提交
2751
    arg_parser.add_argument("--lang", type=str, default='en', nargs="?")
2752
    arg_parser.add_argument("--gpu", type=str2bool, default=True, nargs="?")
HinGwenWoong's avatar
HinGwenWoong 已提交
2753
    arg_parser.add_argument("--kie", type=str2bool, default=False, nargs="?")
2754 2755 2756 2757 2758 2759 2760
    arg_parser.add_argument("--predefined_classes_file",
                            default=os.path.join(os.path.dirname(__file__), "data", "predefined_classes.txt"),
                            nargs="?")
    args = arg_parser.parse_args(argv[1:])

    win = MainWindow(lang=args.lang,
                     gpu=args.gpu,
2761
                     kie_mode=args.kie,
H
HinGwenWoong 已提交
2762
                     default_predefined_class_file=args.predefined_classes_file)
qq_25193841's avatar
qq_25193841 已提交
2763 2764 2765 2766 2767
    win.show()
    return app, win


def main():
2768
    """construct main app and run it"""
qq_25193841's avatar
qq_25193841 已提交
2769 2770 2771 2772 2773
    app, _win = get_main_app(sys.argv)
    return app.exec_()


if __name__ == '__main__':
2774

qq_25193841's avatar
qq_25193841 已提交
2775 2776 2777
    resource_file = './libs/resources.py'
    if not os.path.exists(resource_file):
        output = os.system('pyrcc5 -o libs/resources.py resources.qrc')
2778
        assert output == 0, "operate the cmd have some problems ,please check  whether there is a in the lib " \
qq_25193841's avatar
qq_25193841 已提交
2779
                            "directory resources.py "
2780

qq_25193841's avatar
qq_25193841 已提交
2781
    sys.exit(main())