Skip to content

Commit 74d83e2

Browse files
committed
fix of calc crops
1 parent 87c8bee commit 74d83e2

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

patched_yolo_infer/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ classes_names=result.filtered_classes_names
201201

202202
## __How to automatically determine optimal parameters for patches (crops):__
203203

204-
To efficiently process a large number of images of varying sizes and contents, manually selecting the optimal patch sizes and overlaps can be cumbersome. To address this, an algorithm has been developed to automatically calculate the best parameters for patches (crops).
204+
To efficiently process a large number of images of varying sizes and contents, manually selecting the optimal patch sizes and overlaps can be difficult. To address this, an algorithm has been developed to automatically calculate the best parameters for patches (crops).
205205

206206
The `auto_calculate_crop_values` function operates in two modes:
207207

@@ -221,6 +221,6 @@ img = cv2.imread(img_path)
221221

222222
# Calculate the optimal crop size and overlap for an image
223223
shape_x, shape_y, overlap_x, overlap_y = auto_calculate_crop_values(
224-
image=img, mode="network_analysis", model=YOLO("yolov8m.pt")
224+
image=img, mode="network_based", model=YOLO("yolov8m.pt")
225225
)
226226
```

patched_yolo_infer/functions_extra.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def basic_crop_size_calculation(width, height):
503503
return crop_shape_x, crop_shape_y, crop_overlap_x, crop_overlap_y
504504

505505

506-
def auto_calculate_crop_values(image, mode="network_analysis", model=None, classes_list=None):
506+
def auto_calculate_crop_values(image, mode="network_based", model=None, classes_list=None):
507507
"""
508508
Automatically calculate the optimal crop size and overlap for an image.
509509
@@ -513,7 +513,7 @@ def auto_calculate_crop_values(image, mode="network_analysis", model=None, class
513513
514514
Parameters:
515515
image (numpy.ndarray): The input BGR image.
516-
mode (str): The type of analysis to perform. Can be "image_size_analysis" or "network_analysis".
516+
mode (str): The type of analysis to perform. Can be "resolution_based" or "network_based".
517517
Default is "network_analysis".
518518
model (YOLO): The YOLO model to use for object detection. If None, a default model yolov8m
519519
will be loaded. Default is None.
@@ -527,8 +527,10 @@ def auto_calculate_crop_values(image, mode="network_analysis", model=None, class
527527
height, width = image.shape[:2]
528528

529529
# If the mode is 'image_size_analysis', calculate crop size based on image dimensions
530-
if mode == 'image_size_analysis':
531-
crop_shape_x, crop_shape_y, crop_overlap_x, crop_overlap_y = basic_crop_size_calculation(width, height)
530+
if mode == 'resolution_based':
531+
crop_shape_x, crop_shape_y, crop_overlap_x, crop_overlap_y = basic_crop_size_calculation(
532+
width, height
533+
)
532534
else:
533535
# If no model is provided, load a default YOLO model
534536
if model is None:
@@ -556,7 +558,7 @@ def auto_calculate_crop_values(image, mode="network_analysis", model=None, class
556558
max_height = box_height
557559

558560
# Determine the maximum dimension (width or height) of the detected objects
559-
max_value = max(box_height, max_width)
561+
max_value = max(max_width, max_height)
560562

561563
# Adjust crop size and overlap based on the maximum detected object dimension
562564
if width > height:
@@ -584,4 +586,12 @@ def auto_calculate_crop_values(image, mode="network_analysis", model=None, class
584586
if width // crop_shape_x > 7:
585587
crop_shape_x = width // 7
586588

589+
# Handling cases where patches are not needed along the axes
590+
if height / crop_shape_y < 1.25:
591+
crop_shape_y = height
592+
crop_overlap_y = 0
593+
if width / crop_shape_x < 1.25:
594+
crop_shape_x = width
595+
crop_overlap_x = 0
596+
587597
return crop_shape_x, crop_shape_y, crop_overlap_x, crop_overlap_y

0 commit comments

Comments
 (0)