-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7530586
commit 41507fa
Showing
4 changed files
with
147 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) | ||
# cv2.rectangle(img, c1, c2, color, thickness=2, lineType=cv2.LINE_AA) | ||
# [batch size, channel number, height, width] | ||
# cv2.dnn.NMSBoxes | ||
# input shape (1,3,640,640) BCHW | ||
# output shape (1,5,8400) | ||
|
||
from ultralytics import YOLO | ||
|
||
# Load a model | ||
model = YOLO("yolov8n.pt") # load an official model | ||
model = YOLO("path/to/best.pt") # load a custom trained model | ||
|
||
# Export the model | ||
model.export(format="onnx") | ||
|
||
from ultralytics import YOLO | ||
|
||
# Load the YOLOv8 model | ||
model = YOLO("yolov8n.pt") | ||
|
||
# Export the model to TensorRT format | ||
model.export(format="engine") # creates 'yolov8n.engine' | ||
|
||
# Load the exported TensorRT model | ||
tensorrt_model = YOLO("yolov8n.engine") | ||
|
||
# Run inference | ||
results = tensorrt_model("https://ultralytics.com/images/bus.jpg") | ||
|
||
# logging style: | ||
# PyTorch: starting from '/home/user/ROS/models/maize/Maize.pt' with input shape (1, 3, 640, 640) BCHW and output shape(s) (1, 5, 8400) (5.9 MB) | ||
# ONNX: starting export with onnx 1.16.2 opset 12... | ||
# ONNX: export success ✅ 1.7s, saved as '/home/user/ROS/models/maize/Maize.onnx' (11.7 MB) | ||
# Export complete (3.8s) | ||
# Results saved to /home/user/ROS/models/maize | ||
# Predict: yolo predict task=detect model=/home/user/ROS/models/maize/Maize.onnx imgsz=640 | ||
# Validate: yolo val task=detect model=/home/user/ROS/models/maize/Maize.onnx imgsz=640 data=config.yaml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# stream off: | ||
from ultralytics import YOLO | ||
|
||
# Load a model | ||
model = YOLO("yolov8n.pt") # pretrained YOLOv8n model | ||
|
||
# Run batched inference on a list of images | ||
results = model(["image1.jpg", "image2.jpg"]) # return a list of Results objects | ||
|
||
# Process results list | ||
for result in results: | ||
boxes = result.boxes # Boxes object for bounding box outputs | ||
masks = result.masks # Masks object for segmentation masks outputs | ||
keypoints = result.keypoints # Keypoints object for pose outputs | ||
probs = result.probs # Probs object for classification outputs | ||
obb = result.obb # Oriented boxes object for OBB outputs | ||
result.show() # display to screen | ||
result.save(filename="result.jpg") # save to disk | ||
|
||
# stream on: | ||
from ultralytics import YOLO | ||
|
||
# Load a model | ||
model = YOLO("yolov8n.pt") # pretrained YOLOv8n model | ||
|
||
# Run batched inference on a list of images | ||
results = model(["image1.jpg", "image2.jpg"], stream=True) # return a generator of Results objects | ||
|
||
# Process results generator | ||
for result in results: | ||
boxes = result.boxes # Boxes object for bounding box outputs | ||
masks = result.masks # Masks object for segmentation masks outputs | ||
keypoints = result.keypoints # Keypoints object for pose outputs | ||
probs = result.probs # Probs object for classification outputs | ||
obb = result.obb # Oriented boxes object for OBB outputs | ||
result.show() # display to screen | ||
result.save(filename="result.jpg") # save to disk | ||
|
||
|
||
from ultralytics import YOLO | ||
|
||
# Load a pretrained YOLOv8n model | ||
model = YOLO("yolov8n.pt") | ||
|
||
# Run inference on 'bus.jpg' with arguments | ||
model.predict("bus.jpg", save=True, imgsz=320, conf=0.5) | ||
|
||
from ultralytics import YOLO | ||
|
||
# Load a pretrained YOLOv8n model | ||
model = YOLO("yolov8n.pt") | ||
|
||
# Run inference on an image | ||
results = model("bus.jpg") # results list | ||
|
||
# View results | ||
for r in results: | ||
print(r.boxes) # print the Boxes object containing the detection bounding boxes | ||
|
||
from PIL import Image | ||
|
||
from ultralytics import YOLO | ||
|
||
# Load a pretrained YOLOv8n model | ||
model = YOLO("yolov8n.pt") | ||
|
||
# Run inference on 'bus.jpg' | ||
results = model(["bus.jpg", "zidane.jpg"]) # results list | ||
|
||
# Visualize the results | ||
for i, r in enumerate(results): | ||
# Plot results image | ||
im_bgr = r.plot() # BGR-order numpy array | ||
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image | ||
|
||
# Show results to screen (in supported environments) | ||
r.show() | ||
|
||
# Save results to disk | ||
r.save(filename=f"results{i}.jpg") | ||
|
||
import cv2 | ||
from ultralytics import YOLO | ||
import time | ||
|
||
model = YOLO("/home/user/ROS/models/maize/Maize.engine") | ||
image = cv2.imread("/home/user/ROS/assets/maize/IMG_1822_14.JPG") | ||
|
||
sum = 0 | ||
# stream = True? | ||
for _ in range(100): | ||
tic = time.perf_counter_ns() | ||
result = model.predict( | ||
image, # batch=8 of the same image | ||
verbose=False, | ||
device="cuda", | ||
) | ||
elapsed_time = (time.perf_counter_ns() - tic) / 1e6 | ||
print(f"Elapsed time: {(elapsed_time):.2f} ms") | ||
sum += elapsed_time | ||
annotated_frame = result[0].plot() | ||
cv2.imshow("YOLOv8 Inference", annotated_frame) | ||
if cv2.waitKey(1) & 0xFF == ord("q"): | ||
break | ||
|
||
avearage_time = (sum - 2660) / 100 | ||
print(f"Average time: {avearage_time:.2f} ms") | ||
cv2.destroyAllWindows() |
File renamed without changes.
This file was deleted.
Oops, something went wrong.