Pep 8 (#720)
* rename local variables in main file * additional renaming of functions and variables * Rename main file functions * Rename functions and variables in canvas.py * Rename functions and locals for remaining files * Rename non-Qt derived class' members * Rename members of Qt-derived classes * Fix paint label issue
This commit is contained in:
parent
ef05452f7c
commit
c35f09747a
1508
labelImg.py
1508
labelImg.py
File diff suppressed because it is too large
Load Diff
520
libs/canvas.py
520
libs/canvas.py
@ -39,21 +39,21 @@ class Canvas(QWidget):
|
|||||||
self.mode = self.EDIT
|
self.mode = self.EDIT
|
||||||
self.shapes = []
|
self.shapes = []
|
||||||
self.current = None
|
self.current = None
|
||||||
self.selectedShape = None # save the selected shape here
|
self.selected_shape = None # save the selected shape here
|
||||||
self.selectedShapeCopy = None
|
self.selected_shape_copy = None
|
||||||
self.drawingLineColor = QColor(0, 0, 255)
|
self.drawing_line_color = QColor(0, 0, 255)
|
||||||
self.drawingRectColor = QColor(0, 0, 255)
|
self.drawing_rect_color = QColor(0, 0, 255)
|
||||||
self.line = Shape(line_color=self.drawingLineColor)
|
self.line = Shape(line_color=self.drawing_line_color)
|
||||||
self.prevPoint = QPointF()
|
self.prev_point = QPointF()
|
||||||
self.offsets = QPointF(), QPointF()
|
self.offsets = QPointF(), QPointF()
|
||||||
self.scale = 1.0
|
self.scale = 1.0
|
||||||
self.labelFontSize = 8
|
self.label_font_size = 8
|
||||||
self.pixmap = QPixmap()
|
self.pixmap = QPixmap()
|
||||||
self.visible = {}
|
self.visible = {}
|
||||||
self._hideBackround = False
|
self._hide_background = False
|
||||||
self.hideBackround = False
|
self.hide_background = False
|
||||||
self.hShape = None
|
self.h_shape = None
|
||||||
self.hVertex = None
|
self.h_vertex = None
|
||||||
self._painter = QPainter()
|
self._painter = QPainter()
|
||||||
self._cursor = CURSOR_DEFAULT
|
self._cursor = CURSOR_DEFAULT
|
||||||
# Menus:
|
# Menus:
|
||||||
@ -62,23 +62,23 @@ class Canvas(QWidget):
|
|||||||
self.setMouseTracking(True)
|
self.setMouseTracking(True)
|
||||||
self.setFocusPolicy(Qt.WheelFocus)
|
self.setFocusPolicy(Qt.WheelFocus)
|
||||||
self.verified = False
|
self.verified = False
|
||||||
self.drawSquare = False
|
self.draw_square = False
|
||||||
|
|
||||||
# initialisation for panning
|
# initialisation for panning
|
||||||
self.pan_initial_pos = QPoint()
|
self.pan_initial_pos = QPoint()
|
||||||
|
|
||||||
def setDrawingColor(self, qColor):
|
def set_drawing_color(self, qcolor):
|
||||||
self.drawingLineColor = qColor
|
self.drawing_line_color = qcolor
|
||||||
self.drawingRectColor = qColor
|
self.drawing_rect_color = qcolor
|
||||||
|
|
||||||
def enterEvent(self, ev):
|
def enterEvent(self, ev):
|
||||||
self.overrideCursor(self._cursor)
|
self.override_cursor(self._cursor)
|
||||||
|
|
||||||
def leaveEvent(self, ev):
|
def leaveEvent(self, ev):
|
||||||
self.restoreCursor()
|
self.restore_cursor()
|
||||||
|
|
||||||
def focusOutEvent(self, ev):
|
def focusOutEvent(self, ev):
|
||||||
self.restoreCursor()
|
self.restore_cursor()
|
||||||
|
|
||||||
def isVisible(self, shape):
|
def isVisible(self, shape):
|
||||||
return self.visible.get(shape, True)
|
return self.visible.get(shape, True)
|
||||||
@ -89,44 +89,44 @@ class Canvas(QWidget):
|
|||||||
def editing(self):
|
def editing(self):
|
||||||
return self.mode == self.EDIT
|
return self.mode == self.EDIT
|
||||||
|
|
||||||
def setEditing(self, value=True):
|
def set_editing(self, value=True):
|
||||||
self.mode = self.EDIT if value else self.CREATE
|
self.mode = self.EDIT if value else self.CREATE
|
||||||
if not value: # Create
|
if not value: # Create
|
||||||
self.unHighlight()
|
self.un_highlight()
|
||||||
self.deSelectShape()
|
self.de_select_shape()
|
||||||
self.prevPoint = QPointF()
|
self.prev_point = QPointF()
|
||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def unHighlight(self):
|
def un_highlight(self):
|
||||||
if self.hShape:
|
if self.h_shape:
|
||||||
self.hShape.highlightClear()
|
self.h_shape.highlight_clear()
|
||||||
self.hVertex = self.hShape = None
|
self.h_vertex = self.h_shape = None
|
||||||
|
|
||||||
def selectedVertex(self):
|
def selected_vertex(self):
|
||||||
return self.hVertex is not None
|
return self.h_vertex is not None
|
||||||
|
|
||||||
def mouseMoveEvent(self, ev):
|
def mouseMoveEvent(self, ev):
|
||||||
"""Update line with last point and current coordinates."""
|
"""Update line with last point and current coordinates."""
|
||||||
pos = self.transformPos(ev.pos())
|
pos = self.transform_pos(ev.pos())
|
||||||
|
|
||||||
# Update coordinates in status bar if image is opened
|
# Update coordinates in status bar if image is opened
|
||||||
window = self.parent().window()
|
window = self.parent().window()
|
||||||
if window.filePath is not None:
|
if window.file_path is not None:
|
||||||
self.parent().window().labelCoordinates.setText(
|
self.parent().window().label_coordinates.setText(
|
||||||
'X: %d; Y: %d' % (pos.x(), pos.y()))
|
'X: %d; Y: %d' % (pos.x(), pos.y()))
|
||||||
|
|
||||||
# Polygon drawing.
|
# Polygon drawing.
|
||||||
if self.drawing():
|
if self.drawing():
|
||||||
self.overrideCursor(CURSOR_DRAW)
|
self.override_cursor(CURSOR_DRAW)
|
||||||
if self.current:
|
if self.current:
|
||||||
# Display annotation width and height while drawing
|
# Display annotation width and height while drawing
|
||||||
currentWidth = abs(self.current[0].x() - pos.x())
|
current_width = abs(self.current[0].x() - pos.x())
|
||||||
currentHeight = abs(self.current[0].y() - pos.y())
|
current_height = abs(self.current[0].y() - pos.y())
|
||||||
self.parent().window().labelCoordinates.setText(
|
self.parent().window().label_coordinates.setText(
|
||||||
'Width: %d, Height: %d / X: %d; Y: %d' % (currentWidth, currentHeight, pos.x(), pos.y()))
|
'Width: %d, Height: %d / X: %d; Y: %d' % (current_width, current_height, pos.x(), pos.y()))
|
||||||
|
|
||||||
color = self.drawingLineColor
|
color = self.drawing_line_color
|
||||||
if self.outOfPixmap(pos):
|
if self.out_of_pixmap(pos):
|
||||||
# Don't allow the user to draw outside the pixmap.
|
# Don't allow the user to draw outside the pixmap.
|
||||||
# Clip the coordinates to 0 or max,
|
# Clip the coordinates to 0 or max,
|
||||||
# if they are outside the range [0, max]
|
# if they are outside the range [0, max]
|
||||||
@ -134,53 +134,53 @@ class Canvas(QWidget):
|
|||||||
clipped_x = min(max(0, pos.x()), size.width())
|
clipped_x = min(max(0, pos.x()), size.width())
|
||||||
clipped_y = min(max(0, pos.y()), size.height())
|
clipped_y = min(max(0, pos.y()), size.height())
|
||||||
pos = QPointF(clipped_x, clipped_y)
|
pos = QPointF(clipped_x, clipped_y)
|
||||||
elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
|
elif len(self.current) > 1 and self.close_enough(pos, self.current[0]):
|
||||||
# Attract line to starting point and colorise to alert the
|
# Attract line to starting point and colorise to alert the
|
||||||
# user:
|
# user:
|
||||||
pos = self.current[0]
|
pos = self.current[0]
|
||||||
color = self.current.line_color
|
color = self.current.line_color
|
||||||
self.overrideCursor(CURSOR_POINT)
|
self.override_cursor(CURSOR_POINT)
|
||||||
self.current.highlightVertex(0, Shape.NEAR_VERTEX)
|
self.current.highlight_vertex(0, Shape.NEAR_VERTEX)
|
||||||
|
|
||||||
if self.drawSquare:
|
if self.draw_square:
|
||||||
initPos = self.current[0]
|
init_pos = self.current[0]
|
||||||
minX = initPos.x()
|
min_x = init_pos.x()
|
||||||
minY = initPos.y()
|
min_y = init_pos.y()
|
||||||
min_size = min(abs(pos.x() - minX), abs(pos.y() - minY))
|
min_size = min(abs(pos.x() - min_x), abs(pos.y() - min_y))
|
||||||
directionX = -1 if pos.x() - minX < 0 else 1
|
direction_x = -1 if pos.x() - min_x < 0 else 1
|
||||||
directionY = -1 if pos.y() - minY < 0 else 1
|
direction_y = -1 if pos.y() - min_y < 0 else 1
|
||||||
self.line[1] = QPointF(minX + directionX * min_size, minY + directionY * min_size)
|
self.line[1] = QPointF(min_x + direction_x * min_size, min_y + direction_y * min_size)
|
||||||
else:
|
else:
|
||||||
self.line[1] = pos
|
self.line[1] = pos
|
||||||
|
|
||||||
self.line.line_color = color
|
self.line.line_color = color
|
||||||
self.prevPoint = QPointF()
|
self.prev_point = QPointF()
|
||||||
self.current.highlightClear()
|
self.current.highlight_clear()
|
||||||
else:
|
else:
|
||||||
self.prevPoint = pos
|
self.prev_point = pos
|
||||||
self.repaint()
|
self.repaint()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Polygon copy moving.
|
# Polygon copy moving.
|
||||||
if Qt.RightButton & ev.buttons():
|
if Qt.RightButton & ev.buttons():
|
||||||
if self.selectedShapeCopy and self.prevPoint:
|
if self.selected_shape_copy and self.prev_point:
|
||||||
self.overrideCursor(CURSOR_MOVE)
|
self.override_cursor(CURSOR_MOVE)
|
||||||
self.boundedMoveShape(self.selectedShapeCopy, pos)
|
self.bounded_move_shape(self.selected_shape_copy, pos)
|
||||||
self.repaint()
|
self.repaint()
|
||||||
elif self.selectedShape:
|
elif self.selected_shape:
|
||||||
self.selectedShapeCopy = self.selectedShape.copy()
|
self.selected_shape_copy = self.selected_shape.copy()
|
||||||
self.repaint()
|
self.repaint()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Polygon/Vertex moving.
|
# Polygon/Vertex moving.
|
||||||
if Qt.LeftButton & ev.buttons():
|
if Qt.LeftButton & ev.buttons():
|
||||||
if self.selectedVertex():
|
if self.selected_vertex():
|
||||||
self.boundedMoveVertex(pos)
|
self.bounded_move_vertex(pos)
|
||||||
self.shapeMoved.emit()
|
self.shapeMoved.emit()
|
||||||
self.repaint()
|
self.repaint()
|
||||||
elif self.selectedShape and self.prevPoint:
|
elif self.selected_shape and self.prev_point:
|
||||||
self.overrideCursor(CURSOR_MOVE)
|
self.override_cursor(CURSOR_MOVE)
|
||||||
self.boundedMoveShape(self.selectedShape, pos)
|
self.bounded_move_shape(self.selected_shape, pos)
|
||||||
self.shapeMoved.emit()
|
self.shapeMoved.emit()
|
||||||
self.repaint()
|
self.repaint()
|
||||||
else:
|
else:
|
||||||
@ -192,7 +192,7 @@ class Canvas(QWidget):
|
|||||||
self.update()
|
self.update()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Just hovering over the canvas, 2 posibilities:
|
# Just hovering over the canvas, 2 possibilities:
|
||||||
# - Highlight shapes
|
# - Highlight shapes
|
||||||
# - Highlight vertex
|
# - Highlight vertex
|
||||||
# Update shape/vertex fill and tooltip value accordingly.
|
# Update shape/vertex fill and tooltip value accordingly.
|
||||||
@ -200,43 +200,43 @@ class Canvas(QWidget):
|
|||||||
for shape in reversed([s for s in self.shapes if self.isVisible(s)]):
|
for shape in reversed([s for s in self.shapes if self.isVisible(s)]):
|
||||||
# Look for a nearby vertex to highlight. If that fails,
|
# Look for a nearby vertex to highlight. If that fails,
|
||||||
# check if we happen to be inside a shape.
|
# check if we happen to be inside a shape.
|
||||||
index = shape.nearestVertex(pos, self.epsilon)
|
index = shape.nearest_vertex(pos, self.epsilon)
|
||||||
if index is not None:
|
if index is not None:
|
||||||
if self.selectedVertex():
|
if self.selected_vertex():
|
||||||
self.hShape.highlightClear()
|
self.h_shape.highlight_clear()
|
||||||
self.hVertex, self.hShape = index, shape
|
self.h_vertex, self.h_shape = index, shape
|
||||||
shape.highlightVertex(index, shape.MOVE_VERTEX)
|
shape.highlight_vertex(index, shape.MOVE_VERTEX)
|
||||||
self.overrideCursor(CURSOR_POINT)
|
self.override_cursor(CURSOR_POINT)
|
||||||
self.setToolTip("Click & drag to move point")
|
self.setToolTip("Click & drag to move point")
|
||||||
self.setStatusTip(self.toolTip())
|
self.setStatusTip(self.toolTip())
|
||||||
self.update()
|
self.update()
|
||||||
break
|
break
|
||||||
elif shape.containsPoint(pos):
|
elif shape.contains_point(pos):
|
||||||
if self.selectedVertex():
|
if self.selected_vertex():
|
||||||
self.hShape.highlightClear()
|
self.h_shape.highlight_clear()
|
||||||
self.hVertex, self.hShape = None, shape
|
self.h_vertex, self.h_shape = None, shape
|
||||||
self.setToolTip(
|
self.setToolTip(
|
||||||
"Click & drag to move shape '%s'" % shape.label)
|
"Click & drag to move shape '%s'" % shape.label)
|
||||||
self.setStatusTip(self.toolTip())
|
self.setStatusTip(self.toolTip())
|
||||||
self.overrideCursor(CURSOR_GRAB)
|
self.override_cursor(CURSOR_GRAB)
|
||||||
self.update()
|
self.update()
|
||||||
break
|
break
|
||||||
else: # Nothing found, clear highlights, reset state.
|
else: # Nothing found, clear highlights, reset state.
|
||||||
if self.hShape:
|
if self.h_shape:
|
||||||
self.hShape.highlightClear()
|
self.h_shape.highlight_clear()
|
||||||
self.update()
|
self.update()
|
||||||
self.hVertex, self.hShape = None, None
|
self.h_vertex, self.h_shape = None, None
|
||||||
self.overrideCursor(CURSOR_DEFAULT)
|
self.override_cursor(CURSOR_DEFAULT)
|
||||||
|
|
||||||
def mousePressEvent(self, ev):
|
def mousePressEvent(self, ev):
|
||||||
pos = self.transformPos(ev.pos())
|
pos = self.transform_pos(ev.pos())
|
||||||
|
|
||||||
if ev.button() == Qt.LeftButton:
|
if ev.button() == Qt.LeftButton:
|
||||||
if self.drawing():
|
if self.drawing():
|
||||||
self.handleDrawing(pos)
|
self.handle_drawing(pos)
|
||||||
else:
|
else:
|
||||||
selection = self.selectShapePoint(pos)
|
selection = self.select_shape_point(pos)
|
||||||
self.prevPoint = pos
|
self.prev_point = pos
|
||||||
|
|
||||||
if selection is None:
|
if selection is None:
|
||||||
# pan
|
# pan
|
||||||
@ -244,119 +244,119 @@ class Canvas(QWidget):
|
|||||||
self.pan_initial_pos = pos
|
self.pan_initial_pos = pos
|
||||||
|
|
||||||
elif ev.button() == Qt.RightButton and self.editing():
|
elif ev.button() == Qt.RightButton and self.editing():
|
||||||
self.selectShapePoint(pos)
|
self.select_shape_point(pos)
|
||||||
self.prevPoint = pos
|
self.prev_point = pos
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def mouseReleaseEvent(self, ev):
|
def mouseReleaseEvent(self, ev):
|
||||||
if ev.button() == Qt.RightButton:
|
if ev.button() == Qt.RightButton:
|
||||||
menu = self.menus[bool(self.selectedShapeCopy)]
|
menu = self.menus[bool(self.selected_shape_copy)]
|
||||||
self.restoreCursor()
|
self.restore_cursor()
|
||||||
if not menu.exec_(self.mapToGlobal(ev.pos()))\
|
if not menu.exec_(self.mapToGlobal(ev.pos()))\
|
||||||
and self.selectedShapeCopy:
|
and self.selected_shape_copy:
|
||||||
# Cancel the move by deleting the shadow copy.
|
# Cancel the move by deleting the shadow copy.
|
||||||
self.selectedShapeCopy = None
|
self.selected_shape_copy = None
|
||||||
self.repaint()
|
self.repaint()
|
||||||
elif ev.button() == Qt.LeftButton and self.selectedShape:
|
elif ev.button() == Qt.LeftButton and self.selected_shape:
|
||||||
if self.selectedVertex():
|
if self.selected_vertex():
|
||||||
self.overrideCursor(CURSOR_POINT)
|
self.override_cursor(CURSOR_POINT)
|
||||||
else:
|
else:
|
||||||
self.overrideCursor(CURSOR_GRAB)
|
self.override_cursor(CURSOR_GRAB)
|
||||||
elif ev.button() == Qt.LeftButton:
|
elif ev.button() == Qt.LeftButton:
|
||||||
pos = self.transformPos(ev.pos())
|
pos = self.transform_pos(ev.pos())
|
||||||
if self.drawing():
|
if self.drawing():
|
||||||
self.handleDrawing(pos)
|
self.handle_drawing(pos)
|
||||||
else:
|
else:
|
||||||
# pan
|
# pan
|
||||||
QApplication.restoreOverrideCursor()
|
QApplication.restoreOverrideCursor()
|
||||||
|
|
||||||
def endMove(self, copy=False):
|
def end_move(self, copy=False):
|
||||||
assert self.selectedShape and self.selectedShapeCopy
|
assert self.selected_shape and self.selected_shape_copy
|
||||||
shape = self.selectedShapeCopy
|
shape = self.selected_shape_copy
|
||||||
# del shape.fill_color
|
# del shape.fill_color
|
||||||
# del shape.line_color
|
# del shape.line_color
|
||||||
if copy:
|
if copy:
|
||||||
self.shapes.append(shape)
|
self.shapes.append(shape)
|
||||||
self.selectedShape.selected = False
|
self.selected_shape.selected = False
|
||||||
self.selectedShape = shape
|
self.selected_shape = shape
|
||||||
self.repaint()
|
self.repaint()
|
||||||
else:
|
else:
|
||||||
self.selectedShape.points = [p for p in shape.points]
|
self.selected_shape.points = [p for p in shape.points]
|
||||||
self.selectedShapeCopy = None
|
self.selected_shape_copy = None
|
||||||
|
|
||||||
def hideBackroundShapes(self, value):
|
def hide_background_shapes(self, value):
|
||||||
self.hideBackround = value
|
self.hide_background = value
|
||||||
if self.selectedShape:
|
if self.selected_shape:
|
||||||
# Only hide other shapes if there is a current selection.
|
# Only hide other shapes if there is a current selection.
|
||||||
# Otherwise the user will not be able to select a shape.
|
# Otherwise the user will not be able to select a shape.
|
||||||
self.setHiding(True)
|
self.set_hiding(True)
|
||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def handleDrawing(self, pos):
|
def handle_drawing(self, pos):
|
||||||
if self.current and self.current.reachMaxPoints() is False:
|
if self.current and self.current.reach_max_points() is False:
|
||||||
initPos = self.current[0]
|
init_pos = self.current[0]
|
||||||
minX = initPos.x()
|
min_x = init_pos.x()
|
||||||
minY = initPos.y()
|
min_y = init_pos.y()
|
||||||
targetPos = self.line[1]
|
target_pos = self.line[1]
|
||||||
maxX = targetPos.x()
|
max_x = target_pos.x()
|
||||||
maxY = targetPos.y()
|
max_y = target_pos.y()
|
||||||
self.current.addPoint(QPointF(maxX, minY))
|
self.current.add_point(QPointF(max_x, min_y))
|
||||||
self.current.addPoint(targetPos)
|
self.current.add_point(target_pos)
|
||||||
self.current.addPoint(QPointF(minX, maxY))
|
self.current.add_point(QPointF(min_x, max_y))
|
||||||
self.finalise()
|
self.finalise()
|
||||||
elif not self.outOfPixmap(pos):
|
elif not self.out_of_pixmap(pos):
|
||||||
self.current = Shape()
|
self.current = Shape()
|
||||||
self.current.addPoint(pos)
|
self.current.add_point(pos)
|
||||||
self.line.points = [pos, pos]
|
self.line.points = [pos, pos]
|
||||||
self.setHiding()
|
self.set_hiding()
|
||||||
self.drawingPolygon.emit(True)
|
self.drawingPolygon.emit(True)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def setHiding(self, enable=True):
|
def set_hiding(self, enable=True):
|
||||||
self._hideBackround = self.hideBackround if enable else False
|
self._hide_background = self.hide_background if enable else False
|
||||||
|
|
||||||
def canCloseShape(self):
|
def can_close_shape(self):
|
||||||
return self.drawing() and self.current and len(self.current) > 2
|
return self.drawing() and self.current and len(self.current) > 2
|
||||||
|
|
||||||
def mouseDoubleClickEvent(self, ev):
|
def mouseDoubleClickEvent(self, ev):
|
||||||
# We need at least 4 points here, since the mousePress handler
|
# We need at least 4 points here, since the mousePress handler
|
||||||
# adds an extra one before this handler is called.
|
# adds an extra one before this handler is called.
|
||||||
if self.canCloseShape() and len(self.current) > 3:
|
if self.can_close_shape() and len(self.current) > 3:
|
||||||
self.current.popPoint()
|
self.current.pop_point()
|
||||||
self.finalise()
|
self.finalise()
|
||||||
|
|
||||||
def selectShape(self, shape):
|
def select_shape(self, shape):
|
||||||
self.deSelectShape()
|
self.de_select_shape()
|
||||||
shape.selected = True
|
shape.selected = True
|
||||||
self.selectedShape = shape
|
self.selected_shape = shape
|
||||||
self.setHiding()
|
self.set_hiding()
|
||||||
self.selectionChanged.emit(True)
|
self.selectionChanged.emit(True)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def selectShapePoint(self, point):
|
def select_shape_point(self, point):
|
||||||
"""Select the first shape created which contains this point."""
|
"""Select the first shape created which contains this point."""
|
||||||
self.deSelectShape()
|
self.de_select_shape()
|
||||||
if self.selectedVertex(): # A vertex is marked for selection.
|
if self.selected_vertex(): # A vertex is marked for selection.
|
||||||
index, shape = self.hVertex, self.hShape
|
index, shape = self.h_vertex, self.h_shape
|
||||||
shape.highlightVertex(index, shape.MOVE_VERTEX)
|
shape.highlight_vertex(index, shape.MOVE_VERTEX)
|
||||||
self.selectShape(shape)
|
self.select_shape(shape)
|
||||||
return self.hVertex
|
return self.h_vertex
|
||||||
for shape in reversed(self.shapes):
|
for shape in reversed(self.shapes):
|
||||||
if self.isVisible(shape) and shape.containsPoint(point):
|
if self.isVisible(shape) and shape.contains_point(point):
|
||||||
self.selectShape(shape)
|
self.select_shape(shape)
|
||||||
self.calculateOffsets(shape, point)
|
self.calculate_offsets(shape, point)
|
||||||
return self.selectedShape
|
return self.selected_shape
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def calculateOffsets(self, shape, point):
|
def calculate_offsets(self, shape, point):
|
||||||
rect = shape.boundingRect()
|
rect = shape.bounding_rect()
|
||||||
x1 = rect.x() - point.x()
|
x1 = rect.x() - point.x()
|
||||||
y1 = rect.y() - point.y()
|
y1 = rect.y() - point.y()
|
||||||
x2 = (rect.x() + rect.width()) - point.x()
|
x2 = (rect.x() + rect.width()) - point.x()
|
||||||
y2 = (rect.y() + rect.height()) - point.y()
|
y2 = (rect.y() + rect.height()) - point.y()
|
||||||
self.offsets = QPointF(x1, y1), QPointF(x2, y2)
|
self.offsets = QPointF(x1, y1), QPointF(x2, y2)
|
||||||
|
|
||||||
def snapPointToCanvas(self, x, y):
|
def snap_point_to_canvas(self, x, y):
|
||||||
"""
|
"""
|
||||||
Moves a point x,y to within the boundaries of the canvas.
|
Moves a point x,y to within the boundaries of the canvas.
|
||||||
:return: (x,y,snapped) where snapped is True if x or y were changed, False if not.
|
:return: (x,y,snapped) where snapped is True if x or y were changed, False if not.
|
||||||
@ -370,50 +370,50 @@ class Canvas(QWidget):
|
|||||||
|
|
||||||
return x, y, False
|
return x, y, False
|
||||||
|
|
||||||
def boundedMoveVertex(self, pos):
|
def bounded_move_vertex(self, pos):
|
||||||
index, shape = self.hVertex, self.hShape
|
index, shape = self.h_vertex, self.h_shape
|
||||||
point = shape[index]
|
point = shape[index]
|
||||||
if self.outOfPixmap(pos):
|
if self.out_of_pixmap(pos):
|
||||||
size = self.pixmap.size()
|
size = self.pixmap.size()
|
||||||
clipped_x = min(max(0, pos.x()), size.width())
|
clipped_x = min(max(0, pos.x()), size.width())
|
||||||
clipped_y = min(max(0, pos.y()), size.height())
|
clipped_y = min(max(0, pos.y()), size.height())
|
||||||
pos = QPointF(clipped_x, clipped_y)
|
pos = QPointF(clipped_x, clipped_y)
|
||||||
|
|
||||||
if self.drawSquare:
|
if self.draw_square:
|
||||||
opposite_point_index = (index + 2) % 4
|
opposite_point_index = (index + 2) % 4
|
||||||
opposite_point = shape[opposite_point_index]
|
opposite_point = shape[opposite_point_index]
|
||||||
|
|
||||||
min_size = min(abs(pos.x() - opposite_point.x()), abs(pos.y() - opposite_point.y()))
|
min_size = min(abs(pos.x() - opposite_point.x()), abs(pos.y() - opposite_point.y()))
|
||||||
directionX = -1 if pos.x() - opposite_point.x() < 0 else 1
|
direction_x = -1 if pos.x() - opposite_point.x() < 0 else 1
|
||||||
directionY = -1 if pos.y() - opposite_point.y() < 0 else 1
|
direction_y = -1 if pos.y() - opposite_point.y() < 0 else 1
|
||||||
shiftPos = QPointF(opposite_point.x() + directionX * min_size - point.x(),
|
shift_pos = QPointF(opposite_point.x() + direction_x * min_size - point.x(),
|
||||||
opposite_point.y() + directionY * min_size - point.y())
|
opposite_point.y() + direction_y * min_size - point.y())
|
||||||
else:
|
else:
|
||||||
shiftPos = pos - point
|
shift_pos = pos - point
|
||||||
|
|
||||||
shape.moveVertexBy(index, shiftPos)
|
shape.move_vertex_by(index, shift_pos)
|
||||||
|
|
||||||
lindex = (index + 1) % 4
|
left_index = (index + 1) % 4
|
||||||
rindex = (index + 3) % 4
|
right_index = (index + 3) % 4
|
||||||
lshift = None
|
left_shift = None
|
||||||
rshift = None
|
right_shift = None
|
||||||
if index % 2 == 0:
|
if index % 2 == 0:
|
||||||
rshift = QPointF(shiftPos.x(), 0)
|
right_shift = QPointF(shift_pos.x(), 0)
|
||||||
lshift = QPointF(0, shiftPos.y())
|
left_shift = QPointF(0, shift_pos.y())
|
||||||
else:
|
else:
|
||||||
lshift = QPointF(shiftPos.x(), 0)
|
left_shift = QPointF(shift_pos.x(), 0)
|
||||||
rshift = QPointF(0, shiftPos.y())
|
right_shift = QPointF(0, shift_pos.y())
|
||||||
shape.moveVertexBy(rindex, rshift)
|
shape.move_vertex_by(right_index, right_shift)
|
||||||
shape.moveVertexBy(lindex, lshift)
|
shape.move_vertex_by(left_index, left_shift)
|
||||||
|
|
||||||
def boundedMoveShape(self, shape, pos):
|
def bounded_move_shape(self, shape, pos):
|
||||||
if self.outOfPixmap(pos):
|
if self.out_of_pixmap(pos):
|
||||||
return False # No need to move
|
return False # No need to move
|
||||||
o1 = pos + self.offsets[0]
|
o1 = pos + self.offsets[0]
|
||||||
if self.outOfPixmap(o1):
|
if self.out_of_pixmap(o1):
|
||||||
pos -= QPointF(min(0, o1.x()), min(0, o1.y()))
|
pos -= QPointF(min(0, o1.x()), min(0, o1.y()))
|
||||||
o2 = pos + self.offsets[1]
|
o2 = pos + self.offsets[1]
|
||||||
if self.outOfPixmap(o2):
|
if self.out_of_pixmap(o2):
|
||||||
pos += QPointF(min(0, self.pixmap.width() - o2.x()),
|
pos += QPointF(min(0, self.pixmap.width() - o2.x()),
|
||||||
min(0, self.pixmap.height() - o2.y()))
|
min(0, self.pixmap.height() - o2.y()))
|
||||||
# The next line tracks the new position of the cursor
|
# The next line tracks the new position of the cursor
|
||||||
@ -421,48 +421,48 @@ class Canvas(QWidget):
|
|||||||
# a bit "shaky" when nearing the border and allows it to
|
# a bit "shaky" when nearing the border and allows it to
|
||||||
# go outside of the shape's area for some reason. XXX
|
# go outside of the shape's area for some reason. XXX
|
||||||
# self.calculateOffsets(self.selectedShape, pos)
|
# self.calculateOffsets(self.selectedShape, pos)
|
||||||
dp = pos - self.prevPoint
|
dp = pos - self.prev_point
|
||||||
if dp:
|
if dp:
|
||||||
shape.moveBy(dp)
|
shape.move_by(dp)
|
||||||
self.prevPoint = pos
|
self.prev_point = pos
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def deSelectShape(self):
|
def de_select_shape(self):
|
||||||
if self.selectedShape:
|
if self.selected_shape:
|
||||||
self.selectedShape.selected = False
|
self.selected_shape.selected = False
|
||||||
self.selectedShape = None
|
self.selected_shape = None
|
||||||
self.setHiding(False)
|
self.set_hiding(False)
|
||||||
self.selectionChanged.emit(False)
|
self.selectionChanged.emit(False)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def deleteSelected(self):
|
def delete_selected(self):
|
||||||
if self.selectedShape:
|
if self.selected_shape:
|
||||||
shape = self.selectedShape
|
shape = self.selected_shape
|
||||||
self.shapes.remove(self.selectedShape)
|
self.shapes.remove(self.selected_shape)
|
||||||
self.selectedShape = None
|
self.selected_shape = None
|
||||||
self.update()
|
self.update()
|
||||||
return shape
|
return shape
|
||||||
|
|
||||||
def copySelectedShape(self):
|
def copy_selected_shape(self):
|
||||||
if self.selectedShape:
|
if self.selected_shape:
|
||||||
shape = self.selectedShape.copy()
|
shape = self.selected_shape.copy()
|
||||||
self.deSelectShape()
|
self.de_select_shape()
|
||||||
self.shapes.append(shape)
|
self.shapes.append(shape)
|
||||||
shape.selected = True
|
shape.selected = True
|
||||||
self.selectedShape = shape
|
self.selected_shape = shape
|
||||||
self.boundedShiftShape(shape)
|
self.bounded_shift_shape(shape)
|
||||||
return shape
|
return shape
|
||||||
|
|
||||||
def boundedShiftShape(self, shape):
|
def bounded_shift_shape(self, shape):
|
||||||
# Try to move in one direction, and if it fails in another.
|
# Try to move in one direction, and if it fails in another.
|
||||||
# Give up if both fail.
|
# Give up if both fail.
|
||||||
point = shape[0]
|
point = shape[0]
|
||||||
offset = QPointF(2.0, 2.0)
|
offset = QPointF(2.0, 2.0)
|
||||||
self.calculateOffsets(shape, point)
|
self.calculate_offsets(shape, point)
|
||||||
self.prevPoint = point
|
self.prev_point = point
|
||||||
if not self.boundedMoveShape(shape, point - offset):
|
if not self.bounded_move_shape(shape, point - offset):
|
||||||
self.boundedMoveShape(shape, point + offset)
|
self.bounded_move_shape(shape, point + offset)
|
||||||
|
|
||||||
def paintEvent(self, event):
|
def paintEvent(self, event):
|
||||||
if not self.pixmap:
|
if not self.pixmap:
|
||||||
@ -475,36 +475,36 @@ class Canvas(QWidget):
|
|||||||
p.setRenderHint(QPainter.SmoothPixmapTransform)
|
p.setRenderHint(QPainter.SmoothPixmapTransform)
|
||||||
|
|
||||||
p.scale(self.scale, self.scale)
|
p.scale(self.scale, self.scale)
|
||||||
p.translate(self.offsetToCenter())
|
p.translate(self.offset_to_center())
|
||||||
|
|
||||||
p.drawPixmap(0, 0, self.pixmap)
|
p.drawPixmap(0, 0, self.pixmap)
|
||||||
Shape.scale = self.scale
|
Shape.scale = self.scale
|
||||||
Shape.labelFontSize = self.labelFontSize
|
Shape.label_font_size = self.label_font_size
|
||||||
for shape in self.shapes:
|
for shape in self.shapes:
|
||||||
if (shape.selected or not self._hideBackround) and self.isVisible(shape):
|
if (shape.selected or not self._hide_background) and self.isVisible(shape):
|
||||||
shape.fill = shape.selected or shape == self.hShape
|
shape.fill = shape.selected or shape == self.h_shape
|
||||||
shape.paint(p)
|
shape.paint(p)
|
||||||
if self.current:
|
if self.current:
|
||||||
self.current.paint(p)
|
self.current.paint(p)
|
||||||
self.line.paint(p)
|
self.line.paint(p)
|
||||||
if self.selectedShapeCopy:
|
if self.selected_shape_copy:
|
||||||
self.selectedShapeCopy.paint(p)
|
self.selected_shape_copy.paint(p)
|
||||||
|
|
||||||
# Paint rect
|
# Paint rect
|
||||||
if self.current is not None and len(self.line) == 2:
|
if self.current is not None and len(self.line) == 2:
|
||||||
leftTop = self.line[0]
|
left_top = self.line[0]
|
||||||
rightBottom = self.line[1]
|
right_bottom = self.line[1]
|
||||||
rectWidth = rightBottom.x() - leftTop.x()
|
rect_width = right_bottom.x() - left_top.x()
|
||||||
rectHeight = rightBottom.y() - leftTop.y()
|
rect_height = right_bottom.y() - left_top.y()
|
||||||
p.setPen(self.drawingRectColor)
|
p.setPen(self.drawing_rect_color)
|
||||||
brush = QBrush(Qt.BDiagPattern)
|
brush = QBrush(Qt.BDiagPattern)
|
||||||
p.setBrush(brush)
|
p.setBrush(brush)
|
||||||
p.drawRect(leftTop.x(), leftTop.y(), rectWidth, rectHeight)
|
p.drawRect(left_top.x(), left_top.y(), rect_width, rect_height)
|
||||||
|
|
||||||
if self.drawing() and not self.prevPoint.isNull() and not self.outOfPixmap(self.prevPoint):
|
if self.drawing() and not self.prev_point.isNull() and not self.out_of_pixmap(self.prev_point):
|
||||||
p.setPen(QColor(0, 0, 0))
|
p.setPen(QColor(0, 0, 0))
|
||||||
p.drawLine(self.prevPoint.x(), 0, self.prevPoint.x(), self.pixmap.height())
|
p.drawLine(self.prev_point.x(), 0, self.prev_point.x(), self.pixmap.height())
|
||||||
p.drawLine(0, self.prevPoint.y(), self.pixmap.width(), self.prevPoint.y())
|
p.drawLine(0, self.prev_point.y(), self.pixmap.width(), self.prev_point.y())
|
||||||
|
|
||||||
self.setAutoFillBackground(True)
|
self.setAutoFillBackground(True)
|
||||||
if self.verified:
|
if self.verified:
|
||||||
@ -518,11 +518,11 @@ class Canvas(QWidget):
|
|||||||
|
|
||||||
p.end()
|
p.end()
|
||||||
|
|
||||||
def transformPos(self, point):
|
def transform_pos(self, point):
|
||||||
"""Convert from widget-logical coordinates to painter-logical coordinates."""
|
"""Convert from widget-logical coordinates to painter-logical coordinates."""
|
||||||
return point / self.scale - self.offsetToCenter()
|
return point / self.scale - self.offset_to_center()
|
||||||
|
|
||||||
def offsetToCenter(self):
|
def offset_to_center(self):
|
||||||
s = self.scale
|
s = self.scale
|
||||||
area = super(Canvas, self).size()
|
area = super(Canvas, self).size()
|
||||||
w, h = self.pixmap.width() * s, self.pixmap.height() * s
|
w, h = self.pixmap.width() * s, self.pixmap.height() * s
|
||||||
@ -531,7 +531,7 @@ class Canvas(QWidget):
|
|||||||
y = (ah - h) / (2 * s) if ah > h else 0
|
y = (ah - h) / (2 * s) if ah > h else 0
|
||||||
return QPointF(x, y)
|
return QPointF(x, y)
|
||||||
|
|
||||||
def outOfPixmap(self, p):
|
def out_of_pixmap(self, p):
|
||||||
w, h = self.pixmap.width(), self.pixmap.height()
|
w, h = self.pixmap.width(), self.pixmap.height()
|
||||||
return not (0 <= p.x() <= w and 0 <= p.y() <= h)
|
return not (0 <= p.x() <= w and 0 <= p.y() <= h)
|
||||||
|
|
||||||
@ -546,11 +546,11 @@ class Canvas(QWidget):
|
|||||||
self.current.close()
|
self.current.close()
|
||||||
self.shapes.append(self.current)
|
self.shapes.append(self.current)
|
||||||
self.current = None
|
self.current = None
|
||||||
self.setHiding(False)
|
self.set_hiding(False)
|
||||||
self.newShape.emit()
|
self.newShape.emit()
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def closeEnough(self, p1, p2):
|
def close_enough(self, p1, p2):
|
||||||
# d = distance(p1 - p2)
|
# d = distance(p1 - p2)
|
||||||
# m = (p1-p2).manhattanLength()
|
# m = (p1-p2).manhattanLength()
|
||||||
# print "d %.2f, m %d, %.2f" % (d, m, d - m)
|
# print "d %.2f, m %d, %.2f" % (d, m, d - m)
|
||||||
@ -595,51 +595,51 @@ class Canvas(QWidget):
|
|||||||
self.current = None
|
self.current = None
|
||||||
self.drawingPolygon.emit(False)
|
self.drawingPolygon.emit(False)
|
||||||
self.update()
|
self.update()
|
||||||
elif key == Qt.Key_Return and self.canCloseShape():
|
elif key == Qt.Key_Return and self.can_close_shape():
|
||||||
self.finalise()
|
self.finalise()
|
||||||
elif key == Qt.Key_Left and self.selectedShape:
|
elif key == Qt.Key_Left and self.selected_shape:
|
||||||
self.moveOnePixel('Left')
|
self.move_one_pixel('Left')
|
||||||
elif key == Qt.Key_Right and self.selectedShape:
|
elif key == Qt.Key_Right and self.selected_shape:
|
||||||
self.moveOnePixel('Right')
|
self.move_one_pixel('Right')
|
||||||
elif key == Qt.Key_Up and self.selectedShape:
|
elif key == Qt.Key_Up and self.selected_shape:
|
||||||
self.moveOnePixel('Up')
|
self.move_one_pixel('Up')
|
||||||
elif key == Qt.Key_Down and self.selectedShape:
|
elif key == Qt.Key_Down and self.selected_shape:
|
||||||
self.moveOnePixel('Down')
|
self.move_one_pixel('Down')
|
||||||
|
|
||||||
def moveOnePixel(self, direction):
|
def move_one_pixel(self, direction):
|
||||||
# print(self.selectedShape.points)
|
# print(self.selectedShape.points)
|
||||||
if direction == 'Left' and not self.moveOutOfBound(QPointF(-1.0, 0)):
|
if direction == 'Left' and not self.move_out_of_bound(QPointF(-1.0, 0)):
|
||||||
# print("move Left one pixel")
|
# print("move Left one pixel")
|
||||||
self.selectedShape.points[0] += QPointF(-1.0, 0)
|
self.selected_shape.points[0] += QPointF(-1.0, 0)
|
||||||
self.selectedShape.points[1] += QPointF(-1.0, 0)
|
self.selected_shape.points[1] += QPointF(-1.0, 0)
|
||||||
self.selectedShape.points[2] += QPointF(-1.0, 0)
|
self.selected_shape.points[2] += QPointF(-1.0, 0)
|
||||||
self.selectedShape.points[3] += QPointF(-1.0, 0)
|
self.selected_shape.points[3] += QPointF(-1.0, 0)
|
||||||
elif direction == 'Right' and not self.moveOutOfBound(QPointF(1.0, 0)):
|
elif direction == 'Right' and not self.move_out_of_bound(QPointF(1.0, 0)):
|
||||||
# print("move Right one pixel")
|
# print("move Right one pixel")
|
||||||
self.selectedShape.points[0] += QPointF(1.0, 0)
|
self.selected_shape.points[0] += QPointF(1.0, 0)
|
||||||
self.selectedShape.points[1] += QPointF(1.0, 0)
|
self.selected_shape.points[1] += QPointF(1.0, 0)
|
||||||
self.selectedShape.points[2] += QPointF(1.0, 0)
|
self.selected_shape.points[2] += QPointF(1.0, 0)
|
||||||
self.selectedShape.points[3] += QPointF(1.0, 0)
|
self.selected_shape.points[3] += QPointF(1.0, 0)
|
||||||
elif direction == 'Up' and not self.moveOutOfBound(QPointF(0, -1.0)):
|
elif direction == 'Up' and not self.move_out_of_bound(QPointF(0, -1.0)):
|
||||||
# print("move Up one pixel")
|
# print("move Up one pixel")
|
||||||
self.selectedShape.points[0] += QPointF(0, -1.0)
|
self.selected_shape.points[0] += QPointF(0, -1.0)
|
||||||
self.selectedShape.points[1] += QPointF(0, -1.0)
|
self.selected_shape.points[1] += QPointF(0, -1.0)
|
||||||
self.selectedShape.points[2] += QPointF(0, -1.0)
|
self.selected_shape.points[2] += QPointF(0, -1.0)
|
||||||
self.selectedShape.points[3] += QPointF(0, -1.0)
|
self.selected_shape.points[3] += QPointF(0, -1.0)
|
||||||
elif direction == 'Down' and not self.moveOutOfBound(QPointF(0, 1.0)):
|
elif direction == 'Down' and not self.move_out_of_bound(QPointF(0, 1.0)):
|
||||||
# print("move Down one pixel")
|
# print("move Down one pixel")
|
||||||
self.selectedShape.points[0] += QPointF(0, 1.0)
|
self.selected_shape.points[0] += QPointF(0, 1.0)
|
||||||
self.selectedShape.points[1] += QPointF(0, 1.0)
|
self.selected_shape.points[1] += QPointF(0, 1.0)
|
||||||
self.selectedShape.points[2] += QPointF(0, 1.0)
|
self.selected_shape.points[2] += QPointF(0, 1.0)
|
||||||
self.selectedShape.points[3] += QPointF(0, 1.0)
|
self.selected_shape.points[3] += QPointF(0, 1.0)
|
||||||
self.shapeMoved.emit()
|
self.shapeMoved.emit()
|
||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def moveOutOfBound(self, step):
|
def move_out_of_bound(self, step):
|
||||||
points = [p1+p2 for p1, p2 in zip(self.selectedShape.points, [step]*4)]
|
points = [p1 + p2 for p1, p2 in zip(self.selected_shape.points, [step] * 4)]
|
||||||
return True in map(self.outOfPixmap, points)
|
return True in map(self.out_of_pixmap, points)
|
||||||
|
|
||||||
def setLastLabel(self, text, line_color = None, fill_color = None):
|
def set_last_label(self, text, line_color=None, fill_color=None):
|
||||||
assert text
|
assert text
|
||||||
self.shapes[-1].label = text
|
self.shapes[-1].label = text
|
||||||
if line_color:
|
if line_color:
|
||||||
@ -650,57 +650,57 @@ class Canvas(QWidget):
|
|||||||
|
|
||||||
return self.shapes[-1]
|
return self.shapes[-1]
|
||||||
|
|
||||||
def undoLastLine(self):
|
def undo_last_line(self):
|
||||||
assert self.shapes
|
assert self.shapes
|
||||||
self.current = self.shapes.pop()
|
self.current = self.shapes.pop()
|
||||||
self.current.setOpen()
|
self.current.set_open()
|
||||||
self.line.points = [self.current[-1], self.current[0]]
|
self.line.points = [self.current[-1], self.current[0]]
|
||||||
self.drawingPolygon.emit(True)
|
self.drawingPolygon.emit(True)
|
||||||
|
|
||||||
def resetAllLines(self):
|
def reset_all_lines(self):
|
||||||
assert self.shapes
|
assert self.shapes
|
||||||
self.current = self.shapes.pop()
|
self.current = self.shapes.pop()
|
||||||
self.current.setOpen()
|
self.current.set_open()
|
||||||
self.line.points = [self.current[-1], self.current[0]]
|
self.line.points = [self.current[-1], self.current[0]]
|
||||||
self.drawingPolygon.emit(True)
|
self.drawingPolygon.emit(True)
|
||||||
self.current = None
|
self.current = None
|
||||||
self.drawingPolygon.emit(False)
|
self.drawingPolygon.emit(False)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def loadPixmap(self, pixmap):
|
def load_pixmap(self, pixmap):
|
||||||
self.pixmap = pixmap
|
self.pixmap = pixmap
|
||||||
self.shapes = []
|
self.shapes = []
|
||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def loadShapes(self, shapes):
|
def load_shapes(self, shapes):
|
||||||
self.shapes = list(shapes)
|
self.shapes = list(shapes)
|
||||||
self.current = None
|
self.current = None
|
||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def setShapeVisible(self, shape, value):
|
def set_shape_visible(self, shape, value):
|
||||||
self.visible[shape] = value
|
self.visible[shape] = value
|
||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def currentCursor(self):
|
def current_cursor(self):
|
||||||
cursor = QApplication.overrideCursor()
|
cursor = QApplication.overrideCursor()
|
||||||
if cursor is not None:
|
if cursor is not None:
|
||||||
cursor = cursor.shape()
|
cursor = cursor.shape()
|
||||||
return cursor
|
return cursor
|
||||||
|
|
||||||
def overrideCursor(self, cursor):
|
def override_cursor(self, cursor):
|
||||||
self._cursor = cursor
|
self._cursor = cursor
|
||||||
if self.currentCursor() is None:
|
if self.current_cursor() is None:
|
||||||
QApplication.setOverrideCursor(cursor)
|
QApplication.setOverrideCursor(cursor)
|
||||||
else:
|
else:
|
||||||
QApplication.changeOverrideCursor(cursor)
|
QApplication.changeOverrideCursor(cursor)
|
||||||
|
|
||||||
def restoreCursor(self):
|
def restore_cursor(self):
|
||||||
QApplication.restoreOverrideCursor()
|
QApplication.restoreOverrideCursor()
|
||||||
|
|
||||||
def resetState(self):
|
def reset_state(self):
|
||||||
self.restoreCursor()
|
self.restore_cursor()
|
||||||
self.pixmap = None
|
self.pixmap = None
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def setDrawingShapeToSquare(self, status):
|
def set_drawing_shape_to_square(self, status):
|
||||||
self.drawSquare = status
|
self.draw_square = status
|
||||||
|
|||||||
@ -22,7 +22,7 @@ class ColorDialog(QColorDialog):
|
|||||||
self.default = None
|
self.default = None
|
||||||
self.bb = self.layout().itemAt(1).widget()
|
self.bb = self.layout().itemAt(1).widget()
|
||||||
self.bb.addButton(BB.RestoreDefaults)
|
self.bb.addButton(BB.RestoreDefaults)
|
||||||
self.bb.clicked.connect(self.checkRestore)
|
self.bb.clicked.connect(self.check_restore)
|
||||||
|
|
||||||
def getColor(self, value=None, title=None, default=None):
|
def getColor(self, value=None, title=None, default=None):
|
||||||
self.default = default
|
self.default = default
|
||||||
@ -32,6 +32,6 @@ class ColorDialog(QColorDialog):
|
|||||||
self.setCurrentColor(value)
|
self.setCurrentColor(value)
|
||||||
return self.currentColor() if self.exec_() else None
|
return self.currentColor() if self.exec_() else None
|
||||||
|
|
||||||
def checkRestore(self, button):
|
def check_restore(self, button):
|
||||||
if self.bb.buttonRole(button) & BB.ResetRole and self.default:
|
if self.bb.buttonRole(button) & BB.ResetRole and self.default:
|
||||||
self.setCurrentColor(self.default)
|
self.setCurrentColor(self.default)
|
||||||
|
|||||||
@ -21,7 +21,7 @@ class ComboBox(QWidget):
|
|||||||
self.items = items
|
self.items = items
|
||||||
self.cb.addItems(self.items)
|
self.cb.addItems(self.items)
|
||||||
|
|
||||||
self.cb.currentIndexChanged.connect(parent.comboSelectionChanged)
|
self.cb.currentIndexChanged.connect(parent.combo_selection_changed)
|
||||||
|
|
||||||
layout.addWidget(self.cb)
|
layout.addWidget(self.cb)
|
||||||
self.setLayout(layout)
|
self.setLayout(layout)
|
||||||
|
|||||||
@ -11,26 +11,26 @@ ENCODE_METHOD = DEFAULT_ENCODING
|
|||||||
|
|
||||||
|
|
||||||
class CreateMLWriter:
|
class CreateMLWriter:
|
||||||
def __init__(self, foldername, filename, imgsize, shapes, outputfile, databasesrc='Unknown', localimgpath=None):
|
def __init__(self, folder_name, filename, img_size, shapes, output_file, database_src='Unknown', local_img_path=None):
|
||||||
self.foldername = foldername
|
self.folder_name = folder_name
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.databasesrc = databasesrc
|
self.database_src = database_src
|
||||||
self.imgsize = imgsize
|
self.img_size = img_size
|
||||||
self.boxlist = []
|
self.box_list = []
|
||||||
self.localimgpath = localimgpath
|
self.local_img_path = local_img_path
|
||||||
self.verified = False
|
self.verified = False
|
||||||
self.shapes = shapes
|
self.shapes = shapes
|
||||||
self.outputfile = outputfile
|
self.output_file = output_file
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
if os.path.isfile(self.outputfile):
|
if os.path.isfile(self.output_file):
|
||||||
with open(self.outputfile, "r") as file:
|
with open(self.output_file, "r") as file:
|
||||||
input_data = file.read()
|
input_data = file.read()
|
||||||
outputdict = json.loads(input_data)
|
output_dict = json.loads(input_data)
|
||||||
else:
|
else:
|
||||||
outputdict = []
|
output_dict = []
|
||||||
|
|
||||||
outputimagedict = {
|
output_image_dict = {
|
||||||
"image": self.filename,
|
"image": self.filename,
|
||||||
"annotations": []
|
"annotations": []
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ class CreateMLWriter:
|
|||||||
|
|
||||||
height, width, x, y = self.calculate_coordinates(x1, x2, y1, y2)
|
height, width, x, y = self.calculate_coordinates(x1, x2, y1, y2)
|
||||||
|
|
||||||
shapedict = {
|
shape_dict = {
|
||||||
"label": shape["label"],
|
"label": shape["label"],
|
||||||
"coordinates": {
|
"coordinates": {
|
||||||
"x": x,
|
"x": x,
|
||||||
@ -54,77 +54,77 @@ class CreateMLWriter:
|
|||||||
"height": height
|
"height": height
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputimagedict["annotations"].append(shapedict)
|
output_image_dict["annotations"].append(shape_dict)
|
||||||
|
|
||||||
# check if image already in output
|
# check if image already in output
|
||||||
exists = False
|
exists = False
|
||||||
for i in range(0, len(outputdict)):
|
for i in range(0, len(output_dict)):
|
||||||
if outputdict[i]["image"] == outputimagedict["image"]:
|
if output_dict[i]["image"] == output_image_dict["image"]:
|
||||||
exists = True
|
exists = True
|
||||||
outputdict[i] = outputimagedict
|
output_dict[i] = output_image_dict
|
||||||
break
|
break
|
||||||
|
|
||||||
if not exists:
|
if not exists:
|
||||||
outputdict.append(outputimagedict)
|
output_dict.append(output_image_dict)
|
||||||
|
|
||||||
Path(self.outputfile).write_text(json.dumps(outputdict), ENCODE_METHOD)
|
Path(self.output_file).write_text(json.dumps(output_dict), ENCODE_METHOD)
|
||||||
|
|
||||||
def calculate_coordinates(self, x1, x2, y1, y2):
|
def calculate_coordinates(self, x1, x2, y1, y2):
|
||||||
if x1 < x2:
|
if x1 < x2:
|
||||||
xmin = x1
|
x_min = x1
|
||||||
xmax = x2
|
x_max = x2
|
||||||
else:
|
else:
|
||||||
xmin = x2
|
x_min = x2
|
||||||
xmax = x1
|
x_max = x1
|
||||||
if y1 < y2:
|
if y1 < y2:
|
||||||
ymin = y1
|
y_min = y1
|
||||||
ymax = y2
|
y_max = y2
|
||||||
else:
|
else:
|
||||||
ymin = y2
|
y_min = y2
|
||||||
ymax = y1
|
y_max = y1
|
||||||
width = xmax - xmin
|
width = x_max - x_min
|
||||||
if width < 0:
|
if width < 0:
|
||||||
width = width * -1
|
width = width * -1
|
||||||
height = ymax - ymin
|
height = y_max - y_min
|
||||||
# x and y from center of rect
|
# x and y from center of rect
|
||||||
x = xmin + width / 2
|
x = x_min + width / 2
|
||||||
y = ymin + height / 2
|
y = y_min + height / 2
|
||||||
return height, width, x, y
|
return height, width, x, y
|
||||||
|
|
||||||
|
|
||||||
class CreateMLReader:
|
class CreateMLReader:
|
||||||
def __init__(self, jsonpath, filepath):
|
def __init__(self, json_path, file_path):
|
||||||
self.jsonpath = jsonpath
|
self.json_path = json_path
|
||||||
self.shapes = []
|
self.shapes = []
|
||||||
self.verified = False
|
self.verified = False
|
||||||
self.filename = filepath.split("/")[-1:][0]
|
self.filename = file_path.split("/")[-1:][0]
|
||||||
try:
|
try:
|
||||||
self.parse_json()
|
self.parse_json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("JSON decoding failed")
|
print("JSON decoding failed")
|
||||||
|
|
||||||
def parse_json(self):
|
def parse_json(self):
|
||||||
with open(self.jsonpath, "r") as file:
|
with open(self.json_path, "r") as file:
|
||||||
inputdata = file.read()
|
input_data = file.read()
|
||||||
|
|
||||||
outputdict = json.loads(inputdata)
|
output_dict = json.loads(input_data)
|
||||||
self.verified = True
|
self.verified = True
|
||||||
|
|
||||||
if len(self.shapes) > 0:
|
if len(self.shapes) > 0:
|
||||||
self.shapes = []
|
self.shapes = []
|
||||||
for image in outputdict:
|
for image in output_dict:
|
||||||
if image["image"] == self.filename:
|
if image["image"] == self.filename:
|
||||||
for shape in image["annotations"]:
|
for shape in image["annotations"]:
|
||||||
self.add_shape(shape["label"], shape["coordinates"])
|
self.add_shape(shape["label"], shape["coordinates"])
|
||||||
|
|
||||||
def add_shape(self, label, bndbox):
|
def add_shape(self, label, bnd_box):
|
||||||
xmin = bndbox["x"] - (bndbox["width"] / 2)
|
x_min = bnd_box["x"] - (bnd_box["width"] / 2)
|
||||||
ymin = bndbox["y"] - (bndbox["height"] / 2)
|
y_min = bnd_box["y"] - (bnd_box["height"] / 2)
|
||||||
|
|
||||||
xmax = bndbox["x"] + (bndbox["width"] / 2)
|
x_max = bnd_box["x"] + (bnd_box["width"] / 2)
|
||||||
ymax = bndbox["y"] + (bndbox["height"] / 2)
|
y_max = bnd_box["y"] + (bnd_box["height"] / 2)
|
||||||
|
|
||||||
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]
|
points = [(x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)]
|
||||||
self.shapes.append((label, points, None, None, True))
|
self.shapes.append((label, points, None, None, True))
|
||||||
|
|
||||||
def get_shapes(self):
|
def get_shapes(self):
|
||||||
|
|||||||
@ -6,43 +6,43 @@ except ImportError:
|
|||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
|
|
||||||
from libs.utils import newIcon, labelValidator
|
from libs.utils import new_icon, label_validator
|
||||||
|
|
||||||
BB = QDialogButtonBox
|
BB = QDialogButtonBox
|
||||||
|
|
||||||
|
|
||||||
class LabelDialog(QDialog):
|
class LabelDialog(QDialog):
|
||||||
|
|
||||||
def __init__(self, text="Enter object label", parent=None, listItem=None):
|
def __init__(self, text="Enter object label", parent=None, list_item=None):
|
||||||
super(LabelDialog, self).__init__(parent)
|
super(LabelDialog, self).__init__(parent)
|
||||||
|
|
||||||
self.edit = QLineEdit()
|
self.edit = QLineEdit()
|
||||||
self.edit.setText(text)
|
self.edit.setText(text)
|
||||||
self.edit.setValidator(labelValidator())
|
self.edit.setValidator(label_validator())
|
||||||
self.edit.editingFinished.connect(self.postProcess)
|
self.edit.editingFinished.connect(self.post_process)
|
||||||
|
|
||||||
model = QStringListModel()
|
model = QStringListModel()
|
||||||
model.setStringList(listItem)
|
model.setStringList(list_item)
|
||||||
completer = QCompleter()
|
completer = QCompleter()
|
||||||
completer.setModel(model)
|
completer.setModel(model)
|
||||||
self.edit.setCompleter(completer)
|
self.edit.setCompleter(completer)
|
||||||
|
|
||||||
layout = QVBoxLayout()
|
layout = QVBoxLayout()
|
||||||
layout.addWidget(self.edit)
|
layout.addWidget(self.edit)
|
||||||
self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
|
self.button_box = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
|
||||||
bb.button(BB.Ok).setIcon(newIcon('done'))
|
bb.button(BB.Ok).setIcon(new_icon('done'))
|
||||||
bb.button(BB.Cancel).setIcon(newIcon('undo'))
|
bb.button(BB.Cancel).setIcon(new_icon('undo'))
|
||||||
bb.accepted.connect(self.validate)
|
bb.accepted.connect(self.validate)
|
||||||
bb.rejected.connect(self.reject)
|
bb.rejected.connect(self.reject)
|
||||||
layout.addWidget(bb)
|
layout.addWidget(bb)
|
||||||
|
|
||||||
if listItem is not None and len(listItem) > 0:
|
if list_item is not None and len(list_item) > 0:
|
||||||
self.listWidget = QListWidget(self)
|
self.list_widget = QListWidget(self)
|
||||||
for item in listItem:
|
for item in list_item:
|
||||||
self.listWidget.addItem(item)
|
self.list_widget.addItem(item)
|
||||||
self.listWidget.itemClicked.connect(self.listItemClick)
|
self.list_widget.itemClicked.connect(self.list_item_click)
|
||||||
self.listWidget.itemDoubleClicked.connect(self.listItemDoubleClick)
|
self.list_widget.itemDoubleClicked.connect(self.list_item_double_click)
|
||||||
layout.addWidget(self.listWidget)
|
layout.addWidget(self.list_widget)
|
||||||
|
|
||||||
self.setLayout(layout)
|
self.setLayout(layout)
|
||||||
|
|
||||||
@ -55,22 +55,22 @@ class LabelDialog(QDialog):
|
|||||||
if self.edit.text().strip():
|
if self.edit.text().strip():
|
||||||
self.accept()
|
self.accept()
|
||||||
|
|
||||||
def postProcess(self):
|
def post_process(self):
|
||||||
try:
|
try:
|
||||||
self.edit.setText(self.edit.text().trimmed())
|
self.edit.setText(self.edit.text().trimmed())
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
|
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
|
||||||
self.edit.setText(self.edit.text())
|
self.edit.setText(self.edit.text())
|
||||||
|
|
||||||
def popUp(self, text='', move=True):
|
def pop_up(self, text='', move=True):
|
||||||
self.edit.setText(text)
|
self.edit.setText(text)
|
||||||
self.edit.setSelection(0, len(text))
|
self.edit.setSelection(0, len(text))
|
||||||
self.edit.setFocus(Qt.PopupFocusReason)
|
self.edit.setFocus(Qt.PopupFocusReason)
|
||||||
if move:
|
if move:
|
||||||
cursor_pos = QCursor.pos()
|
cursor_pos = QCursor.pos()
|
||||||
parent_bottomRight = self.parentWidget().geometry()
|
parent_bottom_right = self.parentWidget().geometry()
|
||||||
max_x = parent_bottomRight.x() + parent_bottomRight.width() - self.sizeHint().width()
|
max_x = parent_bottom_right.x() + parent_bottom_right.width() - self.sizeHint().width()
|
||||||
max_y = parent_bottomRight.y() + parent_bottomRight.height() - self.sizeHint().height()
|
max_y = parent_bottom_right.y() + parent_bottom_right.height() - self.sizeHint().height()
|
||||||
max_global = self.parentWidget().mapToGlobal(QPoint(max_x, max_y))
|
max_global = self.parentWidget().mapToGlobal(QPoint(max_x, max_y))
|
||||||
if cursor_pos.x() > max_global.x():
|
if cursor_pos.x() > max_global.x():
|
||||||
cursor_pos.setX(max_global.x())
|
cursor_pos.setX(max_global.x())
|
||||||
@ -79,14 +79,14 @@ class LabelDialog(QDialog):
|
|||||||
self.move(cursor_pos)
|
self.move(cursor_pos)
|
||||||
return self.edit.text() if self.exec_() else None
|
return self.edit.text() if self.exec_() else None
|
||||||
|
|
||||||
def listItemClick(self, tQListWidgetItem):
|
def list_item_click(self, t_qlist_widget_item):
|
||||||
try:
|
try:
|
||||||
text = tQListWidgetItem.text().trimmed()
|
text = t_qlist_widget_item.text().trimmed()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
|
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
|
||||||
text = tQListWidgetItem.text().strip()
|
text = t_qlist_widget_item.text().strip()
|
||||||
self.edit.setText(text)
|
self.edit.setText(text)
|
||||||
|
|
||||||
def listItemDoubleClick(self, tQListWidgetItem):
|
def list_item_double_click(self, t_qlist_widget_item):
|
||||||
self.listItemClick(tQListWidgetItem)
|
self.list_item_click(t_qlist_widget_item)
|
||||||
self.validate()
|
self.validate()
|
||||||
|
|||||||
@ -34,44 +34,44 @@ class LabelFile(object):
|
|||||||
|
|
||||||
def __init__(self, filename=None):
|
def __init__(self, filename=None):
|
||||||
self.shapes = ()
|
self.shapes = ()
|
||||||
self.imagePath = None
|
self.image_path = None
|
||||||
self.imageData = None
|
self.image_data = None
|
||||||
self.verified = False
|
self.verified = False
|
||||||
|
|
||||||
def saveCreateMLFormat(self, filename, shapes, imagePath, imageData, classList, lineColor=None, fillColor=None, databaseSrc=None):
|
def save_create_ml_format(self, filename, shapes, image_path, image_data, class_list, line_color=None, fill_color=None, database_src=None):
|
||||||
imgFolderPath = os.path.dirname(imagePath)
|
img_folder_path = os.path.dirname(image_path)
|
||||||
imgFolderName = os.path.split(imgFolderPath)[-1]
|
img_folder_name = os.path.split(img_folder_path)[-1]
|
||||||
imgFileName = os.path.basename(imagePath)
|
img_file_name = os.path.basename(image_path)
|
||||||
outputFilePath = "/".join(filename.split("/")[:-1])
|
output_file_path = "/".join(filename.split("/")[:-1])
|
||||||
outputFile = outputFilePath + "/" + imgFolderName + JSON_EXT
|
output_file = output_file_path + "/" + img_folder_name + JSON_EXT
|
||||||
|
|
||||||
image = QImage()
|
image = QImage()
|
||||||
image.load(imagePath)
|
image.load(image_path)
|
||||||
imageShape = [image.height(), image.width(),
|
image_shape = [image.height(), image.width(),
|
||||||
1 if image.isGrayscale() else 3]
|
1 if image.isGrayscale() else 3]
|
||||||
writer = CreateMLWriter(imgFolderName, imgFileName,
|
writer = CreateMLWriter(img_folder_name, img_file_name,
|
||||||
imageShape, shapes, outputFile, localimgpath=imagePath)
|
image_shape, shapes, output_file, local_img_path=image_path)
|
||||||
writer.verified = self.verified
|
writer.verified = self.verified
|
||||||
writer.write()
|
writer.write()
|
||||||
|
|
||||||
|
|
||||||
def savePascalVocFormat(self, filename, shapes, imagePath, imageData,
|
def save_pascal_voc_format(self, filename, shapes, image_path, image_data,
|
||||||
lineColor=None, fillColor=None, databaseSrc=None):
|
line_color=None, fill_color=None, database_src=None):
|
||||||
imgFolderPath = os.path.dirname(imagePath)
|
img_folder_path = os.path.dirname(image_path)
|
||||||
imgFolderName = os.path.split(imgFolderPath)[-1]
|
img_folder_name = os.path.split(img_folder_path)[-1]
|
||||||
imgFileName = os.path.basename(imagePath)
|
img_file_name = os.path.basename(image_path)
|
||||||
#imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
|
# imgFileNameWithoutExt = os.path.splitext(img_file_name)[0]
|
||||||
# Read from file path because self.imageData might be empty if saving to
|
# Read from file path because self.imageData might be empty if saving to
|
||||||
# Pascal format
|
# Pascal format
|
||||||
if isinstance(imageData, QImage):
|
if isinstance(image_data, QImage):
|
||||||
image = imageData
|
image = image_data
|
||||||
else:
|
else:
|
||||||
image = QImage()
|
image = QImage()
|
||||||
image.load(imagePath)
|
image.load(image_path)
|
||||||
imageShape = [image.height(), image.width(),
|
image_shape = [image.height(), image.width(),
|
||||||
1 if image.isGrayscale() else 3]
|
1 if image.isGrayscale() else 3]
|
||||||
writer = PascalVocWriter(imgFolderName, imgFileName,
|
writer = PascalVocWriter(img_folder_name, img_file_name,
|
||||||
imageShape, localImgPath=imagePath)
|
image_shape, local_img_path=image_path)
|
||||||
writer.verified = self.verified
|
writer.verified = self.verified
|
||||||
|
|
||||||
for shape in shapes:
|
for shape in shapes:
|
||||||
@ -79,29 +79,29 @@ class LabelFile(object):
|
|||||||
label = shape['label']
|
label = shape['label']
|
||||||
# Add Chris
|
# Add Chris
|
||||||
difficult = int(shape['difficult'])
|
difficult = int(shape['difficult'])
|
||||||
bndbox = LabelFile.convertPoints2BndBox(points)
|
bnd_box = LabelFile.convert_points_to_bnd_box(points)
|
||||||
writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult)
|
writer.add_bnd_box(bnd_box[0], bnd_box[1], bnd_box[2], bnd_box[3], label, difficult)
|
||||||
|
|
||||||
writer.save(targetFile=filename)
|
writer.save(target_file=filename)
|
||||||
return
|
return
|
||||||
|
|
||||||
def saveYoloFormat(self, filename, shapes, imagePath, imageData, classList,
|
def save_yolo_format(self, filename, shapes, image_path, image_data, class_list,
|
||||||
lineColor=None, fillColor=None, databaseSrc=None):
|
line_color=None, fill_color=None, database_src=None):
|
||||||
imgFolderPath = os.path.dirname(imagePath)
|
img_folder_path = os.path.dirname(image_path)
|
||||||
imgFolderName = os.path.split(imgFolderPath)[-1]
|
img_folder_name = os.path.split(img_folder_path)[-1]
|
||||||
imgFileName = os.path.basename(imagePath)
|
img_file_name = os.path.basename(image_path)
|
||||||
#imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
|
# imgFileNameWithoutExt = os.path.splitext(img_file_name)[0]
|
||||||
# Read from file path because self.imageData might be empty if saving to
|
# Read from file path because self.imageData might be empty if saving to
|
||||||
# Pascal format
|
# Pascal format
|
||||||
if isinstance(imageData, QImage):
|
if isinstance(image_data, QImage):
|
||||||
image = imageData
|
image = image_data
|
||||||
else:
|
else:
|
||||||
image = QImage()
|
image = QImage()
|
||||||
image.load(imagePath)
|
image.load(image_path)
|
||||||
imageShape = [image.height(), image.width(),
|
image_shape = [image.height(), image.width(),
|
||||||
1 if image.isGrayscale() else 3]
|
1 if image.isGrayscale() else 3]
|
||||||
writer = YOLOWriter(imgFolderName, imgFileName,
|
writer = YOLOWriter(img_folder_name, img_file_name,
|
||||||
imageShape, localImgPath=imagePath)
|
image_shape, local_img_path=image_path)
|
||||||
writer.verified = self.verified
|
writer.verified = self.verified
|
||||||
|
|
||||||
for shape in shapes:
|
for shape in shapes:
|
||||||
@ -109,13 +109,13 @@ class LabelFile(object):
|
|||||||
label = shape['label']
|
label = shape['label']
|
||||||
# Add Chris
|
# Add Chris
|
||||||
difficult = int(shape['difficult'])
|
difficult = int(shape['difficult'])
|
||||||
bndbox = LabelFile.convertPoints2BndBox(points)
|
bnd_box = LabelFile.convert_points_to_bnd_box(points)
|
||||||
writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult)
|
writer.add_bnd_box(bnd_box[0], bnd_box[1], bnd_box[2], bnd_box[3], label, difficult)
|
||||||
|
|
||||||
writer.save(targetFile=filename, classList=classList)
|
writer.save(target_file=filename, class_list=class_list)
|
||||||
return
|
return
|
||||||
|
|
||||||
def toggleVerify(self):
|
def toggle_verify(self):
|
||||||
self.verified = not self.verified
|
self.verified = not self.verified
|
||||||
|
|
||||||
''' ttf is disable
|
''' ttf is disable
|
||||||
@ -148,31 +148,31 @@ class LabelFile(object):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def isLabelFile(filename):
|
def is_label_file(filename):
|
||||||
fileSuffix = os.path.splitext(filename)[1].lower()
|
file_suffix = os.path.splitext(filename)[1].lower()
|
||||||
return fileSuffix == LabelFile.suffix
|
return file_suffix == LabelFile.suffix
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def convertPoints2BndBox(points):
|
def convert_points_to_bnd_box(points):
|
||||||
xmin = float('inf')
|
x_min = float('inf')
|
||||||
ymin = float('inf')
|
y_min = float('inf')
|
||||||
xmax = float('-inf')
|
x_max = float('-inf')
|
||||||
ymax = float('-inf')
|
y_max = float('-inf')
|
||||||
for p in points:
|
for p in points:
|
||||||
x = p[0]
|
x = p[0]
|
||||||
y = p[1]
|
y = p[1]
|
||||||
xmin = min(x, xmin)
|
x_min = min(x, x_min)
|
||||||
ymin = min(y, ymin)
|
y_min = min(y, y_min)
|
||||||
xmax = max(x, xmax)
|
x_max = max(x, x_max)
|
||||||
ymax = max(y, ymax)
|
y_max = max(y, y_max)
|
||||||
|
|
||||||
# Martin Kersner, 2015/11/12
|
# Martin Kersner, 2015/11/12
|
||||||
# 0-valued coordinates of BB caused an error while
|
# 0-valued coordinates of BB caused an error while
|
||||||
# training faster-rcnn object detector.
|
# training faster-rcnn object detector.
|
||||||
if xmin < 1:
|
if x_min < 1:
|
||||||
xmin = 1
|
x_min = 1
|
||||||
|
|
||||||
if ymin < 1:
|
if y_min < 1:
|
||||||
ymin = 1
|
y_min = 1
|
||||||
|
|
||||||
return (int(xmin), int(ymin), int(xmax), int(ymax))
|
return int(x_min), int(y_min), int(x_max), int(y_max)
|
||||||
|
|||||||
@ -14,13 +14,13 @@ ENCODE_METHOD = DEFAULT_ENCODING
|
|||||||
|
|
||||||
class PascalVocWriter:
|
class PascalVocWriter:
|
||||||
|
|
||||||
def __init__(self, foldername, filename, imgSize,databaseSrc='Unknown', localImgPath=None):
|
def __init__(self, folder_name, filename, img_size, database_src='Unknown', local_img_path=None):
|
||||||
self.foldername = foldername
|
self.folder_name = folder_name
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.databaseSrc = databaseSrc
|
self.database_src = database_src
|
||||||
self.imgSize = imgSize
|
self.img_size = img_size
|
||||||
self.boxlist = []
|
self.box_list = []
|
||||||
self.localImgPath = localImgPath
|
self.local_img_path = local_img_path
|
||||||
self.verified = False
|
self.verified = False
|
||||||
|
|
||||||
def prettify(self, elem):
|
def prettify(self, elem):
|
||||||
@ -31,17 +31,17 @@ class PascalVocWriter:
|
|||||||
root = etree.fromstring(rough_string)
|
root = etree.fromstring(rough_string)
|
||||||
return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode())
|
return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode())
|
||||||
# minidom does not support UTF-8
|
# minidom does not support UTF-8
|
||||||
'''reparsed = minidom.parseString(rough_string)
|
# reparsed = minidom.parseString(rough_string)
|
||||||
return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)'''
|
# return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)
|
||||||
|
|
||||||
def genXML(self):
|
def gen_xml(self):
|
||||||
"""
|
"""
|
||||||
Return XML root
|
Return XML root
|
||||||
"""
|
"""
|
||||||
# Check conditions
|
# Check conditions
|
||||||
if self.filename is None or \
|
if self.filename is None or \
|
||||||
self.foldername is None or \
|
self.folder_name is None or \
|
||||||
self.imgSize is None:
|
self.img_size is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
top = Element('annotation')
|
top = Element('annotation')
|
||||||
@ -49,27 +49,27 @@ class PascalVocWriter:
|
|||||||
top.set('verified', 'yes')
|
top.set('verified', 'yes')
|
||||||
|
|
||||||
folder = SubElement(top, 'folder')
|
folder = SubElement(top, 'folder')
|
||||||
folder.text = self.foldername
|
folder.text = self.folder_name
|
||||||
|
|
||||||
filename = SubElement(top, 'filename')
|
filename = SubElement(top, 'filename')
|
||||||
filename.text = self.filename
|
filename.text = self.filename
|
||||||
|
|
||||||
if self.localImgPath is not None:
|
if self.local_img_path is not None:
|
||||||
localImgPath = SubElement(top, 'path')
|
local_img_path = SubElement(top, 'path')
|
||||||
localImgPath.text = self.localImgPath
|
local_img_path.text = self.local_img_path
|
||||||
|
|
||||||
source = SubElement(top, 'source')
|
source = SubElement(top, 'source')
|
||||||
database = SubElement(source, 'database')
|
database = SubElement(source, 'database')
|
||||||
database.text = self.databaseSrc
|
database.text = self.database_src
|
||||||
|
|
||||||
size_part = SubElement(top, 'size')
|
size_part = SubElement(top, 'size')
|
||||||
width = SubElement(size_part, 'width')
|
width = SubElement(size_part, 'width')
|
||||||
height = SubElement(size_part, 'height')
|
height = SubElement(size_part, 'height')
|
||||||
depth = SubElement(size_part, 'depth')
|
depth = SubElement(size_part, 'depth')
|
||||||
width.text = str(self.imgSize[1])
|
width.text = str(self.img_size[1])
|
||||||
height.text = str(self.imgSize[0])
|
height.text = str(self.img_size[0])
|
||||||
if len(self.imgSize) == 3:
|
if len(self.img_size) == 3:
|
||||||
depth.text = str(self.imgSize[2])
|
depth.text = str(self.img_size[2])
|
||||||
else:
|
else:
|
||||||
depth.text = '1'
|
depth.text = '1'
|
||||||
|
|
||||||
@ -77,95 +77,95 @@ class PascalVocWriter:
|
|||||||
segmented.text = '0'
|
segmented.text = '0'
|
||||||
return top
|
return top
|
||||||
|
|
||||||
def addBndBox(self, xmin, ymin, xmax, ymax, name, difficult):
|
def add_bnd_box(self, x_min, y_min, x_max, y_max, name, difficult):
|
||||||
bndbox = {'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax}
|
bnd_box = {'xmin': x_min, 'ymin': y_min, 'xmax': x_max, 'ymax': y_max}
|
||||||
bndbox['name'] = name
|
bnd_box['name'] = name
|
||||||
bndbox['difficult'] = difficult
|
bnd_box['difficult'] = difficult
|
||||||
self.boxlist.append(bndbox)
|
self.box_list.append(bnd_box)
|
||||||
|
|
||||||
def appendObjects(self, top):
|
def append_objects(self, top):
|
||||||
for each_object in self.boxlist:
|
for each_object in self.box_list:
|
||||||
object_item = SubElement(top, 'object')
|
object_item = SubElement(top, 'object')
|
||||||
name = SubElement(object_item, 'name')
|
name = SubElement(object_item, 'name')
|
||||||
name.text = ustr(each_object['name'])
|
name.text = ustr(each_object['name'])
|
||||||
pose = SubElement(object_item, 'pose')
|
pose = SubElement(object_item, 'pose')
|
||||||
pose.text = "Unspecified"
|
pose.text = "Unspecified"
|
||||||
truncated = SubElement(object_item, 'truncated')
|
truncated = SubElement(object_item, 'truncated')
|
||||||
if int(float(each_object['ymax'])) == int(float(self.imgSize[0])) or (int(float(each_object['ymin']))== 1):
|
if int(float(each_object['ymax'])) == int(float(self.img_size[0])) or (int(float(each_object['ymin'])) == 1):
|
||||||
truncated.text = "1" # max == height or min
|
truncated.text = "1" # max == height or min
|
||||||
elif (int(float(each_object['xmax']))==int(float(self.imgSize[1]))) or (int(float(each_object['xmin']))== 1):
|
elif (int(float(each_object['xmax'])) == int(float(self.img_size[1]))) or (int(float(each_object['xmin'])) == 1):
|
||||||
truncated.text = "1" # max == width or min
|
truncated.text = "1" # max == width or min
|
||||||
else:
|
else:
|
||||||
truncated.text = "0"
|
truncated.text = "0"
|
||||||
difficult = SubElement(object_item, 'difficult')
|
difficult = SubElement(object_item, 'difficult')
|
||||||
difficult.text = str(bool(each_object['difficult']) & 1)
|
difficult.text = str(bool(each_object['difficult']) & 1)
|
||||||
bndbox = SubElement(object_item, 'bndbox')
|
bnd_box = SubElement(object_item, 'bndbox')
|
||||||
xmin = SubElement(bndbox, 'xmin')
|
x_min = SubElement(bnd_box, 'xmin')
|
||||||
xmin.text = str(each_object['xmin'])
|
x_min.text = str(each_object['xmin'])
|
||||||
ymin = SubElement(bndbox, 'ymin')
|
y_min = SubElement(bnd_box, 'ymin')
|
||||||
ymin.text = str(each_object['ymin'])
|
y_min.text = str(each_object['ymin'])
|
||||||
xmax = SubElement(bndbox, 'xmax')
|
x_max = SubElement(bnd_box, 'xmax')
|
||||||
xmax.text = str(each_object['xmax'])
|
x_max.text = str(each_object['xmax'])
|
||||||
ymax = SubElement(bndbox, 'ymax')
|
y_max = SubElement(bnd_box, 'ymax')
|
||||||
ymax.text = str(each_object['ymax'])
|
y_max.text = str(each_object['ymax'])
|
||||||
|
|
||||||
def save(self, targetFile=None):
|
def save(self, target_file=None):
|
||||||
root = self.genXML()
|
root = self.gen_xml()
|
||||||
self.appendObjects(root)
|
self.append_objects(root)
|
||||||
out_file = None
|
out_file = None
|
||||||
if targetFile is None:
|
if target_file is None:
|
||||||
out_file = codecs.open(
|
out_file = codecs.open(
|
||||||
self.filename + XML_EXT, 'w', encoding=ENCODE_METHOD)
|
self.filename + XML_EXT, 'w', encoding=ENCODE_METHOD)
|
||||||
else:
|
else:
|
||||||
out_file = codecs.open(targetFile, 'w', encoding=ENCODE_METHOD)
|
out_file = codecs.open(target_file, 'w', encoding=ENCODE_METHOD)
|
||||||
|
|
||||||
prettifyResult = self.prettify(root)
|
prettify_result = self.prettify(root)
|
||||||
out_file.write(prettifyResult.decode('utf8'))
|
out_file.write(prettify_result.decode('utf8'))
|
||||||
out_file.close()
|
out_file.close()
|
||||||
|
|
||||||
|
|
||||||
class PascalVocReader:
|
class PascalVocReader:
|
||||||
|
|
||||||
def __init__(self, filepath):
|
def __init__(self, file_path):
|
||||||
# shapes type:
|
# shapes type:
|
||||||
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult]
|
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult]
|
||||||
self.shapes = []
|
self.shapes = []
|
||||||
self.filepath = filepath
|
self.file_path = file_path
|
||||||
self.verified = False
|
self.verified = False
|
||||||
try:
|
try:
|
||||||
self.parseXML()
|
self.parse_xml()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def getShapes(self):
|
def get_shapes(self):
|
||||||
return self.shapes
|
return self.shapes
|
||||||
|
|
||||||
def addShape(self, label, bndbox, difficult):
|
def add_shape(self, label, bnd_box, difficult):
|
||||||
xmin = int(float(bndbox.find('xmin').text))
|
x_min = int(float(bnd_box.find('xmin').text))
|
||||||
ymin = int(float(bndbox.find('ymin').text))
|
y_min = int(float(bnd_box.find('ymin').text))
|
||||||
xmax = int(float(bndbox.find('xmax').text))
|
x_max = int(float(bnd_box.find('xmax').text))
|
||||||
ymax = int(float(bndbox.find('ymax').text))
|
y_max = int(float(bnd_box.find('ymax').text))
|
||||||
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]
|
points = [(x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)]
|
||||||
self.shapes.append((label, points, None, None, difficult))
|
self.shapes.append((label, points, None, None, difficult))
|
||||||
|
|
||||||
def parseXML(self):
|
def parse_xml(self):
|
||||||
assert self.filepath.endswith(XML_EXT), "Unsupport file format"
|
assert self.file_path.endswith(XML_EXT), "Unsupported file format"
|
||||||
parser = etree.XMLParser(encoding=ENCODE_METHOD)
|
parser = etree.XMLParser(encoding=ENCODE_METHOD)
|
||||||
xmltree = ElementTree.parse(self.filepath, parser=parser).getroot()
|
xml_tree = ElementTree.parse(self.file_path, parser=parser).getroot()
|
||||||
filename = xmltree.find('filename').text
|
filename = xml_tree.find('filename').text
|
||||||
try:
|
try:
|
||||||
verified = xmltree.attrib['verified']
|
verified = xml_tree.attrib['verified']
|
||||||
if verified == 'yes':
|
if verified == 'yes':
|
||||||
self.verified = True
|
self.verified = True
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.verified = False
|
self.verified = False
|
||||||
|
|
||||||
for object_iter in xmltree.findall('object'):
|
for object_iter in xml_tree.findall('object'):
|
||||||
bndbox = object_iter.find("bndbox")
|
bnd_box = object_iter.find("bndbox")
|
||||||
label = object_iter.find('name').text
|
label = object_iter.find('name').text
|
||||||
# Add chris
|
# Add chris
|
||||||
difficult = False
|
difficult = False
|
||||||
if object_iter.find('difficult') is not None:
|
if object_iter.find('difficult') is not None:
|
||||||
difficult = bool(int(object_iter.find('difficult').text))
|
difficult = bool(int(object_iter.find('difficult').text))
|
||||||
self.addShape(label, bndbox, difficult)
|
self.add_shape(label, bnd_box, difficult)
|
||||||
return True
|
return True
|
||||||
|
|||||||
@ -32,23 +32,23 @@ class Shape(object):
|
|||||||
select_line_color = DEFAULT_SELECT_LINE_COLOR
|
select_line_color = DEFAULT_SELECT_LINE_COLOR
|
||||||
select_fill_color = DEFAULT_SELECT_FILL_COLOR
|
select_fill_color = DEFAULT_SELECT_FILL_COLOR
|
||||||
vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
|
vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
|
||||||
hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
|
h_vertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
|
||||||
point_type = P_ROUND
|
point_type = P_ROUND
|
||||||
point_size = 8
|
point_size = 8
|
||||||
scale = 1.0
|
scale = 1.0
|
||||||
labelFontSize = 8
|
label_font_size = 8
|
||||||
|
|
||||||
def __init__(self, label=None, line_color=None, difficult=False, paintLabel=False):
|
def __init__(self, label=None, line_color=None, difficult=False, paint_label=False):
|
||||||
self.label = label
|
self.label = label
|
||||||
self.points = []
|
self.points = []
|
||||||
self.fill = False
|
self.fill = False
|
||||||
self.selected = False
|
self.selected = False
|
||||||
self.difficult = difficult
|
self.difficult = difficult
|
||||||
self.paintLabel = paintLabel
|
self.paint_label = paint_label
|
||||||
|
|
||||||
self._highlightIndex = None
|
self._highlight_index = None
|
||||||
self._highlightMode = self.NEAR_VERTEX
|
self._highlight_mode = self.NEAR_VERTEX
|
||||||
self._highlightSettings = {
|
self._highlight_settings = {
|
||||||
self.NEAR_VERTEX: (4, self.P_ROUND),
|
self.NEAR_VERTEX: (4, self.P_ROUND),
|
||||||
self.MOVE_VERTEX: (1.5, self.P_SQUARE),
|
self.MOVE_VERTEX: (1.5, self.P_SQUARE),
|
||||||
}
|
}
|
||||||
@ -64,24 +64,24 @@ class Shape(object):
|
|||||||
def close(self):
|
def close(self):
|
||||||
self._closed = True
|
self._closed = True
|
||||||
|
|
||||||
def reachMaxPoints(self):
|
def reach_max_points(self):
|
||||||
if len(self.points) >= 4:
|
if len(self.points) >= 4:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def addPoint(self, point):
|
def add_point(self, point):
|
||||||
if not self.reachMaxPoints():
|
if not self.reach_max_points():
|
||||||
self.points.append(point)
|
self.points.append(point)
|
||||||
|
|
||||||
def popPoint(self):
|
def pop_point(self):
|
||||||
if self.points:
|
if self.points:
|
||||||
return self.points.pop()
|
return self.points.pop()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def isClosed(self):
|
def is_closed(self):
|
||||||
return self._closed
|
return self._closed
|
||||||
|
|
||||||
def setOpen(self):
|
def set_open(self):
|
||||||
self._closed = False
|
self._closed = False
|
||||||
|
|
||||||
def paint(self, painter):
|
def paint(self, painter):
|
||||||
@ -93,40 +93,40 @@ class Shape(object):
|
|||||||
painter.setPen(pen)
|
painter.setPen(pen)
|
||||||
|
|
||||||
line_path = QPainterPath()
|
line_path = QPainterPath()
|
||||||
vrtx_path = QPainterPath()
|
vertex_path = QPainterPath()
|
||||||
|
|
||||||
line_path.moveTo(self.points[0])
|
line_path.moveTo(self.points[0])
|
||||||
# Uncommenting the following line will draw 2 paths
|
# Uncommenting the following line will draw 2 paths
|
||||||
# for the 1st vertex, and make it non-filled, which
|
# for the 1st vertex, and make it non-filled, which
|
||||||
# may be desirable.
|
# may be desirable.
|
||||||
#self.drawVertex(vrtx_path, 0)
|
# self.drawVertex(vertex_path, 0)
|
||||||
|
|
||||||
for i, p in enumerate(self.points):
|
for i, p in enumerate(self.points):
|
||||||
line_path.lineTo(p)
|
line_path.lineTo(p)
|
||||||
self.drawVertex(vrtx_path, i)
|
self.draw_vertex(vertex_path, i)
|
||||||
if self.isClosed():
|
if self.is_closed():
|
||||||
line_path.lineTo(self.points[0])
|
line_path.lineTo(self.points[0])
|
||||||
|
|
||||||
painter.drawPath(line_path)
|
painter.drawPath(line_path)
|
||||||
painter.drawPath(vrtx_path)
|
painter.drawPath(vertex_path)
|
||||||
painter.fillPath(vrtx_path, self.vertex_fill_color)
|
painter.fillPath(vertex_path, self.vertex_fill_color)
|
||||||
|
|
||||||
# Draw text at the top-left
|
# Draw text at the top-left
|
||||||
if self.paintLabel:
|
if self.paint_label:
|
||||||
min_x = sys.maxsize
|
min_x = sys.maxsize
|
||||||
min_y = sys.maxsize
|
min_y = sys.maxsize
|
||||||
min_y_label = int(1.25 * self.labelFontSize)
|
min_y_label = int(1.25 * self.label_font_size)
|
||||||
for point in self.points:
|
for point in self.points:
|
||||||
min_x = min(min_x, point.x())
|
min_x = min(min_x, point.x())
|
||||||
min_y = min(min_y, point.y())
|
min_y = min(min_y, point.y())
|
||||||
if min_x != sys.maxsize and min_y != sys.maxsize:
|
if min_x != sys.maxsize and min_y != sys.maxsize:
|
||||||
font = QFont()
|
font = QFont()
|
||||||
font.setPointSize(self.labelFontSize)
|
font.setPointSize(self.label_font_size)
|
||||||
font.setBold(True)
|
font.setBold(True)
|
||||||
painter.setFont(font)
|
painter.setFont(font)
|
||||||
if(self.label == None):
|
if self.label is None:
|
||||||
self.label = ""
|
self.label = ""
|
||||||
if(min_y < min_y_label):
|
if min_y < min_y_label:
|
||||||
min_y += min_y_label
|
min_y += min_y_label
|
||||||
painter.drawText(min_x, min_y, self.label)
|
painter.drawText(min_x, min_y, self.label)
|
||||||
|
|
||||||
@ -134,15 +134,15 @@ class Shape(object):
|
|||||||
color = self.select_fill_color if self.selected else self.fill_color
|
color = self.select_fill_color if self.selected else self.fill_color
|
||||||
painter.fillPath(line_path, color)
|
painter.fillPath(line_path, color)
|
||||||
|
|
||||||
def drawVertex(self, path, i):
|
def draw_vertex(self, path, i):
|
||||||
d = self.point_size / self.scale
|
d = self.point_size / self.scale
|
||||||
shape = self.point_type
|
shape = self.point_type
|
||||||
point = self.points[i]
|
point = self.points[i]
|
||||||
if i == self._highlightIndex:
|
if i == self._highlight_index:
|
||||||
size, shape = self._highlightSettings[self._highlightMode]
|
size, shape = self._highlight_settings[self._highlight_mode]
|
||||||
d *= size
|
d *= size
|
||||||
if self._highlightIndex is not None:
|
if self._highlight_index is not None:
|
||||||
self.vertex_fill_color = self.hvertex_fill_color
|
self.vertex_fill_color = self.h_vertex_fill_color
|
||||||
else:
|
else:
|
||||||
self.vertex_fill_color = Shape.vertex_fill_color
|
self.vertex_fill_color = Shape.vertex_fill_color
|
||||||
if shape == self.P_SQUARE:
|
if shape == self.P_SQUARE:
|
||||||
@ -152,36 +152,36 @@ class Shape(object):
|
|||||||
else:
|
else:
|
||||||
assert False, "unsupported vertex shape"
|
assert False, "unsupported vertex shape"
|
||||||
|
|
||||||
def nearestVertex(self, point, epsilon):
|
def nearest_vertex(self, point, epsilon):
|
||||||
for i, p in enumerate(self.points):
|
for i, p in enumerate(self.points):
|
||||||
if distance(p - point) <= epsilon:
|
if distance(p - point) <= epsilon:
|
||||||
return i
|
return i
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def containsPoint(self, point):
|
def contains_point(self, point):
|
||||||
return self.makePath().contains(point)
|
return self.make_path().contains(point)
|
||||||
|
|
||||||
def makePath(self):
|
def make_path(self):
|
||||||
path = QPainterPath(self.points[0])
|
path = QPainterPath(self.points[0])
|
||||||
for p in self.points[1:]:
|
for p in self.points[1:]:
|
||||||
path.lineTo(p)
|
path.lineTo(p)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def boundingRect(self):
|
def bounding_rect(self):
|
||||||
return self.makePath().boundingRect()
|
return self.make_path().boundingRect()
|
||||||
|
|
||||||
def moveBy(self, offset):
|
def move_by(self, offset):
|
||||||
self.points = [p + offset for p in self.points]
|
self.points = [p + offset for p in self.points]
|
||||||
|
|
||||||
def moveVertexBy(self, i, offset):
|
def move_vertex_by(self, i, offset):
|
||||||
self.points[i] = self.points[i] + offset
|
self.points[i] = self.points[i] + offset
|
||||||
|
|
||||||
def highlightVertex(self, i, action):
|
def highlight_vertex(self, i, action):
|
||||||
self._highlightIndex = i
|
self._highlight_index = i
|
||||||
self._highlightMode = action
|
self._highlight_mode = action
|
||||||
|
|
||||||
def highlightClear(self):
|
def highlight_clear(self):
|
||||||
self._highlightIndex = None
|
self._highlight_index = None
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
shape = Shape("%s" % self.label)
|
shape = Shape("%s" % self.label)
|
||||||
|
|||||||
@ -19,43 +19,43 @@ class StringBundle:
|
|||||||
|
|
||||||
__create_key = object()
|
__create_key = object()
|
||||||
|
|
||||||
def __init__(self, create_key, localeStr):
|
def __init__(self, create_key, locale_str):
|
||||||
assert(create_key == StringBundle.__create_key), "StringBundle must be created using StringBundle.getBundle"
|
assert(create_key == StringBundle.__create_key), "StringBundle must be created using StringBundle.getBundle"
|
||||||
self.idToMessage = {}
|
self.id_to_message = {}
|
||||||
paths = self.__createLookupFallbackList(localeStr)
|
paths = self.__create_lookup_fallback_list(locale_str)
|
||||||
for path in paths:
|
for path in paths:
|
||||||
self.__loadBundle(path)
|
self.__load_bundle(path)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def getBundle(cls, localeStr=None):
|
def get_bundle(cls, locale_str=None):
|
||||||
if localeStr is None:
|
if locale_str is None:
|
||||||
try:
|
try:
|
||||||
localeStr = locale.getlocale()[0] if locale.getlocale() and len(
|
locale_str = locale.getlocale()[0] if locale.getlocale() and len(
|
||||||
locale.getlocale()) > 0 else os.getenv('LANG')
|
locale.getlocale()) > 0 else os.getenv('LANG')
|
||||||
except:
|
except:
|
||||||
print('Invalid locale')
|
print('Invalid locale')
|
||||||
localeStr = 'en'
|
locale_str = 'en'
|
||||||
|
|
||||||
return StringBundle(cls.__create_key, localeStr)
|
return StringBundle(cls.__create_key, locale_str)
|
||||||
|
|
||||||
def getString(self, stringId):
|
def get_string(self, string_id):
|
||||||
assert(stringId in self.idToMessage), "Missing string id : " + stringId
|
assert(string_id in self.id_to_message), "Missing string id : " + string_id
|
||||||
return self.idToMessage[stringId]
|
return self.id_to_message[string_id]
|
||||||
|
|
||||||
def __createLookupFallbackList(self, localeStr):
|
def __create_lookup_fallback_list(self, locale_str):
|
||||||
resultPaths = []
|
result_paths = []
|
||||||
basePath = ":/strings"
|
base_path = ":/strings"
|
||||||
resultPaths.append(basePath)
|
result_paths.append(base_path)
|
||||||
if localeStr is not None:
|
if locale_str is not None:
|
||||||
# Don't follow standard BCP47. Simple fallback
|
# Don't follow standard BCP47. Simple fallback
|
||||||
tags = re.split('[^a-zA-Z]', localeStr)
|
tags = re.split('[^a-zA-Z]', locale_str)
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
lastPath = resultPaths[-1]
|
last_path = result_paths[-1]
|
||||||
resultPaths.append(lastPath + '-' + tag)
|
result_paths.append(last_path + '-' + tag)
|
||||||
|
|
||||||
return resultPaths
|
return result_paths
|
||||||
|
|
||||||
def __loadBundle(self, path):
|
def __load_bundle(self, path):
|
||||||
PROP_SEPERATOR = '='
|
PROP_SEPERATOR = '='
|
||||||
f = QFile(path)
|
f = QFile(path)
|
||||||
if f.exists():
|
if f.exists():
|
||||||
@ -68,6 +68,6 @@ class StringBundle:
|
|||||||
key_value = line.split(PROP_SEPERATOR)
|
key_value = line.split(PROP_SEPERATOR)
|
||||||
key = key_value[0].strip()
|
key = key_value[0].strip()
|
||||||
value = PROP_SEPERATOR.join(key_value[1:]).strip().strip('"')
|
value = PROP_SEPERATOR.join(key_value[1:]).strip().strip('"')
|
||||||
self.idToMessage[key] = value
|
self.id_to_message[key] = value
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import sys
|
|||||||
from libs.constants import DEFAULT_ENCODING
|
from libs.constants import DEFAULT_ENCODING
|
||||||
|
|
||||||
def ustr(x):
|
def ustr(x):
|
||||||
'''py2/py3 unicode helper'''
|
"""py2/py3 unicode helper"""
|
||||||
|
|
||||||
if sys.version_info < (3, 0, 0):
|
if sys.version_info < (3, 0, 0):
|
||||||
from PyQt4.QtCore import QString
|
from PyQt4.QtCore import QString
|
||||||
|
|||||||
@ -13,25 +13,25 @@ except ImportError:
|
|||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
|
|
||||||
|
|
||||||
def newIcon(icon):
|
def new_icon(icon):
|
||||||
return QIcon(':/' + icon)
|
return QIcon(':/' + icon)
|
||||||
|
|
||||||
|
|
||||||
def newButton(text, icon=None, slot=None):
|
def new_button(text, icon=None, slot=None):
|
||||||
b = QPushButton(text)
|
b = QPushButton(text)
|
||||||
if icon is not None:
|
if icon is not None:
|
||||||
b.setIcon(newIcon(icon))
|
b.setIcon(new_icon(icon))
|
||||||
if slot is not None:
|
if slot is not None:
|
||||||
b.clicked.connect(slot)
|
b.clicked.connect(slot)
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
|
||||||
def newAction(parent, text, slot=None, shortcut=None, icon=None,
|
def new_action(parent, text, slot=None, shortcut=None, icon=None,
|
||||||
tip=None, checkable=False, enabled=True):
|
tip=None, checkable=False, enabled=True):
|
||||||
"""Create a new action and assign callbacks, shortcuts, etc."""
|
"""Create a new action and assign callbacks, shortcuts, etc."""
|
||||||
a = QAction(text, parent)
|
a = QAction(text, parent)
|
||||||
if icon is not None:
|
if icon is not None:
|
||||||
a.setIcon(newIcon(icon))
|
a.setIcon(new_icon(icon))
|
||||||
if shortcut is not None:
|
if shortcut is not None:
|
||||||
if isinstance(shortcut, (list, tuple)):
|
if isinstance(shortcut, (list, tuple)):
|
||||||
a.setShortcuts(shortcut)
|
a.setShortcuts(shortcut)
|
||||||
@ -48,7 +48,7 @@ def newAction(parent, text, slot=None, shortcut=None, icon=None,
|
|||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def addActions(widget, actions):
|
def add_actions(widget, actions):
|
||||||
for action in actions:
|
for action in actions:
|
||||||
if action is None:
|
if action is None:
|
||||||
widget.addSeparator()
|
widget.addSeparator()
|
||||||
@ -58,11 +58,11 @@ def addActions(widget, actions):
|
|||||||
widget.addAction(action)
|
widget.addAction(action)
|
||||||
|
|
||||||
|
|
||||||
def labelValidator():
|
def label_validator():
|
||||||
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
|
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
|
||||||
|
|
||||||
|
|
||||||
class struct(object):
|
class Struct(object):
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.__dict__.update(kwargs)
|
self.__dict__.update(kwargs)
|
||||||
@ -72,21 +72,21 @@ def distance(p):
|
|||||||
return sqrt(p.x() * p.x() + p.y() * p.y())
|
return sqrt(p.x() * p.x() + p.y() * p.y())
|
||||||
|
|
||||||
|
|
||||||
def fmtShortcut(text):
|
def format_shortcut(text):
|
||||||
mod, key = text.split('+', 1)
|
mod, key = text.split('+', 1)
|
||||||
return '<b>%s</b>+<b>%s</b>' % (mod, key)
|
return '<b>%s</b>+<b>%s</b>' % (mod, key)
|
||||||
|
|
||||||
|
|
||||||
def generateColorByText(text):
|
def generate_color_by_text(text):
|
||||||
s = ustr(text)
|
s = ustr(text)
|
||||||
hashCode = int(hashlib.sha256(s.encode('utf-8')).hexdigest(), 16)
|
hash_code = int(hashlib.sha256(s.encode('utf-8')).hexdigest(), 16)
|
||||||
r = int((hashCode / 255) % 255)
|
r = int((hash_code / 255) % 255)
|
||||||
g = int((hashCode / 65025) % 255)
|
g = int((hash_code / 65025) % 255)
|
||||||
b = int((hashCode / 16581375) % 255)
|
b = int((hash_code / 16581375) % 255)
|
||||||
return QColor(r, g, b, 100)
|
return QColor(r, g, b, 100)
|
||||||
|
|
||||||
def have_qstring():
|
def have_qstring():
|
||||||
'''p3/qt5 get rid of QString wrapper as py3 has native unicode str type'''
|
"""p3/qt5 get rid of QString wrapper as py3 has native unicode str type"""
|
||||||
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
|
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
|
||||||
|
|
||||||
def util_qt_strlistclass():
|
def util_qt_strlistclass():
|
||||||
|
|||||||
138
libs/yolo_io.py
138
libs/yolo_io.py
@ -13,67 +13,67 @@ ENCODE_METHOD = DEFAULT_ENCODING
|
|||||||
|
|
||||||
class YOLOWriter:
|
class YOLOWriter:
|
||||||
|
|
||||||
def __init__(self, foldername, filename, imgSize, databaseSrc='Unknown', localImgPath=None):
|
def __init__(self, folder_name, filename, img_size, database_src='Unknown', local_img_path=None):
|
||||||
self.foldername = foldername
|
self.folder_name = folder_name
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.databaseSrc = databaseSrc
|
self.database_src = database_src
|
||||||
self.imgSize = imgSize
|
self.img_size = img_size
|
||||||
self.boxlist = []
|
self.box_list = []
|
||||||
self.localImgPath = localImgPath
|
self.local_img_path = local_img_path
|
||||||
self.verified = False
|
self.verified = False
|
||||||
|
|
||||||
def addBndBox(self, xmin, ymin, xmax, ymax, name, difficult):
|
def add_bnd_box(self, x_min, y_min, x_max, y_max, name, difficult):
|
||||||
bndbox = {'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax}
|
bnd_box = {'xmin': x_min, 'ymin': y_min, 'xmax': x_max, 'ymax': y_max}
|
||||||
bndbox['name'] = name
|
bnd_box['name'] = name
|
||||||
bndbox['difficult'] = difficult
|
bnd_box['difficult'] = difficult
|
||||||
self.boxlist.append(bndbox)
|
self.box_list.append(bnd_box)
|
||||||
|
|
||||||
def BndBox2YoloLine(self, box, classList=[]):
|
def bnd_box_to_yolo_line(self, box, class_list=[]):
|
||||||
xmin = box['xmin']
|
x_min = box['xmin']
|
||||||
xmax = box['xmax']
|
x_max = box['xmax']
|
||||||
ymin = box['ymin']
|
y_min = box['ymin']
|
||||||
ymax = box['ymax']
|
y_max = box['ymax']
|
||||||
|
|
||||||
xcen = float((xmin + xmax)) / 2 / self.imgSize[1]
|
x_center = float((x_min + x_max)) / 2 / self.img_size[1]
|
||||||
ycen = float((ymin + ymax)) / 2 / self.imgSize[0]
|
y_center = float((y_min + y_max)) / 2 / self.img_size[0]
|
||||||
|
|
||||||
w = float((xmax - xmin)) / self.imgSize[1]
|
w = float((x_max - x_min)) / self.img_size[1]
|
||||||
h = float((ymax - ymin)) / self.imgSize[0]
|
h = float((y_max - y_min)) / self.img_size[0]
|
||||||
|
|
||||||
# PR387
|
# PR387
|
||||||
boxName = box['name']
|
box_name = box['name']
|
||||||
if boxName not in classList:
|
if box_name not in class_list:
|
||||||
classList.append(boxName)
|
class_list.append(box_name)
|
||||||
|
|
||||||
classIndex = classList.index(boxName)
|
class_index = class_list.index(box_name)
|
||||||
|
|
||||||
return classIndex, xcen, ycen, w, h
|
return class_index, x_center, y_center, w, h
|
||||||
|
|
||||||
def save(self, classList=[], targetFile=None):
|
def save(self, class_list=[], target_file=None):
|
||||||
|
|
||||||
out_file = None # Update yolo .txt
|
out_file = None # Update yolo .txt
|
||||||
out_class_file = None # Update class list .txt
|
out_class_file = None # Update class list .txt
|
||||||
|
|
||||||
if targetFile is None:
|
if target_file is None:
|
||||||
out_file = open(
|
out_file = open(
|
||||||
self.filename + TXT_EXT, 'w', encoding=ENCODE_METHOD)
|
self.filename + TXT_EXT, 'w', encoding=ENCODE_METHOD)
|
||||||
classesFile = os.path.join(os.path.dirname(os.path.abspath(self.filename)), "classes.txt")
|
classes_file = os.path.join(os.path.dirname(os.path.abspath(self.filename)), "classes.txt")
|
||||||
out_class_file = open(classesFile, 'w')
|
out_class_file = open(classes_file, 'w')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
out_file = codecs.open(targetFile, 'w', encoding=ENCODE_METHOD)
|
out_file = codecs.open(target_file, 'w', encoding=ENCODE_METHOD)
|
||||||
classesFile = os.path.join(os.path.dirname(os.path.abspath(targetFile)), "classes.txt")
|
classes_file = os.path.join(os.path.dirname(os.path.abspath(target_file)), "classes.txt")
|
||||||
out_class_file = open(classesFile, 'w')
|
out_class_file = open(classes_file, 'w')
|
||||||
|
|
||||||
|
|
||||||
for box in self.boxlist:
|
for box in self.box_list:
|
||||||
classIndex, xcen, ycen, w, h = self.BndBox2YoloLine(box, classList)
|
class_index, x_center, y_center, w, h = self.bnd_box_to_yolo_line(box, class_list)
|
||||||
# print (classIndex, xcen, ycen, w, h)
|
# print (classIndex, x_center, y_center, w, h)
|
||||||
out_file.write("%d %.6f %.6f %.6f %.6f\n" % (classIndex, xcen, ycen, w, h))
|
out_file.write("%d %.6f %.6f %.6f %.6f\n" % (class_index, x_center, y_center, w, h))
|
||||||
|
|
||||||
# print (classList)
|
# print (classList)
|
||||||
# print (out_class_file)
|
# print (out_class_file)
|
||||||
for c in classList:
|
for c in class_list:
|
||||||
out_class_file.write(c+'\n')
|
out_class_file.write(c+'\n')
|
||||||
|
|
||||||
out_class_file.close()
|
out_class_file.close()
|
||||||
@ -83,64 +83,64 @@ class YOLOWriter:
|
|||||||
|
|
||||||
class YoloReader:
|
class YoloReader:
|
||||||
|
|
||||||
def __init__(self, filepath, image, classListPath=None):
|
def __init__(self, file_path, image, class_list_path=None):
|
||||||
# shapes type:
|
# shapes type:
|
||||||
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult]
|
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult]
|
||||||
self.shapes = []
|
self.shapes = []
|
||||||
self.filepath = filepath
|
self.file_path = file_path
|
||||||
|
|
||||||
if classListPath is None:
|
if class_list_path is None:
|
||||||
dir_path = os.path.dirname(os.path.realpath(self.filepath))
|
dir_path = os.path.dirname(os.path.realpath(self.file_path))
|
||||||
self.classListPath = os.path.join(dir_path, "classes.txt")
|
self.class_list_path = os.path.join(dir_path, "classes.txt")
|
||||||
else:
|
else:
|
||||||
self.classListPath = classListPath
|
self.class_list_path = class_list_path
|
||||||
|
|
||||||
# print (filepath, self.classListPath)
|
# print (file_path, self.class_list_path)
|
||||||
|
|
||||||
classesFile = open(self.classListPath, 'r')
|
classes_file = open(self.class_list_path, 'r')
|
||||||
self.classes = classesFile.read().strip('\n').split('\n')
|
self.classes = classes_file.read().strip('\n').split('\n')
|
||||||
|
|
||||||
# print (self.classes)
|
# print (self.classes)
|
||||||
|
|
||||||
imgSize = [image.height(), image.width(),
|
img_size = [image.height(), image.width(),
|
||||||
1 if image.isGrayscale() else 3]
|
1 if image.isGrayscale() else 3]
|
||||||
|
|
||||||
self.imgSize = imgSize
|
self.img_size = img_size
|
||||||
|
|
||||||
self.verified = False
|
self.verified = False
|
||||||
# try:
|
# try:
|
||||||
self.parseYoloFormat()
|
self.parse_yolo_format()
|
||||||
# except:
|
# except:
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
def getShapes(self):
|
def get_shapes(self):
|
||||||
return self.shapes
|
return self.shapes
|
||||||
|
|
||||||
def addShape(self, label, xmin, ymin, xmax, ymax, difficult):
|
def add_shape(self, label, x_min, y_min, x_max, y_max, difficult):
|
||||||
|
|
||||||
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]
|
points = [(x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)]
|
||||||
self.shapes.append((label, points, None, None, difficult))
|
self.shapes.append((label, points, None, None, difficult))
|
||||||
|
|
||||||
def yoloLine2Shape(self, classIndex, xcen, ycen, w, h):
|
def yolo_line_to_shape(self, class_index, x_center, y_center, w, h):
|
||||||
label = self.classes[int(classIndex)]
|
label = self.classes[int(class_index)]
|
||||||
|
|
||||||
xmin = max(float(xcen) - float(w) / 2, 0)
|
x_min = max(float(x_center) - float(w) / 2, 0)
|
||||||
xmax = min(float(xcen) + float(w) / 2, 1)
|
x_max = min(float(x_center) + float(w) / 2, 1)
|
||||||
ymin = max(float(ycen) - float(h) / 2, 0)
|
y_min = max(float(y_center) - float(h) / 2, 0)
|
||||||
ymax = min(float(ycen) + float(h) / 2, 1)
|
y_max = min(float(y_center) + float(h) / 2, 1)
|
||||||
|
|
||||||
xmin = int(self.imgSize[1] * xmin)
|
x_min = int(self.img_size[1] * x_min)
|
||||||
xmax = int(self.imgSize[1] * xmax)
|
x_max = int(self.img_size[1] * x_max)
|
||||||
ymin = int(self.imgSize[0] * ymin)
|
y_min = int(self.img_size[0] * y_min)
|
||||||
ymax = int(self.imgSize[0] * ymax)
|
y_max = int(self.img_size[0] * y_max)
|
||||||
|
|
||||||
return label, xmin, ymin, xmax, ymax
|
return label, x_min, y_min, x_max, y_max
|
||||||
|
|
||||||
def parseYoloFormat(self):
|
def parse_yolo_format(self):
|
||||||
bndBoxFile = open(self.filepath, 'r')
|
bnd_box_file = open(self.file_path, 'r')
|
||||||
for bndBox in bndBoxFile:
|
for bndBox in bnd_box_file:
|
||||||
classIndex, xcen, ycen, w, h = bndBox.strip().split(' ')
|
class_index, x_center, y_center, w, h = bndBox.strip().split(' ')
|
||||||
label, xmin, ymin, xmax, ymax = self.yoloLine2Shape(classIndex, xcen, ycen, w, h)
|
label, x_min, y_min, x_max, y_max = self.yolo_line_to_shape(class_index, x_center, y_center, w, h)
|
||||||
|
|
||||||
# Caveat: difficult flag is discarded when saved as yolo format.
|
# Caveat: difficult flag is discarded when saved as yolo format.
|
||||||
self.addShape(label, xmin, ymin, xmax, ymax, False)
|
self.add_shape(label, x_min, y_min, x_max, y_max, False)
|
||||||
|
|||||||
@ -14,17 +14,17 @@ class TestPascalVocRW(unittest.TestCase):
|
|||||||
# Test Write/Read
|
# Test Write/Read
|
||||||
writer = PascalVocWriter('tests', 'test', (512, 512, 1), localImgPath='tests/test.512.512.bmp')
|
writer = PascalVocWriter('tests', 'test', (512, 512, 1), localImgPath='tests/test.512.512.bmp')
|
||||||
difficult = 1
|
difficult = 1
|
||||||
writer.addBndBox(60, 40, 430, 504, 'person', difficult)
|
writer.add_bnd_box(60, 40, 430, 504, 'person', difficult)
|
||||||
writer.addBndBox(113, 40, 450, 403, 'face', difficult)
|
writer.add_bnd_box(113, 40, 450, 403, 'face', difficult)
|
||||||
writer.save('tests/test.xml')
|
writer.save('tests/test.xml')
|
||||||
|
|
||||||
reader = PascalVocReader('tests/test.xml')
|
reader = PascalVocReader('tests/test.xml')
|
||||||
shapes = reader.getShapes()
|
shapes = reader.get_shapes()
|
||||||
|
|
||||||
personBndBox = shapes[0]
|
person_bnd_box = shapes[0]
|
||||||
face = shapes[1]
|
face = shapes[1]
|
||||||
self.assertEqual(personBndBox[0], 'person')
|
self.assertEqual(person_bnd_box[0], 'person')
|
||||||
self.assertEqual(personBndBox[1], [(60, 40), (430, 40), (430, 504), (60, 504)])
|
self.assertEqual(person_bnd_box[1], [(60, 40), (430, 40), (430, 504), (60, 504)])
|
||||||
self.assertEqual(face[0], 'face')
|
self.assertEqual(face[0], 'face')
|
||||||
self.assertEqual(face[1], [(113, 40), (450, 40), (450, 403), (113, 403)])
|
self.assertEqual(face[1], [(113, 40), (450, 40), (450, 403), (113, 403)])
|
||||||
|
|
||||||
@ -54,10 +54,10 @@ class TestCreateMLRW(unittest.TestCase):
|
|||||||
|
|
||||||
# check written json
|
# check written json
|
||||||
with open(output_file, "r") as file:
|
with open(output_file, "r") as file:
|
||||||
inputdata = file.read()
|
input_data = file.read()
|
||||||
|
|
||||||
import json
|
import json
|
||||||
data_dict = json.loads(inputdata)[0]
|
data_dict = json.loads(input_data)[0]
|
||||||
|
|
||||||
self.assertEqual('test.512.512.bmp', data_dict['image'], 'filename not correct in .json')
|
self.assertEqual('test.512.512.bmp', data_dict['image'], 'filename not correct in .json')
|
||||||
self.assertEqual(2, len(data_dict['annotations']), 'output file contains to less annotations')
|
self.assertEqual(2, len(data_dict['annotations']), 'output file contains to less annotations')
|
||||||
@ -84,15 +84,15 @@ class TestCreateMLRW(unittest.TestCase):
|
|||||||
self.assertEqual('face', face[0], 'label is wrong')
|
self.assertEqual('face', face[0], 'label is wrong')
|
||||||
|
|
||||||
face_coords = face[1]
|
face_coords = face[1]
|
||||||
xmin = face_coords[0][0]
|
x_min = face_coords[0][0]
|
||||||
xmax = face_coords[1][0]
|
x_max = face_coords[1][0]
|
||||||
ymin = face_coords[0][1]
|
y_min = face_coords[0][1]
|
||||||
ymax = face_coords[2][1]
|
y_max = face_coords[2][1]
|
||||||
|
|
||||||
self.assertEqual(245, xmin, 'xmin is wrong')
|
self.assertEqual(245, x_min, 'xmin is wrong')
|
||||||
self.assertEqual(350, xmax, 'xmax is wrong')
|
self.assertEqual(350, x_max, 'xmax is wrong')
|
||||||
self.assertEqual(250, ymin, 'ymin is wrong')
|
self.assertEqual(250, y_min, 'ymin is wrong')
|
||||||
self.assertEqual(365, ymax, 'ymax is wrong')
|
self.assertEqual(365, y_max, 'ymax is wrong')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@ -7,20 +7,20 @@ from stringBundle import StringBundle
|
|||||||
class TestStringBundle(unittest.TestCase):
|
class TestStringBundle(unittest.TestCase):
|
||||||
|
|
||||||
def test_loadDefaultBundle_withoutError(self):
|
def test_loadDefaultBundle_withoutError(self):
|
||||||
strBundle = StringBundle.getBundle('en')
|
str_bundle = StringBundle.get_bundle('en')
|
||||||
self.assertEqual(strBundle.getString("openDir"), 'Open Dir', 'Fail to load the default bundle')
|
self.assertEqual(str_bundle.get_string("openDir"), 'Open Dir', 'Fail to load the default bundle')
|
||||||
|
|
||||||
def test_fallback_withoutError(self):
|
def test_fallback_withoutError(self):
|
||||||
strBundle = StringBundle.getBundle('zh-TW')
|
str_bundle = StringBundle.get_bundle('zh-TW')
|
||||||
self.assertEqual(strBundle.getString("openDir"), u'\u958B\u555F\u76EE\u9304', 'Fail to load the zh-TW bundle')
|
self.assertEqual(str_bundle.get_string("openDir"), u'\u958B\u555F\u76EE\u9304', 'Fail to load the zh-TW bundle')
|
||||||
|
|
||||||
def test_setInvaleLocaleToEnv_printErrorMsg(self):
|
def test_setInvaleLocaleToEnv_printErrorMsg(self):
|
||||||
prev_lc = os.environ['LC_ALL']
|
prev_lc = os.environ['LC_ALL']
|
||||||
prev_lang = os.environ['LANG']
|
prev_lang = os.environ['LANG']
|
||||||
os.environ['LC_ALL'] = 'UTF-8'
|
os.environ['LC_ALL'] = 'UTF-8'
|
||||||
os.environ['LANG'] = 'UTF-8'
|
os.environ['LANG'] = 'UTF-8'
|
||||||
strBundle = StringBundle.getBundle()
|
str_bundle = StringBundle.get_bundle()
|
||||||
self.assertEqual(strBundle.getString("openDir"), 'Open Dir', 'Fail to load the default bundle')
|
self.assertEqual(str_bundle.get_string("openDir"), 'Open Dir', 'Fail to load the default bundle')
|
||||||
os.environ['LC_ALL'] = prev_lc
|
os.environ['LC_ALL'] = prev_lc
|
||||||
os.environ['LANG'] = prev_lang
|
os.environ['LANG'] = prev_lang
|
||||||
|
|
||||||
|
|||||||
@ -1,22 +1,22 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
from libs.utils import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText, natural_sort
|
from libs.utils import Struct, new_action, new_icon, add_actions, format_shortcut, generate_color_by_text, natural_sort
|
||||||
|
|
||||||
class TestUtils(unittest.TestCase):
|
class TestUtils(unittest.TestCase):
|
||||||
|
|
||||||
def test_generateColorByGivingUniceText_noError(self):
|
def test_generateColorByGivingUniceText_noError(self):
|
||||||
res = generateColorByText(u'\u958B\u555F\u76EE\u9304')
|
res = generate_color_by_text(u'\u958B\u555F\u76EE\u9304')
|
||||||
self.assertTrue(res.green() >= 0)
|
self.assertTrue(res.green() >= 0)
|
||||||
self.assertTrue(res.red() >= 0)
|
self.assertTrue(res.red() >= 0)
|
||||||
self.assertTrue(res.blue() >= 0)
|
self.assertTrue(res.blue() >= 0)
|
||||||
|
|
||||||
def test_nautalSort_noError(self):
|
def test_nautalSort_noError(self):
|
||||||
l1 = ['f1', 'f11', 'f3']
|
l1 = ['f1', 'f11', 'f3']
|
||||||
exptected_l1 = ['f1', 'f3', 'f11']
|
expected_l1 = ['f1', 'f3', 'f11']
|
||||||
natural_sort(l1)
|
natural_sort(l1)
|
||||||
for idx, val in enumerate(l1):
|
for idx, val in enumerate(l1):
|
||||||
self.assertTrue(val == exptected_l1[idx])
|
self.assertTrue(val == expected_l1[idx])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user