Spaces:
Sleeping
Sleeping
Update yolov12/detect.py
Browse files- yolov12/detect.py +37 -37
yolov12/detect.py
CHANGED
@@ -1,37 +1,37 @@
|
|
1 |
-
import subprocess
|
2 |
-
import os
|
3 |
-
|
4 |
-
def run_yolov12_inference(img_path):
|
5 |
-
project_dir = "static/uploaded"
|
6 |
-
result_dir = os.path.join(project_dir, "yolov12_output")
|
7 |
-
|
8 |
-
command = [
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
]
|
19 |
-
|
20 |
-
subprocess.run(command, check=True)
|
21 |
-
|
22 |
-
# Try reading the prediction txt file
|
23 |
-
file_name = os.path.basename(img_path)
|
24 |
-
label_file = os.path.join(result_dir, "labels", file_name.replace(".jpg", ".txt"))
|
25 |
-
|
26 |
-
is_damaged = False
|
27 |
-
try:
|
28 |
-
with open(label_file, "r") as f:
|
29 |
-
for line in f:
|
30 |
-
class_id = int(line.split()[0])
|
31 |
-
if class_id == 0: # assume damaged_apple is class 0
|
32 |
-
is_damaged = True
|
33 |
-
break
|
34 |
-
except FileNotFoundError:
|
35 |
-
pass
|
36 |
-
|
37 |
-
return is_damaged
|
|
|
1 |
+
import subprocess
|
2 |
+
import os
|
3 |
+
|
4 |
+
def run_yolov12_inference(img_path):
|
5 |
+
project_dir = "static/uploaded"
|
6 |
+
result_dir = os.path.join(project_dir, "yolov12_output")
|
7 |
+
|
8 |
+
command = [
|
9 |
+
"python3", "yolov12/detect.py",
|
10 |
+
"--weights", "yolov12/runs/detect/train7/weights/best.pt",
|
11 |
+
"--source", img_path,
|
12 |
+
"--conf", "0.25",
|
13 |
+
"--save-txt",
|
14 |
+
"--save-conf",
|
15 |
+
"--project", "static/uploaded",
|
16 |
+
"--name", "yolov12_output",
|
17 |
+
"--exist-ok"
|
18 |
+
]
|
19 |
+
|
20 |
+
subprocess.run(command, check=True)
|
21 |
+
|
22 |
+
# Try reading the prediction txt file
|
23 |
+
file_name = os.path.basename(img_path)
|
24 |
+
label_file = os.path.join(result_dir, "labels", file_name.replace(".jpg", ".txt"))
|
25 |
+
|
26 |
+
is_damaged = False
|
27 |
+
try:
|
28 |
+
with open(label_file, "r") as f:
|
29 |
+
for line in f:
|
30 |
+
class_id = int(line.split()[0])
|
31 |
+
if class_id == 0: # assume damaged_apple is class 0
|
32 |
+
is_damaged = True
|
33 |
+
break
|
34 |
+
except FileNotFoundError:
|
35 |
+
pass
|
36 |
+
|
37 |
+
return is_damaged
|