Skip to content

Commit

Permalink
Keep track of output files when saving in multiple steps
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusG committed Jan 29, 2017
1 parent 14ed52f commit 68ec36e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 35 deletions.
37 changes: 24 additions & 13 deletions truth_and_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 16,7 @@
qtCreatorFile = "truth_and_crop_qt4.ui"

# Control flags
DEBUG = True
DEBUG = False

# Constants
APP_NAME = 'Truth and Crop'
Expand Down Expand Up @@ -57,7 57,7 @@ def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.setWindowIcon(QtGui.QIcon('images/icon.png'))
self.setWindowIcon(QtGui.QIcon('images/icon.png'))

# Init
self.class_label = CLASS_OTHER
Expand All @@ -66,6 66,7 @@ def __init__(self):
self.cropping = False
self.toggleSuperPx = False
self.superPxGenerated = False
self.textEditMode.setText("Label")
self.labeled_superpixel_list = []
self.__init_lcds()
self.w = self.wndBox.value()
Expand Down Expand Up @@ -160,6 161,10 @@ def __handle_previous_btn(self, event):

def __handle_crop_btn(self, event):
self.cropping = not self.cropping
if self.cropping == True:
self.textEditMode.setText("Cropping")
else:
self.textEditMode.setText("Label")

# Save the output
def __handle_done_btn(self, event):
Expand All @@ -175,7 180,8 @@ def __handle_done_btn(self, event):
if not os.path.exists(rgb_mask_path):
os.makedirs(rgb_mask_path)

self.original = cv2.cvtColor(self.original, cv2.COLOR_RGB2BGR)
# Convert back to BGR so that OpenCV can write out properly
output_image = cv2.cvtColor(self.original, cv2.COLOR_RGB2BGR).copy()

# Separate currentImage into dir and filename, can discard dir
__, img_name = os.path.split(self.currentImage)
Expand All @@ -190,7 196,7 @@ def __handle_done_btn(self, event):

# Make PASCAL fmt segmentation_mask as well

height, width, __ = self.original.shape
height, width, __ = output_image.shape

# Initialize empty RGB array
array = np.empty((height, width, self.cmap.shape[
Expand All @@ -207,19 213,20 @@ def __handle_done_btn(self, event):
if self.class_4_qty > 0:
array[self.segmentation_mask == 255] = self.cmap[255]

l = len(crop_list)
crop_list_len = len(crop_list)
for i, (x, y) in enumerate(crop_list):

# Detailed cropped image suffix.
details = self.__generate_image_details(img_name, i, x, y)
details = self.__generate_image_details(
img_name, i self.count, x, y)

y_lwr = y - self.w > 0
y_upr = y self.w < height
x_lwr = x - self.w > 0
x_upr = x self.w < width
if y_lwr and y_upr and x_lwr and x_upr:

cropped_image = self.original[
cropped_image = output_image[
y - self.w:y self.w, x - self.w:x self.w, :]
cropped_int_mask = self.segmentation_mask[
y - self.w:y self.w, x - self.w:x self.w]
Expand All @@ -241,11 248,13 @@ def __handle_done_btn(self, event):
x, y, self.w))
print(Style.RESET_ALL)

self.__reset_state()
#print(i)
for i in range(l):
for i in range(crop_list_len):
crop_list.pop()
print(crop_list)
if DEBUG == True:
print(crop_list)

self.count = crop_list_len
self.__reset_state()

# Save the output
def __handle_toggle_btn(self, event):
Expand Down Expand Up @@ -347,6 356,7 @@ def load_new_image(self):
self.load_opencv_to_canvas()
self.__init_lcds()
self.__reset_state()
self.count = 0

def __generate_image_details(self, img_name, count, x, y):

Expand Down Expand Up @@ -439,7 449,8 @@ def btnstate(self, b):
print(b.text() " is deselected")

def update_canvas(self, img, height, width):
print("update_canvas: height=%d,width=%d" % (height,width))
if DEBUG == True:
print("update_canvas: height=%d,width=%d" % (height, width))
bytesPerLine = 3 * width
qImg = QImage(img, width, height,
bytesPerLine, QImage.Format_RGB888)
Expand All @@ -464,7 475,7 @@ def load_opencv_to_canvas(self):
print("self.ds = %d" % self.ds)
self.cv_img = cv2.imread(self.currentImage)[::self.ds, ::self.ds, :]
self.cv_img = cv2.cvtColor(
self.cv_img, cv2.COLOR_BGR2RGB).astype(np.uint8)
self.cv_img, cv2.COLOR_BGR2RGB).astype(np.uint8)

height, width, __ = self.cv_img.shape
self.update_canvas(self.cv_img, height, width)
Expand Down
39 changes: 32 additions & 7 deletions truth_and_crop_qt4.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1209</width>
<width>1212</width>
<height>923</height>
</rect>
</property>
Expand Down Expand Up @@ -62,7 62,7 @@
<property name="geometry">
<rect>
<x>900</x>
<y>380</y>
<y>360</y>
<width>71</width>
<height>141</height>
</rect>
Expand Down Expand Up @@ -124,7 124,7 @@
<property name="geometry">
<rect>
<x>980</x>
<y>380</y>
<y>360</y>
<width>211</width>
<height>141</height>
</rect>
Expand Down Expand Up @@ -439,7 439,7 @@ p, li { white-space: pre-wrap; }
<property name="geometry">
<rect>
<x>890</x>
<y>350</y>
<y>330</y>
<width>311</width>
<height>181</height>
</rect>
Expand All @@ -465,15 465,38 @@ p, li { white-space: pre-wrap; }
<property name="geometry">
<rect>
<x>890</x>
<y>540</y>
<y>530</y>
<width>311</width>
<height>271</height>
<height>281</height>
</rect>
</property>
<property name="title">
<string/>
</property>
</widget>
<widget class="QTextEdit" name="textEditMode">
<property name="geometry">
<rect>
<x>1100</x>
<y>520</y>
<width>104</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_9">
<property name="geometry">
<rect>
<x>1056</x>
<y>520</y>
<width>41</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Mode:</string>
</property>
</widget>
<zorder>groupBox_3</zorder>
<zorder>groupBox_2</zorder>
<zorder>groupBox</zorder>
Expand All @@ -486,13 509,15 @@ p, li { white-space: pre-wrap; }
<zorder>horizontalLayoutWidget_4</zorder>
<zorder>horizontalLayoutWidget_5</zorder>
<zorder>label_5</zorder>
<zorder>textEdit</zorder>
<zorder>label_9</zorder>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1209</width>
<width>1212</width>
<height>19</height>
</rect>
</property>
Expand Down
41 changes: 26 additions & 15 deletions ui_truth_and_crop_qt4.h
Original file line number Diff line number Diff line change
@@ -1,13 1,13 @@
/********************************************************************************
** Form generated from reading UI file 'truth_and_crop_qt4i11763.ui'
** Form generated from reading UI file 'truth_and_crop_qt4Sa2743.ui'
**
** Created by: Qt User Interface Compiler version 5.6.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef TRUTH_AND_CROP_QT4I11763_H
#define TRUTH_AND_CROP_QT4I11763_H
#ifndef TRUTH_AND_CROP_QT4SA2743_H
#define TRUTH_AND_CROP_QT4SA2743_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
Expand Down Expand Up @@ -62,7 62,7 @@ class Ui_TruthAndCrop
QRadioButton *class_mussel;
QRadioButton *class_ciona;
QRadioButton *class_styela;
QRadioButton *class_styela_2;
QRadioButton *class_void;
QVBoxLayout *verticalLayout_6;
QLCDNumber *lcdNumber_0;
QLCDNumber *lcdNumber_1;
Expand Down Expand Up @@ -94,14 94,16 @@ class Ui_TruthAndCrop
QGroupBox *groupBox;
QGroupBox *groupBox_2;
QGroupBox *groupBox_3;
QTextEdit *textEdit;
QLabel *label_9;
QMenuBar *menubar;
QStatusBar *statusbar;

void setupUi(QMainWindow *TruthAndCrop)
{
if (TruthAndCrop->objectName().isEmpty())
TruthAndCrop->setObjectName(QStringLiteral("TruthAndCrop"));
TruthAndCrop->resize(1209, 923);
TruthAndCrop->resize(1212, 923);
centralwidget = new QWidget(TruthAndCrop);
centralwidget->setObjectName(QStringLiteral("centralwidget"));
scrollArea = new QScrollArea(centralwidget);
Expand All @@ -128,7 130,7 @@ class Ui_TruthAndCrop
scrollArea->setWidget(scrollAreaWidgetContents_2);
horizontalLayoutWidget = new QWidget(centralwidget);
horizontalLayoutWidget->setObjectName(QStringLiteral("horizontalLayoutWidget"));
horizontalLayoutWidget->setGeometry(QRect(900, 380, 71, 141));
horizontalLayoutWidget->setGeometry(QRect(900, 360, 71, 141));
verticalLayout_4 = new QVBoxLayout(horizontalLayoutWidget);
verticalLayout_4->setObjectName(QStringLiteral("verticalLayout_4"));
verticalLayout_4->setContentsMargins(0, 0, 0, 0);
Expand Down Expand Up @@ -169,7 171,7 @@ class Ui_TruthAndCrop

horizontalLayoutWidget_2 = new QWidget(centralwidget);
horizontalLayoutWidget_2->setObjectName(QStringLiteral("horizontalLayoutWidget_2"));
horizontalLayoutWidget_2->setGeometry(QRect(980, 380, 211, 141));
horizontalLayoutWidget_2->setGeometry(QRect(980, 360, 211, 141));
verticalLayout_5 = new QVBoxLayout(horizontalLayoutWidget_2);
verticalLayout_5->setObjectName(QStringLiteral("verticalLayout_5"));
verticalLayout_5->setContentsMargins(0, 0, 0, 0);
Expand Down Expand Up @@ -226,10 228,10 @@ class Ui_TruthAndCrop

verticalLayout->addWidget(class_styela);

class_styela_2 = new QRadioButton(horizontalLayoutWidget_3);
class_styela_2->setObjectName(QStringLiteral("class_styela_2"));
class_void = new QRadioButton(horizontalLayoutWidget_3);
class_void->setObjectName(QStringLiteral("class_void"));

verticalLayout->addWidget(class_styela_2);
verticalLayout->addWidget(class_void);


horizontalLayout_4->addLayout(verticalLayout);
Expand Down Expand Up @@ -374,13 376,19 @@ class Ui_TruthAndCrop
label_5->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
groupBox = new QGroupBox(centralwidget);
groupBox->setObjectName(QStringLiteral("groupBox"));
groupBox->setGeometry(QRect(890, 350, 311, 181));
groupBox->setGeometry(QRect(890, 330, 311, 181));
groupBox_2 = new QGroupBox(centralwidget);
groupBox_2->setObjectName(QStringLiteral("groupBox_2"));
groupBox_2->setGeometry(QRect(890, -10, 311, 351));
groupBox_3 = new QGroupBox(centralwidget);
groupBox_3->setObjectName(QStringLiteral("groupBox_3"));
groupBox_3->setGeometry(QRect(890, 540, 311, 271));
groupBox_3->setGeometry(QRect(890, 530, 311, 281));
textEdit = new QTextEdit(centralwidget);
textEdit->setObjectName(QStringLiteral("textEdit"));
textEdit->setGeometry(QRect(1100, 520, 104, 21));
label_9 = new QLabel(centralwidget);
label_9->setObjectName(QStringLiteral("label_9"));
label_9->setGeometry(QRect(1056, 520, 41, 21));
TruthAndCrop->setCentralWidget(centralwidget);
groupBox_3->raise();
groupBox_2->raise();
Expand All @@ -394,9 402,11 @@ class Ui_TruthAndCrop
horizontalLayoutWidget_4->raise();
horizontalLayoutWidget_5->raise();
label_5->raise();
textEdit->raise();
label_9->raise();
menubar = new QMenuBar(TruthAndCrop);
menubar->setObjectName(QStringLiteral("menubar"));
menubar->setGeometry(QRect(0, 0, 1209, 19));
menubar->setGeometry(QRect(0, 0, 1212, 19));
TruthAndCrop->setMenuBar(menubar);
statusbar = new QStatusBar(TruthAndCrop);
statusbar->setObjectName(QStringLiteral("statusbar"));
Expand All @@ -420,7 430,7 @@ class Ui_TruthAndCrop
class_mussel->setText(QApplication::translate("TruthAndCrop", "Mussel", 0));
class_ciona->setText(QApplication::translate("TruthAndCrop", "Ciona", 0));
class_styela->setText(QApplication::translate("TruthAndCrop", "Styela", 0));
class_styela_2->setText(QApplication::translate("TruthAndCrop", "Void", 0));
class_void->setText(QApplication::translate("TruthAndCrop", "Void", 0));
refreshBtn->setText(QApplication::translate("TruthAndCrop", "Refresh", 0));
cropBtn->setText(QApplication::translate("TruthAndCrop", "Crop", 0));
toggleBtn->setText(QApplication::translate("TruthAndCrop", "Toggle", 0));
Expand All @@ -447,6 457,7 @@ class Ui_TruthAndCrop
groupBox->setTitle(QString());
groupBox_2->setTitle(QString());
groupBox_3->setTitle(QString());
label_9->setText(QApplication::translate("TruthAndCrop", "Mode:", 0));
} // retranslateUi

};
Expand All @@ -457,4 468,4 @@ namespace Ui {

QT_END_NAMESPACE

#endif // TRUTH_AND_CROP_QT4I11763_H
#endif // TRUTH_AND_CROP_QT4SA2743_H

0 comments on commit 68ec36e

Please sign in to comment.