Add 'labelImg/' from commit 'b33f965b6d14c14f1e46b247f1bf346e03f2e950'

git-subtree-dir: labelImg
git-subtree-mainline: dbe80aca78
git-subtree-split: b33f965b6d
This commit is contained in:
2023-02-21 21:52:18 +05:30
117 changed files with 10290 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
__version_info__ = ('1', '8', '6')
__version__ = '.'.join(__version_info__)
+748
View File
@@ -0,0 +1,748 @@
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
# from PyQt4.QtOpenGL import *
from libs.shape import Shape
from libs.utils import distance
CURSOR_DEFAULT = Qt.ArrowCursor
CURSOR_POINT = Qt.PointingHandCursor
CURSOR_DRAW = Qt.CrossCursor
CURSOR_MOVE = Qt.ClosedHandCursor
CURSOR_GRAB = Qt.OpenHandCursor
# class Canvas(QGLWidget):
class Canvas(QWidget):
zoomRequest = pyqtSignal(int)
lightRequest = pyqtSignal(int)
scrollRequest = pyqtSignal(int, int)
newShape = pyqtSignal()
selectionChanged = pyqtSignal(bool)
shapeMoved = pyqtSignal()
drawingPolygon = pyqtSignal(bool)
CREATE, EDIT = list(range(2))
epsilon = 24.0
def __init__(self, *args, **kwargs):
super(Canvas, self).__init__(*args, **kwargs)
# Initialise local state.
self.mode = self.EDIT
self.shapes = []
self.current = None
self.selected_shape = None # save the selected shape here
self.selected_shape_copy = None
self.drawing_line_color = QColor(0, 0, 255)
self.drawing_rect_color = QColor(0, 0, 255)
self.line = Shape(line_color=self.drawing_line_color)
self.prev_point = QPointF()
self.offsets = QPointF(), QPointF()
self.scale = 1.0
self.overlay_color = None
self.label_font_size = 8
self.pixmap = QPixmap()
self.visible = {}
self._hide_background = False
self.hide_background = False
self.h_shape = None
self.h_vertex = None
self._painter = QPainter()
self._cursor = CURSOR_DEFAULT
# Menus:
self.menus = (QMenu(), QMenu())
# Set widget options.
self.setMouseTracking(True)
self.setFocusPolicy(Qt.WheelFocus)
self.verified = False
self.draw_square = False
# initialisation for panning
self.pan_initial_pos = QPoint()
def set_drawing_color(self, qcolor):
self.drawing_line_color = qcolor
self.drawing_rect_color = qcolor
def enterEvent(self, ev):
self.override_cursor(self._cursor)
def leaveEvent(self, ev):
self.restore_cursor()
def focusOutEvent(self, ev):
self.restore_cursor()
def isVisible(self, shape):
return self.visible.get(shape, True)
def drawing(self):
return self.mode == self.CREATE
def editing(self):
return self.mode == self.EDIT
def set_editing(self, value=True):
self.mode = self.EDIT if value else self.CREATE
if not value: # Create
self.un_highlight()
self.de_select_shape()
self.prev_point = QPointF()
self.repaint()
def un_highlight(self, shape=None):
if shape == None or shape == self.h_shape:
if self.h_shape:
self.h_shape.highlight_clear()
self.h_vertex = self.h_shape = None
def selected_vertex(self):
return self.h_vertex is not None
def mouseMoveEvent(self, ev):
"""Update line with last point and current coordinates."""
pos = self.transform_pos(ev.pos())
# Update coordinates in status bar if image is opened
window = self.parent().window()
if window.file_path is not None:
self.parent().window().label_coordinates.setText(
'X: %d; Y: %d' % (pos.x(), pos.y()))
# Polygon drawing.
if self.drawing():
self.override_cursor(CURSOR_DRAW)
if self.current:
# Display annotation width and height while drawing
current_width = abs(self.current[0].x() - pos.x())
current_height = abs(self.current[0].y() - pos.y())
self.parent().window().label_coordinates.setText(
'Width: %d, Height: %d / X: %d; Y: %d' % (current_width, current_height, pos.x(), pos.y()))
color = self.drawing_line_color
if self.out_of_pixmap(pos):
# Don't allow the user to draw outside the pixmap.
# Clip the coordinates to 0 or max,
# if they are outside the range [0, max]
size = self.pixmap.size()
clipped_x = min(max(0, pos.x()), size.width())
clipped_y = min(max(0, pos.y()), size.height())
pos = QPointF(clipped_x, clipped_y)
elif len(self.current) > 1 and self.close_enough(pos, self.current[0]):
# Attract line to starting point and colorise to alert the
# user:
pos = self.current[0]
color = self.current.line_color
self.override_cursor(CURSOR_POINT)
self.current.highlight_vertex(0, Shape.NEAR_VERTEX)
if self.draw_square:
init_pos = self.current[0]
min_x = init_pos.x()
min_y = init_pos.y()
min_size = min(abs(pos.x() - min_x), abs(pos.y() - min_y))
direction_x = -1 if pos.x() - min_x < 0 else 1
direction_y = -1 if pos.y() - min_y < 0 else 1
self.line[1] = QPointF(min_x + direction_x * min_size, min_y + direction_y * min_size)
else:
self.line[1] = pos
self.line.line_color = color
self.prev_point = QPointF()
self.current.highlight_clear()
else:
self.prev_point = pos
self.repaint()
return
# Polygon copy moving.
if Qt.RightButton & ev.buttons():
if self.selected_shape_copy and self.prev_point:
self.override_cursor(CURSOR_MOVE)
self.bounded_move_shape(self.selected_shape_copy, pos)
self.repaint()
elif self.selected_shape:
self.selected_shape_copy = self.selected_shape.copy()
self.repaint()
return
# Polygon/Vertex moving.
if Qt.LeftButton & ev.buttons():
if self.selected_vertex():
self.bounded_move_vertex(pos)
self.shapeMoved.emit()
self.repaint()
# Display annotation width and height while moving vertex
point1 = self.h_shape[1]
point3 = self.h_shape[3]
current_width = abs(point1.x() - point3.x())
current_height = abs(point1.y() - point3.y())
self.parent().window().label_coordinates.setText(
'Width: %d, Height: %d / X: %d; Y: %d' % (current_width, current_height, pos.x(), pos.y()))
elif self.selected_shape and self.prev_point:
self.override_cursor(CURSOR_MOVE)
self.bounded_move_shape(self.selected_shape, pos)
self.shapeMoved.emit()
self.repaint()
# Display annotation width and height while moving shape
point1 = self.selected_shape[1]
point3 = self.selected_shape[3]
current_width = abs(point1.x() - point3.x())
current_height = abs(point1.y() - point3.y())
self.parent().window().label_coordinates.setText(
'Width: %d, Height: %d / X: %d; Y: %d' % (current_width, current_height, pos.x(), pos.y()))
else:
# pan
delta = ev.pos() - self.pan_initial_pos
self.scrollRequest.emit(delta.x(), Qt.Horizontal)
self.scrollRequest.emit(delta.y(), Qt.Vertical)
self.update()
return
# Just hovering over the canvas, 2 possibilities:
# - Highlight shapes
# - Highlight vertex
# Update shape/vertex fill and tooltip value accordingly.
self.setToolTip("Image")
priority_list = self.shapes + ([self.selected_shape] if self.selected_shape else [])
for shape in reversed([s for s in priority_list if self.isVisible(s)]):
# Look for a nearby vertex to highlight. If that fails,
# check if we happen to be inside a shape.
index = shape.nearest_vertex(pos, self.epsilon)
if index is not None:
if self.selected_vertex():
self.h_shape.highlight_clear()
self.h_vertex, self.h_shape = index, shape
shape.highlight_vertex(index, shape.MOVE_VERTEX)
self.override_cursor(CURSOR_POINT)
self.setToolTip("Click & drag to move point")
self.setStatusTip(self.toolTip())
self.update()
break
elif shape.contains_point(pos):
if self.selected_vertex():
self.h_shape.highlight_clear()
self.h_vertex, self.h_shape = None, shape
self.setToolTip(
"Click & drag to move shape '%s'" % shape.label)
self.setStatusTip(self.toolTip())
self.override_cursor(CURSOR_GRAB)
self.update()
# Display annotation width and height while hovering inside
point1 = self.h_shape[1]
point3 = self.h_shape[3]
current_width = abs(point1.x() - point3.x())
current_height = abs(point1.y() - point3.y())
self.parent().window().label_coordinates.setText(
'Width: %d, Height: %d / X: %d; Y: %d' % (current_width, current_height, pos.x(), pos.y()))
break
else: # Nothing found, clear highlights, reset state.
if self.h_shape:
self.h_shape.highlight_clear()
self.update()
self.h_vertex, self.h_shape = None, None
self.override_cursor(CURSOR_DEFAULT)
def mousePressEvent(self, ev):
pos = self.transform_pos(ev.pos())
if ev.button() == Qt.LeftButton:
if self.drawing():
self.handle_drawing(pos)
else:
selection = self.select_shape_point(pos)
self.prev_point = pos
if selection is None:
# pan
QApplication.setOverrideCursor(QCursor(Qt.OpenHandCursor))
self.pan_initial_pos = ev.pos()
elif ev.button() == Qt.RightButton and self.editing():
self.select_shape_point(pos)
self.prev_point = pos
self.update()
def mouseReleaseEvent(self, ev):
if ev.button() == Qt.RightButton:
menu = self.menus[bool(self.selected_shape_copy)]
self.restore_cursor()
if not menu.exec_(self.mapToGlobal(ev.pos()))\
and self.selected_shape_copy:
# Cancel the move by deleting the shadow copy.
self.selected_shape_copy = None
self.repaint()
elif ev.button() == Qt.LeftButton and self.selected_shape:
if self.selected_vertex():
self.override_cursor(CURSOR_POINT)
else:
self.override_cursor(CURSOR_GRAB)
elif ev.button() == Qt.LeftButton:
pos = self.transform_pos(ev.pos())
if self.drawing():
self.handle_drawing(pos)
else:
# pan
QApplication.restoreOverrideCursor()
def end_move(self, copy=False):
assert self.selected_shape and self.selected_shape_copy
shape = self.selected_shape_copy
# del shape.fill_color
# del shape.line_color
if copy:
self.shapes.append(shape)
self.selected_shape.selected = False
self.selected_shape = shape
self.repaint()
else:
self.selected_shape.points = [p for p in shape.points]
self.selected_shape_copy = None
def hide_background_shapes(self, value):
self.hide_background = value
if self.selected_shape:
# Only hide other shapes if there is a current selection.
# Otherwise the user will not be able to select a shape.
self.set_hiding(True)
self.repaint()
def handle_drawing(self, pos):
if self.current and self.current.reach_max_points() is False:
init_pos = self.current[0]
min_x = init_pos.x()
min_y = init_pos.y()
target_pos = self.line[1]
max_x = target_pos.x()
max_y = target_pos.y()
self.current.add_point(QPointF(max_x, min_y))
self.current.add_point(target_pos)
self.current.add_point(QPointF(min_x, max_y))
self.finalise()
elif not self.out_of_pixmap(pos):
self.current = Shape()
self.current.add_point(pos)
self.line.points = [pos, pos]
self.set_hiding()
self.drawingPolygon.emit(True)
self.update()
def set_hiding(self, enable=True):
self._hide_background = self.hide_background if enable else False
def can_close_shape(self):
return self.drawing() and self.current and len(self.current) > 2
def mouseDoubleClickEvent(self, ev):
# We need at least 4 points here, since the mousePress handler
# adds an extra one before this handler is called.
if self.can_close_shape() and len(self.current) > 3:
self.current.pop_point()
self.finalise()
def select_shape(self, shape):
self.de_select_shape()
shape.selected = True
self.selected_shape = shape
self.set_hiding()
self.selectionChanged.emit(True)
self.update()
def select_shape_point(self, point):
"""Select the first shape created which contains this point."""
self.de_select_shape()
if self.selected_vertex(): # A vertex is marked for selection.
index, shape = self.h_vertex, self.h_shape
shape.highlight_vertex(index, shape.MOVE_VERTEX)
self.select_shape(shape)
return self.h_vertex
for shape in reversed(self.shapes):
if self.isVisible(shape) and shape.contains_point(point):
self.select_shape(shape)
self.calculate_offsets(shape, point)
return self.selected_shape
return None
def calculate_offsets(self, shape, point):
rect = shape.bounding_rect()
x1 = rect.x() - point.x()
y1 = rect.y() - point.y()
x2 = (rect.x() + rect.width()) - point.x()
y2 = (rect.y() + rect.height()) - point.y()
self.offsets = QPointF(x1, y1), QPointF(x2, y2)
def snap_point_to_canvas(self, x, y):
"""
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.
"""
if x < 0 or x > self.pixmap.width() or y < 0 or y > self.pixmap.height():
x = max(x, 0)
y = max(y, 0)
x = min(x, self.pixmap.width())
y = min(y, self.pixmap.height())
return x, y, True
return x, y, False
def bounded_move_vertex(self, pos):
index, shape = self.h_vertex, self.h_shape
point = shape[index]
if self.out_of_pixmap(pos):
size = self.pixmap.size()
clipped_x = min(max(0, pos.x()), size.width())
clipped_y = min(max(0, pos.y()), size.height())
pos = QPointF(clipped_x, clipped_y)
if self.draw_square:
opposite_point_index = (index + 2) % 4
opposite_point = shape[opposite_point_index]
min_size = min(abs(pos.x() - opposite_point.x()), abs(pos.y() - opposite_point.y()))
direction_x = -1 if pos.x() - opposite_point.x() < 0 else 1
direction_y = -1 if pos.y() - opposite_point.y() < 0 else 1
shift_pos = QPointF(opposite_point.x() + direction_x * min_size - point.x(),
opposite_point.y() + direction_y * min_size - point.y())
else:
shift_pos = pos - point
shape.move_vertex_by(index, shift_pos)
left_index = (index + 1) % 4
right_index = (index + 3) % 4
left_shift = None
right_shift = None
if index % 2 == 0:
right_shift = QPointF(shift_pos.x(), 0)
left_shift = QPointF(0, shift_pos.y())
else:
left_shift = QPointF(shift_pos.x(), 0)
right_shift = QPointF(0, shift_pos.y())
shape.move_vertex_by(right_index, right_shift)
shape.move_vertex_by(left_index, left_shift)
def bounded_move_shape(self, shape, pos):
if self.out_of_pixmap(pos):
return False # No need to move
o1 = pos + self.offsets[0]
if self.out_of_pixmap(o1):
pos -= QPointF(min(0, o1.x()), min(0, o1.y()))
o2 = pos + self.offsets[1]
if self.out_of_pixmap(o2):
pos += QPointF(min(0, self.pixmap.width() - o2.x()),
min(0, self.pixmap.height() - o2.y()))
# The next line tracks the new position of the cursor
# relative to the shape, but also results in making it
# a bit "shaky" when nearing the border and allows it to
# go outside of the shape's area for some reason. XXX
# self.calculateOffsets(self.selectedShape, pos)
dp = pos - self.prev_point
if dp:
shape.move_by(dp)
self.prev_point = pos
return True
return False
def de_select_shape(self):
if self.selected_shape:
self.selected_shape.selected = False
self.selected_shape = None
self.set_hiding(False)
self.selectionChanged.emit(False)
self.update()
def delete_selected(self):
if self.selected_shape:
shape = self.selected_shape
self.un_highlight(shape)
self.shapes.remove(self.selected_shape)
self.selected_shape = None
self.update()
return shape
def copy_selected_shape(self):
if self.selected_shape:
shape = self.selected_shape.copy()
self.de_select_shape()
self.shapes.append(shape)
shape.selected = True
self.selected_shape = shape
self.bounded_shift_shape(shape)
return shape
def bounded_shift_shape(self, shape):
# Try to move in one direction, and if it fails in another.
# Give up if both fail.
point = shape[0]
offset = QPointF(2.0, 2.0)
self.calculate_offsets(shape, point)
self.prev_point = point
if not self.bounded_move_shape(shape, point - offset):
self.bounded_move_shape(shape, point + offset)
def paintEvent(self, event):
if not self.pixmap:
return super(Canvas, self).paintEvent(event)
p = self._painter
p.begin(self)
p.setRenderHint(QPainter.Antialiasing)
p.setRenderHint(QPainter.HighQualityAntialiasing)
p.setRenderHint(QPainter.SmoothPixmapTransform)
p.scale(self.scale, self.scale)
p.translate(self.offset_to_center())
temp = self.pixmap
if self.overlay_color:
temp = QPixmap(self.pixmap)
painter = QPainter(temp)
painter.setCompositionMode(painter.CompositionMode_Overlay)
painter.fillRect(temp.rect(), self.overlay_color)
painter.end()
p.drawPixmap(0, 0, temp)
Shape.scale = self.scale
Shape.label_font_size = self.label_font_size
for shape in self.shapes:
if (shape.selected or not self._hide_background) and self.isVisible(shape):
shape.fill = shape.selected or shape == self.h_shape
shape.paint(p)
if self.current:
self.current.paint(p)
self.line.paint(p)
if self.selected_shape_copy:
self.selected_shape_copy.paint(p)
# Paint rect
if self.current is not None and len(self.line) == 2:
left_top = self.line[0]
right_bottom = self.line[1]
rect_width = right_bottom.x() - left_top.x()
rect_height = right_bottom.y() - left_top.y()
p.setPen(self.drawing_rect_color)
brush = QBrush(Qt.BDiagPattern)
p.setBrush(brush)
p.drawRect(int(left_top.x()), int(left_top.y()), int(rect_width), int(rect_height))
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.drawLine(int(self.prev_point.x()), 0, int(self.prev_point.x()), int(self.pixmap.height()))
p.drawLine(0, int(self.prev_point.y()), int(self.pixmap.width()), int(self.prev_point.y()))
self.setAutoFillBackground(True)
if self.verified:
pal = self.palette()
pal.setColor(self.backgroundRole(), QColor(184, 239, 38, 128))
self.setPalette(pal)
else:
pal = self.palette()
pal.setColor(self.backgroundRole(), QColor(232, 232, 232, 255))
self.setPalette(pal)
p.end()
def transform_pos(self, point):
"""Convert from widget-logical coordinates to painter-logical coordinates."""
return point / self.scale - self.offset_to_center()
def offset_to_center(self):
s = self.scale
area = super(Canvas, self).size()
w, h = self.pixmap.width() * s, self.pixmap.height() * s
aw, ah = area.width(), area.height()
x = (aw - w) / (2 * s) if aw > w else 0
y = (ah - h) / (2 * s) if ah > h else 0
return QPointF(x, y)
def out_of_pixmap(self, p):
w, h = self.pixmap.width(), self.pixmap.height()
return not (0 <= p.x() <= w and 0 <= p.y() <= h)
def finalise(self):
assert self.current
if self.current.points[0] == self.current.points[-1]:
self.current = None
self.drawingPolygon.emit(False)
self.update()
return
self.current.close()
self.shapes.append(self.current)
self.current = None
self.set_hiding(False)
self.newShape.emit()
self.update()
def close_enough(self, p1, p2):
# d = distance(p1 - p2)
# m = (p1-p2).manhattanLength()
# print "d %.2f, m %d, %.2f" % (d, m, d - m)
return distance(p1 - p2) < self.epsilon
# These two, along with a call to adjustSize are required for the
# scroll area.
def sizeHint(self):
return self.minimumSizeHint()
def minimumSizeHint(self):
if self.pixmap:
return self.scale * self.pixmap.size()
return super(Canvas, self).minimumSizeHint()
def wheelEvent(self, ev):
qt_version = 4 if hasattr(ev, "delta") else 5
if qt_version == 4:
if ev.orientation() == Qt.Vertical:
v_delta = ev.delta()
h_delta = 0
else:
h_delta = ev.delta()
v_delta = 0
else:
delta = ev.angleDelta()
h_delta = delta.x()
v_delta = delta.y()
mods = ev.modifiers()
if int(Qt.ControlModifier) | int(Qt.ShiftModifier) == int(mods) and v_delta:
self.lightRequest.emit(v_delta)
elif Qt.ControlModifier == int(mods) and v_delta:
self.zoomRequest.emit(v_delta)
else:
v_delta and self.scrollRequest.emit(v_delta, Qt.Vertical)
h_delta and self.scrollRequest.emit(h_delta, Qt.Horizontal)
ev.accept()
def keyPressEvent(self, ev):
key = ev.key()
if key == Qt.Key_Escape and self.current:
print('ESC press')
self.current = None
self.drawingPolygon.emit(False)
self.update()
elif key == Qt.Key_Return and self.can_close_shape():
self.finalise()
elif key == Qt.Key_Left and self.selected_shape:
self.move_one_pixel('Left')
elif key == Qt.Key_Right and self.selected_shape:
self.move_one_pixel('Right')
elif key == Qt.Key_Up and self.selected_shape:
self.move_one_pixel('Up')
elif key == Qt.Key_Down and self.selected_shape:
self.move_one_pixel('Down')
def move_one_pixel(self, direction):
# print(self.selectedShape.points)
if direction == 'Left' and not self.move_out_of_bound(QPointF(-1.0, 0)):
# print("move Left one pixel")
self.selected_shape.points[0] += QPointF(-1.0, 0)
self.selected_shape.points[1] += QPointF(-1.0, 0)
self.selected_shape.points[2] += QPointF(-1.0, 0)
self.selected_shape.points[3] += QPointF(-1.0, 0)
elif direction == 'Right' and not self.move_out_of_bound(QPointF(1.0, 0)):
# print("move Right one pixel")
self.selected_shape.points[0] += QPointF(1.0, 0)
self.selected_shape.points[1] += QPointF(1.0, 0)
self.selected_shape.points[2] += QPointF(1.0, 0)
self.selected_shape.points[3] += QPointF(1.0, 0)
elif direction == 'Up' and not self.move_out_of_bound(QPointF(0, -1.0)):
# print("move Up one pixel")
self.selected_shape.points[0] += QPointF(0, -1.0)
self.selected_shape.points[1] += QPointF(0, -1.0)
self.selected_shape.points[2] += QPointF(0, -1.0)
self.selected_shape.points[3] += QPointF(0, -1.0)
elif direction == 'Down' and not self.move_out_of_bound(QPointF(0, 1.0)):
# print("move Down one pixel")
self.selected_shape.points[0] += QPointF(0, 1.0)
self.selected_shape.points[1] += QPointF(0, 1.0)
self.selected_shape.points[2] += QPointF(0, 1.0)
self.selected_shape.points[3] += QPointF(0, 1.0)
self.shapeMoved.emit()
self.repaint()
def move_out_of_bound(self, step):
points = [p1 + p2 for p1, p2 in zip(self.selected_shape.points, [step] * 4)]
return True in map(self.out_of_pixmap, points)
def set_last_label(self, text, line_color=None, fill_color=None):
assert text
self.shapes[-1].label = text
if line_color:
self.shapes[-1].line_color = line_color
if fill_color:
self.shapes[-1].fill_color = fill_color
return self.shapes[-1]
def undo_last_line(self):
assert self.shapes
self.current = self.shapes.pop()
self.current.set_open()
self.line.points = [self.current[-1], self.current[0]]
self.drawingPolygon.emit(True)
def reset_all_lines(self):
assert self.shapes
self.current = self.shapes.pop()
self.current.set_open()
self.line.points = [self.current[-1], self.current[0]]
self.drawingPolygon.emit(True)
self.current = None
self.drawingPolygon.emit(False)
self.update()
def load_pixmap(self, pixmap):
self.pixmap = pixmap
self.shapes = []
self.repaint()
def load_shapes(self, shapes):
self.shapes = list(shapes)
self.current = None
self.repaint()
def set_shape_visible(self, shape, value):
self.visible[shape] = value
self.repaint()
def current_cursor(self):
cursor = QApplication.overrideCursor()
if cursor is not None:
cursor = cursor.shape()
return cursor
def override_cursor(self, cursor):
self._cursor = cursor
if self.current_cursor() is None:
QApplication.setOverrideCursor(cursor)
else:
QApplication.changeOverrideCursor(cursor)
def restore_cursor(self):
QApplication.restoreOverrideCursor()
def reset_state(self):
self.de_select_shape()
self.un_highlight()
self.selected_shape_copy = None
self.restore_cursor()
self.pixmap = None
self.update()
def set_drawing_shape_to_square(self, status):
self.draw_square = status
+37
View File
@@ -0,0 +1,37 @@
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QColorDialog, QDialogButtonBox
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
BB = QDialogButtonBox
class ColorDialog(QColorDialog):
def __init__(self, parent=None):
super(ColorDialog, self).__init__(parent)
self.setOption(QColorDialog.ShowAlphaChannel)
# The Mac native dialog does not support our restore button.
self.setOption(QColorDialog.DontUseNativeDialog)
# Add a restore defaults button.
# The default is set at invocation time, so that it
# works across dialogs for different elements.
self.default = None
self.bb = self.layout().itemAt(1).widget()
self.bb.addButton(BB.RestoreDefaults)
self.bb.clicked.connect(self.check_restore)
def getColor(self, value=None, title=None, default=None):
self.default = default
if title:
self.setWindowTitle(title)
if value:
self.setCurrentColor(value)
return self.currentColor() if self.exec_() else None
def check_restore(self, button):
if self.bb.buttonRole(button) & BB.ResetRole and self.default:
self.setCurrentColor(self.default)
+33
View File
@@ -0,0 +1,33 @@
import sys
try:
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QComboBox
except ImportError:
# needed for py3+qt4
# Ref:
# http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
# http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
if sys.version_info.major >= 3:
import sip
sip.setapi('QVariant', 2)
from PyQt4.QtGui import QWidget, QHBoxLayout, QComboBox
class ComboBox(QWidget):
def __init__(self, parent=None, items=[]):
super(ComboBox, self).__init__(parent)
layout = QHBoxLayout()
self.cb = QComboBox()
self.items = items
self.cb.addItems(self.items)
self.cb.currentIndexChanged.connect(parent.combo_selection_changed)
layout.addWidget(self.cb)
self.setLayout(layout)
def update_items(self, items):
self.items = items
self.cb.clear()
self.cb.addItems(self.items)
+20
View File
@@ -0,0 +1,20 @@
SETTING_FILENAME = 'filename'
SETTING_RECENT_FILES = 'recentFiles'
SETTING_WIN_SIZE = 'window/size'
SETTING_WIN_POSE = 'window/position'
SETTING_WIN_GEOMETRY = 'window/geometry'
SETTING_LINE_COLOR = 'line/color'
SETTING_FILL_COLOR = 'fill/color'
SETTING_ADVANCE_MODE = 'advanced'
SETTING_WIN_STATE = 'window/state'
SETTING_SAVE_DIR = 'savedir'
SETTING_PAINT_LABEL = 'paintlabel'
SETTING_LAST_OPEN_DIR = 'lastOpenDir'
SETTING_AUTO_SAVE = 'autosave'
SETTING_SINGLE_CLASS = 'singleclass'
FORMAT_PASCALVOC='PascalVOC'
FORMAT_YOLO='YOLO'
FORMAT_CREATEML='CreateML'
SETTING_DRAW_SQUARE = 'draw/square'
SETTING_LABEL_FILE_FORMAT= 'labelFileFormat'
DEFAULT_ENCODING = 'utf-8'
+135
View File
@@ -0,0 +1,135 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
import json
from pathlib import Path
from libs.constants import DEFAULT_ENCODING
import os
JSON_EXT = '.json'
ENCODE_METHOD = DEFAULT_ENCODING
class CreateMLWriter:
def __init__(self, folder_name, filename, img_size, shapes, output_file, database_src='Unknown', local_img_path=None):
self.folder_name = folder_name
self.filename = filename
self.database_src = database_src
self.img_size = img_size
self.box_list = []
self.local_img_path = local_img_path
self.verified = False
self.shapes = shapes
self.output_file = output_file
def write(self):
if os.path.isfile(self.output_file):
with open(self.output_file, "r") as file:
input_data = file.read()
output_dict = json.loads(input_data)
else:
output_dict = []
output_image_dict = {
"image": self.filename,
"verified": self.verified,
"annotations": []
}
for shape in self.shapes:
points = shape["points"]
x1 = points[0][0]
y1 = points[0][1]
x2 = points[1][0]
y2 = points[2][1]
height, width, x, y = self.calculate_coordinates(x1, x2, y1, y2)
shape_dict = {
"label": shape["label"],
"coordinates": {
"x": x,
"y": y,
"width": width,
"height": height
}
}
output_image_dict["annotations"].append(shape_dict)
# check if image already in output
exists = False
for i in range(0, len(output_dict)):
if output_dict[i]["image"] == output_image_dict["image"]:
exists = True
output_dict[i] = output_image_dict
break
if not exists:
output_dict.append(output_image_dict)
Path(self.output_file).write_text(json.dumps(output_dict), ENCODE_METHOD)
def calculate_coordinates(self, x1, x2, y1, y2):
if x1 < x2:
x_min = x1
x_max = x2
else:
x_min = x2
x_max = x1
if y1 < y2:
y_min = y1
y_max = y2
else:
y_min = y2
y_max = y1
width = x_max - x_min
if width < 0:
width = width * -1
height = y_max - y_min
# x and y from center of rect
x = x_min + width / 2
y = y_min + height / 2
return height, width, x, y
class CreateMLReader:
def __init__(self, json_path, file_path):
self.json_path = json_path
self.shapes = []
self.verified = False
self.filename = os.path.basename(file_path)
try:
self.parse_json()
except ValueError:
print("JSON decoding failed")
def parse_json(self):
with open(self.json_path, "r") as file:
input_data = file.read()
# Returns a list
output_list = json.loads(input_data)
if output_list:
self.verified = output_list[0].get("verified", False)
if len(self.shapes) > 0:
self.shapes = []
for image in output_list:
if image["image"] == self.filename:
for shape in image["annotations"]:
self.add_shape(shape["label"], shape["coordinates"])
def add_shape(self, label, bnd_box):
x_min = bnd_box["x"] - (bnd_box["width"] / 2)
y_min = bnd_box["y"] - (bnd_box["height"] / 2)
x_max = bnd_box["x"] + (bnd_box["width"] / 2)
y_max = bnd_box["y"] + (bnd_box["height"] / 2)
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))
def get_shapes(self):
return self.shapes
+27
View File
@@ -0,0 +1,27 @@
import sys
try:
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QComboBox
except ImportError:
# needed for py3+qt4
# Ref:
# http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
# http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
if sys.version_info.major >= 3:
import sip
sip.setapi('QVariant', 2)
from PyQt4.QtGui import QWidget, QHBoxLayout, QComboBox
class DefaultLabelComboBox(QWidget):
def __init__(self, parent=None, items=[]):
super(DefaultLabelComboBox, self).__init__(parent)
layout = QHBoxLayout()
self.cb = QComboBox()
self.items = items
self.cb.addItems(self.items)
self.cb.currentIndexChanged.connect(parent.default_label_combo_selection_changed)
layout.addWidget(self.cb)
self.setLayout(layout)
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
# needed for py3+qt4
# Ref:
# http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
# http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
if sys.version_info.major >= 3:
import sip
sip.setapi('QVariant', 2)
from PyQt4.QtGui import *
from PyQt4.QtCore import *
# PyQt5: TypeError: unhashable type: 'QListWidgetItem'
class HashableQListWidgetItem(QListWidgetItem):
def __init__(self, *args):
super(HashableQListWidgetItem, self).__init__(*args)
def __hash__(self):
return hash(id(self))
+95
View File
@@ -0,0 +1,95 @@
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from libs.utils import new_icon, label_validator, trimmed
BB = QDialogButtonBox
class LabelDialog(QDialog):
def __init__(self, text="Enter object label", parent=None, list_item=None):
super(LabelDialog, self).__init__(parent)
self.edit = QLineEdit()
self.edit.setText(text)
self.edit.setValidator(label_validator())
self.edit.editingFinished.connect(self.post_process)
model = QStringListModel()
model.setStringList(list_item)
completer = QCompleter()
completer.setModel(model)
self.edit.setCompleter(completer)
self.button_box = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
bb.button(BB.Ok).setIcon(new_icon('done'))
bb.button(BB.Cancel).setIcon(new_icon('undo'))
bb.accepted.connect(self.validate)
bb.rejected.connect(self.reject)
layout = QVBoxLayout()
layout.addWidget(bb, alignment=Qt.AlignmentFlag.AlignLeft)
layout.addWidget(self.edit)
if list_item is not None and len(list_item) > 0:
self.list_widget = QListWidget(self)
for item in list_item:
self.list_widget.addItem(item)
self.list_widget.itemClicked.connect(self.list_item_click)
self.list_widget.itemDoubleClicked.connect(self.list_item_double_click)
layout.addWidget(self.list_widget)
self.setLayout(layout)
def validate(self):
if trimmed(self.edit.text()):
self.accept()
def post_process(self):
self.edit.setText(trimmed(self.edit.text()))
def pop_up(self, text='', move=True):
"""
Shows the dialog, setting the current text to `text`, and blocks the caller until the user has made a choice.
If the user entered a label, that label is returned, otherwise (i.e. if the user cancelled the action)
`None` is returned.
"""
self.edit.setText(text)
self.edit.setSelection(0, len(text))
self.edit.setFocus(Qt.PopupFocusReason)
if move:
cursor_pos = QCursor.pos()
# move OK button below cursor
btn = self.button_box.buttons()[0]
self.adjustSize()
btn.adjustSize()
offset = btn.mapToGlobal(btn.pos()) - self.pos()
offset += QPoint(btn.size().width() // 4, btn.size().height() // 2)
cursor_pos.setX(max(0, cursor_pos.x() - offset.x()))
cursor_pos.setY(max(0, cursor_pos.y() - offset.y()))
parent_bottom_right = self.parentWidget().geometry()
max_x = parent_bottom_right.x() + parent_bottom_right.width() - self.sizeHint().width()
max_y = parent_bottom_right.y() + parent_bottom_right.height() - self.sizeHint().height()
max_global = self.parentWidget().mapToGlobal(QPoint(max_x, max_y))
if cursor_pos.x() > max_global.x():
cursor_pos.setX(max_global.x())
if cursor_pos.y() > max_global.y():
cursor_pos.setY(max_global.y())
self.move(cursor_pos)
return trimmed(self.edit.text()) if self.exec_() else None
def list_item_click(self, t_qlist_widget_item):
text = trimmed(t_qlist_widget_item.text())
self.edit.setText(text)
def list_item_double_click(self, t_qlist_widget_item):
self.list_item_click(t_qlist_widget_item)
self.validate()
+174
View File
@@ -0,0 +1,174 @@
# Copyright (c) 2016 Tzutalin
# Create by TzuTaLin <tzu.ta.lin@gmail.com>
try:
from PyQt5.QtGui import QImage
except ImportError:
from PyQt4.QtGui import QImage
import os.path
from enum import Enum
from libs.create_ml_io import CreateMLWriter
from libs.pascal_voc_io import PascalVocWriter
from libs.pascal_voc_io import XML_EXT
from libs.yolo_io import YOLOWriter
class LabelFileFormat(Enum):
PASCAL_VOC = 1
YOLO = 2
CREATE_ML = 3
class LabelFileError(Exception):
pass
class LabelFile(object):
# It might be changed as window creates. By default, using XML ext
# suffix = '.lif'
suffix = XML_EXT
def __init__(self, filename=None):
self.shapes = ()
self.image_path = None
self.image_data = None
self.verified = False
def save_create_ml_format(self, filename, shapes, image_path, image_data, class_list, line_color=None, fill_color=None, database_src=None):
img_folder_name = os.path.basename(os.path.dirname(image_path))
img_file_name = os.path.basename(image_path)
image = QImage()
image.load(image_path)
image_shape = [image.height(), image.width(),
1 if image.isGrayscale() else 3]
writer = CreateMLWriter(img_folder_name, img_file_name,
image_shape, shapes, filename, local_img_path=image_path)
writer.verified = self.verified
writer.write()
return
def save_pascal_voc_format(self, filename, shapes, image_path, image_data,
line_color=None, fill_color=None, database_src=None):
img_folder_path = os.path.dirname(image_path)
img_folder_name = os.path.split(img_folder_path)[-1]
img_file_name = os.path.basename(image_path)
# imgFileNameWithoutExt = os.path.splitext(img_file_name)[0]
# Read from file path because self.imageData might be empty if saving to
# Pascal format
if isinstance(image_data, QImage):
image = image_data
else:
image = QImage()
image.load(image_path)
image_shape = [image.height(), image.width(),
1 if image.isGrayscale() else 3]
writer = PascalVocWriter(img_folder_name, img_file_name,
image_shape, local_img_path=image_path)
writer.verified = self.verified
for shape in shapes:
points = shape['points']
label = shape['label']
# Add Chris
difficult = int(shape['difficult'])
bnd_box = LabelFile.convert_points_to_bnd_box(points)
writer.add_bnd_box(bnd_box[0], bnd_box[1], bnd_box[2], bnd_box[3], label, difficult)
writer.save(target_file=filename)
return
def save_yolo_format(self, filename, shapes, image_path, image_data, class_list,
line_color=None, fill_color=None, database_src=None):
img_folder_path = os.path.dirname(image_path)
img_folder_name = os.path.split(img_folder_path)[-1]
img_file_name = os.path.basename(image_path)
# imgFileNameWithoutExt = os.path.splitext(img_file_name)[0]
# Read from file path because self.imageData might be empty if saving to
# Pascal format
if isinstance(image_data, QImage):
image = image_data
else:
image = QImage()
image.load(image_path)
image_shape = [image.height(), image.width(),
1 if image.isGrayscale() else 3]
writer = YOLOWriter(img_folder_name, img_file_name,
image_shape, local_img_path=image_path)
writer.verified = self.verified
for shape in shapes:
points = shape['points']
label = shape['label']
# Add Chris
difficult = int(shape['difficult'])
bnd_box = LabelFile.convert_points_to_bnd_box(points)
writer.add_bnd_box(bnd_box[0], bnd_box[1], bnd_box[2], bnd_box[3], label, difficult)
writer.save(target_file=filename, class_list=class_list)
return
def toggle_verify(self):
self.verified = not self.verified
''' ttf is disable
def load(self, filename):
import json
with open(filename, 'rb') as f:
data = json.load(f)
imagePath = data['imagePath']
imageData = b64decode(data['imageData'])
lineColor = data['lineColor']
fillColor = data['fillColor']
shapes = ((s['label'], s['points'], s['line_color'], s['fill_color'])\
for s in data['shapes'])
# Only replace data after everything is loaded.
self.shapes = shapes
self.imagePath = imagePath
self.imageData = imageData
self.lineColor = lineColor
self.fillColor = fillColor
def save(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None):
import json
with open(filename, 'wb') as f:
json.dump(dict(
shapes=shapes,
lineColor=lineColor, fillColor=fillColor,
imagePath=imagePath,
imageData=b64encode(imageData)),
f, ensure_ascii=True, indent=2)
'''
@staticmethod
def is_label_file(filename):
file_suffix = os.path.splitext(filename)[1].lower()
return file_suffix == LabelFile.suffix
@staticmethod
def convert_points_to_bnd_box(points):
x_min = float('inf')
y_min = float('inf')
x_max = float('-inf')
y_max = float('-inf')
for p in points:
x = p[0]
y = p[1]
x_min = min(x, x_min)
y_min = min(y, y_min)
x_max = max(x, x_max)
y_max = max(y, y_max)
# Martin Kersner, 2015/11/12
# 0-valued coordinates of BB caused an error while
# training faster-rcnn object detector.
if x_min < 1:
x_min = 1
if y_min < 1:
y_min = 1
return int(x_min), int(y_min), int(x_max), int(y_max)
+33
View File
@@ -0,0 +1,33 @@
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class LightWidget(QSpinBox):
def __init__(self, title, value=50):
super(LightWidget, self).__init__()
self.setButtonSymbols(QAbstractSpinBox.NoButtons)
self.setRange(0, 100)
self.setSuffix(' %')
self.setValue(value)
self.setToolTip(title)
self.setStatusTip(self.toolTip())
self.setAlignment(Qt.AlignCenter)
def minimumSizeHint(self):
height = super(LightWidget, self).minimumSizeHint().height()
fm = QFontMetrics(self.font())
width = fm.width(str(self.maximum()))
return QSize(width, height)
def color(self):
if self.value() == 50:
return None
strength = int(self.value()/100 * 255 + 0.5)
return QColor(strength, strength, strength)
+171
View File
@@ -0,0 +1,171 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from lxml import etree
import codecs
from libs.constants import DEFAULT_ENCODING
from libs.ustr import ustr
XML_EXT = '.xml'
ENCODE_METHOD = DEFAULT_ENCODING
class PascalVocWriter:
def __init__(self, folder_name, filename, img_size, database_src='Unknown', local_img_path=None):
self.folder_name = folder_name
self.filename = filename
self.database_src = database_src
self.img_size = img_size
self.box_list = []
self.local_img_path = local_img_path
self.verified = False
def prettify(self, elem):
"""
Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf8')
root = etree.fromstring(rough_string)
return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode())
# minidom does not support UTF-8
# reparsed = minidom.parseString(rough_string)
# return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)
def gen_xml(self):
"""
Return XML root
"""
# Check conditions
if self.filename is None or \
self.folder_name is None or \
self.img_size is None:
return None
top = Element('annotation')
if self.verified:
top.set('verified', 'yes')
folder = SubElement(top, 'folder')
folder.text = self.folder_name
filename = SubElement(top, 'filename')
filename.text = self.filename
if self.local_img_path is not None:
local_img_path = SubElement(top, 'path')
local_img_path.text = self.local_img_path
source = SubElement(top, 'source')
database = SubElement(source, 'database')
database.text = self.database_src
size_part = SubElement(top, 'size')
width = SubElement(size_part, 'width')
height = SubElement(size_part, 'height')
depth = SubElement(size_part, 'depth')
width.text = str(self.img_size[1])
height.text = str(self.img_size[0])
if len(self.img_size) == 3:
depth.text = str(self.img_size[2])
else:
depth.text = '1'
segmented = SubElement(top, 'segmented')
segmented.text = '0'
return top
def add_bnd_box(self, x_min, y_min, x_max, y_max, name, difficult):
bnd_box = {'xmin': x_min, 'ymin': y_min, 'xmax': x_max, 'ymax': y_max}
bnd_box['name'] = name
bnd_box['difficult'] = difficult
self.box_list.append(bnd_box)
def append_objects(self, top):
for each_object in self.box_list:
object_item = SubElement(top, 'object')
name = SubElement(object_item, 'name')
name.text = ustr(each_object['name'])
pose = SubElement(object_item, 'pose')
pose.text = "Unspecified"
truncated = SubElement(object_item, 'truncated')
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
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
else:
truncated.text = "0"
difficult = SubElement(object_item, 'difficult')
difficult.text = str(bool(each_object['difficult']) & 1)
bnd_box = SubElement(object_item, 'bndbox')
x_min = SubElement(bnd_box, 'xmin')
x_min.text = str(each_object['xmin'])
y_min = SubElement(bnd_box, 'ymin')
y_min.text = str(each_object['ymin'])
x_max = SubElement(bnd_box, 'xmax')
x_max.text = str(each_object['xmax'])
y_max = SubElement(bnd_box, 'ymax')
y_max.text = str(each_object['ymax'])
def save(self, target_file=None):
root = self.gen_xml()
self.append_objects(root)
out_file = None
if target_file is None:
out_file = codecs.open(
self.filename + XML_EXT, 'w', encoding=ENCODE_METHOD)
else:
out_file = codecs.open(target_file, 'w', encoding=ENCODE_METHOD)
prettify_result = self.prettify(root)
out_file.write(prettify_result.decode('utf8'))
out_file.close()
class PascalVocReader:
def __init__(self, file_path):
# shapes type:
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult]
self.shapes = []
self.file_path = file_path
self.verified = False
try:
self.parse_xml()
except:
pass
def get_shapes(self):
return self.shapes
def add_shape(self, label, bnd_box, difficult):
x_min = int(float(bnd_box.find('xmin').text))
y_min = int(float(bnd_box.find('ymin').text))
x_max = int(float(bnd_box.find('xmax').text))
y_max = int(float(bnd_box.find('ymax').text))
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))
def parse_xml(self):
assert self.file_path.endswith(XML_EXT), "Unsupported file format"
parser = etree.XMLParser(encoding=ENCODE_METHOD)
xml_tree = ElementTree.parse(self.file_path, parser=parser).getroot()
filename = xml_tree.find('filename').text
try:
verified = xml_tree.attrib['verified']
if verified == 'yes':
self.verified = True
except KeyError:
self.verified = False
for object_iter in xml_tree.findall('object'):
bnd_box = object_iter.find("bndbox")
label = object_iter.find('name').text
# Add chris
difficult = False
if object_iter.find('difficult') is not None:
difficult = bool(int(object_iter.find('difficult').text))
self.add_shape(label, bnd_box, difficult)
return True
+45
View File
@@ -0,0 +1,45 @@
import os
import pickle
class Settings(object):
def __init__(self):
# Be default, the home will be in the same folder as labelImg
home = os.path.expanduser("~")
self.data = {}
self.path = os.path.join(home, '.labelImgSettings.pkl')
def __setitem__(self, key, value):
self.data[key] = value
def __getitem__(self, key):
return self.data[key]
def get(self, key, default=None):
if key in self.data:
return self.data[key]
return default
def save(self):
if self.path:
with open(self.path, 'wb') as f:
pickle.dump(self.data, f, pickle.HIGHEST_PROTOCOL)
return True
return False
def load(self):
try:
if os.path.exists(self.path):
with open(self.path, 'rb') as f:
self.data = pickle.load(f)
return True
except:
print('Loading setting failed')
return False
def reset(self):
if os.path.exists(self.path):
os.remove(self.path)
print('Remove setting pkl file ${0}'.format(self.path))
self.data = {}
self.path = None
+209
View File
@@ -0,0 +1,209 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from libs.utils import distance
import sys
DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
class Shape(object):
P_SQUARE, P_ROUND = range(2)
MOVE_VERTEX, NEAR_VERTEX = range(2)
# The following class variables influence the drawing
# of _all_ shape objects.
line_color = DEFAULT_LINE_COLOR
fill_color = DEFAULT_FILL_COLOR
select_line_color = DEFAULT_SELECT_LINE_COLOR
select_fill_color = DEFAULT_SELECT_FILL_COLOR
vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
h_vertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
point_type = P_ROUND
point_size = 16
scale = 1.0
label_font_size = 8
def __init__(self, label=None, line_color=None, difficult=False, paint_label=False):
self.label = label
self.points = []
self.fill = False
self.selected = False
self.difficult = difficult
self.paint_label = paint_label
self._highlight_index = None
self._highlight_mode = self.NEAR_VERTEX
self._highlight_settings = {
self.NEAR_VERTEX: (4, self.P_ROUND),
self.MOVE_VERTEX: (1.5, self.P_SQUARE),
}
self._closed = False
if line_color is not None:
# Override the class line_color attribute
# with an object attribute. Currently this
# is used for drawing the pending line a different color.
self.line_color = line_color
def close(self):
self._closed = True
def reach_max_points(self):
if len(self.points) >= 4:
return True
return False
def add_point(self, point):
if not self.reach_max_points():
self.points.append(point)
def pop_point(self):
if self.points:
return self.points.pop()
return None
def is_closed(self):
return self._closed
def set_open(self):
self._closed = False
def paint(self, painter):
if self.points:
color = self.select_line_color if self.selected else self.line_color
pen = QPen(color)
# Try using integer sizes for smoother drawing(?)
pen.setWidth(max(1, int(round(2.0 / self.scale))))
painter.setPen(pen)
line_path = QPainterPath()
vertex_path = QPainterPath()
line_path.moveTo(self.points[0])
# Uncommenting the following line will draw 2 paths
# for the 1st vertex, and make it non-filled, which
# may be desirable.
# self.drawVertex(vertex_path, 0)
for i, p in enumerate(self.points):
line_path.lineTo(p)
self.draw_vertex(vertex_path, i)
if self.is_closed():
line_path.lineTo(self.points[0])
painter.drawPath(line_path)
painter.drawPath(vertex_path)
painter.fillPath(vertex_path, self.vertex_fill_color)
# Draw text at the top-left
if self.paint_label:
min_x = sys.maxsize
min_y = sys.maxsize
min_y_label = int(1.25 * self.label_font_size)
for point in self.points:
min_x = min(min_x, point.x())
min_y = min(min_y, point.y())
if min_x != sys.maxsize and min_y != sys.maxsize:
font = QFont()
font.setPointSize(self.label_font_size)
font.setBold(True)
painter.setFont(font)
if self.label is None:
self.label = ""
if min_y < min_y_label:
min_y += min_y_label
painter.drawText(int(min_x), int(min_y), self.label)
if self.fill:
color = self.select_fill_color if self.selected else self.fill_color
painter.fillPath(line_path, color)
def draw_vertex(self, path, i):
d = self.point_size / self.scale
shape = self.point_type
point = self.points[i]
if i == self._highlight_index:
size, shape = self._highlight_settings[self._highlight_mode]
d *= size
if self._highlight_index is not None:
self.vertex_fill_color = self.h_vertex_fill_color
else:
self.vertex_fill_color = Shape.vertex_fill_color
if shape == self.P_SQUARE:
path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
elif shape == self.P_ROUND:
path.addEllipse(point, d / 2.0, d / 2.0)
else:
assert False, "unsupported vertex shape"
def nearest_vertex(self, point, epsilon):
index = None
for i, p in enumerate(self.points):
dist = distance(p - point)
if dist <= epsilon:
index = i
epsilon = dist
return index
def contains_point(self, point):
return self.make_path().contains(point)
def make_path(self):
path = QPainterPath(self.points[0])
for p in self.points[1:]:
path.lineTo(p)
return path
def bounding_rect(self):
return self.make_path().boundingRect()
def move_by(self, offset):
self.points = [p + offset for p in self.points]
def move_vertex_by(self, i, offset):
self.points[i] = self.points[i] + offset
def highlight_vertex(self, i, action):
self._highlight_index = i
self._highlight_mode = action
def highlight_clear(self):
self._highlight_index = None
def copy(self):
shape = Shape("%s" % self.label)
shape.points = [p for p in self.points]
shape.fill = self.fill
shape.selected = self.selected
shape._closed = self._closed
if self.line_color != Shape.line_color:
shape.line_color = self.line_color
if self.fill_color != Shape.fill_color:
shape.fill_color = self.fill_color
shape.difficult = self.difficult
return shape
def __len__(self):
return len(self.points)
def __getitem__(self, key):
return self.points[key]
def __setitem__(self, key, value):
self.points[key] = value
+78
View File
@@ -0,0 +1,78 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
if items were added in files in the resources/strings folder,
then execute "pyrcc5 resources.qrc -o resources.py" in the root directory
and execute "pyrcc5 ../resources.qrc -o resources.py" in the libs directory
"""
import re
import os
import sys
import locale
from libs.ustr import ustr
try:
from PyQt5.QtCore import *
except ImportError:
if sys.version_info.major >= 3:
import sip
sip.setapi('QVariant', 2)
from PyQt4.QtCore import *
class StringBundle:
__create_key = object()
def __init__(self, create_key, locale_str):
assert(create_key == StringBundle.__create_key), "StringBundle must be created using StringBundle.getBundle"
self.id_to_message = {}
paths = self.__create_lookup_fallback_list(locale_str)
for path in paths:
self.__load_bundle(path)
@classmethod
def get_bundle(cls, locale_str=None):
if locale_str is None:
try:
locale_str = locale.getdefaultlocale()[0] if locale.getdefaultlocale() and len(
locale.getdefaultlocale()) > 0 else os.getenv('LANG')
except:
print('Invalid locale')
locale_str = 'en'
return StringBundle(cls.__create_key, locale_str)
def get_string(self, string_id):
assert(string_id in self.id_to_message), "Missing string id : " + string_id
return self.id_to_message[string_id]
def __create_lookup_fallback_list(self, locale_str):
result_paths = []
base_path = ":/strings"
result_paths.append(base_path)
if locale_str is not None:
# Don't follow standard BCP47. Simple fallback
tags = re.split('[^a-zA-Z]', locale_str)
for tag in tags:
last_path = result_paths[-1]
result_paths.append(last_path + '-' + tag)
return result_paths
def __load_bundle(self, path):
PROP_SEPERATOR = '='
f = QFile(path)
if f.exists():
if f.open(QIODevice.ReadOnly | QFile.Text):
text = QTextStream(f)
text.setCodec("UTF-8")
while not text.atEnd():
line = ustr(text.readLine())
key_value = line.split(PROP_SEPERATOR)
key = key_value[0].strip()
value = PROP_SEPERATOR.join(key_value[1:]).strip().strip('"')
self.id_to_message[key] = value
f.close()
+39
View File
@@ -0,0 +1,39 @@
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class ToolBar(QToolBar):
def __init__(self, title):
super(ToolBar, self).__init__(title)
layout = self.layout()
m = (0, 0, 0, 0)
layout.setSpacing(0)
layout.setContentsMargins(*m)
self.setContentsMargins(*m)
self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
def addAction(self, action):
if isinstance(action, QWidgetAction):
return super(ToolBar, self).addAction(action)
btn = ToolButton()
btn.setDefaultAction(action)
btn.setToolButtonStyle(self.toolButtonStyle())
self.addWidget(btn)
class ToolButton(QToolButton):
"""ToolBar companion class which ensures all buttons have the same size."""
minSize = (60, 60)
def minimumSizeHint(self):
ms = super(ToolButton, self).minimumSizeHint()
w1, h1 = ms.width(), ms.height()
w2, h2 = self.minSize
ToolButton.minSize = max(w1, w2), max(h1, h2)
return QSize(*ToolButton.minSize)
+17
View File
@@ -0,0 +1,17 @@
import sys
from libs.constants import DEFAULT_ENCODING
def ustr(x):
"""py2/py3 unicode helper"""
if sys.version_info < (3, 0, 0):
from PyQt4.QtCore import QString
if type(x) == str:
return x.decode(DEFAULT_ENCODING)
if type(x) == QString:
# https://blog.csdn.net/friendan/article/details/51088476
# https://blog.csdn.net/xxm524/article/details/74937308
return unicode(x.toUtf8(), DEFAULT_ENCODING, 'ignore')
return x
else:
return x
+117
View File
@@ -0,0 +1,117 @@
from math import sqrt
from libs.ustr import ustr
import hashlib
import re
import sys
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
QT5 = True
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
QT5 = False
def new_icon(icon):
return QIcon(':/' + icon)
def new_button(text, icon=None, slot=None):
b = QPushButton(text)
if icon is not None:
b.setIcon(new_icon(icon))
if slot is not None:
b.clicked.connect(slot)
return b
def new_action(parent, text, slot=None, shortcut=None, icon=None,
tip=None, checkable=False, enabled=True):
"""Create a new action and assign callbacks, shortcuts, etc."""
a = QAction(text, parent)
if icon is not None:
a.setIcon(new_icon(icon))
if shortcut is not None:
if isinstance(shortcut, (list, tuple)):
a.setShortcuts(shortcut)
else:
a.setShortcut(shortcut)
if tip is not None:
a.setToolTip(tip)
a.setStatusTip(tip)
if slot is not None:
a.triggered.connect(slot)
if checkable:
a.setCheckable(True)
a.setEnabled(enabled)
return a
def add_actions(widget, actions):
for action in actions:
if action is None:
widget.addSeparator()
elif isinstance(action, QMenu):
widget.addMenu(action)
else:
widget.addAction(action)
def label_validator():
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
class Struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def distance(p):
return sqrt(p.x() * p.x() + p.y() * p.y())
def format_shortcut(text):
mod, key = text.split('+', 1)
return '<b>%s</b>+<b>%s</b>' % (mod, key)
def generate_color_by_text(text):
s = ustr(text)
hash_code = int(hashlib.sha256(s.encode('utf-8')).hexdigest(), 16)
r = int((hash_code / 255) % 255)
g = int((hash_code / 65025) % 255)
b = int((hash_code / 16581375) % 255)
return QColor(r, g, b, 100)
def have_qstring():
"""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.'))
def util_qt_strlistclass():
return QStringList if have_qstring() else list
def natural_sort(list, key=lambda s:s):
"""
Sort the list into natural alphanumeric order.
"""
def get_alphanum_key_func(key):
convert = lambda text: int(text) if text.isdigit() else text
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
sort_key = get_alphanum_key_func(key)
list.sort(key=sort_key)
# QT4 has a trimmed method, in QT5 this is called strip
if QT5:
def trimmed(text):
return text.strip()
else:
def trimmed(text):
return text.trimmed()
+143
View File
@@ -0,0 +1,143 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
import codecs
import os
from libs.constants import DEFAULT_ENCODING
TXT_EXT = '.txt'
ENCODE_METHOD = DEFAULT_ENCODING
class YOLOWriter:
def __init__(self, folder_name, filename, img_size, database_src='Unknown', local_img_path=None):
self.folder_name = folder_name
self.filename = filename
self.database_src = database_src
self.img_size = img_size
self.box_list = []
self.local_img_path = local_img_path
self.verified = False
def add_bnd_box(self, x_min, y_min, x_max, y_max, name, difficult):
bnd_box = {'xmin': x_min, 'ymin': y_min, 'xmax': x_max, 'ymax': y_max}
bnd_box['name'] = name
bnd_box['difficult'] = difficult
self.box_list.append(bnd_box)
def bnd_box_to_yolo_line(self, box, class_list=[]):
x_min = box['xmin']
x_max = box['xmax']
y_min = box['ymin']
y_max = box['ymax']
x_center = float((x_min + x_max)) / 2 / self.img_size[1]
y_center = float((y_min + y_max)) / 2 / self.img_size[0]
w = float((x_max - x_min)) / self.img_size[1]
h = float((y_max - y_min)) / self.img_size[0]
# PR387
box_name = box['name']
if box_name not in class_list:
class_list.append(box_name)
class_index = class_list.index(box_name)
return class_index, x_center, y_center, w, h
def save(self, class_list=[], target_file=None):
out_file = None # Update yolo .txt
out_class_file = None # Update class list .txt
if target_file is None:
out_file = open(
self.filename + TXT_EXT, 'w', encoding=ENCODE_METHOD)
classes_file = os.path.join(os.path.dirname(os.path.abspath(self.filename)), "classes.txt")
out_class_file = open(classes_file, 'w')
else:
out_file = codecs.open(target_file, 'w', encoding=ENCODE_METHOD)
classes_file = os.path.join(os.path.dirname(os.path.abspath(target_file)), "classes.txt")
out_class_file = open(classes_file, 'w')
for box in self.box_list:
class_index, x_center, y_center, w, h = self.bnd_box_to_yolo_line(box, class_list)
# print (classIndex, x_center, y_center, w, h)
out_file.write("%d %.6f %.6f %.6f %.6f\n" % (class_index, x_center, y_center, w, h))
# print (classList)
# print (out_class_file)
for c in class_list:
out_class_file.write(c+'\n')
out_class_file.close()
out_file.close()
class YoloReader:
def __init__(self, file_path, image, class_list_path=None):
# shapes type:
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color, difficult]
self.shapes = []
self.file_path = file_path
if class_list_path is None:
dir_path = os.path.dirname(os.path.realpath(self.file_path))
self.class_list_path = os.path.join(dir_path, "classes.txt")
else:
self.class_list_path = class_list_path
# print (file_path, self.class_list_path)
classes_file = open(self.class_list_path, 'r')
self.classes = classes_file.read().strip('\n').split('\n')
# print (self.classes)
img_size = [image.height(), image.width(),
1 if image.isGrayscale() else 3]
self.img_size = img_size
self.verified = False
# try:
self.parse_yolo_format()
# except:
# pass
def get_shapes(self):
return self.shapes
def add_shape(self, label, x_min, y_min, x_max, y_max, difficult):
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))
def yolo_line_to_shape(self, class_index, x_center, y_center, w, h):
label = self.classes[int(class_index)]
x_min = max(float(x_center) - float(w) / 2, 0)
x_max = min(float(x_center) + float(w) / 2, 1)
y_min = max(float(y_center) - float(h) / 2, 0)
y_max = min(float(y_center) + float(h) / 2, 1)
x_min = round(self.img_size[1] * x_min)
x_max = round(self.img_size[1] * x_max)
y_min = round(self.img_size[0] * y_min)
y_max = round(self.img_size[0] * y_max)
return label, x_min, y_min, x_max, y_max
def parse_yolo_format(self):
bnd_box_file = open(self.file_path, 'r')
for bndBox in bnd_box_file:
class_index, x_center, y_center, w, h = bndBox.strip().split(' ')
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.
self.add_shape(label, x_min, y_min, x_max, y_max, False)
+26
View File
@@ -0,0 +1,26 @@
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class ZoomWidget(QSpinBox):
def __init__(self, value=100):
super(ZoomWidget, self).__init__()
self.setButtonSymbols(QAbstractSpinBox.NoButtons)
self.setRange(1, 500)
self.setSuffix(' %')
self.setValue(value)
self.setToolTip(u'Zoom Level')
self.setStatusTip(self.toolTip())
self.setAlignment(Qt.AlignCenter)
def minimumSizeHint(self):
height = super(ZoomWidget, self).minimumSizeHint().height()
fm = QFontMetrics(self.font())
width = fm.width(str(self.maximum()))
return QSize(width, height)