Beispiel #1
0
    def __init__(self, app, signed_message):
        super(OutputDialog, self).__init__()
        self.app = app
        self.signed_message = signed_message
        self.setWindowTitle('Digitally Signed Canary Message')
        self.setWindowIcon(QtGui.QIcon(common.get_image_path('icon.png')))
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setModal(True)

        # signed message
        font = QtGui.QFont('Monospace')
        font.setStyleHint(QtGui.QFont.TypeWriter)
        signed_message_label = QtGui.QLabel(self.signed_message)
        signed_message_label.setWordWrap(True)
        signed_message_label.setFont(font)

        # buttons
        buttons_layout = QtGui.QHBoxLayout()
        save_to_file_button = QtGui.QPushButton('Save to File')
        save_to_file_button.clicked.connect(self.save_to_file_clicked)
        copy_to_clipboard_button = QtGui.QPushButton('Copy to Clipboard')
        copy_to_clipboard_button.clicked.connect(self.copy_to_clipboard_clicked)
        buttons_layout.addWidget(save_to_file_button)
        buttons_layout.addWidget(copy_to_clipboard_button)

        # layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(signed_message_label)
        layout.addLayout(buttons_layout)
        self.setLayout(layout)
        self.show()
def main():
    """
    The main() function implements all of the logic that the GUI version of onionshare uses.
    """
    strings.load_strings()

    # start the Qt app
    global qtapp
    qtapp = Application()

    # parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--local-only', action='store_true', dest='local_only', help=strings._("help_local_only"))
    parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open"))
    parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug"))
    parser.add_argument('--transparent', action='store_true', dest='transparent_torification', help=strings._("help_transparent_torification"))
    parser.add_argument('--filenames', metavar='filenames', nargs='+', help=strings._('help_filename'))
    args = parser.parse_args()

    filenames = args.filenames
    if filenames:
        for i in range(len(filenames)):
            filenames[i] = os.path.abspath(filenames[i])

    local_only = bool(args.local_only)
    stay_open = bool(args.stay_open)
    debug = bool(args.debug)
    transparent_torification = bool(args.transparent_torification)

    # create the onionshare icon
    global window_icon
    window_icon = QtGui.QIcon(common.get_image_path('logo.png'))

    # validation
    if filenames:
        valid = True
        for filename in filenames:
            if not os.path.exists(filename):
                alert(strings._("not_a_file", True).format(filename))
                valid = False
        if not valid:
            sys.exit()

    # start the onionshare app
    web.set_stay_open(stay_open)
    web.set_transparent_torification(transparent_torification)
    app = onionshare.OnionShare(debug, local_only, stay_open, transparent_torification)

    # clean up when app quits
    def shutdown():
        app.cleanup()
    qtapp.connect(qtapp, QtCore.SIGNAL("aboutToQuit()"), shutdown)

    # launch the gui
    gui = OnionShareGui(qtapp, app)
    gui.send_files(filenames)

    # all done
    sys.exit(qtapp.exec_())
Beispiel #3
0
    def __init__(self, qtapp, app, web, file_selection):
        super(ServerStatus, self).__init__()
        self.status = self.STATUS_STOPPED
        self.addSpacing(10)

        self.qtapp = qtapp
        self.app = app
        self.web = web
        self.file_selection = file_selection

        # server layout
        self.status_image_stopped = QtGui.QImage(common.get_image_path('server_stopped.png'))
        self.status_image_working = QtGui.QImage(common.get_image_path('server_working.png'))
        self.status_image_started = QtGui.QImage(common.get_image_path('server_started.png'))
        self.status_image_label = QtGui.QLabel()
        self.status_image_label.setFixedWidth(30)
        self.start_server_button = QtGui.QPushButton(strings._('gui_start_server', True))
        self.start_server_button.clicked.connect(self.start_server)
        self.stop_server_button = QtGui.QPushButton(strings._('gui_stop_server', True))
        self.stop_server_button.clicked.connect(self.stop_server)
        server_layout = QtGui.QHBoxLayout()
        server_layout.addWidget(self.status_image_label)
        server_layout.addWidget(self.start_server_button)
        server_layout.addWidget(self.stop_server_button)

        # url layout
        url_font = QtGui.QFont()
        url_font.setPointSize(8)
        self.url_label = QtGui.QLabel()
        self.url_label.setFont(url_font)
        self.url_label.setWordWrap(True)
        self.url_label.setAlignment(QtCore.Qt.AlignCenter)
        self.copy_url_button = QtGui.QPushButton(strings._('gui_copy_url', True))
        self.copy_url_button.clicked.connect(self.copy_url)
        url_layout = QtGui.QHBoxLayout()
        url_layout.addWidget(self.url_label)
        url_layout.addWidget(self.copy_url_button)

        # add the widgets
        self.addLayout(server_layout)
        self.addLayout(url_layout)

        self.update()
Beispiel #4
0
    def __init__(self, qtapp, app, web, file_selection):
        super(ServerStatus, self).__init__()
        self.status = self.STATUS_STOPPED

        self.qtapp = qtapp
        self.app = app
        self.web = web
        self.file_selection = file_selection

        # server layout
        self.status_image_stopped = QtGui.QImage(
            common.get_image_path('server_stopped.png'))
        self.status_image_working = QtGui.QImage(
            common.get_image_path('server_working.png'))
        self.status_image_started = QtGui.QImage(
            common.get_image_path('server_started.png'))
        self.status_image_label = QtGui.QLabel()
        self.status_image_label.setFixedWidth(30)
        self.server_button = QtGui.QPushButton()
        self.server_button.clicked.connect(self.server_button_clicked)
        server_layout = QtGui.QHBoxLayout()
        server_layout.addWidget(self.status_image_label)
        server_layout.addWidget(self.server_button)

        # url layout
        url_font = QtGui.QFont()
        url_font.setPointSize(8)
        self.url_label = QtGui.QLabel()
        self.url_label.setFont(url_font)
        self.url_label.setWordWrap(True)
        self.url_label.setAlignment(QtCore.Qt.AlignCenter)
        self.copy_url_button = QtGui.QPushButton(
            strings._('gui_copy_url', True))
        self.copy_url_button.clicked.connect(self.copy_url)
        url_layout = QtGui.QHBoxLayout()
        url_layout.addWidget(self.url_label)
        url_layout.addWidget(self.copy_url_button)

        # add the widgets
        self.addLayout(server_layout)
        self.addLayout(url_layout)

        self.update()
Beispiel #5
0
def main():
    strings.load_strings()

    # start the Qt app
    global qtapp
    qtapp = Application()

    # parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--local-only', action='store_true', dest='local_only', help=strings._("help_local_only"))
    parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open"))
    parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug"))
    parser.add_argument('--filenames', metavar='filenames', nargs='+', help=strings._('help_filename'))
    args = parser.parse_args()

    filenames = args.filenames
    if filenames:
        for i in range(len(filenames)):
            filenames[i] = os.path.abspath(filenames[i])

    local_only = bool(args.local_only)
    stay_open = bool(args.stay_open)
    debug = bool(args.debug)

    # validation
    if filenames:
        valid = True
        for filename in filenames:
            if not os.path.exists(filename):
                alert(strings._("not_a_file", True).format(filename))
                valid = False
        if not valid:
            sys.exit()

    # create the onionshare icon
    global window_icon
    window_icon = QtGui.QIcon(common.get_image_path('logo.png'))

    # start the onionshare app
    web.set_stay_open(stay_open)
    app = onionshare.OnionShare(debug, local_only, stay_open)

    # clean up when app quits
    def shutdown():
        app.cleanup()
    qtapp.connect(qtapp, QtCore.SIGNAL("aboutToQuit()"), shutdown)

    # launch the gui
    gui = OnionShareGui(qtapp, app)
    gui.send_files(filenames)

    # all done
    sys.exit(qtapp.exec_())
            def __init__(self, parent, image=False):
                self.parent = parent
                super(DropHereLabel, self).__init__(parent=parent)
                self.setAcceptDrops(True)
                self.setAlignment(QtCore.Qt.AlignCenter)

                if image:
                    self.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(common.get_image_path('drop_files.png'))))
                else:
                    self.setText(QtCore.QString(strings._('gui_drag_and_drop', True)))
                    self.setStyleSheet('color: #999999;')

                self.hide()
Beispiel #7
0
            def __init__(self, parent, image=False):
                self.parent = parent
                super(DropHereLabel, self).__init__(parent=parent)
                self.setAcceptDrops(True)
                self.setAlignment(QtCore.Qt.AlignCenter)

                if image:
                    self.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(common.get_image_path('drop_files.png'))))
                else:
                    self.setText(QtCore.QString(strings._('gui_drag_and_drop', True)))
                    self.setStyleSheet('color: #999999;')

                self.hide()
Beispiel #8
0
 def show_image_data_ground_truth(self,
                                  data_df,
                                  image_id,
                                  is_colab,
                                  figsize=(40, 40)):
     # Get the an image id given in the training set for visualization
     vis_df = data_df[data_df['ImageId'] == image_id]
     vis_df = vis_df.reset_index(drop=True)
     class_ids = helpers.get_labels(vis_df)
     masks = helpers.get_masks(vis_df, target_dim=self.target_dim)
     bounding_boxes = helpers.get_bounding_boxes(vis_df, masks)
     class_ids, masks, bounding_boxes = helpers.remove_empty_masks(
         class_ids, masks, bounding_boxes)
     img = Image.open(
         common.get_image_path(self.main_folder_path, image_id,
                               is_colab)).convert("RGB")
     img = helpers.rescale(img, target_dim=self.target_dim)
     self.show_image_data(img,
                          class_ids,
                          masks,
                          bounding_boxes,
                          figsize=figsize)
    content = posts_with_images[post_path]
    header = extract_header(content)
    # :vomit: I should really tidy this up in future but it worksTM
    rendered_path = '/' + '/'.join(
        post_path.split('/content/')[1].split('/')[:-1])
    r = re.findall(IMAGECODE_REGEX, content)
    for match in r:
        image_name = get_image_name(match[0])
        image_metadata.append({
            "title":
            header.get('title'),
            "slug":
            header.get('slug'),
            "post_path":
            rendered_path,
            "image_params":
            match[0],
            "alt_text_present":
            bool(match[1]),
            "alt_text":
            match[1],
            "image_name":
            image_name,
            "image_path":
            get_image_path(post_path, image_name).split('content')[1]
        })

with open(DATA_FILE, 'w') as file:
    json.dump(image_metadata, file, indent=2)

print("~ Gathered image metadata")
Beispiel #10
0
    def __init__(self, app, gpg):
        super(AutoCanaryGui, self).__init__()
        self.app = app
        self.gpg = gpg
        self.settings = Settings()
        self.setWindowTitle('AutoCanary')
        self.setWindowIcon(QtGui.QIcon(common.get_image_path('icon.png')))

        # frequency, year
        self.date_col1_layout = QtGui.QVBoxLayout()

        self.frequency_layout = QtGui.QHBoxLayout()
        self.frequency_label = QtGui.QLabel('Frequency')
        self.frequency = QtGui.QComboBox()
        frequency_options = ["Weekly", "Monthly", "Quarterly", "Semiannually"]
        for option in frequency_options:
            self.frequency.addItem(option)
        option = self.settings.get_frequency()
        if option in frequency_options:
            self.frequency.setCurrentIndex(frequency_options.index(option))
        self.frequency_layout.addWidget(self.frequency_label)
        self.frequency_layout.addWidget(self.frequency)
        self.frequency.activated.connect(self.update_date)

        self.year_layout = QtGui.QHBoxLayout()
        self.year_label = QtGui.QLabel('Year')
        self.year = QtGui.QComboBox()
        y = datetime.date.today().year
        year_options = [str(y - 1), str(y), str(y + 1)]
        for option in year_options:
            self.year.addItem(option)
        option = self.settings.get_year()
        if option in year_options:
            self.year.setCurrentIndex(year_options.index(option))
        self.year_layout.addWidget(self.year_label)
        self.year_layout.addWidget(self.year)
        self.year.activated.connect(self.update_date)

        # weekly dropdown
        self.weekly_layout = QtGui.QHBoxLayout()
        self.weekly_label = QtGui.QLabel('Week')
        self.weekly_dropdown = QtGui.QComboBox()
        self.weekly_layout.addWidget(self.weekly_label)
        self.weekly_layout.addWidget(self.weekly_dropdown)

        # monthly dropdown
        self.monthly_layout = QtGui.QHBoxLayout()
        self.monthly_label = QtGui.QLabel('Month')
        self.monthly_dropdown = QtGui.QComboBox()
        monthly_options = [
            "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"
        ]
        for option in monthly_options:
            self.monthly_dropdown.addItem(option)
        self.monthly_layout.addWidget(self.monthly_label)
        self.monthly_layout.addWidget(self.monthly_dropdown)

        # quarterly radio buttons
        self.quarterly_layout = QtGui.QHBoxLayout()
        self.quarterly_label = QtGui.QLabel('Quarter')
        self.quarterly_q1 = QtGui.QRadioButton("")
        self.quarterly_q2 = QtGui.QRadioButton("")
        self.quarterly_q3 = QtGui.QRadioButton("")
        self.quarterly_q4 = QtGui.QRadioButton("")
        self.quarterly_layout.addWidget(self.quarterly_label)
        self.quarterly_layout.addWidget(self.quarterly_q1)
        self.quarterly_layout.addWidget(self.quarterly_q2)
        self.quarterly_layout.addWidget(self.quarterly_q3)
        self.quarterly_layout.addWidget(self.quarterly_q4)

        # semiannual radio buttons
        self.semiannually_layout = QtGui.QHBoxLayout()
        self.semiannually_label = QtGui.QLabel('Semester')
        self.semiannually_q12 = QtGui.QRadioButton("")
        self.semiannually_q34 = QtGui.QRadioButton("")
        self.semiannually_layout.addWidget(self.semiannually_label)
        self.semiannually_layout.addWidget(self.semiannually_q12)
        self.semiannually_layout.addWidget(self.semiannually_q34)

        # date layout
        self.date_layout = QtGui.QVBoxLayout()
        self.date_layout.addLayout(self.frequency_layout)
        self.date_layout.addLayout(self.year_layout)
        self.date_layout.addLayout(self.weekly_layout)
        self.date_layout.addLayout(self.monthly_layout)
        self.date_layout.addLayout(self.quarterly_layout)
        self.date_layout.addLayout(self.semiannually_layout)

        self.update_date()

        # status
        self.status_layout = QtGui.QHBoxLayout()
        self.status_label = QtGui.QLabel('Status')
        self.status = QtGui.QComboBox()
        status_options = ["All good", "It's complicated"]
        for option in status_options:
            self.status.addItem(option)
        option = self.settings.get_status()
        if option in status_options:
            self.status.setCurrentIndex(status_options.index(option))
        self.status_layout.addWidget(self.status_label)
        self.status_layout.addWidget(self.status)

        # canary text box
        self.textbox = QtGui.QTextEdit()
        self.textbox.setText(self.settings.get_text())

        # key selection
        seckeys = gpg.seckeys_list()
        self.key_selection = QtGui.QComboBox()
        for seckey in seckeys:
            uid = seckey['uid']
            if len(uid) >= 53:
                uid = '{0}...'.format(uid[:50])
            fp = seckey['fp'][-8:]
            text = '{0} [{1}]'.format(uid, fp)
            self.key_selection.addItem(text)
        fp = self.settings.get_fp()
        if fp:
            key_i = 0
            for i, seckey in enumerate(seckeys):
                if seckey['fp'] == fp:
                    key_i = i
            self.key_selection.setCurrentIndex(key_i)

        # buttons
        self.buttons_layout = QtGui.QHBoxLayout()
        self.sign_save_button = QtGui.QPushButton('Save and Sign')
        self.sign_save_button.clicked.connect(self.sign_save_clicked)
        self.sign_once = QtGui.QPushButton('One-Time Sign')
        self.sign_once.clicked.connect(self.sign_once_clicked)
        self.buttons_layout.addWidget(self.sign_save_button)
        self.buttons_layout.addWidget(self.sign_once)

        # layout
        self.layout = QtGui.QVBoxLayout()
        self.layout.addLayout(self.date_layout)
        self.layout.addLayout(self.status_layout)
        self.layout.addWidget(self.textbox)
        self.layout.addWidget(self.key_selection)
        self.layout.addLayout(self.buttons_layout)
        self.setLayout(self.layout)
        self.show()
Beispiel #11
0
    def __init__(self, app, gpg):
        super(AutoCanaryGui, self).__init__()
        self.app = app
        self.gpg = gpg
        self.settings = Settings()
        self.setWindowTitle('AutoCanary')
        self.setWindowIcon(QtGui.QIcon(common.get_image_path('icon.png')))

        # frequency, year
        self.date_col1_layout = QtGui.QVBoxLayout()

        self.frequency_layout = QtGui.QHBoxLayout()
        self.frequency_label = QtGui.QLabel('Frequency')
        self.frequency = QtGui.QComboBox()
        frequency_options = ["Weekly", "Monthly", "Quarterly", "Semiannually"]
        for option in frequency_options:
            self.frequency.addItem(option)
        option = self.settings.get_frequency()
        if option in frequency_options:
            self.frequency.setCurrentIndex(frequency_options.index(option))
        self.frequency_layout.addWidget(self.frequency_label)
        self.frequency_layout.addWidget(self.frequency)
        self.frequency.activated.connect(self.update_date)

        self.year_layout = QtGui.QHBoxLayout()
        self.year_label = QtGui.QLabel('Year')
        self.year = QtGui.QComboBox()
        y = datetime.date.today().year
        year_options = [str(y-1), str(y), str(y+1)]
        for option in year_options:
            self.year.addItem(option)
        option = self.settings.get_year()
        if option in year_options:
            self.year.setCurrentIndex(year_options.index(option))
        self.year_layout.addWidget(self.year_label)
        self.year_layout.addWidget(self.year)
        self.year.activated.connect(self.update_date)

        # weekly dropdown
        self.weekly_layout = QtGui.QHBoxLayout()
        self.weekly_label = QtGui.QLabel('Week')
        self.weekly_dropdown = QtGui.QComboBox()
        self.weekly_layout.addWidget(self.weekly_label)
        self.weekly_layout.addWidget(self.weekly_dropdown)

        # monthly dropdown
        self.monthly_layout = QtGui.QHBoxLayout()
        self.monthly_label = QtGui.QLabel('Month')
        self.monthly_dropdown = QtGui.QComboBox()
        monthly_options = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        for option in monthly_options:
            self.monthly_dropdown.addItem(option)
        self.monthly_layout.addWidget(self.monthly_label)
        self.monthly_layout.addWidget(self.monthly_dropdown)

        # quarterly radio buttons
        self.quarterly_layout = QtGui.QHBoxLayout()
        self.quarterly_label = QtGui.QLabel('Quarter')
        self.quarterly_q1 = QtGui.QRadioButton("")
        self.quarterly_q2 = QtGui.QRadioButton("")
        self.quarterly_q3 = QtGui.QRadioButton("")
        self.quarterly_q4 = QtGui.QRadioButton("")
        self.quarterly_layout.addWidget(self.quarterly_label)
        self.quarterly_layout.addWidget(self.quarterly_q1)
        self.quarterly_layout.addWidget(self.quarterly_q2)
        self.quarterly_layout.addWidget(self.quarterly_q3)
        self.quarterly_layout.addWidget(self.quarterly_q4)

        # semiannual radio buttons
        self.semiannually_layout = QtGui.QHBoxLayout()
        self.semiannually_label = QtGui.QLabel('Semester')
        self.semiannually_q12 = QtGui.QRadioButton("")
        self.semiannually_q34 = QtGui.QRadioButton("")
        self.semiannually_layout.addWidget(self.semiannually_label)
        self.semiannually_layout.addWidget(self.semiannually_q12)
        self.semiannually_layout.addWidget(self.semiannually_q34)

        # date layout
        self.date_layout = QtGui.QVBoxLayout()
        self.date_layout.addLayout(self.frequency_layout)
        self.date_layout.addLayout(self.year_layout)
        self.date_layout.addLayout(self.weekly_layout)
        self.date_layout.addLayout(self.monthly_layout)
        self.date_layout.addLayout(self.quarterly_layout)
        self.date_layout.addLayout(self.semiannually_layout)

        self.update_date()

        # status
        self.status_layout = QtGui.QHBoxLayout()
        self.status_label = QtGui.QLabel('Status')
        self.status = QtGui.QComboBox()
        status_options = ["All good", "It's complicated"]
        for option in status_options:
            self.status.addItem(option)
        option = self.settings.get_status()
        if option in status_options:
            self.status.setCurrentIndex(status_options.index(option))
        self.status_layout.addWidget(self.status_label)
        self.status_layout.addWidget(self.status)

        # canary text box
        self.textbox = QtGui.QTextEdit()
        self.textbox.setText(self.settings.get_text())

        # key selection
        seckeys = gpg.seckeys_list()
        self.key_selection = QtGui.QComboBox()
        for seckey in seckeys:
            uid = seckey['uid']
            if len(uid) >= 53:
                uid = '{0}...'.format(uid[:50])
            fp = seckey['fp'][-8:]
            text = '{0} [{1}]'.format(uid, fp)
            self.key_selection.addItem(text)
        fp = self.settings.get_fp()
        if fp:
            key_i = 0
            for i, seckey in enumerate(seckeys):
                if seckey['fp'] == fp:
                    key_i = i
            self.key_selection.setCurrentIndex(key_i)

        # buttons
        self.buttons_layout = QtGui.QHBoxLayout()
        self.sign_save_button = QtGui.QPushButton('Save and Sign')
        self.sign_save_button.clicked.connect(self.sign_save_clicked)
        self.sign_once = QtGui.QPushButton('One-Time Sign')
        self.sign_once.clicked.connect(self.sign_once_clicked)
        self.buttons_layout.addWidget(self.sign_save_button)
        self.buttons_layout.addWidget(self.sign_once)

        # layout
        self.layout = QtGui.QVBoxLayout()
        self.layout.addLayout(self.date_layout)
        self.layout.addLayout(self.status_layout)
        self.layout.addWidget(self.textbox)
        self.layout.addWidget(self.key_selection)
        self.layout.addLayout(self.buttons_layout)
        self.setLayout(self.layout)
        self.show()
Beispiel #12
0
    def __getitem__(self, idx):
        if self.gather_statistics:
            start = time.time()
        image_id = self.image_ids[idx]
        vis_df = self.data_df[self.data_df['image_id'] == image_id]
        vis_df = vis_df.astype('int32')
        vis_df = vis_df.reset_index(drop=True)
        labels = torch.tensor(vis_df['label'].tolist())

        box_start_ts = time.time()
        boxes = torch.from_numpy(vis_df[['xmin', 'ymin', 'width',
                                         'height']].values).int()
        # convert to [xmin, ymin, xmax, ymax] which is what efficient det expects
        boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
        boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
        # rescale bbox to match the given self.target_dim

        if self.gather_statistics:
            self.inc_by(self.lock, self.total_box_time,
                        time.time() - box_start_ts)

        num_objs = len(labels)

        image_id_idx = idx

        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs, ), dtype=torch.int64)

        target = {}
        labels_field = 'cls'  # 'labels'
        if "faster" in self.model_name:
            target[labels_field] = torch.sub(labels, 1)
            assert torch.min(target[labels_field]) >= 0
            assert torch.max(target[labels_field]) <= self.num_classes - 1
            target[labels_field] = torch.sub(labels, 1).int().numpy()
        else:
            # we only need the correction for the modified model
            target[
                labels_field] = labels  # torch.add(labels, 1)  # refer to fast_collate, this is needed for efficient det
            assert torch.min(target[labels_field]) >= 1
            assert torch.max(target[labels_field]) <= self.num_classes
            target[labels_field] = labels.int().numpy()
        # target["boxes"] = boxes
        target["image_id"] = image_id_idx
        target["iscrowd"] = iscrowd.numpy()
        #         target["image_id"] = torch.tensor(image_id_idx)
        #         target["area"] = torch.tensor(area)
        #         target["iscrowd"] = torch.tensor(iscrowd)

        image_load_start_ts = time.time()
        image_orig = Image.open(
            common.get_image_path(self.main_folder_path, image_id,
                                  self.is_colab)).convert("RGB")
        image, boxes_scaled = helpers.rescale(image_orig,
                                              boxes,
                                              target_dim=self.target_dim)
        # print("Image after rescale 1: shape [{}] min [{}] max [{}]".format(image.shape, torch.min(image), torch.max(image)))
        # boxes_scaled[:, 2] = boxes_scaled[:, 2] - boxes_scaled[:, 0]
        # boxes_scaled[:, 3] = boxes_scaled[:, 3] - boxes_scaled[:, 1]
        target["bbox"] = torch.round(boxes_scaled).double().numpy()
        area = (target["bbox"][:, 3] - target["bbox"][:, 1]) * (
            target["bbox"][:, 2] - target["bbox"][:, 0])
        target["bbox"] = target["bbox"][:, [1, 0, 3, 2]]  # YXYX
        target["area"] = area
        if self.gather_statistics:
            self.inc_by(self.lock, self.total_image_load_time,
                        time.time() - image_load_start_ts)

        # TODO(ofekp): check what happens here when the image is < self.target_dim. What will helpers.py scale method do to the image in this case?
        target["img_size"] = image_orig.size[
            -2:] if self.target_dim is None else (self.target_dim,
                                                  self.target_dim)
        image_orig_max_dim = max(target["img_size"])
        img_scale = self.target_dim / image_orig_max_dim
        target["img_scale"] = 1. / img_scale  # back to original size

        if self.gather_statistics:
            transform_start_ts = time.time()
        if self.transforms is not None:
            image, target = self.transforms(image, target)

        if self.gather_statistics:
            self.inc_by(self.lock, self.total_transform_time,
                        time.time() - transform_start_ts)
            self.inc_by(self.lock, self.images_processed, 1)
            self.inc_by(self.lock, self.total_process_time,
                        time.time() - start)

        assert image.shape[0] <= self.target_dim and image.shape[
            1] <= self.target_dim and image.shape[2] <= self.target_dim
        image = image * 255
        image = image.numpy().astype(np.uint8)  # CHW
        # print("Image after rescale 2: shape [{}] min [{}] max [{}]".format(image.shape, np.min(image), np.max(image)))
        return image, target
Beispiel #13
0
    def __init__(self, parent=None):
        super(FileList, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.QSize(32, 32))
        self.setSortingEnabled(True)

        # drag and drop label
        self.drop_label = QtGui.QLabel(QtCore.QString(strings._('gui_drag_and_drop', True)), parent=self)
        self.drop_label.setAlignment(QtCore.Qt.AlignCenter)
        self.drop_label.setStyleSheet('background: url({0}) no-repeat center center; color: #999999;'.format(common.get_image_path('drop_files.png')))
        self.drop_label.hide()

        self.filenames = []
        self.update()
Beispiel #14
0
    def __getitem__(self, idx):
        if self.gather_statistics:
            start = time.time()
        image_id = self.image_ids[idx]
        vis_df = self.data_df[self.data_df['ImageId'] == image_id]
        vis_df = vis_df.reset_index(drop=True)
        labels = helpers.get_labels(vis_df)
        mask_start_ts = time.time()
        try:
            masks = helpers.get_masks(vis_df, target_dim=self.target_dim)
            for mask in masks:
                assert not torch.any(torch.isnan(mask))
                assert torch.where(mask > 0)[0].shape[0] == torch.sum(
                    mask)  # check only ones and zeros
        except Exception as e:
            self.skipped_images.append(image_id)
            print(
                "ERROR: Skipped image with id [{}] due to a mask exception [{}]"
                .format(image_id, e))
            return
        if self.gather_statistics:
            self.inc_by(self.lock, self.total_mask_time,
                        time.time() - mask_start_ts)

        box_start_ts = time.time()
        boxes = helpers.get_bounding_boxes(vis_df, masks)
        try:
            for box in boxes:
                assert not torch.any(torch.isnan(box))
        except Exception as e:
            self.skipped_images.append(image_id)
            print(
                "ERROR: Skipped image with id [{}] due to a BB exception [{}]".
                format(image_id, e))
            return
        if self.gather_statistics:
            self.inc_by(self.lock, self.total_box_time,
                        time.time() - box_start_ts)

        num_objs = len(labels)

        image_id_idx = idx
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs, ), dtype=torch.int64)

        labels, masks, boxes = helpers.remove_empty_masks(labels, masks, boxes)

        target = {}
        if "faster" in self.model_name:
            target["labels"] = labels
            assert torch.min(target["labels"]) >= 0
            assert torch.max(target["labels"]) <= self.num_classes - 1
        else:
            # we only need the correction for the modified model
            target["labels"] = torch.add(
                labels,
                1)  # refer to fast_collate, this is needed for efficient det
            assert torch.min(target["labels"]) >= 1
            assert torch.max(target["labels"]) <= self.num_classes
        target["masks"] = masks
        target["boxes"] = boxes
        target["image_id"] = image_id_idx
        target["area"] = area
        target["iscrowd"] = iscrowd
        #         target["image_id"] = torch.tensor(image_id_idx)
        #         target["area"] = torch.tensor(area)
        #         target["iscrowd"] = torch.tensor(iscrowd)

        image_load_start_ts = time.time()
        image_orig = Image.open(
            common.get_image_path(self.main_folder_path, image_id,
                                  self.is_colab)).convert("RGB")
        image = helpers.rescale(image_orig, target_dim=self.target_dim)
        if self.gather_statistics:
            self.inc_by(self.lock, self.total_image_load_time,
                        time.time() - image_load_start_ts)

        # TODO(ofekp): check what happens here when the image is < self.target_dim. What will helpers.py scale method do to the image in this case?
        target["img_size"] = image_orig.size[
            -2:] if self.target_dim is None else (self.target_dim,
                                                  self.target_dim)
        image_orig_max_dim = max(target["img_size"])
        img_scale = self.target_dim / image_orig_max_dim
        target["img_scale"] = 1. / img_scale  # back to original size

        if self.gather_statistics:
            transform_start_ts = time.time()
        if self.transforms is not None:
            image, target = self.transforms(image, target)

        if self.gather_statistics:
            self.inc_by(self.lock, self.total_transform_time,
                        time.time() - transform_start_ts)
            self.inc_by(self.lock, self.images_processed, 1)
            self.inc_by(self.lock, self.total_process_time,
                        time.time() - start)

        assert image.shape[0] <= self.target_dim and image.shape[
            1] <= self.target_dim and image.shape[2] <= self.target_dim
        return image, target