示例#1
0
def segment_characters(image, rows, cols):
    # Extract characters from image.  
    countCharacters = 0
    coordinates = []
    amOnCharacter = False
    i = 0
    for col in cols:
        if 1 in col:
            if not amOnCharacter:
                coordinates.append(i)
                countCharacters += 1
                amOnCharacter = True
        else:
            if amOnCharacter:
                coordinates.append(i)
                amOnCharacter = False
        i += 1

    if (len(coordinates) % 2 != 0):
        coordinates.append(i)

    for i in range(0, len(coordinates), 2):
        cropped_image = image.crop(
            (coordinates[i], 0, image.size[0] - (image.size[0] - (coordinates[i + 1])), image.size[1]))
        newfilepath = "CharacterDetection/" + str(i // 2 + 1) + ".png"
        cropped_image.save(newfilepath)

    return coordinates
示例#2
0
def extractLinesFromImage(lines, image):
    dir_name = "LineDetection/"

    i = 1
    for line in lines:
        cropped_image = image.crop(
            (0, line[0], image.size[0], line[1]))
        newfilepath = dir_name + str(i) + ".png"
        cropped_image.save(newfilepath)
        i += 1
def dump_clothing_test_data():
    for index, item in test_labels_clothes.iterrows():
        print(item['filename'])
        image = find_image(item['filename'])
        print(os.getcwd())
        #crop the image from the bounding boxes
        t = image.crop((item['xmin'], item['ymin'], item['xmax'], item['ymax'])) 
        os.chdir('data/transfer_learning/clothes/test_set ') #Incomplete make sure making test/train split
        os.chdir(str(item['classes']))
        t.save(fp = item['filename'])
        os.chdir("../../../../..")
def dump_clothing_training_data():
    for index, item in train_labels_clothes.iterrows():
        print(item['filename'])
        image = find_image(item['filename'])
        print(os.getcwd())
        #crop the image from the bounding boxes
        t = image.crop((item['xmin'] + margin, item['ymin'] + marginHead , item['xmax'] - margin, item['ymax'] - margin)) 
        os.chdir('data/transfer_learning/clothes/training_set ') 
        os.chdir(str(item['classes']))
        t.save(fp = item['filename'])
        os.chdir("../../../../..")
def dump_test_color_data():
    for index, item in test_labels_colors.iterrows():
        print(item['filename'])
        image = find_image(item['filename'])
        print(os.getcwd())
        #crop the image from the bounding boxes
        t = image.crop((item['xmin'] + margin, item['ymin'] + marginHead, item['xmax'] - margin, item['ymax'] - margin)) 
        os.chdir('data/transfer_learning/colors/test_set ') 
        os.chdir(str(item['major_color']))
        t.save(fp = item['filename'])
        os.chdir("../../../../..")
def rescale_image(image):
    size = np.asarray(image.size)
    size = (size * IMAGE_SIZE / min(size)).astype(int)
    image = image.resize(size, resample=Image.LANCZOS)
    w, h = image.size
    image = image.crop((
        (w - IMAGE_SIZE) // 2,
        (h - IMAGE_SIZE) // 2,
        (w + IMAGE_SIZE) // 2,
        (h + IMAGE_SIZE) // 2)
    )
    return image
def dump_test_pattern_data():
    for index, item in test_labels_pattern.iterrows():
        print(item['filename'])
        image = find_image(item['filename'])
        print(os.getcwd())
        marginHead = 20
        margin = 10
        #crop the image from the bounding boxes
        t = image.crop((item['xmin'] + margin, item['ymin'] + marginHead, item['xmax'] - margin, item['ymax'] - margin)) 
        os.chdir('data/transfer_learning/pattern/test_set ') 
        os.chdir(str(item['clothing_pattern']))
        t.save(fp = item['filename'])
        os.chdir("../../../../..")
示例#8
0
def processImage(imagePath):
    image = Image.open(imagePath)
    image = image.crop(bbox)
    (currw, currh) = image.size
    if currw >= currh:
        image = image.resize((width, int(float(currh) * float(width) / float(currw))))
    else:
        image = image.resize((int(float(currw) * float(height) / float(currh)), height))
    background = Image.new('RGB', (width, height), (0, 0, 0))
    background.paste(
        image, (int((width - image.size[0]) / 2), int((height - image.size[1]) / 2))
    )
    return background
示例#9
0
def cropImage(image, x1, y1, x2, y2):
	ima=image.crop((x1,y1,x2,y2))
	return ima
示例#10
0
def threshold_slow(image):
    #Transformar a imagem em array de bytes
    image_process = np.array(image)
    #Pasta onde fica alojados os arquivos cortados
    path_crop_save = save_path + "/crop/"
    print("Carregando imagem....")
    #Nome padrao para os arquivos
    file_name_save = "/imgpixel"
    text = ""
    #Obtendo a dimenssao da imagem escolhida
    largura, altura = image.size
    #Fazendo o calculo da proporcao
    proporcao = largura / altura
    print("Largura: " + str(largura))
    print("Altura: " + str(altura))
    print("Proporcao: " + str(proporcao))
    #Obtendo o pixel da imagem
    pixels = list(image.getdata())
    numl1 = numl2 = numl3 = numl4 = numl5 = 0
    #print("Pixels: "+str(pixels))
    #Loop para percorrer a imagem
    for y in range(0, altura - 50):
        print('linha', y)
        for x in range(0, largura - 50):
            #Inicialmente e realizado o corte na dimensao de 50x50
            #Crop (Esquerda, cima, direita, baixo)
            new_image = image.crop((x, y, x + 50, y + 50)).copy()
            #A imagem cortada e transformada em array pois o metodo de predicacao so aceita iamgem em array
            crop_img_array = np.array(new_image)
            #Essa nova imagem cortada esta em 3 canais(RGB), com isso e acionado o metodo load
            #para transformar em array e em um canal (graysacle)

            #img_gray = load(new_image)
            img_gray = new_image.convert('L')
            #     img_gray.show()
            time.sleep(0.5)
            img_gray = (np.asarray(img_gray).reshape(1, 50, 50, 1)) / 255.0
            #A ideia e marcar o pixel central, na imagem orignal,da que foi cortada para verificar a execucao
            #com isso ha o metodo threshold no opencv para binarizar a iamgem e posteriormente
            #calcular o momento da binarizacao da imagem, que isto e realizado pelo metodo moments
            # ret,thresh = cv2.threshold(img_gray,127,255,0)
            # M = cv2.moments(thresh)
            image_class = loaded_model.predict(img_gray, verbose=0)
            print(">>>>>>>" + str(image_class))
            #Atribui o valor da classe classificada
            # print(img_gray)
            #exit()
            pred = image_class.argmax(axis=-1)
            ''' Lembrando que o 0 e o numero 7 na regua, bem como
                        o 1 e o 8, 2 e o 13 e o 3 e 14, ja para finalizar
                        4 sao as negativas ou lixo
                        '''

            if pred == 0:
                numl1 = numl1 + 1
                #   time.sleep(0.5)
                #Cada corte e salvo na pasta declarada abaixo para comparar se realmente esta fazendo correto
                crop_write = ""
                crop_write = path_crop_save + "L1/" + file_name_save + "{0}-{1}".format(
                    x, y) + ".jpeg"
                cv2.imwrite(crop_write, crop_img_array)
                #Aqui e localizado o centro da imagem, ou seja, do 50x50 e depois marca com a cor de cada camada
                #declarada, neste caso a verde sao para identificar o numero 7 na regua.
                cX = x + 25
                cY = y + 25
                image_process = cv2.circle(image_process, (cX, cY), 5, verde,
                                           -1)
                cv2.imwrite(save_path + "regua.jpg", image_process)
            elif pred == 1:
                numl2 = numl2 + 1
                #     time.sleep(0.5)
                crop_write = ""
                crop_write = path_crop_save + "L2/" + file_name_save + "{0}-{1}".format(
                    x, y) + ".jpeg"
                cv2.imwrite(crop_write, crop_img_array)
                cX = x + 25
                cY = y + 25
                image_process = cv2.circle(image_process, (cX, cY), 5, amarelo,
                                           -1)
                cv2.imwrite(save_path + "regua.jpg", image_process)
            elif pred == 2:
                numl3 = numl3 + 1
                #         time.sleep(0.5)
                crop_write = ""
                crop_write = path_crop_save + "L3/" + file_name_save + "{0}-{1}".format(
                    x, y) + ".jpeg"
                cv2.imwrite(crop_write, crop_img_array)
                cX = x + 25
                cY = y + 25
                image_process = cv2.circle(image_process, (cX, cY), 5, laranja,
                                           -1)
                cv2.imwrite(save_path + "regua.jpg", image_process)
            elif pred == 3:
                numl4 = numl4 + 1
                #          time.sleep(0.5)
                crop_write = ""
                crop_write = path_crop_save + "L4/" + file_name_save + "{0}-{1}".format(
                    x, y) + ".jpeg"
                cv2.imwrite(crop_write, crop_img_array)
                cX = x + 25
                cY = y + 25
                image_process = cv2.circle(image_process, (cX, cY), 5,
                                           vermelho, -1)
                cv2.imwrite(save_path + "regua.jpg", image_process)
            else:
                #print("Nernhum")
                numl5 = numl5 + 1
                print(str(numl5) + "ª vez")
                #crop_write = ""
                #crop_write = path_crop_save+"L5/"+file_name_save+"{0}-{1}".format(x,y)+".jpeg"
                #cv2.imwrite(crop_write,crop_img_array)
                #cX=x+25
                #cY=y+25
                #image_process = cv2.circle(image_process, (cX, cY), 5, azul, -1)
                #cv2.imwrite(save_path+"regua.jpg",image_process)

    print(numl1, numl2, numl3, numl4, numl5)
示例#11
0
    rec_channels = array(rec_channels, dtype=uint8)

    pil_channels = [Image.fromarray(channel) for channel in rec_channels]

    # image and compress image
    # rec_image = Image.merge("YCbCr", channels).convert('RGB')
    rec_pil_image = Image.merge("YCbCr", pil_channels).convert('RGB')

    # compute file size
    # rec_pil_image
    # f2 = BytesIO()
    # rec_pil_image.save(f2, format='JPEG', quality=75)  # qtables=pil_qtables)
    # rec_pil_image_size = len(f2.getvalue())
    return rec_pil_image


if __name__ == '__main__':
    image = Image.open(
        '/home/imagenet-data/train//n03977966/n03977966_11273.JPEG')
    image_size = min(image.size)
    crop_image = image.crop((0, 0, image_size, image_size))

    f1 = BytesIO()
    crop_image.save(f1, format='JPEG', quality=75)  # qtables=pil_qtables)
    print("f1", len(f1.getvalue()))

    deepn_image = DeepN(crop_image)
    f2 = BytesIO()
    deepn_image.save(f2, format='JPEG', quality=75)  # qtables=pil_qtables)
    print("f2", len(f2.getvalue()))
示例#12
0
                    display_str = ''
                    if classes[j] in category_index.keys():
                        class_name = str(category_index[classes[j]]['name'])
                    else:
                        class_name = 'NA'
                    display_str = str(class_name)

                    if class_name in labels_whitelist:
                        display_str = '{}_{}'.format(display_str,
                                                     int(100 * scores[j]))

                        print('cropping box {0} ({1}, {2}, {3}, {4})'.format(
                            display_str, xmin_px, ymin_px, xmax_px, ymax_px))

                        cropped_img = image.crop(
                            (xmin_px, ymin_px, xmax_px, ymax_px))
                        with tf.gfile.Open(
                                os.path.join(
                                    PATH_TO_TEST_IMAGES_CROP_RES_DIR,
                                    '{0}_{1}_{2}.jpg'.format(
                                        image_path_filename, display_str, j)),
                                'w') as fid:
                            cropped_img.save(fid, 'JPEG')

                        features = get_feature(cropped_img)
                        w.writerow({
                            "filename": image_path,
                            "object_class": class_name,
                            "score": scores[j],
                            "ymin": ymin,
                            "xmin": xmin,
示例#13
0
def cropAndAddPadding(image, newfilepath):

    pix_val = list(image.getdata())


    i = 0
    row = []
    rows= []

    el = 0
    for element in pix_val:
        if element[0] == 255:
            el = 0
        else:
            el = 1
            
        if i % image.size[0] == 0 and i != 0:#image.size returns a tuple (width, height)
            rows.append(row)
            row = []
        if i % image.size[0] != 0: 
            row.append(el)
        i += 1	

    col = []
    cols= []

    for i in range(0,len(rows[0])): #za sekoj chlen od prviot row
        col = []
        for j in range(0,len(rows)): #za sekoj row
            col.append(rows[j][i])
        cols.append(col)
    # Cropping

    cropFromTop = 0
    cropFromBottom = 0
    for row in rows:
        if not(1 in row):
            cropFromTop += 1
        else:
            break

    reversed_rows = rows[::-1]

    for row in reversed_rows:
        if not(1 in row):
            cropFromBottom += 1
        else:
            break

    cropFromLeft = 0
    cropFromRight = 0

    for col in cols:
        if not(1 in col):
            cropFromLeft += 1
        else:
            break

    for col in cols[::-1]:
        if not(1 in col):
            cropFromRight += 1
        else:
            break

    cropped_image = image.crop((cropFromLeft, cropFromTop, image.size[0]-cropFromRight, image.size[1]-cropFromBottom))
    cropped_image.save(newfilepath)
    size = (50, 50)

    resized_image = cropped_image
    padding = 0
    im = cv2.imread(newfilepath)
    color = [255, 255, 255]

    if not (resized_image.size[0] == resized_image.size[1]):
        if resized_image.size[0] > resized_image.size[1]:
            padding = (resized_image.size[0]-resized_image.size[1])//2
            croppedThenPadded = cv2.copyMakeBorder(
                im, padding, padding, 0, 0, cv2.BORDER_CONSTANT, value=color)
        else:
            padding = (resized_image.size[1]-resized_image.size[0])//2
            croppedThenPadded = cv2.copyMakeBorder(
                im, 0, 0, padding, padding, cv2.BORDER_CONSTANT, value=color)
        sv = Image.fromarray(croppedThenPadded)
        sv.save(newfilepath)
        x = sv.resize((size[0], size[1]), Image.LANCZOS)
        x.save(newfilepath)
示例#14
0
def cropImage(image, x1, y1, x2, y2):

    #utils.raiseNotDefined()

    cropped_image = image.crop((x1, y1, x2, y2))
    return cropped_image
示例#15
0
def crop_image(image, enable = 0):
    if enable:
        return image.crop((151, 151, image.size[0] - 150, image.size[1] - 150))
    else:
        return image