Spaces:
Sleeping
Sleeping
Upload detect.py
Browse files- yolov12/detect.py +37 -0
yolov12/detect.py
ADDED
@@ -0,0 +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 |
+
"python", "D:/fruit/yolov12/yolo12/yolov12/detect.py",
|
10 |
+
"--weights", "D:/fruit/yolov12/yolo12/yolov12/runs/detect/train7/weights/best.pt",
|
11 |
+
"--source", img_path,
|
12 |
+
"--conf", "0.25",
|
13 |
+
"--save-txt",
|
14 |
+
"--save-conf",
|
15 |
+
"--project", project_dir,
|
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
|