Update readme and delete redundancy codes

This commit is contained in:
Leif 2021-02-07 17:28:10 +08:00
parent ecf8b569d8
commit 35f6d18fab
4 changed files with 127 additions and 264 deletions

View File

@ -206,7 +206,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.labelList = EditInList()
labelListContainer = QWidget()
labelListContainer.setLayout(listLayout)
# self.labelList.itemActivated.connect(self.labelSelectionChanged)
#self.labelList.itemActivated.connect(self.labelSelectionChanged)
self.labelList.itemSelectionChanged.connect(self.labelSelectionChanged)
self.labelList.clicked.connect(self.labelList.item_clicked)
# Connect to itemChanged to detect checkbox changes.
@ -219,7 +219,7 @@ class MainWindow(QMainWindow, WindowMixin):
################## detection box ####################
self.BoxList = QListWidget()
self.BoxList.itemActivated.connect(self.boxSelectionChanged)
#self.BoxList.itemActivated.connect(self.boxSelectionChanged)
self.BoxList.itemSelectionChanged.connect(self.boxSelectionChanged)
self.BoxList.itemDoubleClicked.connect(self.editBox)
# Connect to itemChanged to detect checkbox changes.
@ -453,10 +453,10 @@ class MainWindow(QMainWindow, WindowMixin):
'Ctrl+S', 'save', getStr('saveLabel'), enabled=False)
undoLastPoint = action(getStr("undoLastPoint"), self.canvas.undoLastPoint,
'Ctrl+Z', "undo", "Undo last drawn point", enabled=False)
'Ctrl+Z', "undo", getStr("undoLastPoint"), enabled=False)
undo = action(getStr("undo"), self.undoShapeEdit,
'Ctrl+Z', "undo", "Undo last add and edit of shape", enabled=False)
'Ctrl+Z', "undo", getStr("undo"), enabled=False)
self.editButton.setDefaultAction(edit)
self.newButton.setDefaultAction(create)
@ -578,9 +578,9 @@ class MainWindow(QMainWindow, WindowMixin):
# Custom context menu for the canvas widget:
addActions(self.canvas.menus[0], self.actions.beginnerContext)
addActions(self.canvas.menus[1], (
action('&Copy here', self.copyShape),
action('&Move here', self.moveShape)))
#addActions(self.canvas.menus[1], (
# action('&Copy here', self.copyShape),
# action('&Move here', self.moveShape)))
self.statusBar().showMessage('%s started.' % __appname__)
@ -878,16 +878,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.setDirty()
self.updateComboBox()
# def updateBoxlist(self):
# shape = self.canvas.selectedShape
# item = self.shapesToItemsbox[shape] # listitem
# text = [(int(p.x()), int(p.y())) for p in shape.points]
# item.setText(str(text))
# self.setDirty()
def updateBoxlist(self):
#changedShape = self.canvas.selectedShapes
#changedShape.append(self.canvas.hShape) #changedShape: #
for shape in self.canvas.selectedShapes+[self.canvas.hShape]:
item = self.shapesToItemsbox[shape] # listitem
text = [(int(p.x()), int(p.y())) for p in shape.points]
@ -925,57 +916,24 @@ class MainWindow(QMainWindow, WindowMixin):
if len(self.mImgList) > 0:
self.zoomWidget.setValue(self.zoomWidgetValue + self.imgsplider.value())
# # TODO: UPDATE THIS FUNCTION
# # React to canvas signals.
# def shapeSelectionChanged(self, selected=False):
# if self._noSelectionSlot:
# self._noSelectionSlot = False
# else:
# shape = self.canvas.selectedShape
# if shape:
# self.shapesToItems[shape].setSelected(True)
# self.shapesToItemsbox[shape].setSelected(True) # ADD
# else:
# self.labelList.clearSelection()
# self.actions.delete.setEnabled(selected)
# self.actions.copy.setEnabled(selected)
# self.actions.edit.setEnabled(selected)
# self.actions.shapeLineColor.setEnabled(selected)
# self.actions.shapeFillColor.setEnabled(selected)
# self.actions.singleRere.setEnabled(selected)
# def shapeSelectionChanged(self, selected_shapes):
# if self._noSelectionSlot:
# self._noSelectionSlot = False
# else:
# if self.canvas.selectedShapes:
# for shape in self.canvas.selectedShapes:
# self.shapesToItems[shape].setSelected(True)
# self.shapesToItemsbox[shape].setSelected(True)
# else:
# self.labelList.clearSelection()
#
# n_selected = len(selected_shapes)
# self.actions.delete.setEnabled(n_selected)
# self.actions.copy.setEnabled(n_selected)
# self.actions.edit.setEnabled(n_selected == 1)
def shapeSelectionChanged(self, selected_shapes):
self._noSelectionSlot = True
for shape in self.canvas.selectedShapes: # 为何要反选?
for shape in self.canvas.selectedShapes:
shape.selected = False
self.labelList.clearSelection()
self.canvas.selectedShapes = selected_shapes # 这里没有把选择的两个都加入
self.canvas.selectedShapes = selected_shapes
for shape in self.canvas.selectedShapes:
shape.selected = True
# item = self.labelList.findItemByShape(shape)
# self.labelList.selectItem(item)
# self.labelList.scrollToItem(item)
self.shapesToItems[shape].setSelected(True)
self.shapesToItemsbox[shape].setSelected(True) # ADD 是否可以代替selectItem
self.shapesToItemsbox[shape].setSelected(True)
self.labelList.scrollToItem(self.currentItem()) # QAbstractItemView.EnsureVisible
self.BoxList.scrollToItem(self.currentBox())
self._noSelectionSlot = False
n_selected = len(selected_shapes)
self.actions.singleRere.setEnabled(n_selected)
self.actions.delete.setEnabled(n_selected)
self.actions.copy.setEnabled(n_selected)
self.actions.edit.setEnabled(n_selected == 1)
@ -1115,19 +1073,12 @@ class MainWindow(QMainWindow, WindowMixin):
return False
def copySelectedShape(self):
self.addLabel(self.canvas.copySelectedShape())
for shape in self.canvas.copySelectedShape():
self.addLabel(shape)
# fix copy and delete
self.shapeSelectionChanged(True)
#self.shapeSelectionChanged(True)
# def labelSelectionChanged(self):
# item = self.currentItem()
# self.labelList.scrollToItem(item, QAbstractItemView.EnsureVisible)
# if item and self.canvas.editing():
# self._noSelectionSlot = True
# self.canvas.selectShape(self.itemsToShapes[item])
# shape = self.itemsToShapes[item]
def labelSelectionChanged(self):
if self._noSelectionSlot:
return
@ -1141,20 +1092,13 @@ class MainWindow(QMainWindow, WindowMixin):
self.canvas.deSelectShape()
# def boxSelectionChanged(self):
# item = self.currentBox()
# self.BoxList.scrollToItem(item, QAbstractItemView.EnsureVisible)
# if item and self.canvas.editing():
# self._noSelectionSlot = True
# self.canvas.selectShape(self.itemsToShapesbox[item])
# shape = self.itemsToShapesbox[item]
def boxSelectionChanged(self):
if self._noSelectionSlot:
#self.BoxList.scrollToItem(self.currentBox(), QAbstractItemView.PositionAtCenter)
return
if self.canvas.editing():
selected_shapes = []
for item in self.labelList.selectedItems():
for item in self.BoxList.selectedItems():
selected_shapes.append(self.itemsToShapesbox[item])
if selected_shapes:
self.canvas.selectShapes(selected_shapes)
@ -1637,6 +1581,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.fileListWidget.insertItem(int(currIndex), item)
self.openNextImg()
self.actions.saveRec.setEnabled(True)
self.actions.saveLabel.setEnabled(True)
elif mode == 'Auto':
if annotationFilePath and self.saveLabels(annotationFilePath, mode=mode):
@ -1743,7 +1688,7 @@ class MainWindow(QMainWindow, WindowMixin):
color = self.colorDialog.getColor(self.lineColor, u'Choose line color',
default=DEFAULT_LINE_COLOR)
if color:
self.canvas.selectedShape.line_color = color
for shape in self.canvas.selectedShapes: shape.line_color = color
self.canvas.update()
self.setDirty()
@ -1751,7 +1696,7 @@ class MainWindow(QMainWindow, WindowMixin):
color = self.colorDialog.getColor(self.fillColor, u'Choose fill color',
default=DEFAULT_FILL_COLOR)
if color:
self.canvas.selectedShape.fill_color = color
for shape in self.canvas.selectedShapes: shape.fill_color = color
self.canvas.update()
self.setDirty()
@ -1875,25 +1820,25 @@ class MainWindow(QMainWindow, WindowMixin):
def singleRerecognition(self):
img = cv2.imread(self.filePath)
shape = self.canvas.selectedShape
box = [[int(p.x()), int(p.y())] for p in shape.points]
assert len(box) == 4
img_crop = get_rotate_crop_image(img, np.array(box, np.float32))
if img_crop is None:
msg = 'Can not recognise the detection box in ' + self.filePath + '. Please change manually'
QMessageBox.information(self, "Information", msg)
return
result = self.ocr.ocr(img_crop, cls=True, det=False)
if result[0][0] != '':
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]
self.singleLabel(shape)
self.setDirty()
print(box)
for shape in self.canvas.selectedShapes:
box = [[int(p.x()), int(p.y())] for p in shape.points]
assert len(box) == 4
img_crop = get_rotate_crop_image(img, np.array(box, np.float32))
if img_crop is None:
msg = 'Can not recognise the detection box in ' + self.filePath + '. Please change manually'
QMessageBox.information(self, "Information", msg)
return
result = self.ocr.ocr(img_crop, cls=True, det=False)
if result[0][0] != '':
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]
self.singleLabel(shape)
self.setDirty()
print(box)
def autolcm(self):
vbox = QVBoxLayout()
@ -2046,6 +1991,10 @@ class MainWindow(QMainWindow, WindowMixin):
def autoSaveFunc(self):
if self.autoSaveOption.isChecked():
self.autoSaveNum = 1 # Real auto_Save
try:
self.saveLabelFile()
except:
pass
print('The program will automatically save once after confirming an image')
else:
self.autoSaveNum = 5 # Used for backup
@ -2055,7 +2004,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.canvas.restoreShape()
self.labelList.clear()
self.BoxList.clear()
self.loadShapes(self.canvas.shapes) # 重新加载
self.loadShapes(self.canvas.shapes)
self.actions.undo.setEnabled(self.canvas.isShapeRestorable)
def loadShapes(self, shapes, replace=True):
@ -2089,7 +2038,7 @@ def get_main_app(argv=[]):
app.setWindowIcon(newIcon("app"))
# Tzutalin 201705+: Accept extra agruments to change predefined class file
argparser = argparse.ArgumentParser()
argparser.add_argument("--lang", default='ch', nargs="?")
argparser.add_argument("--lang", default='en', nargs="?")
argparser.add_argument("--predefined_classes_file",
default=os.path.join(os.path.dirname(__file__), "data", "predefined_classes.txt"),
nargs="?")

View File

@ -8,6 +8,10 @@ PPOCRLabel is a semi-automatic graphic annotation tool suitable for OCR field, w
### Recent Update
- 2021.2.5: New batch processing and undo functions (by [Evezerest](https://github.com/Evezerest)):
- Batch processing function: Press and hold the Ctrl key to select the box, you can move, copy, and delete in batches.
- Undo function: In the process of drawing a four-point label box or after editing the box, press Ctrl+Z to undo the previous operation.
- Fix image rotation and size problems, optimize the process of editing the mark frame (by [ninetailskim](https://github.com/ninetailskim)、 [edencfc](https://github.com/edencfc)).
- 2021.1.11: Optimize the labeling experience (by [edencfc](https://github.com/edencfc)),
- Users can choose whether to pop up the label input dialog after drawing the detection box in "View - Pop-up Label Input Dialog".
- The recognition result scrolls synchronously when users click related detection box.
@ -16,7 +20,6 @@ PPOCRLabel is a semi-automatic graphic annotation tool suitable for OCR field, w
### TODO:
- Lock box mode: For the same scene data, the size and position of the locked detection box can be transferred between different pictures.
- Experience optimization: Add undo, batch operation include move, copy, delete and so on, optimize the annotation process.
## Installation
@ -76,12 +79,11 @@ python3 PPOCRLabel.py
7. Double click the result in 'recognition result' list to manually change inaccurate recognition results.
8. Click "Check", the image status will switch to "√",then the program automatically jump to the next(The results will not be written directly to the file at this time).
8. Click "Check", the image status will switch to "√",then the program automatically jump to the next.
9. Click "Delete Image" and the image will be deleted to the recycle bin.
10. Labeling result: the user can save manually through the menu "File - Save Label", while the program will also save automatically after every 5 images confirmed by the user.the manually checked label will be stored in *Label.txt* under the opened picture folder.
Click "PaddleOCR"-"Save Recognition Results" in the menu bar, the recognition training data of such pictures will be saved in the *crop_img* folder, and the recognition label will be saved in *rec_gt.txt*<sup>[4]</sup>.
10. Labeling result: the user can save manually through the menu "File - Save Label", while the program will also save automatically if "File - Auto Save Label Mode" is selected. The manually checked label will be stored in *Label.txt* under the opened picture folder. Click "PaddleOCR"-"Save Recognition Results" in the menu bar, the recognition training data of such pictures will be saved in the *crop_img* folder, and the recognition label will be saved in *rec_gt.txt*<sup>[4]</sup>.
### Note
@ -89,8 +91,7 @@ python3 PPOCRLabel.py
[2] The image status indicates whether the user has saved the image manually. If it has not been saved manually it is "X", otherwise it is "√", PPOCRLabel will not relabel pictures with a status of "√".
[3] After clicking "Re-recognize", the model will overwrite ALL recognition results in the picture.
Therefore, if the recognition result has been manually changed before, it may change after re-recognition.
[3] After clicking "Re-recognize", the model will overwrite ALL recognition results in the picture. Therefore, if the recognition result has been manually changed before, it may change after re-recognition.
[4] The files produced by PPOCRLabel can be found under the opened picture folder including the following, please do not manually change the contents, otherwise it will cause the program to be abnormal.
@ -106,22 +107,24 @@ Therefore, if the recognition result has been manually changed before, it may ch
### Shortcut keys
| Shortcut keys | Description |
| ---------------- | ------------------------------------------------ |
| Ctrl + shift + A | Automatically label all unchecked images |
| Ctrl + shift + R | Re-recognize all the labels of the current image |
| W | Create a rect box |
| Q | Create a four-points box |
| Ctrl + E | Edit label of the selected box |
| Ctrl + R | Re-recognize the selected box |
| Backspace | Delete the selected box |
| Ctrl + V | Check image |
| Ctrl + Shift + d | Delete image |
| D | Next image |
| A | Previous image |
| Ctrl++ | Zoom in |
| Ctrl-- | Zoom out |
| ↑→↓← | Move selected box |
| Shortcut keys | Description |
| ------------------------ | ------------------------------------------------ |
| Ctrl + Shift + A | Automatically label all unchecked images |
| Ctrl + Shift + R | Re-recognize all the labels of the current image |
| W | Create a rect box |
| Q | Create a four-points box |
| Ctrl + E | Edit label of the selected box |
| Ctrl + R | Re-recognize the selected box |
| Ctrl + C | Copy and paste the selected box |
| Ctrl + Left Mouse Button | Multi select the label box |
| Backspace | Delete the selected box |
| Ctrl + V | Check image |
| Ctrl + Shift + d | Delete image |
| D | Next image |
| A | Previous image |
| Ctrl++ | Zoom in |
| Ctrl-- | Zoom out |
| ↑→↓← | Move selected box |
### Built-in Model
@ -136,7 +139,7 @@ Therefore, if the recognition result has been manually changed before, it may ch
PPOCRLabel supports three ways to save Label.txt
- Automatically save: When it detects that the user has manually checked 5 pictures, the program automatically writes the annotations into Label.txt. The user can change the value of ``self.autoSaveNum`` in ``PPOCRLabel.py`` to set the number of images to be automatically saved after confirmation.
- Automatically save: After selecting "File - Auto Save Label Mode", the program will automatically write the annotations into Label.txt every time the user confirms an image. If this option is not turned on, it will be automatically saved after detecting that the user has manually checked 5 images.
- Manual save: Click "File-Save Marking Results" to manually save the label.
- Close application save
@ -167,4 +170,4 @@ For some data that are difficult to recognize, the recognition results will not
### Related
1.[Tzutalin. LabelImg. Git code (2015)](https://github.com/tzutalin/labelImg)
1.[Tzutalin. LabelImg. Git code (2015)](https://github.com/tzutalin/labelImg)

View File

@ -8,6 +8,10 @@ PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具内置P
#### 近期更新
- 2021.2.5新增批处理与撤销功能by [Evezerest](https://github.com/Evezerest))
- 批处理功能按住Ctrl键选择标记框后可批量移动、复制、删除。
- 撤销功能在绘制四点标注框过程中或对框进行编辑操作后按下Ctrl+Z可撤销上一部操作。
- 修复图像旋转和尺寸问题、优化编辑标记框过程by [ninetailskim](https://github.com/ninetailskim)、 [edencfc](https://github.com/edencfc)
- 2021.1.11优化标注体验by [edencfc](https://github.com/edencfc)
- 用户可在“视图 - 弹出标记输入框”选择在画完检测框后标记输入框是否弹出。
- 识别结果与检测框同步滚动。
@ -17,9 +21,8 @@ PPOCRLabel是一款适用于OCR领域的半自动化图形标注工具内置P
#### 尽请期待
- 锁定框模式:针对同一场景数据,被锁定的检测框的大小与位置能在不同图片之间传递。
- 体验优化:增加撤销操作,批量移动、复制、删除等功能。优化标注流程。
如果您对以上内容感兴趣或对完善工具有不一样的想法,欢迎加入我们的队伍与我们共同开发
如果您对以上内容感兴趣或对完善工具有不一样的想法,欢迎加入我们的SIG队伍与我们共同开发。可以在[此处](https://github.com/PaddlePaddle/PaddleOCR/issues/1728)完成问卷和前置任务经过我们确认相关内容后即可正式加入享受SIG福利共同为OCR开源事业贡献特别说明针对PPOCRLabel的改进也属于PaddleOCR前置任务
## 安装
@ -65,9 +68,9 @@ python3 PPOCRLabel.py --lang ch
5. 标记框绘制完成后,用户点击 “确认”,检测框会先被预分配一个 “待识别” 标签。
6. 重新识别:将图片中的所有检测画绘制/调整完成后,点击 “重新识别”PPOCR模型会对当前图片中的**所有检测框**重新识别<sup>[3]</sup>
7. 内容更改:双击识别结果,对不准确的识别结果进行手动更改。
8. 确认标记:点击 “确认”,图片状态切换为 “√”,跳转至下一张(此时不会直接将结果写入文件)
8. **确认标记**:点击 “确认”,图片状态切换为 “√”,跳转至下一张。
9. 删除:点击 “删除图像”,图片将会被删除至回收站。
10. 保存结果:用户可以通过菜单中“文件-保存标记结果”手动保存,同时程序也会在用户每确认5张图片后自动保存一次。手动确认过的标记将会被存放在所打开图片文件夹下的*Label.txt*中。在菜单栏点击 “文件” - "保存识别结果"后,会将此类图片的识别训练数据保存在*crop_img*文件夹下,识别标签保存在*rec_gt.txt*中<sup>[4]</sup>
10. 保存结果:用户可以通过菜单中“文件-保存标记结果”手动保存,同时也可以点击“文件 - 自动保存标记结果”开启自动保存。手动确认过的标记将会被存放在所打开图片文件夹下的*Label.txt*中。在菜单栏点击 “文件” - "保存识别结果"后,会将此类图片的识别训练数据保存在*crop_img*文件夹下,识别标签保存在*rec_gt.txt*中<sup>[4]</sup>
### 注意
@ -99,6 +102,8 @@ python3 PPOCRLabel.py --lang ch
| Q | 新建四点框 |
| Ctrl + E | 编辑所选框标签 |
| Ctrl + R | 重新识别所选标记 |
| Ctrl + C | 复制并粘贴选中的标记框 |
| Ctrl + 鼠标左键 | 多选标记框 |
| Backspace | 删除所选框 |
| Ctrl + V | 确认本张图片标记 |
| Ctrl + Shift + d | 删除本张图片 |
@ -120,7 +125,7 @@ python3 PPOCRLabel.py --lang ch
PPOCRLabel支持三种保存方式
- 程序自动保存当检测到用户手动确认过5张图片后程序自动将标记结果写入Label.txt中。其中用户可通过更改```PPOCRLabel.py```中的```self.autoSaveNum```的数值设置确认几张图片后进行自动保存。
- 自动保存:点击“文件 - 自动保存标记结果”后用户每确认过一张图片程序自动将标记结果写入Label.txt中。若未开启此选项则检测到用户手动确认过5张图片后进行自动保存。
- 手动保存:点击“文件 - 保存标记结果”手动保存标记。
- 关闭应用程序保存

View File

@ -154,39 +154,19 @@ class Canvas(QWidget):
clipped_y = min(max(0, pos.y()), size.height())
pos = QPointF(clipped_x, clipped_y)
elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):# and not self.fourpoint:
elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
# Attract line to starting point and colorise to alert the
# user:
pos = self.current[0]
color = self.current.line_color
self.overrideCursor(CURSOR_POINT)
self.current.highlightVertex(0, Shape.NEAR_VERTEX)
# elif ( # ADD # 合并上下代码 内容一样
# len(self.current) > 1
# and self.fourpoint
# and self.closeEnough(pos, self.current[0])
# ):
# # Attract line to starting point and
# # colorise to alert the user.
# pos = self.current[0]
# self.overrideCursor(CURSOR_POINT)
# self.current.highlightVertex(0, Shape.NEAR_VERTEX)
if self.drawSquare: # 这部分不同
# initPos = self.current[0] # 原先代码
# minX = initPos.x()
# minY = initPos.y()
# min_size = min(abs(pos.x() - minX), abs(pos.y() - minY))
# directionX = -1 if pos.x() - minX < 0 else 1
# directionY = -1 if pos.y() - minY < 0 else 1
# self.line[1] = QPointF(minX + directionX * min_size, minY + directionY * min_size)
self.line.points = [self.current[0], pos] # Labelme代码
if self.drawSquare:
self.line.points = [self.current[0], pos]
self.line.close()
elif self.fourpoint:
# self.line[self.pointnum] = pos # OLD
self.line[0] = self.current[-1]
self.line[1] = pos
@ -200,7 +180,7 @@ class Canvas(QWidget):
self.prevPoint = pos
self.repaint()
return
# 一下都相同
# Polygon copy moving.
if Qt.RightButton & ev.buttons():
if self.selectedShapesCopy and self.prevPoint:
@ -218,7 +198,7 @@ class Canvas(QWidget):
if Qt.LeftButton & ev.buttons():
if self.selectedVertex():
self.boundedMoveVertex(pos)
self.shapeMoved.emit() # 同时选中时的移动
self.shapeMoved.emit()
self.repaint()
self.movingShape = True
elif self.selectedShapes and self.prevPoint:
@ -227,7 +207,7 @@ class Canvas(QWidget):
self.shapeMoved.emit()
self.repaint()
self.movingShape = True
else: # TODO 这部分是多的
else:
#pan
delta_x = pos.x() - self.pan_initial_pos.x()
delta_y = pos.y() - self.pan_initial_pos.y()
@ -248,8 +228,7 @@ class Canvas(QWidget):
if index is not None:
if self.selectedVertex():
self.hShape.highlightClear()
# TODO Pre部分的变量都没有
self.hVertex, self.hShape = index, shape # 倒着来的原因是要更新hShape的值
self.hVertex, self.hShape = index, shape
shape.highlightVertex(index, shape.MOVE_VERTEX)
self.overrideCursor(CURSOR_POINT)
self.setToolTip("Click & drag to move point")
@ -293,39 +272,23 @@ class Canvas(QWidget):
self.finalise()
elif not self.outOfPixmap(pos):
# Create new shape.
self.current = Shape()# self.current = Shape(shape_type=self.createMode) # TODO: 有可能需要制定类型?
self.current = Shape()
self.current.addPoint(pos)
# if self.createMode == "point":
# self.finalise()
# else:
# if self.createMode == "circle":
# self.current.shape_type = "circle"
self.line.points = [pos, pos]
self.setHiding()
self.drawingPolygon.emit(True)
self.update()
else: # 改动后可以增加多选框选点方式从单点变成list
# selection = self.selectShapePoint(pos)
# self.prevPoint = pos
#
# if selection is None:
# #pan
# QApplication.setOverrideCursor(QCursor(Qt.OpenHandCursor))
# self.pan_initial_pos = pos
else:
group_mode = int(ev.modifiers()) == Qt.ControlModifier
self.selectShapePoint(pos, multiple_selection_mode=group_mode)
self.prevPoint = pos
self.pan_initial_pos = pos
# self.repaint()
elif ev.button() == Qt.RightButton and self.editing():
# self.selectShapePoint(pos)
# self.prevPoint = pos
group_mode = int(ev.modifiers()) == Qt.ControlModifier
self.selectShapePoint(pos, multiple_selection_mode=group_mode)
self.prevPoint = pos
# self.repaint() # 只用update
self.update()
def mouseReleaseEvent(self, ev):
@ -338,46 +301,34 @@ class Canvas(QWidget):
# self.selectedShapeCopy = None
self.selectedShapesCopy = []
self.repaint()
# elif ev.button() == Qt.LeftButton and self.selectedShape: # OLD
elif ev.button() == Qt.LeftButton and self.selectedShapes:
if self.selectedVertex():
self.overrideCursor(CURSOR_POINT)
else:
self.overrideCursor(CURSOR_GRAB)
elif ev.button() == Qt.LeftButton and not self.fourpoint: # 暂时去除四点部分的代码
elif ev.button() == Qt.LeftButton and not self.fourpoint:
pos = self.transformPos(ev.pos())
if self.drawing():
self.handleDrawing(pos) # 关键函数
self.handleDrawing(pos)
else:
#pan
QApplication.restoreOverrideCursor() # ?
if self.movingShape and self.hShape: # 加上之后会移动点会崩 用于撤回
if self.movingShape and self.hShape:
index = self.shapes.index(self.hShape)
if (
self.shapesBackups[-1][index].points # 如果新建的框位置变化
self.shapesBackups[-1][index].points
!= self.shapes[index].points
):
self.storeShapes() # 重新backup一下
self.shapeMoved.emit() # 连接updateBoxlist
self.storeShapes()
self.shapeMoved.emit() # connect to updateBoxlist in PPOCRLabel.py
self.movingShape = False
def endMove(self, copy=False):
# assert self.selectedShape and self.selectedShapeCopy
# shape = self.selectedShapeCopy
#del shape.fill_color
#del shape.line_color
# if copy:
# self.shapes.append(shape)
# self.selectedShape.selected = False
# self.selectedShape = shape
# self.repaint()
# else:
# self.selectedShape.points = [p for p in shape.points]
# self.selectedShapeCopy = None
assert self.selectedShapes and self.selectedShapesCopy
assert len(self.selectedShapesCopy) == len(self.selectedShapes)
if copy:
@ -401,7 +352,7 @@ class Canvas(QWidget):
self.setHiding(True)
self.repaint()
def handleDrawing(self, pos): # 没有此函数
def handleDrawing(self, pos):
if self.current and self.current.reachMaxPoints() is False:
if self.fourpoint:
targetPos = self.line[self.pointnum]
@ -411,7 +362,7 @@ class Canvas(QWidget):
if self.pointnum == 3:
self.finalise()
else: # 按住送掉后跳到这里
else:
initPos = self.current[0]
print('initPos', self.current[0])
minX = initPos.x()
@ -448,38 +399,17 @@ class Canvas(QWidget):
self.finalise()
def selectShapes(self, shapes):
# self.deSelectShape()
# shape.selected = True
# self.selectedShape = shape
# self.setHiding()
# self.selectionChanged.emit(True)
# self.update()
for s in shapes: s.seleted = True
self.setHiding()
self.selectionChanged.emit(shapes)
self.update()
# def selectShapePoint(self, point):
# """Select the first shape created which contains this point."""
# self.deSelectShape()
# if self.selectedVertex(): # A vertex is marked for selection.
# index, shape = self.hVertex, self.hShape
# shape.highlightVertex(index, shape.MOVE_VERTEX)
# self.selectShape(shape)
# return self.hVertex
# for shape in reversed(self.shapes):
# if self.isVisible(shape) and shape.containsPoint(point):
# self.selectShape(shape) # 函数
# self.calculateOffsets(shape, point)
# return self.selectedShape
# return None
def selectShapePoint(self, point, multiple_selection_mode):
"""Select the first shape created which contains this point."""
if self.selectedVertex(): # A vertex is marked for selection.
index, shape = self.hVertex, self.hShape
shape.highlightVertex(index, shape.MOVE_VERTEX) # 突出显示
shape.highlightVertex(index, shape.MOVE_VERTEX)
return self.hVertex
else:
for shape in reversed(self.shapes):
@ -487,9 +417,9 @@ class Canvas(QWidget):
self.calculateOffsets(shape, point)
self.setHiding()
if multiple_selection_mode:
if shape not in self.selectedShapes: # list TODO:为什么是2个刚开始应该是1个
if shape not in self.selectedShapes: # list
self.selectionChanged.emit(
self.selectedShapes + [shape] # 选择+未选择
self.selectedShapes + [shape]
)
else:
self.selectionChanged.emit([shape])
@ -539,7 +469,8 @@ class Canvas(QWidget):
else:
shiftPos = pos - point
if shape[0].x()==shape[3].x() and shape[1].x()==shape[2].x() and shape[0].y()==shape[1].y():
if [shape[0].x(), shape[0].y(), shape[2].x(), shape[2].y()] \
== [shape[3].x(),shape[1].y(),shape[1].x(),shape[3].y()]:
shape.moveVertexBy(index, shiftPos)
lindex = (index + 1) % 4
rindex = (index + 3) % 4
@ -559,6 +490,7 @@ class Canvas(QWidget):
def boundedMoveShape(self, shapes, pos):
if type(shapes).__name__ != 'list': shapes = [shapes]
if self.outOfPixmap(pos):
return False # No need to move
o1 = pos + self.offsets[0]
@ -582,36 +514,19 @@ class Canvas(QWidget):
return False
def deSelectShape(self):
# if self.selectedShape:
# self.selectedShape.selected = False
# self.selectedShape = None
# self.setHiding(False)
# self.selectionChanged.emit(False)
# self.update()
if self.selectedShapes:
# TODO少了两个清空
for shape in self.selectedShapes: shape.selected=False
self.setHiding(False)
self.selectionChanged.emit([])
self.update()
# def deleteSelected(self):
# if self.selectedShape:
# shape = self.selectedShape
# self.shapes.remove(self.selectedShape)
# self.selectedShape = None
# self.update()
# return shape
def deleteSelected(self):
deleted_shapes = []
if self.selectedShapes:
#self.storeShapes()
for shape in self.selectedShapes:
self.shapes.remove(shape)
#self.shapesBackups.append(shape)
deleted_shapes.append(shape)
self.storeShapes() # 这里应该是先储存
self.storeShapes()
self.selectedShapes = []
self.update()
return deleted_shapes
@ -622,32 +537,25 @@ class Canvas(QWidget):
shapesBackup.append(shape.copy())
if len(self.shapesBackups) >= 10:
self.shapesBackups = self.shapesBackups[-9:]
self.shapesBackups.append(shapesBackup) # 每删除或保存一次都会backup一次
self.shapesBackups.append(shapesBackup)
def copySelectedShape(self):
# if self.selectedShape:
# shape = self.selectedShape.copy()
# self.deSelectShape()
# self.shapes.append(shape)
# shape.selected = True
# self.selectedShape = shape
# self.boundedShiftShape(shape)
# return shape
if self.selectedShapes:
self.selectedShapesCopy = [s.copy() for s in self.selectedShapes]
self.boundedShiftShapes(self.selectedShapesCopy)
self.endMove(copy=True)
return self.selectedShapes
def boundedShiftShape(self, shape):
def boundedShiftShapes(self, shapes):
# Try to move in one direction, and if it fails in another.
# Give up if both fail.
point = shape[0]
offset = QPointF(2.0, 2.0)
self.calculateOffsets(shape, point)
self.prevPoint = point
if not self.boundedMoveShape(shape, point - offset):
self.boundedMoveShape(shape, point + offset)
for shape in shapes:
point = shape[0]
offset = QPointF(2.0, 2.0)
self.calculateOffsets(shape, point)
self.prevPoint = point
if not self.boundedMoveShape(shape, point - offset):
self.boundedMoveShape(shape, point + offset)
def paintEvent(self, event):
if not self.pixmap:
@ -801,14 +709,14 @@ class Canvas(QWidget):
self.update()
elif key == Qt.Key_Return and self.canCloseShape():
self.finalise()
# elif key == Qt.Key_Left and self.selectedShape:
# self.moveOnePixel('Left')
# elif key == Qt.Key_Right and self.selectedShape:
# self.moveOnePixel('Right')
# elif key == Qt.Key_Up and self.selectedShape:
# self.moveOnePixel('Up')
# elif key == Qt.Key_Down and self.selectedShape:
# self.moveOnePixel('Down')
elif key == Qt.Key_Left and self.selectedShape:
self.moveOnePixel('Left')
elif key == Qt.Key_Right and self.selectedShape:
self.moveOnePixel('Right')
elif key == Qt.Key_Up and self.selectedShape:
self.moveOnePixel('Up')
elif key == Qt.Key_Down and self.selectedShape:
self.moveOnePixel('Down')
def moveOnePixel(self, direction):
# print(self.selectedShape.points)
@ -851,7 +759,6 @@ class Canvas(QWidget):
if fill_color:
self.shapes[-1].fill_color = fill_color
#self.shapesBackups.pop() # 新建shape后要pop?
self.storeShapes()
return self.shapes[-1]
@ -930,13 +837,12 @@ class Canvas(QWidget):
def setDrawingShapeToSquare(self, status):
self.drawSquare = status
def restoreShape(self): # 用于撤销
def restoreShape(self):
if not self.isShapeRestorable:
return
self.shapesBackups.pop() # latest
shapesBackup = self.shapesBackups.pop()
self.shapes = shapesBackup
#self.shapes.append(self.shapesBackups.pop()) # 为何这里之前是只将back赋值呢
self.selectedShapes = []
for shape in self.shapes:
shape.selected = False