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

Pep 8 #720

Merged
merged 8 commits into from
Mar 14, 2021
Merged

Pep 8 #720

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
1,564 changes: 782 additions & 782 deletions labelImg.py

Large diffs are not rendered by default.

544 changes: 272 additions & 272 deletions libs/canvas.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions libs/colorDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, parent=None):
self.default = None
self.bb = self.layout().itemAt(1).widget()
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):
self.default = default
Expand All @@ -32,6 +32,6 @@ def getColor(self, value=None, title=None, default=None):
self.setCurrentColor(value)
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:
self.setCurrentColor(self.default)
2 changes: 1 addition & 1 deletion libs/combobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, parent=None, items=[]):
self.items = items
self.cb.addItems(self.items)

self.cb.currentIndexChanged.connect(parent.comboSelectionChanged)
self.cb.currentIndexChanged.connect(parent.combo_selection_changed)

layout.addWidget(self.cb)
self.setLayout(layout)
Expand Down
88 changes: 44 additions & 44 deletions libs/create_ml_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@


class CreateMLWriter:
def __init__(self, foldername, filename, imgsize, shapes, outputfile, databasesrc='Unknown', localimgpath=None):
self.foldername = foldername
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.databasesrc = databasesrc
self.imgsize = imgsize
self.boxlist = []
self.localimgpath = localimgpath
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.outputfile = outputfile
self.output_file = output_file

def write(self):
if os.path.isfile(self.outputfile):
with open(self.outputfile, "r") as file:
if os.path.isfile(self.output_file):
with open(self.output_file, "r") as file:
input_data = file.read()
outputdict = json.loads(input_data)
output_dict = json.loads(input_data)
else:
outputdict = []
output_dict = []

outputimagedict = {
output_image_dict = {
"image": self.filename,
"annotations": []
}
Expand All @@ -45,7 +45,7 @@ def write(self):

height, width, x, y = self.calculate_coordinates(x1, x2, y1, y2)

shapedict = {
shape_dict = {
"label": shape["label"],
"coordinates": {
"x": x,
Expand All @@ -54,77 +54,77 @@ def write(self):
"height": height
}
}
outputimagedict["annotations"].append(shapedict)
output_image_dict["annotations"].append(shape_dict)

# check if image already in output
exists = False
for i in range(0, len(outputdict)):
if outputdict[i]["image"] == outputimagedict["image"]:
for i in range(0, len(output_dict)):
if output_dict[i]["image"] == output_image_dict["image"]:
exists = True
outputdict[i] = outputimagedict
output_dict[i] = output_image_dict
break

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):
if x1 < x2:
xmin = x1
xmax = x2
x_min = x1
x_max = x2
else:
xmin = x2
xmax = x1
x_min = x2
x_max = x1
if y1 < y2:
ymin = y1
ymax = y2
y_min = y1
y_max = y2
else:
ymin = y2
ymax = y1
width = xmax - xmin
y_min = y2
y_max = y1
width = x_max - x_min
if width < 0:
width = width * -1
height = ymax - ymin
height = y_max - y_min
# x and y from center of rect
x = xmin + width / 2
y = ymin + height / 2
x = x_min + width / 2
y = y_min + height / 2
return height, width, x, y


class CreateMLReader:
def __init__(self, jsonpath, filepath):
self.jsonpath = jsonpath
def __init__(self, json_path, file_path):
self.json_path = json_path
self.shapes = []
self.verified = False
self.filename = filepath.split("/")[-1:][0]
self.filename = file_path.split("/")[-1:][0]
try:
self.parse_json()
except ValueError:
print("JSON decoding failed")

def parse_json(self):
with open(self.jsonpath, "r") as file:
inputdata = file.read()
with open(self.json_path, "r") as file:
input_data = file.read()

outputdict = json.loads(inputdata)
output_dict = json.loads(input_data)
self.verified = True

if len(self.shapes) > 0:
self.shapes = []
for image in outputdict:
for image in output_dict:
if image["image"] == self.filename:
for shape in image["annotations"]:
self.add_shape(shape["label"], shape["coordinates"])

def add_shape(self, label, bndbox):
xmin = bndbox["x"] - (bndbox["width"] / 2)
ymin = bndbox["y"] - (bndbox["height"] / 2)
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)

xmax = bndbox["x"] + (bndbox["width"] / 2)
ymax = bndbox["y"] + (bndbox["height"] / 2)
x_max = bnd_box["x"] + (bnd_box["width"] / 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))

def get_shapes(self):
Expand Down
50 changes: 25 additions & 25 deletions libs/labelDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@
from PyQt4.QtGui import *
from PyQt4.QtCore import *

from libs.utils import newIcon, labelValidator
from libs.utils import new_icon, label_validator

BB = QDialogButtonBox


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)

self.edit = QLineEdit()
self.edit.setText(text)
self.edit.setValidator(labelValidator())
self.edit.editingFinished.connect(self.postProcess)
self.edit.setValidator(label_validator())
self.edit.editingFinished.connect(self.post_process)

model = QStringListModel()
model.setStringList(listItem)
model.setStringList(list_item)
completer = QCompleter()
completer.setModel(model)
self.edit.setCompleter(completer)

layout = QVBoxLayout()
layout.addWidget(self.edit)
self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
bb.button(BB.Ok).setIcon(newIcon('done'))
bb.button(BB.Cancel).setIcon(newIcon('undo'))
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.addWidget(bb)

if listItem is not None and len(listItem) > 0:
self.listWidget = QListWidget(self)
for item in listItem:
self.listWidget.addItem(item)
self.listWidget.itemClicked.connect(self.listItemClick)
self.listWidget.itemDoubleClicked.connect(self.listItemDoubleClick)
layout.addWidget(self.listWidget)
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)

Expand All @@ -55,22 +55,22 @@ def validate(self):
if self.edit.text().strip():
self.accept()

def postProcess(self):
def post_process(self):
try:
self.edit.setText(self.edit.text().trimmed())
except AttributeError:
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
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.setSelection(0, len(text))
self.edit.setFocus(Qt.PopupFocusReason)
if move:
cursor_pos = QCursor.pos()
parent_bottomRight = self.parentWidget().geometry()
max_x = parent_bottomRight.x() + parent_bottomRight.width() - self.sizeHint().width()
max_y = parent_bottomRight.y() + parent_bottomRight.height() - self.sizeHint().height()
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())
Expand All @@ -79,14 +79,14 @@ def popUp(self, text='', move=True):
self.move(cursor_pos)
return self.edit.text() if self.exec_() else None

def listItemClick(self, tQListWidgetItem):
def list_item_click(self, t_qlist_widget_item):
try:
text = tQListWidgetItem.text().trimmed()
text = t_qlist_widget_item.text().trimmed()
except AttributeError:
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
text = tQListWidgetItem.text().strip()
text = t_qlist_widget_item.text().strip()
self.edit.setText(text)

def listItemDoubleClick(self, tQListWidgetItem):
self.listItemClick(tQListWidgetItem)
def list_item_double_click(self, t_qlist_widget_item):
self.list_item_click(t_qlist_widget_item)
self.validate()
Loading