コード例 #1
0
    def load_image_file(filename):
        try:
            raw_DicomData = pydicom.dcmread(filename)

            # pat_name = raw_DicomData.PatientName
            # display_name = pat_name.family_name + ", " + pat_name.given_name
            # Patient_Name = display_name
            # Patient_ID = raw_DicomData.PatientID
            # Modality = raw_DicomData.Modality
            # Study_Date = raw_DicomData.StudyDate

            DicomData = raw_DicomData.pixel_array
            # imageArr = convert_color_space(DicomData, 'YBR_FULL', 'RGB')
            image_pil = Image.fromarray(DicomData)

        except IOError:
            logger.error("Failed opening image file: {}".format(filename))
            return

        # apply orientation to image according to exif
        image_pil = utils.apply_exif_orientation(image_pil)


        with io.BytesIO() as f:
            ext = osp.splitext(filename)[1].lower()
            if PY2 and QT4:
                format = "PNG"
            elif ext in [".jpg", ".jpeg"]:
                format = "JPEG"
            else:
                format = "PNG"
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read()
コード例 #2
0
 def readLstFile(self, path_in):
     """Reads the .lst file and generates corresponding iterator.
     Parameters
     ----------
     path_in: string
     Returns
     -------
     item iterator that contains information in .lst file
     """
     with open(path_in) as fin:
         while True:
             self.checkAborted()
             line = fin.readline()
             if not line:
                 break
             line = [i.strip() for i in line.strip().split('\t')]
             line_len = len(line)
             # check the data format of .lst file
             if line_len < 3:
                 logger.warning(
                     'lst should have at least has three parts, but only has {} parts for {}}'
                     .format(line_len, line))
                 continue
             try:
                 item = [int(line[0])
                         ] + [line[-1]] + [float(i) for i in line[1:-1]]
             except Exception as e:
                 logger.error(
                     'Parsing lst met error for {}, detail: {}'.format(
                         line, e))
                 continue
             yield item
コード例 #3
0
    def load_image_file(filename):
        if osp.splitext(filename)[1] == ".dcm":
            if osp.exists(filename.replace(".dcm", ".jpg")):
                filename = filename.replace(".dcm", ".jpg")
            else:
                if os.name == 'posix':
                    subprocess.call(["./script.sh", filename])
                elif os.name == 'nt':
                    subprocess.call(["script.bat", filename],
                                    **subprocess_args())
                filename = filename.replace(".dcm", ".jpg")

        try:
            image_pil = PIL.Image.open(filename)
        except IOError:
            logger.error("Failed opening image file: {}".format(filename))
            return

        # apply orientation to image according to exif
        image_pil = utils.apply_exif_orientation(image_pil)

        with io.BytesIO() as f:
            ext = osp.splitext(filename)[1].lower()
            if PY2 and QT4:
                format = "PNG"
            elif ext in [".jpg", ".jpeg"]:
                format = "JPEG"
            else:
                format = "PNG"
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read()
コード例 #4
0
    def getLabels(self):
        labels = []
        input_folder = os.path.dirname(self.input_folder_or_file)
        label_file = os.path.join(input_folder,
                                  FormatImageRecord._files['labels'])
        if os.path.isfile(label_file):
            logger.debug('Load labels from file {}'.format(label_file))
            for i, line in enumerate(open(label_file).readlines()):
                labels.append(line)
        else:
            labels = set([])
            logger.debug('No label file found. Start reading dataset')
            record = mx.recordio.MXRecordIO(self.input_folder_or_file, 'r')
            record.reset()
            self.num_samples = 0
            while True:
                try:
                    item = record.read()
                    if not item:
                        break
                    header, s = mx.recordio.unpack(item)
                    for i in range(4, len(header.label), 5):
                        label_idx = str(header.label[i])
                        labels.append(label_idx)
                        self.num_samples = self.num_samples + 1
                except Exception as e:
                    logger.error(traceback.format_exc())
            record.close()

        return list(labels)
コード例 #5
0
    def load_image_file(filename):

        raw_data = None

        ext = osp.splitext(filename)[1].lower()
        if ext == '.dcm':
            image_pil, raw_data = LabelFile.dcm2pil_image(filename)
        else:
            try:
                image_pil = PIL.Image.open(filename)
            except IOError:
                logger.error('Failed opening image file: {}'.format(filename))
                return

            # apply orientation to image according to exif
            image_pil = utils.apply_exif_orientation(image_pil)

        with io.BytesIO() as f:
            ext = osp.splitext(filename)[1].lower()
            if PY2 and QT4:
                format = 'PNG'
            elif ext in ['.jpg', '.jpeg']:
                format = 'JPEG'
            else:
                format = 'PNG'
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read(), raw_data
コード例 #6
0
ファイル: label_file.py プロジェクト: perlestot/labelme
    def load_image_file(filename):
        try:
            # image_pil = PIL.Image.open(filename)
            array = pydicom.read_file(filename).pixel_array
            image_pil = PIL.Image.fromarray(array)
            print('size:', image_pil.size)
            print('Max val:', np.max(image_pil), 'Min val:', np.min(image_pil))
        except IOError:
            logger.error("Failed opening image file: {}".format(filename))
            return

        # apply orientation to image according to exif
        # image_pil = utils.apply_exif_orientation(image_pil)

        with io.BytesIO() as f:
            ext = osp.splitext(filename)[1].lower()
            if PY2 and QT4:
                format = "PNG"
            elif ext in [".jpg", ".jpeg"]:
                format = "JPEG"
            else:
                # .dcm will be saved as "I"
                format = "PNG"

            print('format:', format)
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read()
コード例 #7
0
    def get_default_batch_size(self, network):
        try:
            selected_network = self.networks.currentText()
            network_size_base, network_size_per_batch = network.getGpuSizes()

            import GPUtil
            gpus = GPUtil.getGPUs()
            gpu = gpus[0]

            # Estimate best possible batch size
            # Always take 1GB off to have memory left for peaks
            batch_size = int(
                math.floor((gpu.memoryFree - network_size_base) /
                           network_size_per_batch))
            estimated_memory = gpu.memoryUsed + network_size_base + batch_size * network_size_per_batch
            logger.debug(
                'Estimating batch size: GPU {} (ID:{}) uses {}MB of {}MB. With {} ({}MB, {}MB) the estimated GPU usage is {}MB at a batch size of {}'
                .format(gpu.name, gpu.id, gpu.memoryUsed, gpu.memoryTotal,
                        selected_network, network_size_base,
                        network_size_per_batch, estimated_memory, batch_size))

            return batch_size

        except Exception as e:
            logger.error(traceback.format_exc())
コード例 #8
0
    def makeRecFile(self, data_folder, output_folder, file_name_rec,
                    file_name_idx, file_name_lst):
        image_list = self.readLstFile(
            os.path.join(output_folder, file_name_lst))
        record = mx.recordio.MXIndexedRecordIO(
            os.path.join(output_folder, file_name_idx),
            os.path.join(output_folder, file_name_rec), 'w')

        self.checkAborted()

        args = Map({
            'root': data_folder,
            'pass_through': True,
            'resize': 0,
            'center_crop': False,
            'quality': 95,
            'encoding': '.jpg',
            'pack_label': True,
        })
        try:
            import Queue as queue
        except ImportError:
            import queue
        q_out = queue.Queue()
        cnt = 0
        pre_time = time.time()
        failed_images = []
        for i, item in enumerate(image_list):
            try:
                self.imageEncode(args, i, item, q_out)
                if q_out.empty():
                    continue
                _a, s, _b = q_out.get()
                record.write_idx(item[0], s)
                if cnt % 1000 == 0:
                    cur_time = time.time()
                    logger.debug('time: {} count: {}'.format(
                        cur_time - pre_time, cnt))
                    pre_time = cur_time
                cnt += 1
                self.thread.update.emit(_('Writing dataset ...'), -1, -1)
                self.checkAborted()

            except Exception as e:
                failed_images.append(item)
                logger.error(traceback.format_exc())

        if len(failed_images) > 0:
            msg = _('The following images could not be exported:'
                    ) + '\n' + ', '.join(failed_images)
            self.thread.message.emit(_('Warning'), msg, MessageType.Warning)
            if cnt == 0:
                self.throwUserException(
                    _('Dataset contains no images for export'))

        logger.debug('total time: {} total count: {}'.format(
            time.time() - pre_time, cnt))
コード例 #9
0
 def set_current_format(self, format_key):
     try:
         formats = Export.config('formats')
         format_text = formats[format_key]
         for i in range(self.networks.count()):
             text = self.formats.itemText(i)
             if text == format_text:
                 self.formats.setCurrentIndex(i)
                 break
     except Exception as e:
         logger.error(traceback.format_exc())
コード例 #10
0
 def get_current_network_key(self):
     try:
         selected_network = self.networks.currentText()
         networks = Training.config('networks')
         func_name = None
         for key in networks:
             if selected_network in networks[key]:
                 return key
     except Exception as e:
         logger.error(traceback.format_exc())
         return None
コード例 #11
0
 def set_current_network(self, network_key):
     try:
         networks = Training.config('networks')
         network_text = networks[network_key]
         for i in range(self.networks.count()):
             text = self.networks.itemText(i)
             if text == network_text:
                 self.networks.setCurrentIndex(i)
                 break
     except Exception as e:
         logger.error(traceback.format_exc())
コード例 #12
0
 def getContext(self, gpus=None):
     if gpus is None or gpus == '':
         return [mx.cpu()]
     ctx = [mx.gpu(int(i)) for i in gpus.split(',') if i.strip()]
     try:
         tmp = mx.nd.array([1, 2, 3], ctx=ctx[0])
     except mx.MXNetError as e:
         ctx = [mx.cpu()]
         logger.error(traceback.format_exc())
         logger.warning('Unable to use GPU. Using CPU instead')
     logger.debug('Use context: {}'.format(ctx))
     return ctx
コード例 #13
0
 def _check_image_height_and_width(imageData, imageHeight, imageWidth):
     img_arr = utils.img_b64_to_arr(imageData)
     if imageHeight is not None and img_arr.shape[0] != imageHeight:
         logger.error(
             'imageHeight does not match with imageData or imagePath, '
             'so getting imageHeight from actual image.')
         imageHeight = img_arr.shape[0]
     if imageWidth is not None and img_arr.shape[1] != imageWidth:
         logger.error(
             'imageWidth does not match with imageData or imagePath, '
             'so getting imageWidth from actual image.')
         imageWidth = img_arr.shape[1]
     return imageHeight, imageWidth
コード例 #14
0
 def on_message(self, title, message, kind=None):
     log_msg = 'Show message: {}'.format(message)
     mb = QtWidgets.QMessageBox
     if kind == MessageType.Warning:
         logger.warn(log_msg)
         mb.warning(self, title, message)
     elif kind == MessageType.Error:
         logger.error(log_msg)
         mb.critical(self, title, message)
     elif kind == MessageType.Question:
         logger.info(log_msg)
         mb.question(self, title, message)
     else:
         logger.info(log_msg)
         mb.information(self, title, message)
コード例 #15
0
 def get_current_network(self):
     try:
         selected_network = self.networks.currentText()
         networks = Training.config('networks')
         func_name = None
         for key in networks:
             if selected_network in networks[key]:
                 func_name = key
                 break
         if func_name is not None:
             network = Training.config('objects')[func_name]()
             return network
     except Exception as e:
         logger.error(traceback.format_exc())
         return None
コード例 #16
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('in_file', help='Input file or directory.')
    parser.add_argument('-O', '--output')
    args = parser.parse_args()

    if not distutils.spawn.find_executable('docker'):
        logger.error('Please install docker.')
        sys.exit(1)

    try:
        out_file = labelme_on_docker(args.in_file, args.output)
        if out_file:
            print('Saved to: %s' % out_file)
    except RuntimeError as e:
        sys.stderr.write(e.__str__() + '\n')
        sys.exit(1)
コード例 #17
0
def excepthook(exctype, excvalue, tracebackobj):

    app = QtWidgets.QApplication.instance()
    app.closeAllWindows()

    tbinfofile = StringIO()
    traceback.print_tb(tracebackobj, None, tbinfofile)
    tbinfofile.seek(0)
    tbinfo = tbinfofile.read()
    notice = _('An error occured with following message')
    errmsg = '%s:\n\n%s' % (notice, str(excvalue))
    
    msg = '\n'.join([errmsg, '-' * 80, tbinfo])
    logger.error(msg)

    errorbox = QtWidgets.QMessageBox()
    errorbox.setText(msg)
    errorbox.exec_()
コード例 #18
0
    def load_image_file(filename):
        try:
            image_pil = PIL.Image.open(filename)
        except IOError:
            logger.error("Failed opening image file: {}".format(filename))
            return

        # apply orientation to image according to exif
        image_pil = utils.apply_exif_orientation(image_pil)

        with io.BytesIO() as f:
            ext = osp.splitext(filename)[1].lower()
            if PY2 and QT4:
                format = "PNG"
            elif ext in [".jpg", ".jpeg"]:
                format = "JPEG"
            else:
                format = "PNG"
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read()
コード例 #19
0
ファイル: label_file.py プロジェクト: QinZXX/labelme
    def load_image_file(filename):
        try:
            image_pil = PIL.Image.open(filename)
        except IOError:
            logger.error('Failed opening image file: {}'.format(filename))
            return

        # apply orientation to image according to exif
        image_pil = utils.apply_exif_orientation(image_pil)

        with io.BytesIO() as f:
            ext = osp.splitext(
                filename)[1].lower()  # 分离文件名和扩展名的元祖,ext为扩展名的小写(带.)
            if PY2 and QT4:
                format = 'PNG'
            elif ext in ['.jpg', '.jpeg']:
                format = 'JPEG'
            else:
                format = 'PNG'
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read()
コード例 #20
0
    def load_image_file(filename):
        try:
            image_pil = PIL.Image.open(filename)   # here at this place, I get the output image in the windows
            image_pil, corp_img_bbox = my_crop(filename)
        except IOError:
            logger.error('Failed opening image file: {}'.format(filename))
            return

        # apply orientation to image according to exif
        image_pil = utils.apply_exif_orientation(image_pil)

        with io.BytesIO() as f:
            ext = osp.splitext(filename)[1].lower()
            if PY2 and QT4:
                format = 'PNG'
            elif ext in ['.jpg', '.jpeg']:
                format = 'JPEG'
            else:
                format = 'PNG'
            image_pil.save(f, format=format)
            f.seek(0)
            return f.read()
コード例 #21
0
 def getText(key):
     try:
         config = get_config()
         current_path = os.path.dirname(os.path.abspath(__file__))
         locale_dir = os.path.join(current_path, '..', 'locale')
         local_file = os.path.join(locale_dir, config['language'],
                                   'LC_MESSAGES', 'help_messages.json')
         global_file = os.path.join(locale_dir, 'help_messages.json')
         language_file = global_file
         if os.path.isfile(local_file):
             language_file = local_file
         if not os.path.isfile(language_file):
             logger.error('Help file {} not found'.format(language_file))
             return _('Help file could not be found')
         with open(local_file, 'r', encoding='utf-8') as f:
             data = json.load(f)
             if key in data.keys():
                 return data[key]
         logger.error('No content found for help with key {}'.format(key))
     except Exception as e:
         logger.error('Error while obtaining help message: {}'.format(
             traceback.format_exc()))
     return ''
コード例 #22
0
ファイル: main.py プロジェクト: stricklandming/labelme
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--version',
                        '-V',
                        action='store_true',
                        help='show version')
    parser.add_argument('--reset-config',
                        action='store_true',
                        help='reset qt config')
    parser.add_argument(
        '--logger-level',
        default='info',
        choices=['debug', 'info', 'warning', 'fatal', 'error'],
        help='logger level',
    )
    parser.add_argument('filename', nargs='?', help='image or label filename')
    parser.add_argument(
        '--output',
        '-O',
        '-o',
        help='output file or directory (if it ends with .json it is '
        'recognized as file, else as directory)')
    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
    parser.add_argument(
        '--config',
        dest='config_file',
        help='config file (default: %s)' % default_config_file,
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        '--nodata',
        dest='store_data',
        action='store_false',
        help='stop storing image data to JSON file',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--autosave',
        dest='auto_save',
        action='store_true',
        help='auto save',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--nosortlabels',
        dest='sort_labels',
        action='store_false',
        help='stop sorting labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--flags',
        help='comma separated list of flags OR file containing flags',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labelflags',
        dest='label_flags',
        help='yaml string of label specific flags OR file containing json '
        'string of label specific flags (ex. {person-\d+: [male, tall], '
        'dog-\d+: [black, brown, white], .*: [occluded]})',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labels',
        help='comma separated list of labels OR file containing labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--validatelabel',
        dest='validate_label',
        choices=['exact', 'instance'],
        help='label validation types',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--keep-prev',
        action='store_true',
        help='keep annotation of previous frame',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--epsilon',
        type=float,
        help='epsilon to find nearest vertex on canvas',
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if args.version:
        print('{0} {1}'.format(__appname__, __version__))
        sys.exit(0)

    logger.setLevel(getattr(logging, args.logger_level.upper()))

    if hasattr(args, 'flags'):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, 'r', encoding='utf-8') as f:
                args.flags = [l.strip() for l in f if l.strip()]
        else:
            args.flags = [l for l in args.flags.split(',') if l]

    if hasattr(args, 'labels'):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                args.labels = [l.strip() for l in f if l.strip()]
        else:
            args.labels = [l for l in args.labels.split(',') if l]

    if hasattr(args, 'label_flags'):
        if os.path.isfile(args.label_flags):
            with codecs.open(args.label_flags, 'r', encoding='utf-8') as f:
                args.label_flags = yaml.safe_load(f)
        else:
            args.label_flags = yaml.safe_load(args.label_flags)

    config_from_args = args.__dict__
    config_from_args.pop('version')
    reset_config = config_from_args.pop('reset_config')
    filename = config_from_args.pop('filename')
    output = config_from_args.pop('output')
    config_file = config_from_args.pop('config_file')
    config = get_config(config_from_args, config_file)

    if not config['labels'] and config['validate_label']:
        logger.error('--labels must be specified with --validatelabel or '
                     'validate_label: true in the config file '
                     '(ex. ~/.labelmerc).')
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith('.json'):
            output_file = output
        else:
            output_dir = output

    translator = QtCore.QTranslator()
    translator.load(QtCore.QLocale.system().name(),
                    osp.dirname(osp.abspath(__file__)) + '/translate')
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon('icon'))
    app.installTranslator(translator)
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info('Resetting Qt config: %s' % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.show()
    win.raise_()
    sys.exit(app.exec_())
コード例 #23
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--version', '-V', action='store_true', help='show version'
    )
    parser.add_argument(
        '--reset-config', action='store_true', help='reset qt config'
    )
    parser.add_argument(
        '--logger-level',
        default='info',
        choices=['debug', 'info', 'warning', 'fatal', 'error'],
        help='logger level',
    )
    parser.add_argument('filename', nargs='?', help='image or label filename')
    parser.add_argument(
        '--output',
        '-O',
        '-o',
        help='output file or directory (if it ends with .json it is '
             'recognized as file, else as directory)'
    )
    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
    parser.add_argument(
        '--config',
        dest='config_file',
        help='config file (default: %s)' % default_config_file,
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        '--nodata',
        dest='store_data',
        action='store_false',
        help='stop storing image data to JSON file',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--autosave',
        dest='auto_save',
        action='store_true',
        help='auto save',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--nosortlabels',
        dest='sort_labels',
        action='store_false',
        help='stop sorting labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--flags',
        help='comma separated list of flags OR file containing flags',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labelflags',
        dest='label_flags',
        help='yaml string of label specific flags OR file containing json '
             'string of label specific flags (ex. {person-\d+: [male, tall], '
             'dog-\d+: [black, brown, white], .*: [occluded]})',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labels',
        help='comma separated list of labels OR file containing labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--validatelabel',
        dest='validate_label',
        choices=['exact', 'instance'],
        help='label validation types',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--keep-prev',
        action='store_true',
        help='keep annotation of previous frame',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--epsilon',
        type=float,
        help='epsilon to find nearest vertex on canvas',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--debug-mode',
        dest='debug_mode',
        action='store_true',
        help='start in debug mode',
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if args.version:
        print('{0} {1}'.format(__appname__, __version__))
        sys.exit(0)

    if hasattr(args, 'debug_mode'):
        logger.addStreamHandler(logging.DEBUG)
    else:
        level = args.logger_level.upper()
        logger.addStreamHandler(getattr(logging, level))

    if hasattr(args, 'flags'):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, 'r', encoding='utf-8') as f:
                args.flags = [l.strip() for l in f if l.strip()]
        else:
            args.flags = [l for l in args.flags.split(',') if l]

    if hasattr(args, 'labels'):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                args.labels = [l.strip() for l in f if l.strip()]
        else:
            args.labels = [l for l in args.labels.split(',') if l]

    if hasattr(args, 'label_flags'):
        if os.path.isfile(args.label_flags):
            with codecs.open(args.label_flags, 'r', encoding='utf-8') as f:
                args.label_flags = yaml.load(f)
        else:
            args.label_flags = yaml.load(args.label_flags)

    config_from_args = args.__dict__
    config_from_args.pop('version')
    reset_config = config_from_args.pop('reset_config')
    filename = config_from_args.pop('filename')
    output = config_from_args.pop('output')
    config_file = config_from_args.pop('config_file')
    config = get_config(config_from_args, config_file)

    # localization
    current_path = os.path.dirname(os.path.abspath(__file__))
    locale_dir = os.path.join(current_path, 'locale')
    if os.path.isfile(os.path.join(locale_dir, config['language'], 'LC_MESSAGES', 'labelme.po')):
        lang = gettext.translation('labelme', localedir=locale_dir, languages=[config['language']])
        lang.install()
    else:
        gettext.install('labelme')
    locale.setlocale(locale.LC_ALL, config['language'])

    if not config['labels'] and config['validate_label']:
        logger.error('--labels must be specified with --validatelabel or '
                     'validate_label: true in the config file '
                     '(ex. ~/.labelmerc).')
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith('.json'):
            output_file = output
        else:
            output_dir = output

    # MXNet environment variables
    if 'MXNET_GPU_MEM_POOL_TYPE' in os.environ:
        logger.debug('Environment variable MXNET_GPU_MEM_POOL_TYPE = {}'.format(os.environ['MXNET_GPU_MEM_POOL_TYPE']))
    if 'MXNET_CUDNN_AUTOTUNE_DEFAULT' in os.environ:
        logger.debug('Environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT = {}'.format(os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT']))
    if 'MXNET_HOME' in os.environ:
        logger.debug('Environment variable MXNET_HOME = {}'.format(os.environ['MXNET_HOME']))

    # # Qt environment variable
    # if 'QT_AUTO_SCREEN_SCALE_FACTOR' in os.environ:
    #     logger.debug('Environment variable QT_AUTO_SCREEN_SCALE_FACTOR = {}'.format(os.environ['QT_AUTO_SCREEN_SCALE_FACTOR']))

    # Enable high dpi for 4k monitors
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    if hasattr(QtWidgets.QStyleFactory, 'AA_UseHighDpiPixmaps'):
        QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
    
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon('digivod'))
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info('Resetting Qt config: %s' % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.showMaximized()
    win.raise_()
    sys.excepthook = excepthook
    win.check_startup()
    sys.exit(app.exec_())
コード例 #24
0
ファイル: main.py プロジェクト: wkentaro/labelme
def main():
    try:
        _main()
    except Exception as e:
        logger.error(e)
コード例 #25
0
ファイル: main.py プロジェクト: wkentaro/labelme
def _main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--version', '-V', action='store_true', help='show version'
    )
    parser.add_argument(
        '--reset-config', action='store_true', help='reset qt config'
    )
    parser.add_argument(
        '--logger-level',
        default='info',
        choices=['debug', 'info', 'warning', 'fatal', 'error'],
        help='logger level',
    )
    parser.add_argument('filename', nargs='?', help='image or label filename')
    parser.add_argument(
        '--output',
        '-O',
        '-o',
        help='output file or directory (if it ends with .json it is '
             'recognized as file, else as directory)'
    )
    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
    parser.add_argument(
        '--config',
        dest='config_file',
        help='config file (default: %s)' % default_config_file,
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        '--nodata',
        dest='store_data',
        action='store_false',
        help='stop storing image data to JSON file',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--autosave',
        dest='auto_save',
        action='store_true',
        help='auto save',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--nosortlabels',
        dest='sort_labels',
        action='store_false',
        help='stop sorting labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--flags',
        help='comma separated list of flags OR file containing flags',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labels',
        help='comma separated list of labels OR file containing labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--validatelabel',
        dest='validate_label',
        choices=['exact', 'instance'],
        help='label validation types',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--keep-prev',
        action='store_true',
        help='keep annotation of previous frame',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--epsilon',
        type=float,
        help='epsilon to find nearest vertex on canvas',
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if args.version:
        print('{0} {1}'.format(__appname__, __version__))
        sys.exit(0)

    logger.setLevel(getattr(logging, args.logger_level.upper()))

    if hasattr(args, 'flags'):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, 'r', encoding='utf-8') as f:
                args.flags = [l.strip() for l in f if l.strip()]
        else:
            args.flags = [l for l in args.flags.split(',') if l]

    if hasattr(args, 'labels'):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                args.labels = [l.strip() for l in f if l.strip()]
        else:
            args.labels = [l for l in args.labels.split(',') if l]

    config_from_args = args.__dict__
    config_from_args.pop('version')
    reset_config = config_from_args.pop('reset_config')
    filename = config_from_args.pop('filename')
    output = config_from_args.pop('output')
    config_file = config_from_args.pop('config_file')
    config = get_config(config_from_args, config_file)

    if not config['labels'] and config['validate_label']:
        logger.error('--labels must be specified with --validatelabel or '
                     'validate_label: true in the config file '
                     '(ex. ~/.labelmerc).')
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith('.json'):
            output_file = output
        else:
            output_dir = output

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon('icon'))
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info('Resetting Qt config: %s' % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.show()
    win.raise_()
    sys.exit(app.exec_())
コード例 #26
0
 def error(self, args=None):
     logger.error('Error occured in worker thread: {}'.format(str(args)))
     self.quit()
コード例 #27
0
    def saveDataset(self, samples_per_image, split):
        image_id = 0
        num_samples = 0

        self.checkAborted()

        data = self.getEmptyData()

        class_name_to_id = {}
        labels = self.intermediate.getLabels()
        for i, label in enumerate(labels):
            class_id = i
            class_name = label.strip()
            class_name_to_id[class_name] = class_id
            data['categories'].append(dict(
                supercategory=None,
                id=class_id,
                name=class_name,
            ))

        output_folder = self.output_folder
        output_file = self.getOutputFileName(split)
        out_ann_file = os.path.join(output_folder, output_file)
        out_ann_dir = os.path.dirname(out_ann_file)
        if not os.path.exists(out_ann_dir):
            os.makedirs(out_ann_dir)
        image_folder = os.path.join(output_folder, split)
        if not os.path.exists(image_folder):
            os.makedirs(image_folder)

        input_folder = self.input_folder_or_file

        self.checkAborted()

        failed_images = []
        for image in samples_per_image:
            try:
                samples = samples_per_image[image]
                num_samples = num_samples + len(samples)
                base = replace_special_chars(os.path.splitext(image)[0])
                out_img_file = os.path.join(image_folder, base + '.jpg')
                img_file = os.path.normpath(os.path.join(input_folder, image))
                image = PIL.Image.open(img_file)
                img = np.asarray(image)
                if not os.path.exists(out_img_file):
                    save_image_as_jpeg(image, out_img_file)

                self.checkAborted()

                data['images'].append(dict(
                    license=0,
                    url=None,
                    file_name=os.path.basename(out_img_file),
                    height=img.shape[0],
                    width=img.shape[1],
                    date_captured=None,
                    id=image_id,
                ))

                masks = {}                                     # for area
                segmentations = collections.defaultdict(list)  # for segmentation
                for sample in samples:
                    label = sample.label
                    shape_type = sample.shape_type
                    points = sample.points
                    
                    mask = shape_to_mask(
                        img.shape[:2], points, shape_type
                    )

                    points = np.asarray(points).flatten().tolist()

                    self.checkAborted()

                    if label in masks:
                        masks[label].append(mask)
                        segmentations[label].append(points)
                    else:
                        masks[label] = [mask]
                        segmentations[label] = [points]

                for label, mask in masks.items():
                    for i in range(len(mask)):
                        m = mask[i]
                        cls_name = label.split('-')[0]
                        if cls_name not in class_name_to_id:
                            continue
                        cls_id = class_name_to_id[cls_name]

                        m = np.asfortranarray(m.astype(np.uint8))
                        m = pycocotools.mask.encode(m)
                        area = float(pycocotools.mask.area(m))
                        bbox = pycocotools.mask.toBbox(m).flatten().tolist()

                        data['annotations'].append(dict(
                            id=len(data['annotations']),
                            image_id=image_id,
                            category_id=cls_id,
                            segmentation=segmentations[label][i],
                            area=area,
                            bbox=bbox,
                            iscrowd=0,
                        ))

                        self.thread.update.emit(_('Writing samples ...'), -1, -1)
                        self.checkAborted()

                image_id = image_id + 1

            except Exception as e:
                failed_images.append(image)
                logger.error(traceback.format_exc())

        if len(failed_images) > 0:
            msg = _('The following images could not be exported:') + '\n' + ', '.join(failed_images)
            self.thread.message.emit(_('Warning'), msg, MessageType.Warning)
            if len(data['annotations']) == 0:
                self.throwUserException(_('Dataset contains no images for export'))

        self.checkAborted()

        with open(out_ann_file, 'w') as f:
            json.dump(data, f)

        return num_samples
コード例 #28
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--version",
                        "-V",
                        action="store_true",
                        help="show version")
    parser.add_argument("--reset-config",
                        action="store_true",
                        help="reset qt config")
    parser.add_argument(
        "--logger-level",
        default="info",
        choices=["debug", "info", "warning", "fatal", "error"],
        help="logger level",
    )
    parser.add_argument("filename", nargs="?", help="image or label filename")
    parser.add_argument(
        "--output",
        "-O",
        "-o",
        help="output file or directory (if it ends with .json it is "
        "recognized as file, else as directory)",
    )
    default_config_file = os.path.join(os.path.expanduser("~"), ".labelmerc")
    parser.add_argument(
        "--config",
        dest="config",
        help="config file or yaml-format string (default: {})".format(
            default_config_file),
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        "--nodata",
        dest="store_data",
        action="store_false",
        help="stop storing image data to JSON file",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--autosave",
        dest="auto_save",
        action="store_true",
        help="auto save",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--nosortlabels",
        dest="sort_labels",
        action="store_false",
        help="stop sorting labels",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--flags",
        help="comma separated list of flags OR file containing flags",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--labelflags",
        dest="label_flags",
        help=r"yaml string of label specific flags OR file containing json "
        r"string of label specific flags (ex. {person-\d+: [male, tall], "
        r"dog-\d+: [black, brown, white], .*: [occluded]})",  # NOQA
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--labels",
        help="comma separated list of labels OR file containing labels",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--validatelabel",
        dest="validate_label",
        choices=["exact"],
        help="label validation types",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--keep-prev",
        action="store_true",
        help="keep annotation of previous frame",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--epsilon",
        type=float,
        help="epsilon to find nearest vertex on canvas",
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if os.name == 'posix':
        st = os.stat('./script.sh')
        os.chmod('./script.sh', st.st_mode | stat.S_IEXEC)

    if args.version:
        print("{0} {1}".format(__appname__, __version__))
        sys.exit(0)

    logger.setLevel(getattr(logging, args.logger_level.upper()))

    if hasattr(args, "flags"):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, "r", encoding="utf-8") as f:
                args.flags = [line.strip() for line in f if line.strip()]
        else:
            args.flags = [line for line in args.flags.split(",") if line]

    if hasattr(args, "labels"):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, "r", encoding="utf-8") as f:
                args.labels = [line.strip() for line in f if line.strip()]
        else:
            args.labels = [line for line in args.labels.split(",") if line]

    if hasattr(args, "label_flags"):
        if os.path.isfile(args.label_flags):
            with codecs.open(args.label_flags, "r", encoding="utf-8") as f:
                args.label_flags = yaml.safe_load(f)
        else:
            args.label_flags = yaml.safe_load(args.label_flags)

    config_from_args = args.__dict__
    config_from_args.pop("version")
    reset_config = config_from_args.pop("reset_config")
    filename = config_from_args.pop("filename")
    output = config_from_args.pop("output")
    config_file_or_yaml = config_from_args.pop("config")
    config = get_config(config_file_or_yaml, config_from_args)

    if not config["labels"] and config["validate_label"]:
        logger.error("--labels must be specified with --validatelabel or "
                     "validate_label: true in the config file "
                     "(ex. ~/.labelmerc).")
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith(".json"):
            output_file = output
        else:
            output_dir = output

    translator = QtCore.QTranslator()
    translator.load(
        QtCore.QLocale.system().name(),
        osp.dirname(osp.abspath(__file__)) + "/translate",
    )
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon("icon"))
    app.installTranslator(translator)
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info("Resetting Qt config: %s" % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.show()
    win.raise_()
    app.exec_()
    win.terminate_subprocess()
    sys.exit(1)
コード例 #29
0
ファイル: voc.py プロジェクト: digivod-gmbh/training-solution
    def saveDataset(self, samples_per_image, output_folder, input_folder,
                    split):
        num_samples = 0
        labels = self.intermediate.getLabels()
        class_names = list(labels)

        self.checkAborted()

        output_file = self.getOutputFileName(split)
        out_split_file = os.path.join(output_folder, output_file)
        open(out_split_file, 'w').close()

        failed_images = []
        for image in samples_per_image:
            try:
                samples = samples_per_image[image]
                base = replace_special_chars(os.path.splitext(image)[0])
                out_img_file = os.path.join(output_folder,
                                            FormatVoc._directories['images'],
                                            base + '.jpg')
                out_xml_file = os.path.join(
                    output_folder, FormatVoc._directories['annotations'],
                    base + '.xml')
                img_file = os.path.normpath(os.path.join(input_folder, image))
                image = PIL.Image.open(img_file)
                img = np.asarray(image)
                if not os.path.exists(out_img_file):
                    save_image_as_jpeg(image, out_img_file)

                self.checkAborted()

                #samples_count = len(samples) if len(samples) > 0 else -1
                with open(out_split_file, 'a') as f:
                    f.write(base + '\n')
                    #f.write(base + ' ' + str(samples_count) + '\n')

                # Convert grayscale image to rgb
                if len(img.shape) == 2:
                    img = np.stack((img, ) * 3, axis=-1)

                maker = lxml.builder.ElementMaker()
                xml = maker.annotation(
                    maker.folder(),
                    maker.filename(base + '.jpg'),
                    maker.database(),  # e.g., The VOC2007 Database
                    maker.annotation(),  # e.g., Pascal VOC2007
                    maker.image(),  # e.g., flickr
                    maker.size(
                        maker.height(str(img.shape[0])),
                        maker.width(str(img.shape[1])),
                        maker.depth(str(img.shape[2])),
                    ),
                    maker.segmented(),
                )

                self.checkAborted()

                bboxes = []
                labels = []
                for sample in samples:
                    points = sample.points
                    label = sample.label
                    shape_type = sample.shape_type

                    class_name = label
                    class_id = class_names.index(class_name)

                    # VOC can only handle bounding boxes
                    # Therefore polygons are converted to rectangles
                    if shape_type == 'rectangle':
                        (xmin, ymin), (xmax, ymax) = points
                    elif shape_type == 'polygon':
                        xmin, ymin, xmax, ymax = polygon_to_bbox(points)
                    else:
                        continue
                    bboxes.append([xmin, ymin, xmax, ymax])
                    labels.append(class_id)

                    xml.append(
                        maker.object(
                            maker.name(label),
                            maker.pose(),
                            maker.truncated(str(0)),
                            maker.difficult(str(0)),
                            maker.bndbox(
                                maker.xmin(str(xmin)),
                                maker.ymin(str(ymin)),
                                maker.xmax(str(xmax)),
                                maker.ymax(str(ymax)),
                            ),
                        ))

                    self.thread.update.emit(_('Writing sample ...'), -1, -1)
                    self.checkAborted()

                self.checkAborted()

                with open(out_xml_file, 'wb') as f:
                    f.write(lxml.etree.tostring(xml, pretty_print=True))

                num_samples = num_samples + len(samples)

            except Exception as e:
                failed_images.append(image)
                logger.error(traceback.format_exc())

        if len(failed_images) > 0:
            msg = _('The following images could not be exported:'
                    ) + '\n' + ', '.join(failed_images)
            self.thread.message.emit(_('Warning'), msg, MessageType.Warning)
            if num_samples == 0:
                self.throwUserException(
                    _('Dataset contains no images for export'))

        return num_samples
コード例 #30
0
ファイル: main.py プロジェクト: pabgonza/labelme
def main():
    try:
        _main()
    except Exception as e:
        logger.error(e)
コード例 #31
0
    def prepare_resume_training(self, training_folder):
        try:
            config_file = os.path.join(training_folder,
                                       Training.config('config_file'))
            json_data = {}
            with open(config_file, 'r') as f:
                json_data = json.load(f)

            # Dataset tab
            self.create_dataset_checkbox.widget.setChecked(False)
            self.set_current_format(json_data['dataset'])
            self.train_dataset_folder.setText(
                json_data['args']['train_dataset'])
            if json_data['args']['validate_dataset']:
                self.val_dataset_folder.setText(
                    json_data['args']['validate_dataset'])
            else:
                self.val_dataset_folder.setText('')
            output_folder = os.path.normpath(
                os.path.join(training_folder, '..'))
            self.output_folder.setText(output_folder)
            self.training_name.setText(json_data['args']['training_name'] +
                                       '_resume')

            # Network tab
            self.set_current_network(json_data['network'])
            self.args_epochs.setValue(json_data['args']['epochs'])
            self.args_batch_size.setValue(json_data['args']['batch_size'])
            self.args_learning_rate.setValue(
                json_data['args']['learning_rate'])
            self.args_early_stop_epochs.setValue(
                json_data['args']['early_stop_epochs'])
            gpus = json_data['args']['gpus'].split(',')
            l = len(self.gpu_label_text) + 1
            for box in self.gpu_checkboxes:
                checked = box.text()[l:] in gpus
                box.setChecked(checked)

            # Resume
            self.resume_file.clear()
            params_pattern = '{}_*_*.params'.format(
                json_data['args']['save_prefix'])
            params_path = os.path.join(training_folder, params_pattern)
            for params_file in reversed(glob.glob(params_path)):
                parts = os.path.splitext(params_file)[0].split('_')
                epoch = int(parts[-2]) + 1
                accuracy = float(parts[-1])
                label = '{} {} ({}={})'.format(_('Epoch'), epoch,
                                               _('Accuracy'), accuracy)
                self.resume_file.addItem(label, (params_file, epoch))
            self.resume_file.setCurrentIndex(0)
            self.resume_file_group.show()

            self.training_btn.setText(_('Resume training'))

        except Exception as e:
            logger.error(traceback.format_exc())
            mb = QtWidgets.QMessageBox
            mb.warning(self, _('Training'),
                       _('Applying config of previous training failed'))
            self.resume_folder.setText('')
コード例 #32
0
    def importToIntermediate(self, annotation_file, output_folder, input_folder):
        with open(annotation_file, 'r') as f:
            data = json.load(f)

        class_id_to_name = {}
        for category in data['categories']:
            class_id_to_name[category['id']] = category['name']
        
        split = self.fileNameToSplit(annotation_file)
        if split is False:
            raise Exception(_('Split could not be detected'))
        else:
            split = '../' + split

        self.thread.update.emit(_('Loading dataset ...'), 10, -1)
        self.checkAborted()

        image_id_to_path = {}
        image_id_to_size = {}
        for idx, image in enumerate(data['images']):
            src_image = os.path.join(input_folder, split, image['file_name'])
            dst_image = os.path.join(output_folder, os.path.basename(image['file_name']))
            if not os.path.exists(dst_image):
                shutil.copyfile(src_image, dst_image)
            image_id_to_path[image['id']] = dst_image
            image_id_to_size[image['id']] = (image['height'], image['width'])

            percentage = idx / len(data['images']) * 40
            self.thread.update.emit(_('Loading dataset ...'), 10 + percentage, -1)
            self.checkAborted()

        for idx, annotation in enumerate(data['annotations']):
            image_id = annotation['image_id']
            image_path = image_id_to_path[image_id]
            image_size = image_id_to_size[image_id]
            label_name = class_id_to_name[annotation['category_id']]
            segmentations = annotation['segmentation']
            # In case segmentation is in form [[x1, y1, x2, y2, ...]]
            if len(segmentations) == 1 and len(segmentations[0]) > 1:
                segmentations = annotation['segmentation'][0]
            is_crowd = annotation['iscrowd'] == 1
            points = []

            try:
                # Skip annotations with flag iscrowd
                if not is_crowd:
                    for i in range(0, len(segmentations)-1, 2):
                        points.append([float(segmentations[i]), float(segmentations[i+1])])
                    if self.isPolygon(points):
                        if self.isBBox(points):
                            points = self.bboxToPolygon(points)
                        self.intermediate.addSample(image_path, image_size, label_name, points, 'polygon')
                    else:
                        bbox = annotation['bbox']
                        points = [bbox[0], bbox[1], bbox[0]+bbox[2], bbox[1]+bbox[3]]
                        self.intermediate.addSample(image_path, image_size, label_name, points, 'rectangle')
            except Exception as e:
                logger.error(traceback.format_exc())

            percentage = idx / len(data['annotations']) * 40
            self.thread.update.emit(_('Loading dataset ...'), 50 + percentage, -1)
            self.checkAborted()