Given scale, ratio, height and width, return sampled coordinates of the videos.
(
scale, ratio, height, width, num_repeat=10, log_scale=True, switch_hw=False
)
| 501 | |
| 502 | |
| 503 | def _get_param_spatial_crop( |
| 504 | scale, ratio, height, width, num_repeat=10, log_scale=True, switch_hw=False |
| 505 | ): |
| 506 | """ |
| 507 | Given scale, ratio, height and width, return sampled coordinates of the videos. |
| 508 | """ |
| 509 | for _ in range(num_repeat): |
| 510 | area = height * width |
| 511 | target_area = random.uniform(*scale) * area |
| 512 | if log_scale: |
| 513 | log_ratio = (math.log(ratio[0]), math.log(ratio[1])) |
| 514 | aspect_ratio = math.exp(random.uniform(*log_ratio)) |
| 515 | else: |
| 516 | aspect_ratio = random.uniform(*ratio) |
| 517 | |
| 518 | w = int(round(math.sqrt(target_area * aspect_ratio))) |
| 519 | h = int(round(math.sqrt(target_area / aspect_ratio))) |
| 520 | |
| 521 | if np.random.uniform() < 0.5 and switch_hw: |
| 522 | w, h = h, w |
| 523 | |
| 524 | if 0 < w <= width and 0 < h <= height: |
| 525 | i = random.randint(0, height - h) |
| 526 | j = random.randint(0, width - w) |
| 527 | return i, j, h, w |
| 528 | |
| 529 | # Fallback to central crop |
| 530 | in_ratio = float(width) / float(height) |
| 531 | if in_ratio < min(ratio): |
| 532 | w = width |
| 533 | h = int(round(w / min(ratio))) |
| 534 | elif in_ratio > max(ratio): |
| 535 | h = height |
| 536 | w = int(round(h * max(ratio))) |
| 537 | else: # whole image |
| 538 | w = width |
| 539 | h = height |
| 540 | i = (height - h) // 2 |
| 541 | j = (width - w) // 2 |
| 542 | return i, j, h, w |
| 543 | |
| 544 | |
| 545 | def random_resized_crop( |
no test coverage detected