Esempio n. 1
0
def convert(path_list, annots, batch_size, max_idx, idx):
    start = idx * batch_size
    end = min(start + batch_size, max_idx)
    path_list = path_list[start:end]
    meta = pd.DataFrame(columns=['uid', 'flag', 'z_len', 'y_len', 'x_len'])
    labels = pd.DataFrame(columns=['uid', 'flag', 'z', 'y', 'x', 'diam', 'vol'])
    for i, path in enumerate(path_list):
        if os.path.basename(path) == 'b8bb02d229361a623a4dc57aa0e5c485':
            # ITK cannot read this file
            continue
        print('Converting %s' % path)
        scan_data, uid = read_scan(path)
        if scan_data is None:
            continue

        slices, spacing, origin = get_data(scan_data)
        skip = 0

        video.clip(slices, settings.low_thresh, settings.high_thresh)
        msk = mask.get_mask(slices, uid)
        # msk = mask.segment_lung_mask(slices, uid)
        slices = video.normalize(slices, settings.low_thresh, settings.high_thresh)
        mask.apply_mask(slices, msk)
        slices, starts = trim(slices)
        valid, flag = process_annotations(uid, annots, origin, labels, slices.shape, starts)
        if not valid:
            print('Ignoring %s - bad metadata' % path)
            continue

        video.write_data(slices, os.path.join(output_path, uid))
        meta.loc[meta.shape[0]] = dict(uid=uid, flag=flag, z_len=slices.shape[0],
                                       y_len=slices.shape[1], x_len=slices.shape[2])
    return meta, labels
Esempio n. 2
0
def convert(path_list, annots, batch_size, max_idx, idx):
    start = idx * batch_size
    end = min(start + batch_size, max_idx)
    path_list = path_list[start:end]
    meta = pd.DataFrame(columns=['uid', 'flag', 'z_len', 'y_len', 'x_len'])
    labels = pd.DataFrame(columns=['uid', 'flag', 'z', 'y', 'x', 'diam', 'vol'])
    for i, path in enumerate(path_list):
        if os.path.basename(path) == 'b8bb02d229361a623a4dc57aa0e5c485':
            # ITK cannot read this file
            continue
        print('Converting %s' % path)
        scan_data, uid = read_scan(path)
        if scan_data is None:
            continue

        slices, spacing, origin = get_data(scan_data)
        skip = 0

        video.clip(slices, settings.low_thresh, settings.high_thresh)
        msk = mask.get_mask(slices, uid)
        slices = video.normalize(slices, settings.low_thresh, settings.high_thresh)
        mask.apply_mask(slices, msk)
        slices, starts = trim(slices)
        valid, flag = process_annotations(uid, annots, origin, labels, slices.shape, starts)
        if not valid:
            print('Ignoring %s - bad metadata' % path)
            continue

        video.write_data(slices, os.path.join(output_path, uid))
        meta.loc[meta.shape[0]] = dict(uid=uid, flag=flag, z_len=slices.shape[0],
                                       y_len=slices.shape[1], x_len=slices.shape[2])
    return meta, labels
Esempio n. 3
0
def phase():

    # Get path, filenames, and mask setup
    path = path_entry.get()
    path_raw, path_images, filenames = get_path(path, filetype="h5")
    first_file = os.path.join(path, filenames[0])
    mask, coord = get_mask(first_file, border=2)
    mask = mask[coord[0] : coord[1], coord[2] : coord[3]]

    # Setup summary string
    deb = "x size," + str(coord[1] - coord[0]) + "\n"
    deb += "y size," + str(coord[3] - coord[2]) + "\n"
    deb += "number of files," + str(len(filenames)) + "\n"
    deb += "file, instensity, modulation, time\n"

    # Run 9-bin phase wrap
    i = 0
    maxf = str(len(filenames))
    # t.set(str(i)+'/'+maxf)
    # win.update()
    # zz = 0
    zz = time.clock()

    for ii in xrange(1, 14):
        zz = time.clock()
        for jj in xrange(10):
            pool = Pool(processes=ii)
            A = []
            for filename in filenames:
                A.append((filename, path, path_raw, path_images, mask, coord))
            imap1 = pool.map(get_phase, A)
            # imap1 = map(get_phase,A)
            pool.close()

            for x in imap1:
                # t.set(str(i)+'/'+maxf)
                # win.update()
                # i+=1
                deb += x
                # print x
        zz = time.clock() - zz
        print ("Process=%d, time=%f") % (ii, zz / 10)

    """    
    for filename in filenames:
        t.set(str(i))
        win.update()
        z = time.clock()
        deb = get_phase(filename, path, path_raw, path_images, mask, coord, deb)
        zz+=time.clock()-z
        i+=1
    """

    # Print summary and save to ./debug.csv
    # print deb
    f_debug = os.path.join(path, "debug.csv")
    f_debug = open(f_debug, "w")
    f_debug.write(deb)
    f_debug.close()
    """
Esempio n. 4
0
 def play(self, word):
     feeble_attempts = 0
     guessed_letters = set()
     wrong_letters = set()
     word_mask = mask.get_mask(word, guessed_letters)
     while feeble_attempts < NUMBER_OF_FEEBLE_ATTEMPTS and word != word_mask:
         letter = self.guesser.guess_letter(word_mask, guessed_letters, wrong_letters)
         if letter in guessed_letters or letter in wrong_letters:
             raise Exception()
         if letter in word:
             guessed_letters.add(letter)
             word_mask = mask.get_mask(word, guessed_letters)
         else:
             wrong_letters.add(letter)
             feeble_attempts += 1
     return word == word_mask, word_mask, feeble_attempts
Esempio n. 5
0
def test(nyu2_loader, model, width, height):
    for i, image in enumerate(nyu2_loader):     
        image = torch.autograd.Variable(image, volatile=True).cuda()
        out = model(image)
        out = out.view(out.size(2),out.size(3)).data.cpu().numpy()
        max_pix = out.max()
        min_pix = out.min()
        out = (out-min_pix)/(max_pix-min_pix)*255
        out = cv2.resize(out,(width,height),interpolation=cv2.INTER_CUBIC)
        cv2.imwrite(os.path.join(args.output, "out_grey.png"),out)
        out_grey = cv2.imread(os.path.join(args.output, "out_grey.png"),0)
        out_color = cv2.applyColorMap(out_grey, cv2.COLORMAP_JET)
        cv2.imwrite(os.path.join(args.output, "out_color.png"),out_color)
        vol = get_volume(out_grey, args.json)
        print("Volume:")
        print(vol)
        print("unit: cm^3")
        out_file = open(os.path.join(args.output, "out.txt"), "w")
        out_file.write("Volume:\n")
        out_file.write(str(vol))
        out_file.write("\n")
        out_file.write("unit: cm^3")
        out_file.close()
        get_mask(out_grey, args.json, args.output)
if __name__ == "__main__":
    args = parser.parse_args()
    cmap = args.cmap
    labels = np.load(args.labels)
    scale = args.scale_factor
    mask = None

    if args.video:
        # Load video and reshape labels
        frame = skvideo.io.vread(args.video, num_frames=1)[0]
        M, N, _ = frame.shape

        # Upscale the labels if needed
        if scale > 1:
            upscaled = scale_labels(labels, M, N, scale)
            labels_grid = upscaled
        else:
            labels_grid = np.reshape(labels, (M, N))

        # Display the image, with or without masking out the background
        if args.use_mask:
            mask = get_mask(skvideo.io.vread(args.video))
            show_with_mask(labels_grid, mask)
        else:
            show_overlay(labels_grid,
                         frame,
                         args.cmap,
                         save_image=args.save_image)
    else:
        show_lables(labels, cmap=cmap, save_image=args.save_image)
Esempio n. 7
0
        sys.stdout.write('.')
    '''
    return "%s,%f,%f\n" % (filename, ave_int, ave_mod)

    



if __name__ == '__main__':
    
    #path = diropenbox('Pick directory to process',default=r'c:\phase')
    path = r'C:\Users\jsaredy\Desktop\4 1_20130710'
    #path = r'C:\Users\jsaredy\Desktop\run3'
    path_raw, path_images, filenames = get_path(path, 'h5')
    first_file = os.path.join(path,filenames[0])
    mask,coord = get_mask(first_file, border=2)
    mask = mask[coord[0]:coord[1],coord[2]:coord[3]]

    #coord [0] = x_min, [1] = x_max, [2] = y_min, [3] = y_max
    deb = ''
    #for filename in filenames:
    #    deb = get_phase(filename, path, path_raw, path_images, mask, coord, deb)
    pool = Pool()
    A=[]
    for filename in filenames:
        A.append((filename, path, path_raw, path_images, mask, coord))
    zz = time.clock()
    imap1 = pool.imap(get_phase,A)
    #imap1 = map(get_phase,A)
    pool.close()
    pool.join()
Esempio n. 8
0
#

print("Loading video %s..." % filename)
num_frames = 0
vid = skvideo.io.vread(filename, num_frames=num_frames,
                       outputdict={"-pix_fmt": "gray"})[:, :, :, 0]
vid_color = skvideo.io.vread(filename, num_frames=num_frames)
frame_rate = skvideo.io.ffprobe(filename)['video']['@avg_frame_rate']
T, M, N = vid.shape
if args.small_size:
    M = min(M, 150)
    N = min(N, 150)
if args.small_time:
    T = min(T, 149)
width = N
is_loopable = get_mask(vid_color)

np.set_printoptions(precision=2)

if USE_EMBEDDINGS:
    embeddings_helper.setup(args, T) # TODO: something's off - shouldn't need all the frames
    frames = embeddings_helper.export_frames()

lookup_table.compute_lookup_table(vid_color)

def id_to_loc(id):
    if id < 0: raise Exception("ID must be >= 0")
    i = id // width
    j = id % width
    if i >= M or j >= N:
        raise Exception("Node ID {} too big coord would be ({}, {})".format(id, i, j))
Esempio n. 9
0
def main():
    sheet = cv2.imread("media/sheet.png")
    s = open("media/rows.txt", "r").read()[:-1]  # Remove newline
    rows = [int(n) for n in s.split(" ")]

    nb_rows = len(rows)
    nb_cols = max(rows)

    # Get sprite positions from mask
    mask = get_mask()
    boxes = get_bounding_boxes(mask)  # box is [x, y, w, h]

    # Sanity check
    assert len(boxes) == sum(rows)

    # Sort boxes
    boxes = sort_boxes(boxes, rows)

    # Get sprites from initial sheet, using sorted boxes positions
    sprites = get_sprites(boxes, sheet)

    # Save each sprite
    # for i in range(nb_rows):
    #     for j in range(rows[i]):
    #         cv2.imwrite(f"media/sprite_{i}{j}.png", sprites[i][j])

    # Get sprite max size
    sizes = []
    for r in sprites:
        for s in r:
            sizes.append(len(s))
            sizes.append(len(s[0]))
    sprite_size = max(sizes) + 10

    # Create new sprite sheet
    new_sheet = np.zeros((nb_rows * sprite_size, nb_cols * sprite_size, 3))

    # From bottom
    for i in range(nb_rows):
        for j in range(rows[i]):
            pos_i = i * sprite_size + sprite_size
            pos_j = j * sprite_size + sprite_size // 2
            new_sheet[pos_i - sprites[i][j].shape[0]:pos_i, pos_j:pos_j +
                      sprites[i][j].shape[1], ] = sprites[i][j].copy()

    cv2.imwrite("media/fixed_sheet.png", new_sheet)

    # Transparent background
    img = Image.open('media/fixed_sheet.png')
    img = img.convert("RGBA")
    datas = img.getdata()

    newData = []
    for item in datas:
        if item[0] == 0 and item[1] == 0 and item[2] == 0:
            newData.append((255, 255, 255, 0))
        else:
            newData.append(item)

    img.putdata(newData)
    img.save("media/transparent_sheet.png", "PNG")

    print("Done!")
Esempio n. 10
0
 def test_get_mask(self):
     self.assertEqual(mask.get_mask('spam', []), '****')
     self.assertEqual(mask.get_mask('spam', ['a', 'p']), '*pa*')
     self.assertEqual(mask.get_mask('spam', ['a']), '**a*')
     self.assertEqual(mask.get_mask('spam', ['m', 'a', 'p', 's']), 'spam')
     self.assertEqual(mask.get_mask('', []), '')
Esempio n. 11
0
        okeys = []
        fmtt = ''
        header = '# '
        for i in xrange(len(args.outputs)):
            kupper = args.outputs[i].upper()
            if kupper == 'COADD_OBJECTS_ID' or kupper == 'MODEST_CLASS':
                fmtt += '%d '
            else:
                fmtt += '%.6f '
            header += kupper + ' '
            okeys.append(kupper)
    else:
        okeys = ''

    if args.ids == None:
        Bmask = mask.get_mask(Mk, args.path + fitsfile, output_keys=okeys)
        masked_data = Bmask['MASK']
        maskout = 'masked_data_' + range_z
    else:
        Bmask = mask.get_mask(Mk, args.path + fitsfile, output_keys=okeys, input_ids=args.ids)
        masked_data = Bmask['MASK']
        maskout = 'masked_data_ids'

    if args.outputs != None:
        fileoutputs = 'Output_catalog.txt'
        if args.root != None: fileoutputs = args.root + '_' + fileoutputs
        temp = Bmask[args.outputs[0].upper()]
        for i in xrange(1, len(args.outputs)):
            temp = vstack((temp, Bmask[args.outputs[i].upper()]))
        with open(fileoutputs, 'wb') as fout:
            fout.write(header + '\n')