Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Add color for class 1 #933

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ __pycache__/
.unison*
.attach*
tmp.*

.venv/
24 changes: 24 additions & 0 deletions better_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Getting Started

After creating and activating a virutal env, run these commands to get setup:
```
pip install pyqt5
pip install lxml
pyrcc5 -o libs/resources.py resources.qrc
```
Then finally run:
```
labelImg.py
```

## To Do
- easy swap class colors
- undo
- dark mode
- Integrate Model
- Model outputs preview/generate
- poly labelling

# Done
- Several custom colors, changeable in constants.py file
- toggle vertices on/off in view tab
37 changes: 18 additions & 19 deletions labelImg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python

# -*- coding: utf-8 -*-
import argparse
import codecs
Expand All @@ -9,20 +9,9 @@
import webbrowser as wb
from functools import partial

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 *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

from libs.combobox import ComboBox
from libs.default_label_combobox import DefaultLabelComboBox
Expand All @@ -31,6 +20,7 @@
from libs.utils import *
from libs.settings import Settings
from libs.shape import Shape, DEFAULT_LINE_COLOR, DEFAULT_FILL_COLOR
import libs.shape
from libs.stringBundle import StringBundle
from libs.canvas import Canvas
from libs.zoomWidget import ZoomWidget
Expand All @@ -50,7 +40,6 @@

__appname__ = 'labelImg'


class WindowMixin(object):

def menu(self, title, actions=None):
Expand All @@ -69,7 +58,6 @@ def toolbar(self, title, actions=None):
self.addToolBar(Qt.LeftToolBarArea, toolbar)
return toolbar


class MainWindow(QMainWindow, WindowMixin):
FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = list(range(3))

Expand Down Expand Up @@ -294,6 +282,13 @@ def get_format_meta(format):
'Ctrl+A', 'hide', get_str('showAllBoxDetail'),
enabled=False)

hide_vertices = action(get_str('hideAllVertices'), partial(self.toggle_vertices, False),
'Ctrl+k', 'hide', get_str('hideAllBoxDetail'),
enabled=False)
show_vertices = action(get_str('showAllVertices'), partial(self.toggle_vertices, True),
'Ctrl+T', 'hide', get_str('showAllBoxDetail'),
enabled=False)

help_default = action(get_str('tutorialDefault'), self.show_default_tutorial_dialog, None, 'help', get_str('tutorialDetail'))
show_info = action(get_str('info'), self.show_info_dialog, None, 'help', get_str('info'))
show_shortcut = action(get_str('shortcut'), self.show_shortcuts_dialog, None, 'help', get_str('shortcut'))
Expand Down Expand Up @@ -399,7 +394,7 @@ def get_format_meta(format):
delete, shape_line_color, shape_fill_color),
onLoadActive=(
close, create, create_mode, edit_mode),
onShapesPresent=(save_as, hide_all, show_all))
onShapesPresent=(save_as, hide_all, show_all, hide_vertices, show_vertices))

self.menus = Struct(
file=self.menu(get_str('menu_file')),
Expand Down Expand Up @@ -435,6 +430,7 @@ def get_format_meta(format):
self.display_label_option,
labels, advanced_mode, None,
hide_all, show_all, None,
hide_vertices, show_vertices, None,
zoom_in, zoom_out, zoom_org, None,
fit_window, fit_width, None,
light_brighten, light_darken, light_org))
Expand All @@ -456,7 +452,7 @@ def get_format_meta(format):
self.actions.advanced = (
open, open_dir, change_save_dir, open_next_image, open_prev_image, save, save_format, None,
create_mode, edit_mode, None,
hide_all, show_all)
hide_all, show_all, hide_vertices, show_vertices)

self.statusBar().showMessage('%s started.' % __appname__)
self.statusBar().show()
Expand Down Expand Up @@ -1090,6 +1086,9 @@ def toggle_polygons(self, value):
for item, shape in self.items_to_shapes.items():
item.setCheckState(Qt.Checked if value else Qt.Unchecked)

def toggle_vertices(self, value):
libs.shape.ENABLE_VERTICES = value

def load_file(self, file_path=None):
"""Load the specified file, or the last opened file if None."""
self.reset_state()
Expand Down
17 changes: 7 additions & 10 deletions libs/canvas.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

# from PyQt4.QtOpenGL import *

# from PyQt4.QtOpenGL import *
from libs.constants import *
from libs.shape import Shape
from libs.utils import distance

Expand Down Expand Up @@ -538,7 +535,7 @@ def paintEvent(self, event):
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.setPen(DRAWING_COLOR)
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()))

Expand All @@ -549,7 +546,7 @@ def paintEvent(self, event):
self.setPalette(pal)
else:
pal = self.palette()
pal.setColor(self.backgroundRole(), QColor(232, 232, 232, 255))
pal.setColor(self.backgroundRole(), BACKGROUND_COLOR)
self.setPalette(pal)

p.end()
Expand Down
11 changes: 11 additions & 0 deletions libs/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@
SETTING_DRAW_SQUARE = 'draw/square'
SETTING_LABEL_FILE_FORMAT= 'labelFileFormat'
DEFAULT_ENCODING = 'utf-8'

# DEFINE COLORS
from PyQt5.QtGui import *
CLASS_COLOR_0 = QColor(0, 0, 255, 255)
CLASS_COLOR_1 = QColor(153, 51, 255, 255)
DRAWING_COLOR = QColor(0, 0, 0)
BACKGROUND_COLOR = QColor(0, 0, 0, 200)

HOVER_COLOR = QColor(106, 180, 255, 125)
VERTEX_FILL_COLOR = QColor(0, 0, 0, 0)

35 changes: 19 additions & 16 deletions libs/shape.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
#!/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 PyQt5.QtGui import *
from PyQt5.QtCore import *

from libs.utils import distance
from libs.constants import *
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_VERTEX_FILL_COLOR = QColor(59, 219, 255, 50)
DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
ENABLE_VERTICES = False


class Shape(object):
Expand Down Expand Up @@ -131,7 +128,8 @@ def paint(self, painter):
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
# color = self.select_fill_color if self.selected else self.fill_color
color = self.select_fill_color if self.selected else HOVER_COLOR
painter.fillPath(line_path, color)

def draw_vertex(self, path, i):
Expand All @@ -143,14 +141,19 @@ def draw_vertex(self, path, i):
d *= size
if self._highlight_index is not None:
self.vertex_fill_color = self.h_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:
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"
self.vertex_fill_color = VERTEX_FILL_COLOR
if ENABLE_VERTICES:
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)



def nearest_vertex(self, point, epsilon):
index = None
Expand Down
10 changes: 3 additions & 7 deletions libs/stringBundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
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 *

from PyQt5.QtCore import *



class StringBundle:
Expand Down
25 changes: 12 additions & 13 deletions libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
import hashlib
import re
import sys
from libs.constants import *

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

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
QT5 = True

def new_icon(icon):
return QIcon(':/' + icon)


def new_button(text, icon=None, slot=None):
b = QPushButton(text)
if icon is not None:
Expand All @@ -27,7 +21,6 @@ def new_button(text, icon=None, slot=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."""
Expand Down Expand Up @@ -85,7 +78,13 @@ def generate_color_by_text(text):
r = int((hash_code / 255) % 255)
g = int((hash_code / 65025) % 255)
b = int((hash_code / 16581375) % 255)
return QColor(r, g, b, 100)
if text == '0':
color = CLASS_COLOR_0
if text == '1':
color = CLASS_COLOR_1
else:
color = QColor(r, g, b, 100)
return color


def have_qstring():
Expand Down
4 changes: 4 additions & 0 deletions resources/strings/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ editBox=&Edit RectBox
editBoxDetail=Move and edit Boxs
hideAllBox=&Hide RectBox
showAllBox=&Show RectBox
hideAllVertices=&Hide Vertices
showAllVertices=&Show Vertices
tutorialDefault=Tutorial
tutorialChrome=Tutorial(Chrome)
tutorialDetail=Show demo
Expand Down Expand Up @@ -79,6 +81,8 @@ advancedMode=Advanced Mode
advancedModeDetail=Swtich to advanced mode
showAllBoxDetail=Show all bounding boxes
hideAllBoxDetail=Hide all bounding boxes
showAllVerticesDetail=Show all vertices
hideAllVerticesDetail=Hide all vertices
menu_file=&File
menu_edit=&Edit
menu_view=&View
Expand Down