Пример #1
0
def melanoma_dataset():
    train, test, sub = read_data(args.data_folder)
    train = add_fold(train)
    train, test = anatom_site(train, test)
    train, test = sex_onehot(train, test)
    train, test = age_group(train, test)
    train, test = age_group_onehot(train, test)
    train, test = age_approx(train, test)
    train, test = delete_column(train, test)

    train.to_csv(args.output_folder + '/train.csv', index=False)
    test.to_csv(args.output_folder + '/test.csv', index=False)
    sub.to_csv(args.output_folder + '/sample_submission.csv', index=False)

    resize_image(args.data_folder, args.output_folder)
def create_image_list(img_path, crop_size=None, down_scale=None):
    img = Image.open(img_path[0])
    h = img.height
    w = img.width

    if (crop_size is not None) and (down_scale is not None):
        img = np.array(img)
        img = resize_image(img, crop_size=crop_size, down_scale=down_scale)
        h, w, _ = img.shape

    image_list = []
    for img_id, path in enumerate(img_path):
        image_list += [{
            'id':
            img_id,
            'license':
            0,
            'coco_url':
            'https://www.google.com/',
            'flickr_url':
            'https://www.google.com/',
            'width':
            w,
            'height':
            h,
            'file_name':
            path.split('/')[-1],
            'date_captured':
            datetime.datetime.now().replace(microsecond=0).isoformat(' '),
        }]
    return image_list
Пример #3
0
def generate_single_sample(row):
    """ Generated a single sample from a driving log entry.

    param: row: the Pandas row
    returns: the sample image and the corresponding steering angle
    """
    camera_selector = np.random.randint(3)
    angle = row[3]
    if camera_selector == 1:
        angle += SIDE_CAMERA_ANGLE
    elif camera_selector == 2:
        angle -= SIDE_CAMERA_ANGLE
    img = cv2.imread(re.search('IMG\/.+', row[camera_selector]).group())
    # Crop the car hood before shifting the image to prevent it appearing on the picture
    img = crop_hood(img)
    img, angle = shift_image(img, angle)
    # Crop the sky after shifting the image to minimize generating the sky in case of shifting the picture down
    img = crop_sky(img)
    img = shift_brightness(img)
    if np.random.randint(2) != 0:
        img = cv2.flip(img, 1)
        angle = -angle
    img = resize_image(img)
    img = normalize_image(img)
    return img, angle
Пример #4
0
def resize_images(images):
    images_res = []
    for img in images:
        img2 = ip.make_square_image_middle(img)
        img2 = ip.resize_image(img2, 104)
        images_res.append(img2)
    return images_res
Пример #5
0
    def GET(self, gallery_id, name):
        ext = name.split(".")[-1].lower() # Gather extension

        cType = {
            "png":"images/png",
            "jpg":"images/jpeg",
            "gif":"images/gif",
            "ico":"images/x-icon"            
        }

        path = web.db.image_path(gallery_id, name)
        # web.header("Content-Type", cType[ext]) # Set the Header
        f = None
        try:
            f = open(path['src'], 'rb') # Notice 'rb' for reading images
        except IOError:
            image_processing.resize_image(
                path['original'], path['src'], 1024, 780, mkdir=True)
            f = open(path['src'], 'rb') # Notice 'rb' for reading images
        finally:
            if f:
                return f.read()
            else:
                raise web.notfound()
Пример #6
0
def run(inFile: str, words: set, display: bool = False, out: str = None):
    try:
        os.mkdir(os.path.join(os.getcwd(), "PuzzleSearchResults"))
    except FileExistsError:
        print(
            "CRITICAL ERROR:" +
            os.path.join(os.getcwd(), "PuzzleSearchResults"),
            "already exists prior to program start. Unsure if it is safe to write to."
        )
        # raise FileExistsError
        exit(-1)
    else:
        resize_image(inFile)

    img = cv2.imread(
        os.path.join(os.getcwd(), "PuzzleSearchResults", "working.png"))
    cv2.imwrite(
        os.path.join(os.getcwd(), "PuzzleSearchResults", "original.png"), img)
    preprocess_puzzle(
        words, os.path.join(os.getcwd(), "PuzzleSearchResults", "working.png"))

    draw_results()
    if out is not None:
        img = cv2.imread(
            os.path.join(os.getcwd(), "PuzzleSearchResults", "working.png"))
        cv2.imwrite(out, img)

    if display:
        img = cv2.imread(
            os.path.join(os.getcwd(), "PuzzleSearchResults", "working.png"))
        cv2.namedWindow('Puzzle Search Results', cv2.WINDOW_NORMAL)
        cv2.imshow('Puzzle Search Results', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    delete_files()
Пример #7
0
def get_format(signature):

    # Resize image into boundaries (60,200)
    if signature.shape[0] > 60:
        signature = ip.resize_image(signature, height=60)
    if signature.shape[1] > 200:
        signature = ip.resize_image(signature, width=200)

    # Build input for CNN
    h, w = signature.shape
    format_input = np.zeros((1, 60, 200))
    format_input[0, 0:h, 0:w] = signature

    format_input = np.expand_dims(format_input, axis=3)
    format_input = format_input.astype('float32')
    format_input /= 255

    # Make Prediction
    prediction = F_MODEL.predict(format_input)

    # Get Format
    format = np.argmax(prediction[0])

    return format
Пример #8
0
def make_template_images(template_filepaths, size=None):
    """Make dictionary of images from template_filepaths.
    :param template_filepaths:
    :param size:
    """
    templates = {}
    for filepath in template_filepaths:
        template = imp.load_image(filepath)
        if size is not None:
            template = imp.resize_image(template, size)
        template = imp.image_gray(template)
        template = imp.image_bin_otsu(template)
        template = imp.invert(template)
        templates[filepath] = template
    return templates
Пример #9
0
def resize_imgs(img_name_vector):
    resize_img_name_vector = []
    exists_paths           = {}
    # we resize and save images
    img_id = 0
    for img_path in img_name_vector:
        if img_path not in exists_paths:
            input_img = cv2.imread(img_path)
            resized_img = resize_image(input_img)
            cv2.imwrite(f'./resize_images/{img_id}.jpg',resized_img)
            link_name = f'./resize_images/{img_id}.jpg'
            exists_paths[img_path] = img_id
            img_id += 1
        else:
            link_name = f'./resize_images/{exists_paths[img_path]}.jpg'
        resize_img_name_vector.append(link_name)
    
    return resize_img_name_vector
Пример #10
0
def select_user_roi(image_path):
    '''
    由于原图的分辨率较大,这里缩小后获取ROI,返回时需要重新scale对应原图
    :param image_path:
    :return:
    '''
    orig_image = image_processing.read_image(image_path)
    orig_shape = np.shape(orig_image)
    resize_image = image_processing.resize_image(orig_image,
                                                 resize_height=800,
                                                 resize_width=None)
    re_shape = np.shape(resize_image)
    g_rect = get_image_roi(resize_image)
    orgi_rect = image_processing.scale_rect(g_rect, re_shape, orig_shape)
    roi_image = image_processing.get_rect_image(orig_image, orgi_rect)
    image_processing.cv_show_image("RECT", roi_image)
    image_processing.show_image_rect("image", orig_image, orgi_rect)
    return orgi_rect
def find_whole_notes(image, regions, bar_lines, clefs, time_signatures,
                     staff, staff_spacing, staff_distance, min_match=0.61):
    note_templates = {}
    for templateName in search_for_templates(["note_heads/whole", "note_heads/double_whole"]):
        template = imp.load_image(templateName)
        template = imp.resize_image(template, (int(round(staff_spacing)), int(round(staff_spacing))))
        template = imp.image_gray(template)
        template = imp.image_bin_otsu(template)
        template = imp.invert(template)
        note_templates[templateName] = template

    rel_staff = get_rel_staff(staff, staff_distance)
    # find note_heads
    print("Finding whole note heads...")
    notes = []
    possible_regions = get_possible_whole_note_regions(regions, bar_lines, clefs, time_signatures, staff_spacing)
    for region in possible_regions:
        reg_top = min([r for r, c in region])
        reg_height, reg_width = get_region_image(image, region).shape[:2]
        if staff_spacing <= reg_width and reg_height >= staff_spacing:
            min_r = rel_staff[0][-1]
            line_index = 0.5
            while abs(min_r - reg_top) >= staff_spacing // 2:
                if min_r > reg_top:
                    min_r -= staff_spacing // 2
                    line_index -= 0.5
                else:
                    min_r += staff_spacing // 2
                    line_index += 0.5
            min_r = int(min_r)

            max_r = max([r for r, c in region])
            for start_r in range(min_r, max_r, int(staff_spacing // 2)):
                sub_region = [value for value in region
                              if start_r + staff_spacing >= value[0] >= start_r]
                if len(sub_region) > 0:
                    sub_region_img = get_region_image(image, sub_region)
                    best_match = template_match(sub_region_img,
                                                template_images=note_templates,
                                                print_results=False)
                    if min_match <= best_match[1]:
                        notes += [(region, line_index, best_match)]
                line_index += 0.5
    return notes
Пример #12
0
def call_tensorflow(tokenizer, imagedata, num_quotes=5):
    imagedata = resize_image(imagedata)
    feature = call_inception(imagedata)
    retpred = []
    for r in range(num_quotes):
        prediction = call_autoencoder_server(feature)
        prediction = np.reshape(prediction, (-1))
        pred = ''
        for index, j in enumerate(prediction[1:]):
            word = str([key for key, value in tokenizer.items()
                        if value == j][0])
            if word != '<end>':
                if word == '<unk>':
                    continue
                pred += (word + ' ')
            else:
                break
        retpred.append(pred.capitalize()[0:-1])
    return retpred
def cnn_output(image):
    image = resize_image(image)
    image = image.reshape((1, 1, 64, 64))
    image = image.astype('float32')
    test_dataset = FacialDatabase(image, 'image')
    test_trainloader = DataLoader(test_dataset)
    image /= 255
    model = torch.load("facial_recognition.pt")
    model.eval()

    dataiter = iter(test_trainloader)
    img, lbl = dataiter.next()
    output = model(img)

    sm = nn.Softmax(dim=1)
    sm_output = sm(output)
    print(sm_output)

    probs, index = torch.max(sm_output, dim=1)
    return probs, index
def find_vertical_notes(org_image, regions, staff, staff_spacing, staff_distance,
                        tolerance=None, flag_min_match=0.7, note_head_min_match=0.8,
                        half_note_head_min_match=0.65):
    image = org_image.copy()

    rel_staff = get_rel_staff(staff, staff_distance)

    recognized_notes = []
    unrecognized_regions = []
    small_regions = []

    note_heads_templates = {}
    for templateName in search_for_templates(["note_heads/filled"]):
        template = imp.load_image(templateName)
        template = imp.resize_image(template, (int(round(staff_spacing)), int(round(staff_spacing))))
        template = imp.image_gray(template)
        template = imp.image_bin_otsu(template)
        template = imp.invert(template)
        note_heads_templates[templateName] = template

    half_note_heads_templates = {}

    for templateName in search_for_templates(["note_heads/half", "note_heads/whole"]):
        template = imp.load_image(templateName)
        template = imp.resize_image(template, (int(round(staff_spacing)), int(round(staff_spacing))))
        template = imp.image_gray(template)
        template = imp.image_bin_otsu(template)
        template = imp.invert(template)
        half_note_heads_templates[templateName] = template

    for index, region in enumerate(regions):
        org_reg_c = min([c for r, c in region])
        org_reg_r = min([r for r, c in region])
        region_image = irr.get_region_image(image, region)
        if len(region_image) > 3 * staff_spacing:
            img_vert_lines = imo.open_image_vertically(region_image, staff_spacing, 3)
            vertical_lines = find_regions(img_vert_lines)[1]
            remove_white_pixels([region_image], vertical_lines)

            avg_vert_line_thickness = find_avg_thickness(vertical_lines)

            if tolerance is None:
                tolerance = avg_vert_line_thickness

            # Find beams by searching for regions between vertical lines
            print("Finding full beams...")
            full_beams = []
            connected_regions = []
            separate_regions = []
            sub_regions = find_regions(region_image)[1]
            for sub_region in sub_regions:
                start_vert_line = None
                end_vert_line = None
                lines = []
                min_col = min([c for r, c in sub_region])
                max_col = max([c for r, c in sub_region])
                for line in vertical_lines:
                    min_line_col = min([c for r, c in line])
                    max_line_col = max([c for r, c in line])
                    if -tolerance <= min_col - max_line_col <= tolerance:
                        start_vert_line = line
                        lines += [line]
                    elif -tolerance <= min_line_col - max_col <= tolerance:
                        end_vert_line = line
                        lines += [line]
                    elif min_col < min_line_col < max_col:
                        lines += [line]
                if start_vert_line is not None and end_vert_line is not None:
                    full_beams += [(sub_region, start_vert_line, end_vert_line, lines)]
                elif len(lines) > 0:
                    connected_regions += [(sub_region,
                                           lines[0])]
                else:
                    separate_regions += [sub_region]

            # Find half beams
            print("Finding half beams...")
            half_beams = []
            for sub_region in connected_regions:
                max_row = max([r for r, c in sub_region[0]])
                min_row = min([r for r, c in sub_region[0]])
                min_col = min([c for r, c in sub_region[0]])
                max_col = max([c for r, c in sub_region[0]])
                for beam in full_beams:
                    if sub_region[1] in beam[3]:
                        try:
                            min_beam_row = min([r for r, c in beam[0] if min_col <= c <= max_col])
                            max_beam_row = max([r for r, c in beam[0] if min_col <= c <= max_col])
                        except ValueError:
                            continue
                        distance = None
                        if min_row > max_beam_row:
                            distance = min_row - max_beam_row
                        elif max_row < min_beam_row:
                            distance = min_beam_row - max_row
                        if distance is not None and distance < staff_spacing:
                            half_beams += [sub_region]
                            break

            for half_beam in half_beams:
                connected_regions.remove(half_beam)

            # find flags
            print("Finding flags...")
            flags = []
            flag_templates = search_for_templates("flags")
            for sub_region in connected_regions:
                best_match = template_match(get_region_image(region_image, sub_region[0]),
                                            template_filepaths=flag_templates,
                                            resize=True, print_results=False)
                if flag_min_match <= best_match[1]:
                    flags += [(sub_region, best_match)]

            for flag, match in flags:
                connected_regions.remove(flag)

            # find note_heads
            print("Finding note heads...")
            note_heads = []
            for connected_region in connected_regions:
                min_r = rel_staff[0][-1]
                line_index = 0.5
                while abs(min_r - org_reg_r) >= staff_spacing // 2:
                    if min_r > org_reg_r:
                        min_r -= staff_spacing // 2
                        line_index -= 0.5
                    else:
                        min_r += staff_spacing // 2
                        line_index += 0.5
                min_r -= org_reg_r
                min_r = int(min_r)

                max_r = max([r for r, c in connected_region[0]])
                for start_r in range(min_r, max_r, int(staff_spacing // 2)):
                    sub_region = [value for value in connected_region[0]
                                  if start_r + staff_spacing >= value[0] >= start_r]
                    if len(sub_region) > 0:
                        sub_region_img = get_region_image(region_image, sub_region)
                        best_match = template_match(sub_region_img,
                                                    template_images=note_heads_templates,
                                                    print_results=False)
                        if note_head_min_match <= best_match[1]:
                            note_heads += [(sub_region, connected_region, line_index, best_match)]
                        else:
                            best_match = template_match(sub_region_img,
                                                        template_images=half_note_heads_templates,
                                                        print_results=False)
                            if half_note_head_min_match <= best_match[1]:
                                note_heads += [(sub_region, connected_region, line_index,
                                                ("templates/note_heads/half_01", best_match[1]))]
                    line_index += 0.5

            for note_head, connected_region, line_index, match in note_heads:
                if connected_region in connected_regions:
                    connected_regions.remove(connected_region)

            # recognize note
            print("Recognizing notes' properties...")
            checked_flags = []
            notes = []
            for note_head, connected_region, line_index, match in note_heads:
                height = line_index
                note_head_type = match[0].split('/')[-1].split('_')[0]
                flags_and_beams = 0
                line = connected_region[1]

                for beam in full_beams:
                    if line in beam[3]:
                        flags_and_beams += 1
                for half_beam in half_beams:
                    if line == half_beam[1]:
                        flags_and_beams += 1
                for flag, flag_match in flags:
                    if line == flag[1]:
                        flags_and_beams += int(flag_match[0].split('/')[-1].split('_')[0]) / 8
                        checked_flags += [(flag, flag_match)]

                if flags_and_beams > 0:
                    duration = 1 / 4.
                    for i in range(flags_and_beams):
                        duration /= 2
                else:
                    duration = 1 / 4. if note_head_type == "filled" else 0.5

                notes += [(min([c for r, c in connected_region[0]]), height, note_head_type, duration)]

            for flag in flags:
                if flag not in checked_flags:
                    connected_region = flag[0]
                    min_r = rel_staff[0][-1]
                    line_index = 0.5
                    while abs(min_r - org_reg_r) >= staff_spacing // 2:
                        if min_r > org_reg_r:
                            min_r -= staff_spacing // 2
                            line_index -= 0.5
                        else:
                            min_r += staff_spacing // 2
                            line_index += 0.5
                    min_r -= org_reg_r
                    min_r = int(min_r)

                    max_r = max([r for r, c in connected_region[0]])
                    for start_r in range(min_r, max_r, int(staff_spacing // 2)):
                        sub_region = [value for value in connected_region[0]
                                      if start_r + staff_spacing >= value[0] >= start_r]
                        if len(sub_region) > 0:
                            best_match = template_match(get_region_image(region_image,sub_region),
                                                        template_images=half_note_heads_templates,
                                                        print_results=False)
                            if half_note_head_min_match <= best_match[1]:
                                note_heads += [(sub_region, connected_region, line_index,
                                                ("templates/note_heads/half_01", best_match[1]))]
                                notes += [(min([c for r, c in connected_region[0]]), line_index, "half", 0.5)]
                        line_index += 0.5

            notes = sorted(notes)
            recognized_notes += [(region, org_reg_c, notes)]
            unrecognized_regions += [(region, connected_regions, separate_regions)]

            if False:
                # Remove flags and beams from original image
                to_remove = []
                to_remove_from_region_image = []

                for flag, match in flags:
                    for r, c in flag[0]:
                        to_remove += [(r + org_reg_r, c + org_reg_c)]
                        to_remove_from_region_image += [(r, c)]
                for half_beam in half_beams:
                    for r, c in half_beam[0]:
                        to_remove += [(r + org_reg_r, c + org_reg_c)]
                        to_remove_from_region_image += [(r, c)]
                for beam in full_beams:
                    for r, c in beam[0]:
                        to_remove += [(r + org_reg_r, c + org_reg_c)]
                        to_remove_from_region_image += [(r, c)]
                for note_head, connected_region, line_index, match in note_heads:
                    for r, c in note_head:
                        to_remove += [(r + org_reg_r, c + org_reg_c)]
                        to_remove_from_region_image += [(r, c)]
                for line in vertical_lines:
                    for r, c in line:
                        to_remove += [(r + org_reg_r, c + org_reg_c)]
                        to_remove_from_region_image += [(r, c)]

                remove_white_pixels([image], [to_remove])
                remove_white_pixels([region_image], [to_remove_from_region_image])
        else:
            small_regions += [region]
            # remove_white_pixels([image], [region])
    recognized_notes.sort(key=lambda x: x[1])
    return recognized_notes
Пример #15
0
def preprocess_image(image, new_image_shape):
    image = red_to_white(image)
    image = rgb_to_gray(image)
    image = crop_whitespace(image)
    image = resize_image(image, new_image_shape)
    return image
Пример #16
0
	img = images[i]
	y_pred = predictions[i]
	max_p = max(y_pred)
	c_index = np.where(y_pred == max_p)[0][0]
	print(c_index)
	c = classes[c_index]
	text = c + " " + str(max_p)
	img = ip.add_text_to_image(text, img)
	ip.show_image(img)'''

filename = os.fsdecode("dolphin.jpg")
image = scipy.ndimage.imread(filename, mode='RGB')
ip.show_image(image)

image2 = ip.make_square_image_middle(image)
image2 = ip.resize_image(image2, 104)

y_pred = model.predict(np.array([image2]))[0]
max_p = max(y_pred)
c_index = np.where(y_pred == max_p)[0][0]
print(c_index)
c = classes[c_index]
text = c + " " + str(max_p)
img = ip.add_text_to_image(text, image)
ip.show_image(img)

image2 = ip.make_square_image_middle(image)
image2 = ip.resize_image(image2, 315)
step = 7
scale = 1.5
crop_images = []