shalchianmh commited on
Commit
a88994a
1 Parent(s): 4a0a894

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +55 -0
README.md CHANGED
@@ -81,6 +81,61 @@ results[0].show()
81
  path = model.export(format="onnx") # return path to exported model
82
  ```
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  See YOLOv8 [Python Docs](https://docs.ultralytics.com/usage/python/) for more examples.
85
 
86
  </details>
 
81
  path = model.export(format="onnx") # return path to exported model
82
  ```
83
 
84
+
85
+ ### Inference
86
+
87
+ You can use the model with this code to see how it detects, the style you plot or save detected object is up to you, but here is an example:
88
+
89
+ ```python
90
+ from ultralytics import YOLO
91
+ import matplotlib.pyplot as plt
92
+ import cv2
93
+
94
+ # Load the YOLO model
95
+ model = YOLO("path/to/local/model.pt")
96
+
97
+ # Define the input image file path
98
+ file_path = "path/to/image"
99
+
100
+ # Get the prediction results
101
+ results = model([file_path])
102
+
103
+ # Read the input image
104
+ img = cv2.imread(file_path)
105
+
106
+ # Iterate over the results to extract bounding box and display both input and cropped output
107
+ for result in results:
108
+ maxa = result.boxes.conf.argmax() # Get the index of the highest confidence box
109
+ x, y, w, h = result.boxes.xywh[maxa] # Extract coordinates and size
110
+ print(f"Bounding box: x={x}, y={y}, w={w}, h={h}")
111
+
112
+ # Crop the detected object from the image
113
+ crop_img = img[int(y-h/2):int(y+h/2), int(x-w/2):int(x+w/2)]
114
+
115
+ # Convert the image from BGR to RGB for display with matplotlib
116
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
117
+ crop_img_rgb = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB)
118
+
119
+ # Plot the input image and cropped image side by side
120
+ plt.figure(figsize=(10, 5))
121
+
122
+ # Display the input image
123
+ plt.subplot(1, 2, 1)
124
+ plt.imshow(img_rgb)
125
+ plt.title("Input Image")
126
+ plt.axis("off")
127
+
128
+ # Display the cropped image (output)
129
+ plt.subplot(1, 2, 2)
130
+ plt.imshow(crop_img_rgb)
131
+ plt.title("Cropped Output")
132
+ plt.axis("off")
133
+
134
+ plt.show()
135
+ ```
136
+
137
+
138
+
139
  See YOLOv8 [Python Docs](https://docs.ultralytics.com/usage/python/) for more examples.
140
 
141
  </details>