|
| 1 | +# Run Ultralytics YOLOv8 pretrained model |
| 2 | + |
| 3 | +YouTube tutorial link: [YOLOv8: Real-Time Object Detection Simplified](https://youtu.be/vegL__weCxY) |
| 4 | + |
| 5 | +First, I recommend you to install the required packages in a virtual environment: |
| 6 | +```bash |
| 7 | +mltu==1.2.3 |
| 8 | +ultralytics==8.1.28 |
| 9 | +torch==2.0.0 |
| 10 | +torchvision==0.15.1 |
| 11 | +onnxruntime==1.15.1 |
| 12 | +onnx==1.12.0 |
| 13 | +``` |
| 14 | + |
| 15 | +### Run the pretrained Ultralytics YOLOv8 within torch environment on webcam: |
| 16 | +```python |
| 17 | +import cv2 |
| 18 | +from ultralytics.engine.model import Model as BaseModel |
| 19 | +from mltu.torch.yolo.detectors.torch_detector import Detector as TorchDetector |
| 20 | + |
| 21 | +input_width, input_height = 640, 640 |
| 22 | +confidence_threshold = 0.5 |
| 23 | +iou_threshold = 0.5 |
| 24 | + |
| 25 | +base_model = BaseModel("yolov8m.pt") |
| 26 | +detector = TorchDetector(base_model.model, input_width, input_height, base_model.names, confidence_threshold, iou_threshold) |
| 27 | + |
| 28 | +cap = cv2.VideoCapture(0) |
| 29 | +while True: |
| 30 | + ret, frame = cap.read() |
| 31 | + if not ret: |
| 32 | + break |
| 33 | + |
| 34 | + # Perform Yolo object detection |
| 35 | + detections = detector(frame) |
| 36 | + |
| 37 | + # Apply the detections to the frame |
| 38 | + frame = detections.applyToFrame(frame) |
| 39 | + |
| 40 | + # Print the FPS |
| 41 | + print(detector.fps) |
| 42 | + |
| 43 | + # Display the output image |
| 44 | + cv2.imshow("Object Detection", frame) |
| 45 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 46 | + break |
| 47 | + |
| 48 | +cap.release() |
| 49 | +cv2.destroyAllWindows() |
| 50 | +``` |
| 51 | + |
| 52 | +## Convert the pretrained model to ONNX: |
| 53 | +```python |
| 54 | +import torch |
| 55 | +from ultralytics.engine.model import Model as BaseModel |
| 56 | + |
| 57 | +base_model = BaseModel("yolov8m.pt") |
| 58 | + |
| 59 | +classes = base_model.names |
| 60 | +input_width, input_height = 640, 640 |
| 61 | +input_shape = (1, 3, input_width, input_height) |
| 62 | +model = base_model.model |
| 63 | + |
| 64 | +# place model on cpu |
| 65 | +model.to("cpu") |
| 66 | + |
| 67 | +# set the model to inference mode |
| 68 | +model.eval() |
| 69 | + |
| 70 | +# convert the model to ONNX format |
| 71 | +dummy_input = torch.randn(input_shape).to("cpu") |
| 72 | + |
| 73 | +# Export the model |
| 74 | +torch.onnx.export( |
| 75 | + model, |
| 76 | + dummy_input, |
| 77 | + "yolov8m.onnx", |
| 78 | + export_params=True, |
| 79 | + input_names = ["input"], |
| 80 | + output_names = ["output"], |
| 81 | + dynamic_axes = { |
| 82 | + "input": {0: "batch_size", 2: "height", 3: "width"}, |
| 83 | + "output": {0: "batch_size", 2: "anchors"} |
| 84 | + } |
| 85 | +) |
| 86 | + |
| 87 | +# Add the class names to the model as metadata |
| 88 | +import onnx |
| 89 | + |
| 90 | +metadata = {"classes": classes} |
| 91 | + |
| 92 | +# Load the ONNX model |
| 93 | +onnx_model = onnx.load("yolov8m.onnx") |
| 94 | + |
| 95 | +# Add the metadata dictionary to the onnx model's metadata_props attribute |
| 96 | +for key, value in metadata.items(): |
| 97 | + meta = onnx_model.metadata_props.add() |
| 98 | + meta.key = key |
| 99 | + meta.value = str(value) |
| 100 | + |
| 101 | +# Save the modified ONNX model |
| 102 | +onnx.save(onnx_model, "yolov8m.onnx") |
| 103 | +``` |
| 104 | + |
| 105 | +## Run the YOLOv8 ONNX model with ONNX Runtime: |
| 106 | +```python |
| 107 | +import cv2 |
| 108 | +from ultralytics.engine.model import Model as BaseModel |
| 109 | +from mltu.torch.yolo.detectors.onnx_detector import Detector as OnnxDetector |
| 110 | + |
| 111 | +input_width, input_height = 640, 640 |
| 112 | +confidence_threshold = 0.5 |
| 113 | +iou_threshold = 0.5 |
| 114 | + |
| 115 | +detector = OnnxDetector("yolov8m.onnx", input_width, input_height, confidence_threshold, iou_threshold) |
| 116 | + |
| 117 | +cap = cv2.VideoCapture(0) |
| 118 | +while True: |
| 119 | + ret, frame = cap.read() |
| 120 | + if not ret: |
| 121 | + break |
| 122 | + |
| 123 | + # Perform Yolo object detection |
| 124 | + detections = detector(frame) |
| 125 | + |
| 126 | + # Apply the detections to the frame |
| 127 | + frame = detections.applyToFrame(frame) |
| 128 | + |
| 129 | + # Print the FPS |
| 130 | + print(detector.fps) |
| 131 | + |
| 132 | + # Display the output image |
| 133 | + cv2.imshow("Object Detection", frame) |
| 134 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 135 | + break |
| 136 | + |
| 137 | +cap.release() |
| 138 | +cv2.destroyAllWindows() |
| 139 | +``` |
0 commit comments