Esempio n. 1
0
    def generate(N, K):
        zss, gt = _sample(N)
        gt[0] = resize_img(gt[0], gt[1])

        for key, (mu, std) in zss.items():
            # sample from particular gaussian by multiplying + adding
            if key == 'gen_samples':
                sample = torch.randn(N * N, model.n_latents).to(device)
                sample = sample.mul(std).add_(mu)
            else:
                sample = torch.randn(N, model.n_latents).to(device)
                sample = sample.mul(std).add_(mu)

            # generate
            mnist_mean = torch.sigmoid(model.image_dec(sample)).view(-1, 1, 28, 28)
            mnist_std = torch.tensor([0.1]).expand_as(mnist_mean).to(device)
            svhn_mean = torch.sigmoid(model.sent_dec(sample)).view(-1, 3, 32, 32)
            svhn_std = torch.tensor([0.1]).expand_as(svhn_mean).to(device)

            if key == 'gen_samples':
                mnist_sample = torch.randn((K,*(mnist_mean.size()))).to(device)
                mnist_gen = mnist_sample.mul(mnist_std).add_(mnist_mean).transpose(0,1)
                ms = [make_grid(t, nrow=int(sqrt(K)), padding=0) for t in mnist_gen.data.cpu()]
                save_image(torch.stack(ms), '{}/gen_samples_0_{:03d}.png'.format(runPath, epoch), nrow=N)

                svhn_sample = torch.randn((K,*(svhn_mean.size()))).to(device)
                svhn_gen = svhn_sample.mul(svhn_std).add_(svhn_mean).transpose(0,1)
                ss = [make_grid(t, nrow=int(sqrt(K)), padding=0) for t in svhn_gen.data.cpu()]
                save_image(torch.stack(ss), '{}/gen_samples_1_{:03d}.png'.format(runPath, epoch), nrow=N)
            else:
                gt_idx = key.split('_')[-1]
                mnist_recon = torch.cat([gt[int(gt_idx)].cpu(), resize_img(mnist_mean.cpu().data, svhn_mean)])
                svhn_recon = torch.cat([gt[int(gt_idx)].cpu(), svhn_mean.cpu().data])
                save_image(mnist_recon, '{}/{}x0_{:03d}.png'.format(runPath, key, epoch))
                save_image(svhn_recon, '{}/{}x1_{:03d}.png'.format(runPath, key, epoch))
Esempio n. 2
0
    def on_created(self, event):
        img_name = event.src_path.split('/')[-1]
        img_path = 'static/img/'

        resize_img(img_name, f'{img_path}/original/', f'{img_path}/small/')
        add_watermark('ЛЁХА', img_name, f'{img_path}/original/', f'{img_path}/copyright/')

        print(event)
Esempio n. 3
0
def test_model(env, model):
    model.eval()

    env.max_path_length = 200
    with torch.no_grad():
        obs = env.reset()
        for _ in range(env.max_path_length):
            #Prepare tensors
            img = env._get_viewer('human')._read_pixels_as_in_window()
            img = resize_img(img)
            img = np.expand_dims(img, axis=0)
            t_img = torch.from_numpy(img).to(dtype=torch.float,
                                             device=args.device)
            config = np.expand_dims(obs[:3], axis=0)
            t_config = torch.from_numpy(config).to(dtype=torch.float,
                                                   device=args.device)

            #Execute action
            action = model(t_img, t_config)
            a = action.squeeze().cpu().numpy()
            obs, reward, done, info = env.step(a)
            env.render()

            # Save video
            if (_ < 1):
                time.sleep(0.25)
        close(env)
Esempio n. 4
0
	def _next(self, lr_size, hr_size, img_idx=None):
		if img_idx == None:
			img_idx = np.random.randint(self.ds_size)

		gt = self.gt[img_idx]
		h, w, _ = gt.shape
		
		y, x = randPos(h, w, hr_size)
		hr = np_crop(gt, y, x, hr_size)
		hr_flip, hr_rot = get_rand_aug()
		hr = augmentation(hr, hr_flip, hr_rot)

		lr = resize_img(hr, lr_size, lr_size, Image.BICUBIC)
		upcubic = resize_img(lr, hr_size, hr_size, Image.BICUBIC)
		
		return lr, upcubic, hr
Esempio n. 5
0
def pair_embedding_to_video(sequence, model, params, video_name, fps):
    class_num = params.NUM_CLASSES
    embedding_dim = params.EMBEDDING_DIM
    image_size = params.IMG_SIZE
    OS = params.OUTPUT_SIZE
    boards = []
    for i in range(len(sequence) - 1):
        prev_image = sequence[i]['image']
        image = sequence[i + 1]['image']
        board = np.zeros((image_size * 2, image_size * 2, 3))
        x, _ = utils.prep_double_frame(sequence[i], sequence[i + 1])
        outputs = model.predict(x)
        outputs = np.squeeze(outputs)
        embedding_pred = outputs[:, :, (class_num * 2):(class_num * 2 +
                                                        embedding_dim)]
        prev_embedding_pred = outputs[:, :,
                                      (class_num * 2 +
                                       embedding_dim):((class_num * 2 +
                                                        embedding_dim * 2))]
        combined_embedding_pred = np.zeros((OS, OS * 2, embedding_dim))
        combined_embedding_pred[:, :OS, :] = prev_embedding_pred
        combined_embedding_pred[:, OS:, :] = embedding_pred
        board[:image_size, :image_size, :] = prev_image
        board[:image_size, image_size:, :] = image
        pc = principal_component_analysis(combined_embedding_pred,
                                          embedding_dim)
        pc = utils.resize_img(pc, image_size, image_size * 2)
        board[image_size:, image_size:, :] = pc[:, image_size:, :]
        board[image_size:, :image_size, :] = pc[:, :image_size, :]
        board = float_to_uint8(board)
        boards.append(board)
    imgs_to_video(boards, video_name, fps)
    return
Esempio n. 6
0
def validate(scale):
    dbs = []

    for i in range(len(eval_gt_imgs)):
        hr = eval_gt_imgs[i]
        hr = modular_crop(hr, scale)
        h, w, _ = hr.shape

        lr = resize_img(hr, h // scale, w // scale)
        upcubic = resize_img(lr, h, w)

        inputs = np.asarray([lr], np.float32), np.asarray([upcubic],
                                                          np.float32), scale
        output = net(inputs)
        y_pred = clip255(unnormalize(output))
        dbs.append(psnr(y_pred, hr))

    return np.mean(dbs)
 def submit(self):
     if self.canvas.coords_list:
         img = resize_img(list_to_sparse_matrix(self.canvas.coords_list))
         preds = self.model.predict(img)[0].tolist()
         preds_dict = {i: j for i, j in enumerate(preds)}
         max_val = max(preds_dict, key=preds_dict.get)
         self.popup(f"Prediction: {max_val} Confidence: {round(preds_dict[max_val]*100,2)}%")
     else:
         self.popup('You must enter a digit.')
Esempio n. 8
0
def main(img_name: str, foreground_art: str, background_art: str,
         operator_color: str) -> None:
    """Given the necessary information, create a wallpaper for the operator using PIL.
    """
    # Create a new RGBA image
    wip_img = Image.new("RGBA", DIMENSIONS)
    draw = ImageDraw.Draw(wip_img)

    # Verify that the operator has E2 art (rather, there's an URL for it)
    # has_e2_img = str(background_art) != "nan"
    ignore_bg_image = background_art == ""

    # Load the background
    bg_path = os.path.join("static", "resources", "bg.png")
    bg = Image.open(bg_path, mode="r").convert("RGBA")

    # Load E2 image if there is one
    if ignore_bg_image != True:
        res = requests.get(background_art)
        e2_img = Image.open(BytesIO(res.content), mode="r").convert("RGBA")
        # Change the image's opacity
        e2_img = utils.change_alpha(e2_img, E2_ALPHA)
        # Resize the image
        e2_img = utils.resize_img(e2_img, 1.05)
        # Center the E2 image horizontally
        e2_coords = utils.calculate_e2_coordinates(e2_img, DIMENSIONS)

    # Load E0 image
    res = requests.get(foreground_art)
    e0_img = Image.open(BytesIO(res.content), mode="r").resize(
        (1024, 1024)).convert("RGBA")
    # Calculate the drawing coordinates for the E0 image
    e0_coords = utils.calculate_e0_coordinates(e0_img, DIMENSIONS)

    # Create the E0 image shadow
    shadow = Image.new("RGBA", e0_img.size, color=operator_color)
    # Calculate the drawing coordinates for the E0 image shadow
    shadow_coords = [coord + SHADOW_OFFSET for coord in e0_coords]

    # Add the background
    wip_img.paste(bg)
    # Add the colored footer polygon
    utils.create_and_paste_footer(wip_img, operator_color)
    # Add the E2 image if there is one
    if ignore_bg_image != True:
        wip_img.paste(e2_img, e2_coords, mask=e2_img)
    # Add the E0 image shadow
    wip_img.paste(shadow, shadow_coords, mask=e0_img)
    # Add the E0 image
    wip_img.paste(e0_img, e0_coords, mask=e0_img)
    # Save the resulting wallpaper
    wip_img.save(img_name)
Esempio n. 9
0
def standardize_img(img_file_path, img_w, img_h):
    """
        Input: 
            img_file_path : this is path to image file
        Output:
            img : an three dimensions numpy array of the image above. It is standardized by
                resizing the image to particular shape (img_w, img_h) 
                and dividing by 255 to make value range (0, 1)
    """
    img = cv2.imread(img_file_path)
    img = resize_img(img, (img_w, img_h))
    img = img / 255
    return np.array(img)
Esempio n. 10
0
def process_cameras():
    cameras = dbmngr.get_all_cameras()

    # Download a frame from each of the cameras added
    for camera in cameras:
        camera_id = camera['camera_id']
        camera_url = camera['url']
        camera_coord = camera['coordinates']

        original_img, img = resize_img(in_url=camera_url)
        img_to_predict = np.asarray([img])

        fight_prediction = fights_detector.predict(img_to_predict)
        print('Fight prediction: ', fight_prediction,
              np.argmax(fight_prediction))
        fire_prediction = fire_detector.predict(img_to_predict)
        print('Fire prediction: ', fire_prediction, np.argmax(fire_prediction))
        crash_prediction = crash_detector.predict(img_to_predict)
        print('Crash prediction: ', crash_prediction,
              np.argmax(crash_prediction))
        #gun_detection = detect_gun(original_img)
        #print('Gun detection: ', gun_detection)

        fight_prediction = np.argmax(fight_prediction)
        fire_prediction = np.argmax(fire_prediction)
        crash_prediction = np.argmax(crash_prediction)

        if fight_prediction == 0:
            print('Camera #%s - Fight detected!' % camera_id)
            incident_id = dbmngr.add_incident(camera_id, 'warning',
                                              incident_descriptions['fight'],
                                              camera_coord)
            save_footage(incident_id, original_img)
        if fire_prediction == 0:
            print('Camera #%s - Fire detected!' % camera_id)
            incident_id = dbmngr.add_incident(camera_id, 'danger',
                                              incident_descriptions['fire'],
                                              camera_coord)
            save_footage(incident_id, original_img)
        if crash_prediction == 0:
            print('Camera #%s - Car crash detected!' % camera_id)
            incident_id = dbmngr.add_incident(
                camera_id, 'danger', incident_descriptions['car_crash'],
                camera_coord)
            save_footage(incident_id, original_img)
        #if gun_detection:
        #print('Camera #%s - Gun detected!' % camera_id)
        #incident_id = dbmngr.add_incident(camera_id, 'danger', incident_descriptions['weapon'], camera_coord)
        #save_footage(incident_id, original_img)

        dbmngr.count_incidents(camera_id)
    def process_batch(
        self, batch_sample
    ):  # batch_sample: a list containing the paths to the center/left/right images and steering angle.
        '''
        Samples in each batch are processed.
        The center, left, right, and horizontally flipped images are used and processed.
        '''

        # New augmented images and steering angles
        images = []
        steering_angles = []

        # original measured steering_angle
        steering_angle = np.float32(batch_sample[3])  # 4th column in csv

        for image_directory_index in range(3):
            image_file_name = batch_sample[image_directory_index].split(
                '/'
            )[-1]  # split using '/' and [-1] indicates the last element in the list

            # addition from center/left/right images
            image = cv2.imread(
                self.image_directory +
                image_file_name)  # opencv reads images in BGR color
            cropped_image = utils.crop_img(image)
            blurred_image = utils.blur_img(cropped_image)  # gaussian blurring
            resized_image = utils.resize_img(blurred_image)
            yuv_image = utils.bgr2yuv(
                resized_image)  # bgr to yuv (as nVidia suggested)
            images.append(yuv_image)

            # steer angle correction on left/right images to balance the dataset with non-zero steering angles
            if image_directory_index == 1:  # left camera
                steering_angles.append(steering_angle +
                                       self.steering_recovery_factor)

            elif image_directory_index == 2:  # right camera
                steering_angles.append(steering_angle -
                                       self.steering_recovery_factor)

            else:  # center camera
                steering_angles.append(steering_angle)

            # if the magnitude of steering angle is bigger than 0.2, flipped image of center is added.
            if image_directory_index == 0 and abs(steering_angle) > 0.2:
                # if image_directory_index == 0:
                images.append(utils.flip_img(yuv_image))
                # images.append(utils.flip_img(cropped_resized))
                steering_angles.append(steering_angle * (-1.0))

        return images, steering_angles
Esempio n. 12
0
def read_image(img_path):
    """
    读取图片
    :param img_path:
    :return:
    """
    origin = Image.open(img_path)
    img = resize_img(origin, target_size)
    if img.mode != 'RGB':
        img = img.convert('RGB')
    img = np.array(img).astype('float32').transpose((2, 0, 1))  # HWC to CHW
    img -= 127.5
    img *= 0.007843
    img = img[np.newaxis, :]
    return origin, img
Esempio n. 13
0
def get_flow_from_shapes(shapes_info, image_size):
    img = Image.new(mode='RGB', size=(image_size, image_size), color=(0, 0, 0))
    draw_img = ImageDraw.Draw(img)
    num = len(shapes_info)
    for i in range(num):
        shape_info = shapes_info[i]
        draw_img = draw_flow(shape_info, draw_img)
    flow = np.asarray(img, dtype = np.float)
    flow = np.copy(flow[:, :, :2])
    flow[flow != 0] -= 100
    max_v = int(0.1 * image_size)
    flow = flow / max_v
    output_size = image_size // 4
    flow = utils.resize_img(flow, output_size, output_size)
    return flow
Esempio n. 14
0
def detect_face(frame: Frame) -> dict:
    ts = time.time()
    cfg = frame.cfg.face_detect

    img, scale = resize_img(
        frame.img_gray if cfg.convert_gray else frame.image,
        cfg.resize_img_pixel)

    dets = detector(img, cfg.upsample)

    count = len(dets)
    max_area = 0
    zones = []
    for d in dets:
        l, t, r, b = d.left(), d.top(), d.right(), d.bottom()
        left = int(l / scale)
        top = int(t / scale)
        width = int((r - l) / scale)
        height = int((b - t) / scale)
        area = width * height
        if max_area < area:
            max_area = area
        zones.append((left, top, width, height))
        if cfg.draw_rect:
            print('Rect:', (l, t, r, b), scale, ':',
                  (left, top, width, height), area, max_area)
            cv2.rectangle(frame.image, (left, top),
                          (left + width, top + height), cfg.draw_rect_color,
                          cfg.draw_rect_thickness)

    ret = {
        'face_detected': count,
        'face_area': max_area,
        'face_zones': zones,
        'face_ts': time.time() - ts,
        'face_img': frame.image
    }

    img_path = cfg.save_image_path
    if count > 0 and img_path:
        file_name = (f'face-{frame.index}-{count}-{max_area}.jpg')
        cv2.imwrite(img_path + '/' + file_name, frame.image)
        ret['face_file'] = file_name

    return ret
Esempio n. 15
0
File: app.py Progetto: hfutxqd/kinss
 def get(self):
     src = request.args.get("src")
     url = request.args.get("url")
     headers = {
         'User-Agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
         'Referer': urlparse(url).scheme + '://' + urlparse(url).netloc
     }
     r = requests.get(src, headers=headers)
     if r.status_code == requests.codes.OK:
         if src.split('.')[-1] == 'gif':
             res = make_response(r.content)
             res.headers['Content-Type'] = r.headers['Content-Type']
             return res
         raw = resize_img(io.BytesIO(r.content))
         return send_file(raw, mimetype='image/jpeg')
     else:
         r.raise_for_status()
    def paint(self, event):

        x, y = event.x, event.y

        if self.canvas.old_coords:
            x1, y1 = self.canvas.old_coords
            d = math.sqrt(((x - x1)**2) + ((y - y1)**2))
            if d <= 20:
                self.canvas.create_line(x, y, x1, y1, capstyle=tk.ROUND, width=10)
            else:
                self.canvas.old_coords = None

        self.canvas.old_coords = x, y

        if x < self.canvas.winfo_width() and y < self.canvas.winfo_height():

            self.canvas.coords_list.append((x, y))
            img = resize_img(list_to_sparse_matrix(self.canvas.coords_list))
            preds = self.model.predict(img)[0].tolist()
            preds_dict = {i: j for i, j in enumerate(preds)}
            self.plot.update_plot(preds_dict.values())
Esempio n. 17
0
    def __getitem__(self, idx):
        self.current_item_path = self.inputs[idx]
        input_img = correct_dims(nib.load(self.inputs[idx]).get_data())
        label_img = gen_mask(self.labels[idx])

        # Resize to input image and label to size (self.size x self.size x self.size)
        if self.sampling_mode == "resize":
            ex, label = resize_img(input_img, label_img, self.size)

        # Constant center-crop sample of size (self.size x self.size x self.size)
        elif self.sampling_mode == 'center':
            ex, label = center_crop(input_img, label_img, self.size)

        # Find centers of lesion masks and crop image to include them
        # to measure consistent validation performance with small crops
        elif self.sampling_mode == "center_val":
            ex, label = find_and_crop_lesions(input_img, label_img, self.size,
                                              self.deterministic)

        # Randomly crop sample of size (self.size x self.size x self.size)
        elif self.sampling_mode == "random":
            ex, label = random_crop(input_img, label_img, self.size)

        else:
            print("Invalid sampling mode.")
            exit()

        ex = np.divide(ex, 255.0)
        label = np.array([(label > 0).astype(int)]).squeeze()
        # (experimental) APPLY RANDOM FLIPPING ALONG EACH AXIS
        if not self.deterministic:
            for i in range(3):
                if random() > 0.5:
                    ex = np.flip(ex, i)
                    label = np.flip(label, i)
        inputs = torch.from_numpy(ex.copy()).type(
            torch.FloatTensor).unsqueeze(0)
        labels = torch.from_numpy(label.copy()).type(
            torch.FloatTensor).unsqueeze(0)
        return inputs, labels
Esempio n. 18
0
def preprocess(img, bbox_labels, input_size, mode):
    """
    数据预处理,如果是训练模型,还会做一些图像增强和标注的轻微扰动
    :param img:
    :param bbox_labels:
    :param input_size:
    :param mode:
    :return:
    """
    sample_labels = np.array(bbox_labels)
    if mode == 'train':
        img = distort_image(img)
        im_width, im_height = img.size
        sample_labels = disturbance_box(sample_labels, im_width, im_height)
        img, sample_labels = ramdom_rotate(img, sample_labels)

    img = resize_img(img, input_size)
    img = np.array(img).astype('float32')
    img -= train_parameters['mean_rgb']
    img = img.transpose((2, 0, 1))  # HWC to CHW
    img *= 0.007843
    return img, sample_labels
Esempio n. 19
0
def transdir(imgdir, size):
    filenum = 0
    try:
        list = os.listdir(imgdir)
    except:
        print "Donot exist image dir: " + imgdir
        return
    for line in list:
        filepath = os.path.join(imgdir, line)
        if os.path.isdir(filepath):
            continue
        elif os.path:
            if filepath.find("jpg") or filepath.find("jpeg") or filepath.find(
                    "png"):
                output_file_name = str(filenum) + '.png'
                if utils.resize_img(filepath,
                                    imgdir + '/resize/' + output_file_name,
                                    size, False, size) == RESIZE_SUCCESS:
                    filenum += 1
                    print "Transform file " + filepath + " to " + output_file_name + " size:" + str(
                        size)
    print "Transform file " + imgdir + " finished. filenum: " + str(filenum)
Esempio n. 20
0
    def _sample(N):
        model.eval()
        for batch_idx, dataT in enumerate(test_loader):
            mnist, svhn = unpack_data(dataT, device=device)
            break
        gt = [mnist[:N], svhn[:N], torch.cat([resize_img(mnist[:N], svhn[:N]), svhn[:N]])]
        zss = OrderedDict()

        # mode 1: generate
        zss['gen_samples'] = [torch.zeros((N * N, model.n_latents)).to(device),
                              torch.ones((N * N, model.n_latents)).to(device)]

        # mode 2: mnist --> mnist, mnist --> svhn
        mu, logvar = model.infer(image=gt[0])
        zss['recon_0'] = [mu, logvar.mul(0.5).exp_()]

        # mode 3: svhn --> mnist, svhn --> svhn
        mu, logvar = model.infer(sent=gt[1])
        zss['recon_1'] = [mu, logvar.mul(0.5).exp_()]

        # mode 4: mnist, svhn --> mnist, mnist, svhn --> svhn
        mu, logvar = model.infer(image=gt[0], sent=gt[1])
        zss['recon_2'] = [mu, logvar.mul(0.5).exp_()]
        return zss, gt
def telemetry(sid, data):
    if data:
        # The current steering angle of the car
        steering_angle = data["steering_angle"]
        # The current throttle of the car
        throttle = data["throttle"]
        # The current speed of the car
        speed = data["speed"]
        # The current image from the center camera of the car
        imgString = data["image"]
        image = Image.open(BytesIO(base64.b64decode(imgString)))
        image_array = np.asarray(image)

        ## modified lines ##
        image_array = utils.crop_img(image_array)
        image_array = utils.blur_img(image_array)
        image_array = utils.resize_img(image_array)
        image_array = utils.rgb2yuv(image_array)

        # This model currently assumes that the features of the model are just the images. Feel free to change this.
        steering_angle = float(
            model.predict(image_array[None, :, :, :], batch_size=1))
        # Speed control using PI Controller
        throttle = controller.update(float(speed))

        print(steering_angle, throttle)
        send_control(steering_angle, throttle)

        # save frame
        if args.image_folder != '':
            timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
            image_filename = os.path.join(args.image_folder, timestamp)
            image.save('{}.jpg'.format(image_filename))
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
Esempio n. 22
0
def Generator(image_paths, mask_paths, shape=(256,256), batch_size = 32, \
    preprocessing_mask_paths=None):
    # ================ Debugging routine follows =============
    if DEBUG_LEVEL >= 2:
        assert image_paths, \
            "Empty list of image paths provided in Generator"
        assert mask_paths, \
            "Empty list od mask_paths provided in Generator"
    # ========================================================
    batch_size = batch_size if batch_size < len(image_paths) else len(
        image_paths)
    idx = 0
    while True:
        images_to_load = image_paths[idx * batch_size:idx * batch_size +
                                     batch_size]
        corresponding_masks = mask_paths[idx * batch_size:idx * batch_size +
                                         batch_size]

        X = list(map(lambda path: cv2.imread(path), images_to_load)
                 )  #TODO: this should be updated to a no cv2 solution
        y = list(map(lambda path: cv2.imread(path), corresponding_masks))

        # ================ Debugging routine follows =============
        if DEBUG_LEVEL >= 2:
            X_shapes = list(map(lambda x: x.shape, X))
            y_shapes = list(map(lambda x: x.shape, y))
            assert X_shapes == y_shapes
        # ========================================================
        if preprocessing_mask_paths:
            preprocessing_masks_to_load = preprocessing_mask_paths[
                idx * batch_size:idx * batch_size + batch_size]
            preprocessing_masks = list(
                map(lambda path: cv2.imread(path),
                    preprocessing_masks_to_load))

            # ================ Debugging routine follows =============
            if DEBUG_LEVEL >= 2:
                preprocess_mask_shapes = list(map(lambda x: x.shape, \
                    preprocessing_masks))
                assert preprocess_mask_shapes == X_shapes, \
                    "Preprocessing masks and images do not feature the same shape"
            # ========================================================

            l = list(zip(X, preprocessing_masks))

            # do segment the X,y with the preprocessing mask
            X = list(map(lambda el: get_segment_crop(el[0], mask=el[1]), l))
            l = zip(y, preprocessing_masks)
            y = list(map(lambda el: get_segment_crop(el[0], mask=el[1]), l))
            # FIXME: set the shape here ...
            X = list(map(lambda img: resize_img(img), X))
            y = list(map(lambda mask: resize_img(mask), y))

        for i, img in enumerate(y):
            if img is None:
                print(' [EROOR INFO]  None image Loaded:  ',
                      corresponding_masks[i])
                exit(1)

        #X = list(map(lambda img: cv2.resize(img, shape) if img.shape!=shape else img, X))
        #y = list(map(lambda img: cv2.resize(img, shape) if img.shape!=shape else img, y))

        #X, y = augment(X, y) #FIXME: this is an issue ...

        X = list(map(lambda img: cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), X))
        y = list(map(lambda img: cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), y))

        X = list(map(lambda img: exposure.equalize_hist(img), X))
        y = list(map(lambda img: img // 255, y))

        # expand dims to fit the network architecture
        X = list(map(lambda x: np.expand_dims(x, 2), X))
        y = list(map(lambda x: np.expand_dims(x, 2), y))
        if len(image_paths) < batch_size:
            batch_size = len(image_paths)

        # ================ Debugging routine follows =============
        if DEBUG_LEVEL > 1:
            assert image_paths, "Empty image_paths list"
        if DEBUG_LEVEL > 0:
            assert batch_size, "Batch_size is 0 really?"
        # ========================================================

        idx = (idx + 1) % (len(image_paths) // batch_size)
        yield np.array(X), np.array(y)
Esempio n. 23
0
def stabilize(args):
    # args.resize = True
    print("reading images")
    if args.img_dir[-4:] == '.mp4' or args.img_dir[-4:] == '.avi':
        from utils import vid2img_lists
        img_lists = vid2img_lists(args.img_dir)
    else:
        from utils import file2lists
        img_lists = file2lists(os.path.join(args.img_dir, 'img_lists.txt'))
        img_lists = [item_i for item_i in img_lists if item_i[-3:] == 'png']
        img_lists = sorted(img_lists)
        img_lists = [
            os.path.join(args.img_dir, item_i) for item_i in img_lists
        ]
        img_lists = [cv2.imread(fn)[:, :, ::-1] for fn in img_lists]
    raw_shape = img_lists[0].shape
    if args.resize:
        img_lists = [pad_img(img, pwc_opt.pyr_lvls) for img in img_lists]
    else:
        from utils import resize_img
        img_lists = [resize_img(img, pwc_opt.pyr_lvls) for img in img_lists]

    first_img_p, mid_img_p, end_img_p = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]), \
                                  tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]), \
                                  tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3])

    out_img_ts, debug_out_ts = build_model_test(first_img_p,
                                                mid_img_p,
                                                end_img_p,
                                                training=False,
                                                trainable=True)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                            log_device_placement=False))
    # sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver()
    saver.restore(
        sess, checkpoint_path + args.lr_str + '/stab.ckpt-' + str(args.modeli))

    for iter in range(args.stab_iter):
        print("--------iter {}---------".format(iter))
        next_img_lists = []
        for k in range(args.skip):
            next_img_lists.append(img_lists[k])
        for k in range(args.skip, len(img_lists) - args.skip):
            cur_img = img_lists[k]
            first_img = img_lists[k - args.skip]
            end_img = img_lists[k + args.skip]
            cur_img_, first_img_, end_img_ = list(
                map(lambda x: np.expand_dims(x, 0),
                    [cur_img, first_img, end_img]))
            out_img, debug_out = sess.run([out_img_ts, debug_out_ts],
                                          feed_dict={
                                              first_img_p: first_img_,
                                              mid_img_p: cur_img_,
                                              end_img_p: end_img_
                                          })
            out_img = out_img.squeeze()
            out_img = np.array(out_img * 255.0).astype(np.uint8)
            next_img_lists.append(out_img)

            if args.debug:
                debug_img_lists_k = [
                    'first_img', 'cur_img', 'end_img', 'out_img'
                ]
                debug_img_lists_v = [
                    first_img[:, :, ::-1], cur_img[:, :, ::-1],
                    end_img[:, :, ::-1], out_img[:, :, ::-1]
                ]
                debug_img_lists = dict(
                    zip(debug_img_lists_k, debug_img_lists_v))
                # write_imgs(debug_img_lists, k, args.debug_out_dir)

                [
                    warped_first, warped_end, img_int, flow_pred0, flow_pred1,
                    flow_pred2
                ] = debug_out
                debug_flow_lists_k = [
                    'first2end_flow', 'end2first_flow', 'mid2int_flow'
                ]
                debug_flow_lists_v = [
                    flow_pred0[0], flow_pred2[0], flow_pred1[0]
                ]
                debug_flow_lists = dict(
                    zip(debug_flow_lists_k, debug_flow_lists_v))
                write_flows(debug_flow_lists, k, args.debug_out_dir)

        for k in range(len(img_lists) - args.skip, len(img_lists)):
            next_img_lists.append(img_lists[k])
        img_lists = next_img_lists

    if args.resize:
        img_lists = [unpad_img(img, raw_shape) for img in img_lists]
    else:
        from utils import back_resize_img
        img_lists = [back_resize_img(img, raw_shape) for img in img_lists]

    # import pdb;pdb.set_trace();
    if args.img_dir[-4:] == '.mp4':
        from utils import save2vid
        save2vid(img_lists, args.out_dir, args.img_dir)
    else:
        save_img_lists(img_lists, args.out_dir)
Esempio n. 24
0
#Open Software Project - Final project / 홍유진 장다솜

import cv2
import numpy as np
import os
import argparse
from utils import find_vertices, resize_img

for file in os.listdir("./input"):
    filename, extention = file.split(".")

    # Read the image and resize it
    image = cv2.imread("./input/" + file)
    image = resize_img(image)

    # Show the resized original image.
    # cv2.imshow('INPUT',image)

    # Convert to grayscale and find edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    edge = cv2.Canny(blur, 50, 150)
    #cv2.imshow('Canny',edge)

    #Find and draw contours
    contours, _ = cv2.findContours(edge.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(image, contours, -1, [0, 255, 0], 2)
    #cv2.imshow('Contours',image)

    #Find the part of the document(maximum area) in the image devided by contours
Esempio n. 25
0
        segmentation_model.load_weights(weight_file[0])

        # make directories
        out_dir = os.path.join(out_home_dir, task)
        if not os.path.isdir(out_dir):
            os.makedirs(out_dir)

        # iterate all images
        for img_dir in img_dirs:
            filenames = utils.all_files_under(img_dir)
            for filename in filenames:
                assert "IDRiD_" in os.path.basename(filename)
                # load an img (tensor shape of [1,h,w,3])
                img = utils.imagefiles2arrs([filename])
                _, h, w, _ = img.shape
                assert h == 2848 and w == 4288
                resized_img = utils.resize_img(img)

                # run inference
                segmented = segmentation_model.predict(
                    utils.normalize(resized_img), batch_size=1)

                # cut by threshold
                segmented = segmented[0, ..., 0]
                segmented_sizeup = utils.sizeup(segmented)

                # save the result
                Image.fromarray(
                    (segmented_sizeup * 255).astype(np.uint8)).save(
                        os.path.join(out_dir, os.path.basename(filename)))
Esempio n. 26
0
 def resize(img):
     return utils.resize_img(img, target_size=(input_height, input_width))
Esempio n. 27
0
import cv2
import math
import numpy as np
import matplotlib.pyplot as plt
import subprocess, os

import utils
import subprocess, os

orb = cv2.ORB_create()
# orb is an alternative to SIFT

test_img = utils.read_img('test1.jpg')

#resizing must be dynamic
test_img = utils.resize_img(test_img, 0.2)
# display('original', original)

# keypoints and descriptors
# (kp1, des1) = orb.detectAndCompute(test_img, None)
(kp1, des1) = orb.detectAndCompute(test_img, None)

training_set = os.listdir('training-data/')
for ind in range(0, len(training_set)):
    training_set[ind] = 'training-data/' + training_set[ind]

max_val = 8
max_pt = -1
max_kp = 0
good = []
Esempio n. 28
0
SAA - self adaptive algorithm
FAS - fast adaptive similarity filter
Images from https://pixabay.com/images/search/
"""
import time

import utils
import constants
import NEAVF

start = time.time()

# Read, resize and plot images
size = (300, 400)
img_list = utils.read_images(constants.DATASET_PATH)
img_list = utils.resize_img(img_list, size)
# utils.show_image_list(img_list, "Initial images!")

img_list_gauss = []
img_list_impulsive = []
index = 0
for img in img_list:
    img_list_gauss.append(utils.gaussian_noise(img, 0.1))
    img_list_impulsive.append(utils.salt_and_pepper(img, 0.1))
    index += 1
# utils.show_image_list(img_list_gauss, "Images with gaussian noise! 0.1")
# utils.show_image_list(img_list_impulsive, "Image with salt and pepper noise! 0.1")

# Two images with 2 values of noise
img_list_gauss_005 = []
img_list_gauss_015 = []
Esempio n. 29
0
IMG_HEIGHT = 256
IMG_WIDTH = 256
list_shapes = []

print('Reading train and val..')
for i in range(total_train):
	print(i)
	img_path = train_fold + '/' + train_ids[i] + '/images/' + train_ids[i] + '.png'
	img = cv2.imread(img_path,1)  #256 x 256 x 3?
	# pdb.set_trace()
	mask = create_mask(train_fold + '/' + train_ids[i], shape = (img.shape[0], img.shape[1],1))
	print(mask.shape)
	old_shape = img.shape
	img = np.dstack([normalize_img(img[:,:,q]) for q in range(3) ])
	if (img.shape != (IMG_HEIGHT, IMG_WIDTH, 3)): # resize it
		img = resize_img(img, shape = (IMG_HEIGHT, IMG_WIDTH, 3))
		mask = resize_img(mask, shape = (IMG_HEIGHT, IMG_WIDTH,1))
		# if img.shape in list_shapes:
		# 	pass
		# else:
		# 	list_shapes.append(img.shape)
	# pdb.set_trace()
	# cv2.imshow('image',img)
	# cv2.waitKey(0)
	# cv2.imshow('mask',mask)
	# cv2.waitKey(0)
	# pdb.set_trace()

	if i<n_train:
		train_imgs[i,:,:,:] = img
		train_masks[i,:,:,:] = mask
Esempio n. 30
0
def get_image_from_shapes(shapes, image_size):
    output_size = image_size // 4

    img           = Image.new(mode='RGB', size=(image_size, image_size), color=(255, 255, 255))
    mask          = Image.new(mode='I',   size=(image_size, image_size), color=0)
    full_mask     = Image.new(mode='I',   size=(image_size, image_size), color=0)
    class_mask    = Image.new(mode='I',   size=(image_size, image_size), color=0)

    draw_img        = ImageDraw.Draw(img)
    draw_mask       = ImageDraw.Draw(mask)
    draw_full_mask  = ImageDraw.Draw(full_mask)
    draw_class_mask = ImageDraw.Draw(class_mask)

    background_all_ones = np.array([
        (0, 0), (image_size, 0), (image_size, image_size), (0, image_size), (0, 0)])
    corners = utils.totuple(background_all_ones)
    draw_full_mask.polygon([tuple(p) for p in corners], fill=1, outline=1)
    full_masks_list = []
    full_masks_list.append(full_mask)

    draws = {
        'draw_img': draw_img,
        'draw_mask': draw_mask,
        'draw_class_mask': draw_class_mask
    }
    
    num_shapes = len(shapes)
    instance_to_class = np.zeros(shape=(num_shapes+1))
    velocities = np.zeros((num_shapes, 2))
    for i in range(num_shapes):
        shape_info = shapes[i]
        if 'velocity' in shape_info:
            velocities[i, :] = shape_info['velocity']
        full_mask = Image.new(mode='I', size=(image_size, image_size), color=0)
        draw_full_mask = ImageDraw.Draw(full_mask)
        draws['draw_full_mask'] = draw_full_mask
        draws = draw_shapes(shape_info, draws, i+1)
        full_masks_list.append(full_mask)
        instance_to_class[i+1] = shape_info['shape_choice_int']

    image = np.asarray(img) / 255.0
    mask = np.asarray(mask)
    class_mask = np.asarray(class_mask)
    stacked_full_masks = np.zeros((image_size, image_size, num_shapes+1))
    full_masks = []
    # NOTE: The values of [x_center] [y_center] [width] [height] 
    # are normalized by the width/height of the image, so they are 
    # float numbers ranging from 0 to 1.
    bboxes = []
    for i in range(num_shapes+1):
        full_mask = np.asarray(full_masks_list[i], dtype=np.uint8)
        stacked_full_masks[:, :, i] = full_mask
        if i > 0:
            bbox = utils.mask2bbox(full_mask, image_size)
            if bbox:
                bboxes.append(bbox)
                full_mask = utils.resize_img(full_mask, output_size, output_size)
                full_masks.append(full_mask)
    mask_count = np.sum(stacked_full_masks, axis=2)
    occ_mask = np.zeros((image_size, image_size))
    update_idx = (mask_count >= 3)
    obj_idx_array = stacked_full_masks[update_idx]
    obj_idx_pool = np.linspace(0, num_shapes, num_shapes+1)
    obj_idx = np.multiply(obj_idx_pool, (obj_idx_array).astype(int))
    num_update_pixels = obj_idx.shape[0]
    obj_unique_idx = np.zeros((num_update_pixels, 1))
    for i in range(num_update_pixels):
        obj_unique_idx[i] = np.unique(obj_idx[i, :])[-2]
    occ_mask[update_idx] = np.squeeze(obj_unique_idx)
    occ_class_mask = np.zeros_like(class_mask)
    for i in range(num_shapes+1):
        occ_class_mask[occ_mask == i] = instance_to_class[i]

    classes = [shape_info['shape_choice_int'] for shape_info in shapes]
    
    # cast to data-efficient type
    image           = image.astype(np.uint8)
    mask            = mask.astype(np.uint8)
    occ_mask        = occ_mask.astype(np.uint8)
    class_mask      = class_mask.astype(np.uint8)
    occ_class_mask  = occ_class_mask.astype(np.uint8)

    # resize before saving
    mask            = utils.resize_img(mask, output_size, output_size)
    occ_mask        = utils.resize_img(occ_mask, output_size, output_size)
    class_mask      = utils.resize_img(class_mask, output_size, output_size)
    occ_class_mask  = utils.resize_img(occ_class_mask, output_size, output_size)

    image_info = {
        'image':                image,
        'instance_mask':        mask,
        'occ_instance_mask':    occ_mask,
        'class_mask':           class_mask,
        'occ_class_mask':       occ_class_mask,
        'full_masks':           full_masks,
        'velocities':           velocities,
        'classes':              classes,
        'bboxes':               bboxes
    }

    return image_info