Create predict.py
Browse files- predict.py +81 -0
predict.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
predict.py β inference helper for yolov8s-rdd2022
|
| 3 |
+
Usage:
|
| 4 |
+
python predict.py --source path/to/images --weights yolov8s_best.pt
|
| 5 |
+
"""
|
| 6 |
+
import argparse
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from ultralytics import YOLO
|
| 9 |
+
|
| 10 |
+
CLASS_NAMES = [
|
| 11 |
+
"D00-LongitudinalCrack",
|
| 12 |
+
"D10-TransverseCrack",
|
| 13 |
+
"D20-AlligatorCrack",
|
| 14 |
+
"D40-Pothole",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
def run(source, weights, conf=0.25, iou=0.45, imgsz=640, device=0, save=True):
|
| 18 |
+
model = YOLO(weights)
|
| 19 |
+
results = model.predict(
|
| 20 |
+
source=source,
|
| 21 |
+
conf=conf,
|
| 22 |
+
iou=iou,
|
| 23 |
+
imgsz=imgsz,
|
| 24 |
+
device=device,
|
| 25 |
+
save=save,
|
| 26 |
+
save_txt=True,
|
| 27 |
+
project="runs/detect",
|
| 28 |
+
name="rdd_predict",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
pothole_count = 0
|
| 32 |
+
total_boxes = 0
|
| 33 |
+
class_counts = {name: 0 for name in CLASS_NAMES}
|
| 34 |
+
|
| 35 |
+
for r in results:
|
| 36 |
+
total_boxes += len(r.boxes)
|
| 37 |
+
for cls in r.boxes.cls.tolist():
|
| 38 |
+
class_counts[CLASS_NAMES[int(cls)]] += 1
|
| 39 |
+
if int(cls) == 3:
|
| 40 |
+
pothole_count += 1
|
| 41 |
+
|
| 42 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
+
# β DETECTION SUMMARY β
|
| 44 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
print("\n" + "="*52)
|
| 46 |
+
print(" π DETECTION SUMMARY")
|
| 47 |
+
print("="*52)
|
| 48 |
+
print(f" Total detections : {total_boxes}")
|
| 49 |
+
for name, count in class_counts.items():
|
| 50 |
+
bar = "β" * min(count, 30)
|
| 51 |
+
print(f" {name:<30} : {count:>4} {bar}")
|
| 52 |
+
print("-"*52)
|
| 53 |
+
|
| 54 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
+
# β π³οΈ POTHOLE (D40) REPORT β
|
| 56 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
pct = pothole_count / max(total_boxes, 1) * 100
|
| 58 |
+
print("\n π³οΈ POTHOLE (D40) REPORT")
|
| 59 |
+
print(" " + "-"*48)
|
| 60 |
+
print(f" Pothole count : {pothole_count}")
|
| 61 |
+
print(f" % of all detections : {pct:.1f}%")
|
| 62 |
+
if pct > 30:
|
| 63 |
+
print(" β οΈ HIGH pothole density β road surface critical")
|
| 64 |
+
elif pct > 10:
|
| 65 |
+
print(" β οΈ MODERATE pothole presence")
|
| 66 |
+
else:
|
| 67 |
+
print(" β
LOW pothole presence")
|
| 68 |
+
print("="*52 + "\n")
|
| 69 |
+
|
| 70 |
+
return results
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
ap = argparse.ArgumentParser()
|
| 75 |
+
ap.add_argument("--source", required=True, help="image / folder / video path")
|
| 76 |
+
ap.add_argument("--weights", default="yolov8s_best.pt")
|
| 77 |
+
ap.add_argument("--conf", type=float, default=0.25)
|
| 78 |
+
ap.add_argument("--iou", type=float, default=0.45)
|
| 79 |
+
ap.add_argument("--device", default="0")
|
| 80 |
+
args = ap.parse_args()
|
| 81 |
+
run(args.source, args.weights, args.conf, args.iou, device=args.device)
|