:param img: PIL Input Image :param mask: PIL Input Mask :return: PIL output PIL (mask, crop) of self.crop_size
(self, img, mask)
| 633 | return detected_peaks |
| 634 | |
| 635 | def __call__(self, img, mask): |
| 636 | """ |
| 637 | :param img: PIL Input Image |
| 638 | :param mask: PIL Input Mask |
| 639 | :return: PIL output PIL (mask, crop) of self.crop_size |
| 640 | """ |
| 641 | assert img.size == mask.size |
| 642 | |
| 643 | scale_amt = random.uniform(self.scale_min, self.scale_max) |
| 644 | w = int(scale_amt * img.size[0]) |
| 645 | h = int(scale_amt * img.size[1]) |
| 646 | |
| 647 | if scale_amt < 1.0: |
| 648 | img, mask = img.resize((w, h), Image.BICUBIC), mask.resize((w, h), |
| 649 | Image.NEAREST) |
| 650 | return self.crop(img, mask) |
| 651 | else: |
| 652 | # Smart Crop ( Class Uniform's ABN) |
| 653 | origw, origh = mask.size |
| 654 | img_new, mask_new = \ |
| 655 | img.resize((w, h), Image.BICUBIC), mask.resize((w, h), Image.NEAREST) |
| 656 | interested_class = self.class_list # [16, 15, 14] # Train, Truck, Bus |
| 657 | data = np.array(mask) |
| 658 | arr = np.zeros((1024, 2048)) |
| 659 | for class_of_interest in interested_class: |
| 660 | # hist = np.histogram(data==class_of_interest) |
| 661 | map = np.where(data == class_of_interest, data, 0) |
| 662 | map = map.astype('float64') / map.sum() / class_of_interest |
| 663 | map[np.isnan(map)] = 0 |
| 664 | arr = arr + map |
| 665 | |
| 666 | origarr = arr |
| 667 | window_size = 250 |
| 668 | |
| 669 | # Given a list of classes of interest find the points on the image that are |
| 670 | # of interest to crop from |
| 671 | sum_arr = np.zeros((1024, 2048)).astype('float32') |
| 672 | tmp = np.zeros((1024, 2048)).astype('float32') |
| 673 | for x in range(0, arr.shape[0] - window_size, window_size): |
| 674 | for y in range(0, arr.shape[1] - window_size, window_size): |
| 675 | sum_arr[int(x + window_size / 2), int(y + window_size / 2)] = origarr[ |
| 676 | x:x + window_size, |
| 677 | y:y + window_size].sum() |
| 678 | tmp[x:x + window_size, y:y + window_size] = \ |
| 679 | origarr[x:x + window_size, y:y + window_size].sum() |
| 680 | |
| 681 | # Scaling Ratios in X and Y for non-uniform images |
| 682 | ratio = (float(origw) / w, float(origh) / h) |
| 683 | output = self.detect_peaks(sum_arr) |
| 684 | coord = (np.column_stack(np.where(output))).tolist() |
| 685 | |
| 686 | # Check if there are any peaks in the images to crop from if not do standard |
| 687 | # cropping behaviour |
| 688 | if len(coord) == 0: |
| 689 | return self.crop(img_new, mask_new) |
| 690 | else: |
| 691 | # If peaks are detected, random peak selection followed by peak |
| 692 | # coordinate scaling to new scaled image and then random |
nothing calls this directly
no test coverage detected