Tejeshwar's picture
Update yolov12/yolo.py
0715ea0 verified
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