def transform(self, Xb, yb):
     temp_Xb = np.empty((Xb.shape[0], Xb.shape[1], CROP_SIZE, CROP_SIZE))
     for i, x in enumerate(Xb):
         x_transposed = x.transpose(1, 2, 0)
         x_resized = utils.resize_rgb(x_transposed, w=CROP_SIZE, h=CROP_SIZE)
         x_new = x_resized.transpose(2, 0, 1)
         temp_Xb[i, :, :, :] = x_new
     return temp_Xb, yb
 def transform(self, Xb, yb):
     temp_Xb = np.empty((Xb.shape[0], Xb.shape[1], CROP_SIZE, CROP_SIZE))
     for i, x in enumerate(Xb):
         x_transposed = x.transpose(1,2,0) 
         x_resized = utils.resize_rgb(x_transposed, w=CROP_SIZE, h=CROP_SIZE)
         x_new = x_resized.transpose(2,0,1)
         temp_Xb[i, :, :, :]  = x_new
     return temp_Xb, yb
 def transform(self, Xb, yb):
     # if Xb.shape[0] < 128:
     # pdb.set_trace()
     Xb, yb = super(FlipBatchIterator, self).transform(Xb, yb)
     # X = np.empty((Xb.shape[0], 3, utils.CROP_SIZE, utils.CROP_SIZE))
     # for i, x in enumerate(Xb):
     # patch = utils.neural_to_cv2(x)
     # patch = Augmenter.random_flip(patch)
     # patch = Augmenter.random_crop(patch, (CROP_SIZE, CROP_SIZE))
     # X[i, :, :,: ] = utils.cv2_to_neural(patch, w=utils.CROP_SIZE, h=utils.CROP_SIZE)
     temp_Xb = np.empty((Xb.shape[0], Xb.shape[1], CROP_SIZE, CROP_SIZE))
     for i, x in enumerate(Xb):
         patch = x.transpose(1, 2, 0)
         patch = Augmenter.random_flip(patch)
         patch = Augmenter.random_crop(patch, (CROP_SIZE, CROP_SIZE))
         x_resized = utils.resize_rgb(patch, w=CROP_SIZE, h=CROP_SIZE)
         x_new = x_resized.transpose(2, 0, 1)
         temp_Xb[i, :, :, :] = x_new
     return temp_Xb, yb
 def transform(self, Xb, yb):
     #if Xb.shape[0] < 128:
         #pdb.set_trace()
     Xb, yb = super(FlipBatchIterator, self).transform(Xb, yb)
     #X = np.empty((Xb.shape[0], 3, utils.CROP_SIZE, utils.CROP_SIZE))
     #for i, x in enumerate(Xb):
         #patch = utils.neural_to_cv2(x)
         #patch = Augmenter.random_flip(patch) 
         #patch = Augmenter.random_crop(patch, (CROP_SIZE, CROP_SIZE))
         #X[i, :, :,: ] = utils.cv2_to_neural(patch, w=utils.CROP_SIZE, h=utils.CROP_SIZE)
     temp_Xb = np.empty((Xb.shape[0], Xb.shape[1], CROP_SIZE, CROP_SIZE))
     for i, x in enumerate(Xb):
         patch = x.transpose(1,2,0) 
         patch = Augmenter.random_flip(patch)
         patch = Augmenter.random_crop(patch, (CROP_SIZE, CROP_SIZE))
         x_resized = utils.resize_rgb(patch, w=CROP_SIZE, h=CROP_SIZE)
         x_new = x_resized.transpose(2,0,1)
         temp_Xb[i, :, :, :]  = x_new
     return temp_Xb, yb
    def detect_roofs(self, img_name, in_path=None):
        in_path = self.in_path if in_path is None else in_path 
        try:
            rgb_unrotated = cv2.imread(in_path+img_name, flags=cv2.IMREAD_COLOR)
            gray = cv2.cvtColor(rgb_unrotated, cv2.COLOR_BGR2GRAY)
            gray = cv2.equalizeHist(gray)

            if self.downsized:
                rgb_unrotated = utils.resize_rgb(rgb_unrotated, h=rgb_unrotated.shape[0]/2, w=rgb_unrotated.shape[1]/2)
                gray = utils.resize_grayscale(gray, w=gray.shape[1]/2, h=gray.shape[0]/2)

        except IOError as e:
            print e
            sys.exit(-1)
        else:
            for roof_type, detectors in self.roof_detectors.iteritems():
                for i, detector in enumerate(detectors):
                    for angle in self.angles:
                        #for thatch we only need one angle
                        if self.rotate_detectors[i] == False and angle>0 or (roof_type=='thatch' and angle>0):#roof_type == 'thatch' and angle>0:
                            continue

                        print 'Detecting with detector: '+str(i)
                        print 'ANGLE '+str(angle)

                        with Timer() as t: 
                            rotated_image = utils.rotate_image(gray, angle) if angle>0 else gray
                            delete_image = utils.rotate_image_RGB(rgb_unrotated, angle) if angle>0 else gray
                            detections, _ = self.detect_and_rectify(detector, rotated_image, angle, rgb_unrotated.shape[:2], rgb_rotated=delete_image) 
                            if self.downsized:
                                detections = detections*2
                            self.viola_detections.set_detections(roof_type=roof_type, img_name=img_name, 
                                    angle=angle, detection_list=detections, img=rotated_image)

                        print 'Time detection: {0}'.format(t.secs)
                        self.viola_detections.total_time += t.secs
                        if DEBUG:
                            rgb_to_write = cv2.imread(in_path+img_name, flags=cv2.IMREAD_COLOR)
                            utils.draw_detections(detections, rgb_to_write, color=(255,0,0))
                            cv2.imwrite('{0}{3}{1}_{2}.jpg'.format('', img_name[:-4], angle, roof_type), rgb_to_write)
            return rgb_unrotated
        margin_0 = np.random.randint(0, margin_0)
        margin_1 = np.random.randint(0, margin_1)

        min_0 = np.random.randint(0, margin_0) if margin_0 > 0 else 0
        min_1 = np.random.randint(0, margin_1) if margin_1 > 0 else 0

        patch = img[min_0 : (min_0 + dst_shape[0]), min_1 : (min_1 + dst_shape[1]), :]
        return patch


if __name__ == "__main__":
    path = utils.get_path(data_fold=utils.TRAINING, in_or_out=utils.IN)
    roofs = DataLoader.get_all_patches_folder(folder_path=path, grayscale=False, merge_imgs=False)
    for img_name, roof_types in roofs.iteritems():
        for roof_type, roof_list in roof_types.iteritems():
            print roof_type
            if roof_type == "metal":
                continue
            for i, roof in enumerate(roof_list):
                cv2.imwrite("debug/{}_{}_1_{}.jpg".format(img_name, i, "normal"), roof)
                roof = utils.resize_rgb(roof, w=utils.PATCH_H, h=utils.PATCH_W)
                # rotate it
                # roof = Augmenter().random_full_rotation(roof)
                # cv2.imwrite('debug/{}_{}_2_{}.jpg'.format(img_name, i, 'rotated'), roof)
                # flip it
                roof = Augmenter().random_flip(roof)
                cv2.imwrite("debug/{}_{}_3_{}.jpg".format(img_name, i, "flip"), roof)
                # crop it
                roof = Augmenter().random_crop(roof, (utils.CROP_SIZE, utils.CROP_SIZE))
                cv2.imwrite("debug/{}_{}_4_{}.jpg".format(img_name, i, "crop"), roof)
                    min_1:(min_1 + dst_shape[1]), :]
        return patch


if __name__ == '__main__':
    path = utils.get_path(data_fold=utils.TRAINING, in_or_out=utils.IN)
    roofs = DataLoader.get_all_patches_folder(folder_path=path,
                                              grayscale=False,
                                              merge_imgs=False)
    for img_name, roof_types in roofs.iteritems():
        for roof_type, roof_list in roof_types.iteritems():
            print roof_type
            if roof_type == 'metal':
                continue
            for i, roof in enumerate(roof_list):
                cv2.imwrite(
                    'debug/{}_{}_1_{}.jpg'.format(img_name, i, 'normal'), roof)
                roof = utils.resize_rgb(roof, w=utils.PATCH_H, h=utils.PATCH_W)
                #rotate it
                #roof = Augmenter().random_full_rotation(roof)
                #cv2.imwrite('debug/{}_{}_2_{}.jpg'.format(img_name, i, 'rotated'), roof)
                #flip it
                roof = Augmenter().random_flip(roof)
                cv2.imwrite('debug/{}_{}_3_{}.jpg'.format(img_name, i, 'flip'),
                            roof)
                #crop it
                roof = Augmenter().random_crop(
                    roof, (utils.CROP_SIZE, utils.CROP_SIZE))
                cv2.imwrite('debug/{}_{}_4_{}.jpg'.format(img_name, i, 'crop'),
                            roof)
Exemple #8
0
    def detect_roofs(self, img_name, in_path=None):
        in_path = self.in_path if in_path is None else in_path
        try:
            rgb_unrotated = cv2.imread(in_path + img_name,
                                       flags=cv2.IMREAD_COLOR)
            gray = cv2.cvtColor(rgb_unrotated, cv2.COLOR_BGR2GRAY)
            gray = cv2.equalizeHist(gray)

            if self.downsized:
                rgb_unrotated = utils.resize_rgb(rgb_unrotated,
                                                 h=rgb_unrotated.shape[0] / 2,
                                                 w=rgb_unrotated.shape[1] / 2)
                gray = utils.resize_grayscale(gray,
                                              w=gray.shape[1] / 2,
                                              h=gray.shape[0] / 2)

        except IOError as e:
            print e
            sys.exit(-1)
        else:
            for roof_type, detectors in self.roof_detectors.iteritems():
                for i, detector in enumerate(detectors):
                    for angle in self.angles:
                        #for thatch we only need one angle
                        if self.rotate_detectors[i] == False and angle > 0 or (
                                roof_type == 'thatch' and angle > 0
                        ):  #roof_type == 'thatch' and angle>0:
                            continue

                        print 'Detecting with detector: ' + str(i)
                        print 'ANGLE ' + str(angle)

                        with Timer() as t:
                            rotated_image = utils.rotate_image(
                                gray, angle) if angle > 0 else gray
                            delete_image = utils.rotate_image_RGB(
                                rgb_unrotated, angle) if angle > 0 else gray
                            detections, _ = self.detect_and_rectify(
                                detector,
                                rotated_image,
                                angle,
                                rgb_unrotated.shape[:2],
                                rgb_rotated=delete_image)
                            if self.downsized:
                                detections = detections * 2
                            self.viola_detections.set_detections(
                                roof_type=roof_type,
                                img_name=img_name,
                                angle=angle,
                                detection_list=detections,
                                img=rotated_image)

                        print 'Time detection: {0}'.format(t.secs)
                        self.viola_detections.total_time += t.secs
                        if DEBUG:
                            rgb_to_write = cv2.imread(in_path + img_name,
                                                      flags=cv2.IMREAD_COLOR)
                            utils.draw_detections(detections,
                                                  rgb_to_write,
                                                  color=(255, 0, 0))
                            cv2.imwrite(
                                '{0}{3}{1}_{2}.jpg'.format(
                                    '', img_name[:-4], angle, roof_type),
                                rgb_to_write)
            return rgb_unrotated