Upload 3 files for sample code use
Browse files- .gitattributes +2 -0
- oyster_sample.jpg +3 -0
- oyster_sample_annotated.jpg +3 -0
- oysters.py +74 -0
.gitattributes
CHANGED
@@ -594,3 +594,5 @@ images/val/FrenchHermit1_toplit.jpg filter=lfs diff=lfs merge=lfs -text
|
|
594 |
images/val/holyground_77.jpg filter=lfs diff=lfs merge=lfs -text
|
595 |
images/val/oysters_14Feb2024_3.jpg filter=lfs diff=lfs merge=lfs -text
|
596 |
images/val/real_disks_2.jpg filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
594 |
images/val/holyground_77.jpg filter=lfs diff=lfs merge=lfs -text
|
595 |
images/val/oysters_14Feb2024_3.jpg filter=lfs diff=lfs merge=lfs -text
|
596 |
images/val/real_disks_2.jpg filter=lfs diff=lfs merge=lfs -text
|
597 |
+
oyster_sample_annotated.jpg filter=lfs diff=lfs merge=lfs -text
|
598 |
+
oyster_sample.jpg filter=lfs diff=lfs merge=lfs -text
|
oyster_sample.jpg
ADDED
![]() |
Git LFS Details
|
oyster_sample_annotated.jpg
ADDED
![]() |
Git LFS Details
|
oysters.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Python3 script for YOLO prediction and sizing of oyster objects.
|
3 |
+
|
4 |
+
To ensure compatible dependencies, set up a new venv virtual environment
|
5 |
+
and install all required dependencies with just one installation after updating
|
6 |
+
the PIP installer.
|
7 |
+
$ pip install -U pip
|
8 |
+
$ pip install ultralytics
|
9 |
+
"""
|
10 |
+
|
11 |
+
from ultralytics import YOLO
|
12 |
+
|
13 |
+
results_dir = 'oyster_yolo_results'
|
14 |
+
predict_name = 'example_oyster_results'
|
15 |
+
model = YOLO( 'aquaculture_oysters.pt')
|
16 |
+
oyster_img = 'oyster_sample.jpg'
|
17 |
+
|
18 |
+
# The size standard disk diameter. In this case, as millimeters,
|
19 |
+
# using a cutout from a 3 inch hockey puck.
|
20 |
+
std_mm = 76.2
|
21 |
+
|
22 |
+
# Run inference with these arguments
|
23 |
+
prediction_kwargs = dict(
|
24 |
+
save=True, # True Saves image to project/name.
|
25 |
+
save_txt=True, # Relative bounding box coordinates. up to conf limit.
|
26 |
+
save_conf=True, # Adds conf scores to bbox coordinates
|
27 |
+
augment=False,
|
28 |
+
show_labels=True, # True shows class labels in the saved image.
|
29 |
+
line_width=2,
|
30 |
+
project=results_dir, # bbox txt goes into runs/detect/labels.
|
31 |
+
name=predict_name,
|
32 |
+
exist_ok=False, # True, overwrites existing project/name
|
33 |
+
)
|
34 |
+
|
35 |
+
results = model.predict(
|
36 |
+
source=oyster_img,
|
37 |
+
half=False, # False is much faster than True
|
38 |
+
imgsz=960,
|
39 |
+
conf=0.7,
|
40 |
+
device='cpu',
|
41 |
+
iou=0.7,
|
42 |
+
max_det=400,
|
43 |
+
classes=[0,1], # 0 is disks, 1 is oysters.
|
44 |
+
**prediction_kwargs
|
45 |
+
)
|
46 |
+
|
47 |
+
# Display detection bounding boxes with labels and confidence values.
|
48 |
+
results[0].show(line_width=2)
|
49 |
+
|
50 |
+
# Calculate and print size metrics, Are based on longest bounding box dimension.
|
51 |
+
# Does not account for longer lengths of oysters oriented on the diagonal.
|
52 |
+
# An empirically determined correction factor is recommended for better accuracy.
|
53 |
+
# A good correction function is used in the companion GitHub repository.
|
54 |
+
boxes_tensor = results[0].boxes.xywh
|
55 |
+
boxes_numpy = boxes_tensor.cpu().numpy().astype(int)
|
56 |
+
w_h = boxes_numpy[:, 2:4]
|
57 |
+
px_lengths = w_h.max(axis=1)
|
58 |
+
|
59 |
+
classes = results[0].boxes.cls
|
60 |
+
class_distribution = classes.cpu().numpy().astype(int)
|
61 |
+
num_disks = len(class_distribution[class_distribution == 0])
|
62 |
+
num_oysters = len(class_distribution[class_distribution == 1])
|
63 |
+
oyster_px_sizes = px_lengths[class_distribution == 1]
|
64 |
+
disk_px_sizes = px_lengths[class_distribution == 0]
|
65 |
+
px2mm_factor = std_mm / disk_px_sizes.mean()
|
66 |
+
oyster_mean_mm = round(oyster_px_sizes.mean() * px2mm_factor, 2)
|
67 |
+
smallest = round(oyster_px_sizes.min() * px2mm_factor, 2)
|
68 |
+
largest = round(oyster_px_sizes.max() * px2mm_factor, 2)
|
69 |
+
|
70 |
+
print(f'Mean oyster size, mm: {oyster_mean_mm}')
|
71 |
+
print(f'Oyster size range, mm: [{smallest} -- {largest}]')
|
72 |
+
|
73 |
+
|
74 |
+
|