-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_flair.py
More file actions
63 lines (50 loc) · 2.29 KB
/
run_flair.py
File metadata and controls
63 lines (50 loc) · 2.29 KB
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
# This script runs the flair model using the correct settings file (make sure to check/change the paths and the config file location and update the contents of the .config file if needed)
# Author: Jolien Cremers
import subprocess
import re
CONFIG_FILE = "/AIML4OS/WP7/model/config/config_detect_small_irg.yaml"
PATHS_FILE = "/AIML4OS/WP7/model/config/flair_swin-small-irg_settings.txt"
def update_config_line(file_path, key, value):
with open(file_path, "r") as f:
lines = f.readlines()
pattern = re.compile(rf"^{key}:\s.*")
with open(file_path, "w") as f:
for line in lines:
if pattern.match(line):
f.write(f"{key}: {value}\n")
else:
f.write(line)
print(" Running flair-detect with:")
print(f" Config: {CONFIG_FILE}")
print(f" Path list: {PATHS_FILE}")
print("-----------------------------------------")
with open(PATHS_FILE, "r") as f:
for line in f:
line = line.strip()
if not line:
continue
# Parse line using comma or space as delimiter
tokens = re.split(r"[,\s]+", line)
if len(tokens) < 5:
print(f"Skipping invalid line: {line}")
continue
OUTPUT_PATH, OUTPUT_NAME, INPUT_PATH, PIXELS, MARGIN = tokens
if not INPUT_PATH or not OUTPUT_PATH:
print(f"Skipping invalid line: {line}")
continue
update_config_line(CONFIG_FILE, "input_img_path", INPUT_PATH)
update_config_line(CONFIG_FILE, "output_path", OUTPUT_PATH)
update_config_line(CONFIG_FILE, "output_name", OUTPUT_NAME)
update_config_line(CONFIG_FILE, "img_pixels_detection", PIXELS)
update_config_line(CONFIG_FILE, "margin", MARGIN)
print(" Updated config with:")
print(f" input_img_path: {INPUT_PATH}")
print(f" output_path: {OUTPUT_PATH}")
print(f" output_name: {OUTPUT_NAME}")
print(f" img_pixels_detection: {PIXELS}")
print(f" margin: {MARGIN}")
print(f'Running: flair-detect --conf="{CONFIG_FILE}"')
subprocess.run(["flair-detect", "--conf", CONFIG_FILE], check=True)
print(" Done this model")
print("-----------------------------")
print("All models estimated")