Example #1
0
def addList(data_file: DataFileExtended, path: str,
            list_filename: list) -> None:
    with open(os.path.join(path, list_filename)) as fp:
        paths = {}
        data = []
        while True:
            start_time = time.time()
            for line in fp:  # continue with the iteration over the file iterator where we stopped last time
                timestamp = None
                external_id = None
                annotation_id = None
                try:
                    line, timestamp, external_id, annotation_id = line.strip(
                    ).split()
                except:
                    line = line.strip().split()[0]

                file_path, file_name = os.path.split(line)
                if file_path not in paths.keys():
                    paths[file_path] = data_file.table_path(path=file_path)
                    paths[file_path].save()

                if timestamp:
                    timestamp = datetime.strptime(timestamp, '%Y%m%d-%H%M%S')
                    # TODO implement getting time stamps from file for file lists
                else:
                    data_file.getTimeStamp(file_name)

                # extract the extension and frame number
                extension = os.path.splitext(file_name)[1]
                frames = getFrameNumber(line, extension)
                # add the file to the database
                data_new = data_file.add_image(file_name,
                                               extension,
                                               external_id,
                                               frames,
                                               path=paths[file_path],
                                               full_path=os.path.join(
                                                   file_path, file_name),
                                               timestamp=timestamp,
                                               commit=False)
                data.extend(data_new)

                if time.time() - start_time > 0.1:
                    break
            else:  # break if we have reached the end of the file
                break
            data_file.add_bulk(data)
            data = []
        data_file.add_bulk(data)

    BroadCastEvent2("ImagesAdded")
Example #2
0
def addPath(data_file: DataFileExtended,
            path: str,
            file_filter: str = "",
            layer_entry: "Layer" = None,
            subdirectories: bool = False,
            use_natsort: bool = False,
            select_file: str = None,
            window: "ClickPointsWindow" = None):
    # if we should add subdirectories, add them or create a list with only one path
    if subdirectories:
        path_list = iter(sorted(GetSubdirectories(path)))
    else:
        path_list = iter([path])

    if layer_entry is None:
        # get a layer for the paths
        layer_entry = data_file.getLayer("default", create=True)

    # with data_file.db.atomic():
    data = []
    while True:
        # iterate over all folders
        for path in path_list:
            # use glob if a filter is active or just get all the files
            if file_filter != "":
                file_list = glob.glob(os.path.join(path, file_filter))
                file_list = [
                    os.path.split(filename)[1] for filename in file_list
                ]
            else:
                file_list = GetFilesInDirectory(path)
            # if no files are left skip this folder
            if len(file_list) == 0:
                print(
                    "WARNING: folder %s doesn't contain any files ClickPoints can read."
                    % path)
                continue
            # add the folder to the database
            path_entry = data_file.add_path(path)
            # maybe sort the files
            if use_natsort:
                file_list = iter(natsorted(file_list))
            else:
                file_list = iter(sorted(file_list))
            # iterate over all files
            while True:
                for filename in file_list:
                    # extract the extension and frame number
                    extension = os.path.splitext(filename)[1]
                    frames = getFrameNumber(os.path.join(path, filename),
                                            extension)
                    # if the file is not properly readable, skip it
                    if frames == 0:
                        continue
                    # add the file to the database
                    try:
                        data.extend(
                            data_file.add_image(filename,
                                                extension,
                                                None,
                                                frames,
                                                path=path_entry,
                                                layer=layer_entry,
                                                full_path=os.path.join(
                                                    path, filename),
                                                commit=False))
                    except OSError as err:
                        print("ERROR:", err)
                    if len(data) > 100 or filename == select_file:
                        # split the data array in slices of 100
                        for i in range(int(len(data) / 100) + 1):
                            data_file.add_bulk(data[i * 100:i * 100 + 100])
                        data = []
                        # if the file is the file which should be selected jump to that frame
                        if filename == select_file:
                            file = data_file.table_image.get(
                                filename=select_file)
                            window.first_frame = file.sort_index
                            select_file = None
                        break
                else:
                    break
            if len(data) > 100:
                data_file.add_bulk(data)
                data = []
        else:
            data_file.add_bulk(data)
            break
    # data_file.start_adding_timestamps()
    BroadCastEvent2("ImagesAdded")