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