Update README.md
Browse files
README.md
CHANGED
|
@@ -1,79 +1,67 @@
|
|
| 1 |
|
| 2 |
-
# Bottle Detection with Ultralytics YOLO
|
| 3 |
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
1. **Install dependencies** (recommended: use a virtual environment):
|
| 9 |
-
```bash
|
| 10 |
-
python3 -m venv .venv
|
| 11 |
-
source .venv/bin/activate
|
| 12 |
-
pip install ultralytics
|
| 13 |
-
```
|
| 14 |
-
|
| 15 |
-
2. **Prepare your dataset**
|
| 16 |
-
- Organize your dataset as:
|
| 17 |
-
```
|
| 18 |
-
dataset/
|
| 19 |
-
images/
|
| 20 |
-
train/
|
| 21 |
-
val/
|
| 22 |
-
labels/
|
| 23 |
-
train/
|
| 24 |
-
val/
|
| 25 |
-
data.yaml
|
| 26 |
-
```
|
| 27 |
-
- The `data.yaml` should specify absolute paths for `train` and `val` images.
|
| 28 |
-
|
| 29 |
-
## Training a Model
|
| 30 |
-
|
| 31 |
-
Edit and run `train.py` to train a model from scratch or fine-tune a pretrained model:
|
| 32 |
|
| 33 |
-
|
| 34 |
-
from ultralytics import YOLO
|
| 35 |
-
|
| 36 |
-
model = YOLO('yolov8n.pt') # or your custom/pretrained .pt file
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
batch=16,
|
| 43 |
-
project='runs/train',
|
| 44 |
-
name='experiment_name',
|
| 45 |
-
)
|
| 46 |
```
|
| 47 |
|
| 48 |
-
|
| 49 |
-
- You can adjust `epochs`, `imgsz`, `batch`, and other parameters as needed.
|
| 50 |
-
- For small objects, consider increasing `imgsz` (e.g., 1024).
|
| 51 |
-
- Use a GPU for best performance.
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
Edit and run `infer.py` to run inference on images:
|
| 56 |
-
|
| 57 |
-
```
|
| 58 |
from ultralytics import YOLO
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|
|
|
|
| 62 |
results = model.predict(
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
)
|
| 68 |
-
```
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
## References
|
| 77 |
-
|
| 78 |
- [Ultralytics YOLO Docs](https://docs.ultralytics.com/)
|
| 79 |
- [YOLOv8 GitHub](https://github.com/ultralytics/ultralytics)
|
|
|
|
| 1 |
|
|
|
|
| 2 |
|
| 3 |
+
# Quick Inference with Ultralytics YOLO
|
| 4 |
|
| 5 |
+
This guide shows how to load a trained or pretrained YOLO model and run inference, returning the center coordinates of detected objects for class 0 and 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
## Environment Setup
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
```bash
|
| 10 |
+
python3 -m venv .venv
|
| 11 |
+
source .venv/bin/activate
|
| 12 |
+
pip install ultralytics
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
```
|
| 14 |
|
| 15 |
+
## Inference Example
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
from ultralytics import YOLO
|
| 19 |
|
| 20 |
+
# 1. Load your model
|
| 21 |
+
model = YOLO("/home/vmo/Workspace/bottle_detection/runs/detect/runs/train/yolo11n_bottle/weights/best.pt")
|
| 22 |
|
| 23 |
+
# 2. Run prediction
|
| 24 |
results = model.predict(
|
| 25 |
+
source="/home/vmo/Workspace/bottle_detection/raw_data_test/ros_4_25_45/images/frame_000104.jpg",
|
| 26 |
+
save=False,
|
| 27 |
+
conf=0.15,
|
| 28 |
+
classes=[0, 1]
|
| 29 |
)
|
|
|
|
| 30 |
|
| 31 |
+
# 3. Extract Center Coordinates
|
| 32 |
+
# We use a dictionary to store lists of centers in case there are multiple bottles/caps
|
| 33 |
+
centers = {0: [], 1: []}
|
| 34 |
+
|
| 35 |
+
for r in results:
|
| 36 |
+
# .xywh returns [x_center, y_center, width, height] in pixels
|
| 37 |
+
# .cls returns the class index (0 or 1)
|
| 38 |
+
boxes = r.boxes.xywh.cpu().numpy()
|
| 39 |
+
clss = r.boxes.cls.cpu().numpy()
|
| 40 |
+
|
| 41 |
+
for i, box in enumerate(boxes):
|
| 42 |
+
x_c, y_c, w, h = box
|
| 43 |
+
class_id = int(clss[i])
|
| 44 |
+
centers[class_id].append((float(x_c), float(y_c)))
|
| 45 |
+
|
| 46 |
+
# 4. Display or use the coordinates
|
| 47 |
+
bottle_centers = centers[0]
|
| 48 |
+
cap_centers = centers[1]
|
| 49 |
+
|
| 50 |
+
print(f"Bottle Centers (Class 0): {bottle_centers}")
|
| 51 |
+
print(f"Cap Centers (Class 1): {cap_centers}")
|
| 52 |
+
|
| 53 |
+
# Example: If you need the first detection for a calculation
|
| 54 |
+
if bottle_centers and cap_centers:
|
| 55 |
+
b_x, b_y = bottle_centers[0]
|
| 56 |
+
c_x, c_y = cap_centers[0]
|
| 57 |
+
print(f"Primary Bottle Center: x={b_x:.2f}, y={b_y:.2f}")
|
| 58 |
+
```
|
| 59 |
|
| 60 |
+
## Notes
|
| 61 |
+
- Replace `/absolute/path/to/weights/best.pt` with your trained or pretrained model path.
|
| 62 |
+
- Replace `/path/to/image/or/folder` with your image or folder path.
|
| 63 |
+
- The function `get_centers` returns a dictionary with lists of center coordinates for class 0 and 1.
|
| 64 |
|
| 65 |
## References
|
|
|
|
| 66 |
- [Ultralytics YOLO Docs](https://docs.ultralytics.com/)
|
| 67 |
- [YOLOv8 GitHub](https://github.com/ultralytics/ultralytics)
|