Spaces:
Sleeping
Sleeping
import os | |
import subprocess | |
def run_yolov12_inference(img_path): | |
project_dir = "static/uploaded" | |
result_dir = os.path.join(project_dir, "yolov12_output") | |
label_file = os.path.join(result_dir, "labels", os.path.basename(img_path).replace(".jpg", ".txt")) | |
print(f"πΈ Running YOLOv12 on image: {img_path}") | |
print(f"π Expected label file: {label_file}") | |
command = [ | |
"python3", "yolov12/detector.py", | |
"--source", img_path, | |
"--project", project_dir, | |
"--name", "yolov12_output", | |
"--conf", "0.1", # β¬ οΈ Lowered for test | |
"--save-txt", | |
"--save-conf" | |
] | |
try: | |
subprocess.run(command, check=True) | |
print("β Subprocess finished successfully.") | |
except Exception as e: | |
print("β Subprocess failed:", e) | |
return False | |
is_damaged = False | |
if os.path.exists(label_file): | |
print(f"β Found label file: {label_file}") | |
with open(label_file, "r") as f: | |
lines = f.readlines() | |
print(f"π Label file contents:\n{lines}") | |
for line in lines: | |
try: | |
class_id = int(line.split()[0]) | |
print(f"π Detected class ID: {class_id}") | |
if class_id == 0: # class 0 is damaged | |
is_damaged = True | |
print("π Damage detected (class 0)") | |
break | |
except Exception as parse_error: | |
print("β οΈ Failed to parse line:", line, "Error:", parse_error) | |
else: | |
print("β οΈ No label file found:", label_file) | |
print(f"π§ͺ Final result: is_damaged = {is_damaged}") | |
return is_damaged | |