File size: 1,742 Bytes
d333e06
af3d691
d333e06
 
 
 
af3d691
d333e06
0715ea0
 
 
d333e06
0715ea0
d333e06
 
 
0715ea0
 
 
d333e06
 
af3d691
 
0715ea0
af3d691
 
0715ea0
d333e06
 
0715ea0
 
 
d333e06
0715ea0
 
 
 
 
 
 
 
 
 
 
 
 
af3d691
d333e06
0715ea0
d333e06
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
46
47
48
49
50
51
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