forked from yyfz/Pi3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
122 lines (100 loc) · 3.89 KB
/
benchmark.py
File metadata and controls
122 lines (100 loc) · 3.89 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
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
113
114
115
116
117
118
119
120
121
122
import os
import sys
import subprocess
import argparse
import re
from typing import List
def modify_pi3_file(pi3_path: str, original_content: str, mode: int) -> None:
"""
Modifies the pi3.py file in memory and writes the new content.
Uses regex to replace 'MODE = ...' line.
"""
print(f" -> Modifying '{os.path.basename(pi3_path)}' to set MODE = {mode}...")
pattern = r"MODE\s*=\s*\d+"
replacement = f"MODE = {mode}"
new_content = re.sub(pattern, replacement, original_content)
with open(pi3_path, 'w', encoding='utf-8') as f:
f.write(new_content)
def run_example_script(cur_path: str, output_dir: str, data_path: str, mode: int) -> None:
"""
Constructs and runs the example.py command, capturing its output.
"""
example_script_path = os.path.join(cur_path, "example.py")
save_path = os.path.join(output_dir, f"{mode}.glb")
log_path = os.path.join(output_dir, f"{mode}.log")
command = [
sys.executable,
example_script_path,
"--data_path", data_path,
"--save_path", save_path,
"--conf", "20"
]
print(f" -> Executing command: {' '.join(command)}")
print(f" -> Saving stdout to '{os.path.relpath(log_path, cur_path)}'...")
result = subprocess.run(
command,
capture_output=True,
text=True,
encoding='utf-8'
)
with open(log_path, 'w', encoding='utf-8') as log_file:
log_file.write("--- STDOUT ---\n")
log_file.write(result.stdout)
log_file.write("\n\n--- STDERR ---\n")
log_file.write(result.stderr)
if result.returncode == 0:
print(f" -> Successfully generated '{os.path.relpath(save_path, cur_path)}'.")
else:
print(f" -> WARNING: Command for mode {mode} finished with exit code {result.returncode}.")
print(f" Check '{os.path.relpath(log_path, cur_path)}' for details.")
def main():
# 1. Setup argparse
parser = argparse.ArgumentParser(
description="Run pi3.py example script for multiple modes."
)
parser.add_argument(
"--data_path",
type=str,
required=True,
help="Path to the directory containing input images (consistent with example.py)."
)
args = parser.parse_args()
data_path = args.data_path
# 2. Define paths
modes: List[int] = [0, 1, 2, 3, 4]
cur_path = os.path.dirname(os.path.abspath(__file__))
pi3_path = os.path.join(cur_path, "pi3", "models", "pi3.py")
example_py_path = os.path.join(cur_path, "example.py")
output_dir = os.path.join(cur_path, "output")
os.makedirs(output_dir, exist_ok=True)
# 3. Check if required files/dirs exist
if not os.path.isfile(pi3_path):
print(f"Error: The target file '{pi3_path}' does not exist.")
sys.exit(1)
if not os.path.isfile(example_py_path):
print(f"Error: The script to run '{example_py_path}' does not exist.")
sys.exit(1)
if not os.path.isdir(data_path):
print(f"Error: The specified data path '{data_path}' does not exist.")
sys.exit(1)
print(f"Found pi3.py at: {pi3_path}")
print(f"Outputs will be saved to: {output_dir}")
print("-" * 40)
# 4. Read the original content of pi3.py
with open(pi3_path, 'r', encoding='utf-8') as f:
original_pi3_content = f.read()
try:
# 5. Loop through each mode
for mode in modes:
print(f"Processing MODE = {mode}...")
modify_pi3_file(pi3_path, original_pi3_content, mode)
run_example_script(cur_path, output_dir, data_path, mode)
print("-" * 40)
finally:
# 6. Restore the original content of pi3.py
print("Restoring original pi3.py file...")
with open(pi3_path, 'w', encoding='utf-8') as f:
f.write(original_pi3_content)
print("Done.")
if __name__ == "__main__":
main()