Spaces:
Sleeping
Sleeping
Update yolov12/yolo.py
Browse files- yolov12/yolo.py +26 -8
yolov12/yolo.py
CHANGED
@@ -6,27 +6,45 @@ def run_yolov12_inference(img_path):
|
|
6 |
result_dir = os.path.join(project_dir, "yolov12_output")
|
7 |
label_file = os.path.join(result_dir, "labels", os.path.basename(img_path).replace(".jpg", ".txt"))
|
8 |
|
|
|
|
|
|
|
9 |
command = [
|
10 |
-
"python3", "yolov12/detector.py",
|
11 |
"--source", img_path,
|
12 |
"--project", project_dir,
|
13 |
"--name", "yolov12_output",
|
|
|
|
|
|
|
14 |
]
|
15 |
|
16 |
try:
|
17 |
subprocess.run(command, check=True)
|
|
|
18 |
except Exception as e:
|
19 |
print("β Subprocess failed:", e)
|
|
|
20 |
|
21 |
is_damaged = False
|
22 |
-
|
|
|
|
|
23 |
with open(label_file, "r") as f:
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
print("β οΈ No label file found:", label_file)
|
31 |
|
|
|
32 |
return is_damaged
|
|
|
6 |
result_dir = os.path.join(project_dir, "yolov12_output")
|
7 |
label_file = os.path.join(result_dir, "labels", os.path.basename(img_path).replace(".jpg", ".txt"))
|
8 |
|
9 |
+
print(f"πΈ Running YOLOv12 on image: {img_path}")
|
10 |
+
print(f"π Expected label file: {label_file}")
|
11 |
+
|
12 |
command = [
|
13 |
+
"python3", "yolov12/detector.py",
|
14 |
"--source", img_path,
|
15 |
"--project", project_dir,
|
16 |
"--name", "yolov12_output",
|
17 |
+
"--conf", "0.1", # β¬
οΈ Lowered for test
|
18 |
+
"--save-txt",
|
19 |
+
"--save-conf"
|
20 |
]
|
21 |
|
22 |
try:
|
23 |
subprocess.run(command, check=True)
|
24 |
+
print("β
Subprocess finished successfully.")
|
25 |
except Exception as e:
|
26 |
print("β Subprocess failed:", e)
|
27 |
+
return False
|
28 |
|
29 |
is_damaged = False
|
30 |
+
|
31 |
+
if os.path.exists(label_file):
|
32 |
+
print(f"β
Found label file: {label_file}")
|
33 |
with open(label_file, "r") as f:
|
34 |
+
lines = f.readlines()
|
35 |
+
print(f"π Label file contents:\n{lines}")
|
36 |
+
for line in lines:
|
37 |
+
try:
|
38 |
+
class_id = int(line.split()[0])
|
39 |
+
print(f"π Detected class ID: {class_id}")
|
40 |
+
if class_id == 0: # class 0 is damaged
|
41 |
+
is_damaged = True
|
42 |
+
print("π Damage detected (class 0)")
|
43 |
+
break
|
44 |
+
except Exception as parse_error:
|
45 |
+
print("β οΈ Failed to parse line:", line, "Error:", parse_error)
|
46 |
+
else:
|
47 |
print("β οΈ No label file found:", label_file)
|
48 |
|
49 |
+
print(f"π§ͺ Final result: is_damaged = {is_damaged}")
|
50 |
return is_damaged
|