paint defect detection
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
|
||||
import cv2
|
||||
import torch
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from ultralytics import YOLO
|
||||
|
||||
# ---------------- CONFIGURATION ----------------
|
||||
MODEL_PATH = "/home/YOUR_USERNAME/best.pt"
|
||||
IMAGE_SIZE = 832
|
||||
CONFIDENCE = 0.25
|
||||
# ------------------------------------------------
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Paint defect detection using trained YOLO26s"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--source",
|
||||
default="0",
|
||||
help="Camera index (0), image path, or video path"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
default="inference_results",
|
||||
help="Directory for saved results"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-display",
|
||||
action="store_true",
|
||||
help="Do not display the inference window"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
model_path = Path(MODEL_PATH)
|
||||
if not model_path.is_file():
|
||||
raise FileNotFoundError(
|
||||
f"Model not found: {model_path}. Update MODEL_PATH."
|
||||
)
|
||||
|
||||
print("Loading paint defect detection model...")
|
||||
print("PyTorch version:", torch.__version__)
|
||||
print("CUDA available:", torch.cuda.is_available())
|
||||
|
||||
if not torch.cuda.is_available():
|
||||
print("WARNING: CUDA is unavailable. Inference may run on CPU.")
|
||||
device = "cpu"
|
||||
else:
|
||||
device = 0
|
||||
print("GPU:", torch.cuda.get_device_name(0))
|
||||
|
||||
model = YOLO(str(model_path))
|
||||
print("Classes:", model.names)
|
||||
|
||||
# Camera input: live inference
|
||||
if args.source.isdigit():
|
||||
camera_id = int(args.source)
|
||||
cap = cv2.VideoCapture(camera_id)
|
||||
|
||||
if not cap.isOpened():
|
||||
raise RuntimeError(
|
||||
f"Could not open camera {camera_id}. "
|
||||
"Check the camera connection and device index."
|
||||
)
|
||||
|
||||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
|
||||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
|
||||
|
||||
output_dir = Path(args.output)
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
output_path = output_dir / "paint_defects_camera.mp4"
|
||||
|
||||
fps = cap.get(cv2.CAP_PROP_FPS)
|
||||
if not fps or fps <= 1:
|
||||
fps = 20.0
|
||||
|
||||
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||
|
||||
writer = None
|
||||
if not args.no_display:
|
||||
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
||||
writer = cv2.VideoWriter(
|
||||
str(output_path), fourcc, fps, (width, height)
|
||||
)
|
||||
|
||||
print("Camera inference started. Press Q to stop.")
|
||||
|
||||
try:
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
print("Could not read camera frame.")
|
||||
break
|
||||
|
||||
results = model.predict(
|
||||
source=frame,
|
||||
imgsz=IMAGE_SIZE,
|
||||
conf=CONFIDENCE,
|
||||
device=device,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
annotated = results[0].plot()
|
||||
|
||||
if writer is not None:
|
||||
writer.write(annotated)
|
||||
|
||||
if not args.no_display:
|
||||
cv2.imshow("Paint Defect Detection", annotated)
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
finally:
|
||||
cap.release()
|
||||
if writer is not None:
|
||||
writer.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
print("Camera inference stopped.")
|
||||
if not args.no_display:
|
||||
print("Annotated video:", output_path)
|
||||
|
||||
else:
|
||||
# Image or video inference; save annotated output.
|
||||
results = model.predict(
|
||||
source=args.source,
|
||||
imgsz=IMAGE_SIZE,
|
||||
conf=CONFIDENCE,
|
||||
device=device,
|
||||
save=True,
|
||||
project=args.output,
|
||||
name="predict",
|
||||
exist_ok=True,
|
||||
show=not args.no_display,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
for result in results:
|
||||
print("Processed:", result.path)
|
||||
if result.boxes is not None:
|
||||
for box in result.boxes:
|
||||
class_id = int(box.cls.item())
|
||||
confidence = float(box.conf.item())
|
||||
print(
|
||||
f" {model.names[class_id]}: "
|
||||
f"{confidence:.2%}"
|
||||
)
|
||||
|
||||
print("Results saved under:", args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user