Spaces:
Sleeping
Sleeping
File size: 1,384 Bytes
d3d97d9 23a1c71 d3d97d9 23a1c71 d3d97d9 |
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 |
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
|