File size: 3,412 Bytes
81e69dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
def check_exists_files(files,dirs,exit_on_error=True):
    if files is not None:
        if isinstance(files, str):  
            files = [files]
        for file in files:
            if not os.path.isfile(file):
                print(f"File {file} not found")
                if exit_on_error:
                    exit(1)
                else:
                    return 1
    if dirs is not None:
        if isinstance(dirs, str):  
            dirs = [dirs]
        for dir in dirs:
            if not os.path.isdir(dir):
                print(f"Dir {dir} not found")
                if exit_on_error:
                    exit(1)
                else:
                    return 1
    return 0

image_extensions =[".jpg"]

def add_name_suffix(file_name,suffix,replace_suffix=False):
    if not suffix.startswith("_"):#force add
        suffix="_"+suffix

    name,ext = os.path.splitext(file_name)
    if replace_suffix:
        index = name.rfind("_")
        if index!=-1:
            return f"{name[0:index]}{suffix}{ext}"
        
    return f"{name}{suffix}{ext}"

def replace_extension(file_name,new_extension,suffix=None,replace_suffix=False):
    if not new_extension.startswith("."):
        new_extension="."+new_extension

    name,ext = os.path.splitext(file_name)
    new_file = f"{name}{new_extension}"
    if suffix:
        return add_name_suffix(name+new_extension,suffix,replace_suffix)
    return new_file

def list_digit_images(input_dir,sort=True):
    digit_images = []
    global image_extensions
    files = os.listdir(input_dir)
    for file in files:
        if file.endswith(".jpg"):#TODO check image
            base,ext = os.path.splitext(file)
            if not base.isdigit():
                continue
            digit_images.append(file) 

    if sort:
        digit_images.sort()

    return digit_images
def list_suffix_images(input_dir,suffix,is_digit=True,sort=True):
    digit_images = []
    global image_extensions
    files = os.listdir(input_dir)
    for file in files:
        if file.endswith(".jpg"):#TODO check image
            base,ext = os.path.splitext(file)
            if base.endswith(suffix):
                if is_digit:
                    if not base.replace(suffix,"").isdigit():
                        continue
                digit_images.append(file) 

    if sort:
        digit_images.sort()

    return digit_images

import time

class ProgressTracker:
    """
    ๅ‡ฆ็†ใฎ้€ฒๆ—็Šถๆณใ‚’่ฟฝ่ทกใ—ใ€็ตŒ้Žๆ™‚้–“ใจๆฎ‹ใ‚Šๆ™‚้–“ใ‚’่กจ็คบใ™ใ‚‹ใ‚ฏใƒฉใ‚นใ€‚
    """

    def __init__(self,key, total_target):
        """
        ใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟ

        Args:
            total_target (int): ๅ‡ฆ็†ๅฏพ่ฑกใฎ็ทๆ•ฐ
        """
        self.key = key
        self.total_target = total_target
        self.complete_target = 0
        self.start_time = time.time()

    def update(self):
        """
        ้€ฒๆ—ใ‚’1ใค้€ฒใ‚ใ‚‹ใ€‚
        ็ตŒ้Žๆ™‚้–“ใจๆฎ‹ใ‚Šๆ™‚้–“ใ‚’่กจ็คบใ™ใ‚‹ใ€‚
        """
        self.complete_target += 1
        current_time = time.time()
        consumed_time = current_time - self.start_time
        remain_time = (consumed_time / self.complete_target) * (self.total_target - self.complete_target) if self.complete_target > 0 else 0 
        print(f"stepped {self.key} {self.total_target} of {self.complete_target}, consumed {(consumed_time / 60):.1f} min, remain {(remain_time / 60):.1f} min")