Exemple #1
0
    def generate(cls, index, text, font, out_dir, size, extension,
                 skewing_angle, random_skew, blur, random_blur,
                 background_type, distorsion_type, distorsion_orientation,
                 is_handwritten, name_format, width, alignment, text_color,
                 orientation, space_width, margins, fit):
        image = None
        text = get_display(text)

        margin_top, margin_left, margin_bottom, margin_right = margins
        horizontal_margin = margin_left + margin_right
        vertical_margin = margin_top + margin_bottom

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = handwritten_text_generator.generate(text, text_color, fit)
        else:
            image = computer_text_generator.generate(text, font, text_color,
                                                     size, orientation,
                                                     space_width, fit)

        random_angle = random.randint(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img  # Mind = blown
        elif distorsion_type == 1:
            distorted_img = distorsion_generator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        elif distorsion_type == 2:
            distorted_img = distorsion_generator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        else:
            distorted_img = distorsion_generator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(
                distorted_img.size[0] *
                (float(size - vertical_margin) / float(distorted_img.size[1])))
            resized_img = distorted_img.resize(
                (new_width, size - vertical_margin), Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + horizontal_margin
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(
                float(distorted_img.size[1]) *
                (float(size - horizontal_margin) /
                 float(distorted_img.size[0])))
            resized_img = distorted_img.resize(
                (size - horizontal_margin, new_height), Image.ANTIALIAS)
            background_width = size
            background_height = new_height + vertical_margin
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = background_generator.gaussian_noise(
                background_height, background_width)
        elif background_type == 1:
            background = background_generator.plain_white(
                background_height, background_width)
        elif background_type == 2:
            background = background_generator.quasicrystal(
                background_height, background_width)
        else:
            background = background_generator.picture(background_height,
                                                      background_width)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0 or width == -1:
            background.paste(resized_img, (margin_left, margin_top),
                             resized_img)
        elif alignment == 1:
            background.paste(
                resized_img,
                (int(background_width / 2 - new_text_width / 2), margin_top),
                resized_img)
        else:
            background.paste(
                resized_img,
                (background_width - new_text_width - margin_right, margin_top),
                resized_img)

        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))))

        #####################################
        # Generate name for resulting image #
        #####################################
        text = get_display(text)

        if name_format == 0:
            image_name = '{}.{}'.format(text, extension)
        elif name_format == 1:
            image_name = '{}.{}'.format(text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(text, extension)
        else:
            print('{} is not a valid name format. Using default.'.format(
                name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
Exemple #2
0
    def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, random_skew, blur, random_blur, background_type, distorsion_type, distorsion_orientation, is_handwritten, name_format, width, alignment, text_color, orientation, space_width, margins, fit):
    
        num_zero = 6 - len(str(index))
        image_name = '{}.{}'.format('0'*num_zero+str(index),extension)
    
    
        file = open("bb_result.txt","a+")
        file.write("\n"+image_name.split(".")[0] + " " +text.replace(" ", ""))
        file.close
        
        
        image = None

        size_percentage = rnd.randrange(75,125,5)
        size = int(size *  (size_percentage/100))

        
        margin_top, margin_left, margin_bottom, margin_right = margins
        horizontal_margin = margin_left + margin_right
        vertical_margin = margin_top + margin_bottom

#        print("margins is :" , margins)

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = handwritten_text_generator.generate(text, text_color, fit)
        else:
            image = computer_text_generator.generate(text, font, text_color, size, orientation, space_width, fit)

        random_angle = rnd.randint(0-skewing_angle, skewing_angle)

        rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img # Mind = blown
        elif distorsion_type == 1:
            distorted_img = distorsion_generator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 2:
            distorted_img = distorsion_generator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        else:
            distorted_img = distorsion_generator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(distorted_img.size[0] * (float(size - vertical_margin) / float(distorted_img.size[1])))
            resized_img = distorted_img.resize((new_width, size - vertical_margin), Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + horizontal_margin
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(float(distorted_img.size[1]) * (float(size - horizontal_margin) / float(distorted_img.size[0])))
            resized_img = distorted_img.resize((size - horizontal_margin, new_height), Image.ANTIALIAS)
            background_width = size
            background_height = new_height + vertical_margin
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = background_generator.gaussian_noise(background_height, background_width)
        elif background_type == 1:
            background = background_generator.plain_white(background_height, background_width)
        elif background_type == 2:
            background = background_generator.quasicrystal(background_height, background_width)
        else:
            background = background_generator.picture(background_height, background_width)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size
#        print("resized_img.size is : ", resized_img.size)

        if alignment == 0 or width == -1:
            background.paste(resized_img, (margin_left, margin_top), resized_img)
        elif alignment == 1:
            background.paste(resized_img, (int(background_width / 2 - new_text_width / 2), margin_top), resized_img)
        
            x0 = int(background_width / 2 - new_text_width / 2 )
            y0 = margin_top
            x1 = x0 + resized_img.size[0]
            y1 = y0 + resized_img.size[1]
            
            number =int((len(text)+1)/2)
#            print("digits number in text is : ", number)
#            print("text is : ", text)
            angle = math.radians(random_angle)
#            print("random_angle is ", random_angle)
#digit_width = int(((x1-x0)/number)*(1+0.005*(number)))
            digit_width = int((1+angle/90)*(new_text_width/number))
        
            
            for i in range(number):
                x00 = x0 + i * digit_width+ 0.7*(1.5*i-number/2)
                x11 = x0 + (i+1) * (digit_width)+ 0.7*(1.5*i-number/2)
                txt_draw = ImageDraw.Draw(background)
    
                x_center = 0.5 * background.size[0]
                y_center = 0.5 * background.size[1]
                
                # test
                rx0 = x00
                ry0 = (y0-y_center)*math.cos(angle) - (x00-x_center)*math.sin(angle) + y_center
                
                rx1 = x11
                ry1 = (y1-y_center)*math.cos(angle) - (x11-x_center)*math.sin(angle) + y_center

            
#                rx0 = (x00-x_center)*math.cos(angle) + (y0-y_center)*math.sin(angle) + x_center
#                ry0 = (y0-y_center)*math.cos(angle) - (x00-x_center)*math.sin(angle) + y_center
#
#                rx1 = (x11-x_center)*math.cos(angle) + (y1-y_center)*math.sin(angle) + x_center
#                ry1 = (y1-y_center)*math.cos(angle) - (x11-x_center)*math.sin(angle) + y_center

                # the rotated bounding box for each digit is (rx0,ry0,rx1,ry1)
#################################                txt_draw.rectangle((rx0,ry0,rx1,ry1), fill=None, outline=None, width=1)
                # print("the rotated bounding box for each digit is ", rx0,ry0,rx1,ry1)

                file = open("bb_result.txt","a+")
#                file.write("\nbbox coordinates : x_min: "+ str(rx0) +" y_min: "+str(ry0)+" x_max: "+ str(rx1)+" y_max: "+str(ry1))
                file.write(" "+ str(rx0)+" " + str(ry0)+" " +str(rx1)+" "+str(ry1))
                file.close
        
        else:
            background.paste(resized_img, (background_width - new_text_width - margin_right, margin_top), resized_img)

                
        
        ##################################
        # Apply gaussian blur #
        ##################################
        
        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else rnd.randint(0, blur))
            )
        )
        
        
       


        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
        final_img_copy =np.array(final_image.convert('RGB'))

        
        file = open("bb_result.txt","a+")
        file.write(" "+str(final_img_copy.shape[1])+" "+str(final_img_copy.shape[0]))
        file.close
    def generate(cls, ithread, index, text, word_labels, font, out_dir,
                 save4cycleGAN, tf_flag, tfr_out_dir, writer, size, extension,
                 skewing_angle, random_skew, blur, random_blur,
                 background_type, distorsion_type, perspective_type,
                 distorsion_orientation, is_handwritten, name_format, width,
                 alignment, text_color, orientation, space_width, margins, fit,
                 trte):
        image = None

        margin_top, margin_left, margin_bottom, margin_right = margins
        horizontal_margin = margin_left + margin_right
        vertical_margin = margin_top + margin_bottom

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = handwritten_text_generator.generate(text, text_color, fit)
        else:
            image = computer_text_generator.generate(text, font, text_color,
                                                     size, orientation,
                                                     space_width, fit)

        random_angle = random.randint(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)
        # plt.imshow(rotated_img)
        # plt.show()
        ##################################
        #  Apply perspective to image    #
        ##################################
        if perspective_type == 0:
            perspectived_img = rotated_img
        elif perspective_type == 1:
            perspectived_img = perspective_generator._apply_func_perspective(
                rotated_img)
            perspectived_img = perspectived_img

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = perspectived_img  # Mind = blown
        elif distorsion_type == 1:
            distorted_img = distorsion_generator.sin(
                perspectived_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        elif distorsion_type == 2:
            distorted_img = distorsion_generator.cos(
                perspectived_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        else:
            distorted_img = distorsion_generator.random(
                perspectived_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        # plt.imshow(distorted_img)
        # plt.show()

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(
                distorted_img.size[0] *
                (float(size - vertical_margin) / float(distorted_img.size[1])))
            resized_img = distorted_img.resize(
                (new_width, size - vertical_margin), Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + horizontal_margin
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(
                float(distorted_img.size[1]) *
                (float(size - horizontal_margin) /
                 float(distorted_img.size[0])))
            resized_img = distorted_img.resize(
                (size - horizontal_margin, new_height), Image.ANTIALIAS)
            background_width = size
            background_height = new_height + vertical_margin
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = background_generator.gaussian_noise(
                background_height, background_width)
        elif background_type == 1:
            background = background_generator.plain_white(
                background_height, background_width)
        elif background_type == 2:
            background = background_generator.quasicrystal(
                background_height, background_width)
        elif background_type == 3:
            background = background_generator.picture(background_height,
                                                      background_width)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0 or width == -1:
            background.paste(resized_img, (margin_left, margin_top),
                             resized_img)
        elif alignment == 1:
            background.paste(
                resized_img,
                (int(background_width / 2 - new_text_width / 2), margin_top),
                resized_img)
        else:
            background.paste(
                resized_img,
                (background_width - new_text_width - margin_right, margin_top),
                resized_img)
        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))))
        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}_{}.{}'.format(text, str(index),
                                              str(font.split('/')[-1]),
                                              extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index), extension)
        elif name_format == 3:
            image_name = '{}_{}_{}.{}'.format(text, str(index),
                                              str(font.split('/')[-1]),
                                              extension)
        else:
            print('{} is not a valid name format. Using default.'.format(
                name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        final_image

        # tf record writer
        # init for the first time

        # Save the image
        # final_image.convert('RGB').save(os.path.join(out_dir, image_name))
        # img_OpenCV_rawBGR = cv2.cvtColor(np.asarray(final_image.convert('RGB')), cv2.COLOR_RGB2BGR)
        # img_OpenCV_rawRGB = np.asarray(final_image.convert('RGB'))
        img_OpenCV_rawGRAY = cv2.cvtColor(
            np.asarray(final_image.convert('RGB')), cv2.COLOR_RGB2GRAY)
        img_OpenCV = img_OpenCV_rawGRAY
        plt.imshow(img_OpenCV)
        plt.show()

        # cv2.imshow("img_OpenCV_rawBGR",img_OpenCV_rawBGR)
        # cv2.imshow("img_OpenCV_rawRGB",img_OpenCV_rawRGB)
        # cv2.imshow("img_OpenCV_rawGRAY",img_OpenCV_rawGRAY)
        # cv2.waitKey(0)

        # img_OpenCV = rgb2gray(img_OpenCV_raw)

        if orientation == 1:
            im_size = img_OpenCV.shape
            img_re_width = im_size[1]
            # img_re_width = int(im_size[1] * img_re_height * 1.0 / im_size[0])
            # img_OpenCV = cv2.resize(img_OpenCV, (img_re_height, img_re_width))
        else:
            im_size = img_OpenCV.shape
            img_re_width = int(im_size[1] * img_re_height * 1.0 / im_size[0])
            img_OpenCV = cv2.resize(img_OpenCV, (img_re_width, img_re_height))

        if name_format == 3:
            if not os.path.exists(out_dir + "/{}_{}".format(trte, ithread)):
                os.makedirs(out_dir + "/{}_{}".format(trte, ithread))

            name = out_dir + "/{}_{}/tmp-{}—{}-font{}.jpg".format(
                trte, ithread, index, word_labels, str(font.split('/')[-1]))

            if save4cycleGAN:
                img_OpenCV = cv2.resize(img_OpenCV, (256, 256))

            cv2.imwrite(name, img_OpenCV)

            if tf_flag == 1:
                img = tf.gfile.GFile(name, 'rb').read()
                write_one_data(writer, img, word_labels, img_re_width)
    def generate(cls, index, text, font, out_dir, size, extension, channel,
                 skewing_angle, random_skew, blur, random_blur,
                 background_type, distorsion_type, distorsion_orientation,
                 random_process, noise, erode, dilate, incline, 
                 is_handwritten, name_format, width, alignment, text_color,
                 orientation, space_width, margins, fit):

        image = None

        margin_top, margin_left, margin_bottom, margin_right = margins
        horizontal_margin = margin_left + margin_right
        vertical_margin = margin_top + margin_bottom

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = handwritten_text_generator.generate(text, text_color, fit)
        else:
            image = computer_text_generator.generate(text, font, text_color, size, orientation, space_width, fit)

        random_angle = random.random()*random.randint(0-skewing_angle, skewing_angle)
        skewing_angle = skewing_angle if not random_skew else random_angle
        rotated_img = image.rotate(skewing_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################

        if distorsion_type == 0:
            distorted_img = rotated_img # Mind = blown
        elif distorsion_type == 1:
            distorted_img = distorsion_generator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 2:
            distorted_img = distorsion_generator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        else:
            distorted_img = distorsion_generator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(distorted_img.size[0] * (float(size - vertical_margin) / float(distorted_img.size[1])))
            resized_img = distorted_img.resize((new_width, size - vertical_margin), Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + horizontal_margin
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(float(distorted_img.size[1]) * (float(size - horizontal_margin) / float(distorted_img.size[0])))
            resized_img = distorted_img.resize((size - horizontal_margin, new_height), Image.ANTIALIAS)
            background_width = size
            background_height = new_height + vertical_margin
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        
        if background_type == 0:
            background = background_generator.gaussian_noise(background_height, background_width)
        elif background_type == 1:
            background = background_generator.plain_white(background_height, background_width)
        elif background_type == 2:
            background = background_generator.quasicrystal(background_height, background_width)
        else:
            background = background_generator.picture(background_height, background_width)

        #############################
        # Place text with alignment #
        #############################
        
        new_text_width, _ = resized_img.size

        if alignment == 0 or width == -1:
            background.paste(resized_img, (margin_left, margin_top), resized_img)
        elif alignment == 1:
            background.paste(resized_img, (int(background_width / 2 - new_text_width / 2), margin_top), resized_img)
        else:
            background.paste(resized_img, (background_width - new_text_width - margin_right, margin_top), resized_img)

        ##################################
        # Apply gaussian blur #
        ##################################
        
        blur = blur if not random_blur else random.random()*blur
        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius = blur
            )
        )
               
        ##################################
        # Apply image process #
        ##################################
        
        final_image = np.asarray(final_image)
        incline = min(incline, int(final_image.shape[1]/4))
        final_image, noise, dilate, erode, incline = image_process.do(random_process, noise, erode, dilate, incline, final_image)
        final_image = Image.fromarray(final_image)
        
        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index),extension)
        else:
            print('{} is not a valid name format. Using default.'.format(name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        if channel == 3:
            final_image = final_image.convert('RGB')
        elif channel ==1:
            final_image = final_image.convert('L')
        #final_image.save(os.path.join(out_dir, image_name))

        # Resize
        final_image = np.asarray(final_image)
        if orientation == 0:
            final_image = cv2.resize(final_image, (int(final_image.shape[1] * 32 / final_image.shape[0]), 32))
        else:
            final_image = cv2.resize(final_image, (32, int(final_image.shape[0] * 32 / final_image.shape[1])))

        if platform.system() == "Windows":
            font = font.split('\\')[1].split('.')[0]
        else:  # linux
            font = font.split('/')[2].split('.')[0]
                          
        background = ['Guassian', 'Plain white', 'Quasicrystal','Image']
        distorsion = ['None', 'Sine wave', 'Cosine wave']
        
        data = pd.DataFrame(np.array([[index, text, len(text), final_image.shape[1], size, font, skewing_angle, blur,
                                       distorsion[distorsion_type], background[background_type],
                                       noise, erode, dilate, incline]]),
                            columns=['index', 'text', 'text_length', 'img_shape', 'font_size', 'font_id', 'skew_angle',
                                     'blur', 'distorsion_type', 'background_type','num_noise','erode','dilate','incline'])
        if channel == 1 :
            final_image = np.asarray(torch.Tensor(np.asarray(final_image)).unsqueeze(0).permute(1,2,0),dtype='uint8')
        return data, final_image