forked from xuebinqin/U-2-Net
-
Notifications
You must be signed in to change notification settings - Fork 16
/
alpha.py
executable file
·51 lines (38 loc) · 1.36 KB
/
alpha.py
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
import os
import subprocess
import threading
import time
def extract_alpha(input_file_path, output_file_path):
if not os.path.exists(output_file_path):
subprocess.run(
["magick", input_file_path, "-strip", "-alpha", "extract", output_file_path]
)
current_dir = os.getcwd()
input_dir = os.path.join(current_dir, "clean")
output_dir = os.path.join(current_dir, "masks")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
threads = []
print("This is Pequod, arriving shortly at LZ to extract team Alpha!")
# Extracting alpha using ImageMagick
files = os.listdir(input_dir)
total_files = len(files)
for idx, file in enumerate(files, start=1):
if idx % 20 == 0:
print(f"Processing file {idx} out of {total_files}")
if file.endswith(".png"):
input_file_path = os.path.join(input_dir, file)
output_file_path = os.path.join(output_dir, file)
# Start a new thread to process the image
t = threading.Thread(
target=extract_alpha, args=(input_file_path, output_file_path)
)
threads.append(t)
t.start()
# Control the number of threads to prevent overwhelming the system.
while threading.active_count() > 8: # reduce as necessary!
time.sleep(0.5)
# Wait for all threads to finish
for t in threads:
t.join()
print("Masks extracted!")