import subprocess import os from pathlib import Path def run_yolov12_inference(img_path): project_dir = "static/uploaded" result_dir = os.path.join(project_dir, "yolov12_output") command = [ "python3", "yolov12/detect.py", "--weights", "yolov12/runs/detect/train7/weights/best.pt", "--source", img_path, "--conf", "0.25", "--save-txt", "--save-conf", "--project", "static/uploaded", "--name", "yolov12_output", "--exist-ok" ] # TEMPORARY: simulate label output filename = Path(args.source).name label_path = Path(args.project) / args.name / "labels" label_path.mkdir(parents=True, exist_ok=True) with open(label_path / filename.replace(".jpg", ".txt"), "w") as f: f.write("0 0.5 0.5 1 1 0.90\n") # fake class 0 subprocess.run(command, check=True) # Try reading the prediction txt file file_name = os.path.basename(img_path) label_file = os.path.join(result_dir, "labels", file_name.replace(".jpg", ".txt")) is_damaged = False try: with open(label_file, "r") as f: for line in f: class_id = int(line.split()[0]) if class_id == 0: # assume damaged_apple is class 0 is_damaged = True break except FileNotFoundError: pass return is_damaged