Ejemplo n.º 1
0
def create_b64_image(payload):
    def buffered_flo(content):
        clean = re.sub("data:application/octet-stream;base64,", '', content)
        floBytes = io.BytesIO(base64.b64decode(clean))
        return io.BufferedReader(floBytes)

    img = fz.convert_from_file(buffered_flo(payload['content']))
    uv = fz.convert_from_file(buffered_flo(payload['content']), mode='uv')

    rgb = save_image(img, payload['name'])
    u = save_image(uv[...,0], payload['name']+'.u', cmap='binary')
    v = save_image(uv[...,1], payload['name']+'.v', cmap='binary')
    return rgb, u, v
Ejemplo n.º 2
0
def get_optical_flow(first_img, second_image):
    first_tensor = torch.FloatTensor(
        numpy.array(first_img)[:, :, ::-1].transpose(2, 0, 1).astype(
            numpy.float32) * (1.0 / 255.0))
    second_tensor = torch.FloatTensor(
        numpy.array(second_image)[:, :, ::-1].transpose(2, 0, 1).astype(
            numpy.float32) * (1.0 / 255.0))
    tensor_output = estimate(first_tensor, second_tensor)

    object_output = open(arguments_strOut, 'wb')

    numpy.array([80, 73, 69, 72], numpy.uint8).tofile(object_output)
    numpy.array(
        [tensor_output.size(2), tensor_output.size(1)],
        numpy.int32).tofile(object_output)
    numpy.array(tensor_output.detach().numpy().transpose(1, 2, 0),
                numpy.float32).tofile(object_output)

    object_output.close()

    files = glob.glob(arguments_strOut)
    img = fz.convert_from_file(files[0])

    # print('returning flo: ')

    return img
Ejemplo n.º 3
0
    def startOf(self): # A slot takes no params

        img_list = utils.listDirectory(self.img_dir)
        for ind in range(len(img_list) - 1):
            print("Running optical flow on:", img_list[ind], img_list[ind+1])
            flo_file = os.path.join(self.flow_dir,"{0}.flo".format(ind))
            pwc.setupArguments(model=self.of_model, first_img=img_list[ind],
            second_img=img_list[ind+1], save_path=flo_file)
            pwc.run()

            # Transform from flo to png
            flow = fz.convert_from_file(flo_file)
            Image.fromarray(flow).save(os.path.join(self.flow_dir,"{0}.png".format(ind)))
            self.update.emit(ind)
Ejemplo n.º 4
0
def show_flow(path: str):
    rgb = convert_from_file(path)

    filename = path.split('/')[-1]

    rgb = cv2.putText(rgb,
                      filename,
                      org=(50, 50),
                      fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                      fontScale=1,
                      color=(255, 255, 255))

    cv2.imshow("", rgb)
    return cv2.waitKey()
Ejemplo n.º 5
0
def vis_flow(flow, img1, img2, name=""):
    import cv2
    import flowiz as fz
    batch_size = img1.shape[0]
    for pair_index in range(batch_size):
        cv2.imwrite(name+str(pair_index)+'_img1_test.png', img1[pair_index, :, :, ::-1])
        cv2.imwrite(name+str(pair_index)+'_img2_test.png', img2[pair_index, :, :, ::-1])
        # save a flow file
        output_single = flow[pair_index,:,:,:]
        out_file = name+str(pair_index)+'_out.flo'
        objOutput = open(out_file, 'wb')
        np.array([ 80, 73, 69, 72 ], np.uint8).tofile(objOutput)
        np.array([ output_single.shape[2], output_single.shape[1] ], np.int32).tofile(objOutput)
        np.array(output_single.cpu().detach().numpy().transpose(1, 2, 0), np.float32).tofile(objOutput)
        objOutput.close()
        # visualize file
        out_img = fz.convert_from_file(out_file)
        cv2.imwrite(name+str(pair_index)+'_flow_out.png', out_img[:,:,::-1])    
Ejemplo n.º 6
0
 def startOf(self):
     """Start optical flow calculation
     """
     img_list = utils.listDirectory(self.img_dir)
     of_list = [(img_list[ind], img_list[ind + 1], ind)
                for ind in range(len(img_list) - 1)]
     for ind in range(len(img_list) - 1):
         logging.info("Running optical flow on: {0} {1}".format(
             img_list[ind], img_list[ind + 1]))
         flo_file = os.path.join(self.of_dir, "{0}.flo".format(ind))
         pwc.setupArguments(
             model=self.of_model,
             first_img=img_list[ind],
             second_img=img_list[ind + 1],
             save_path=flo_file,
         )
         pwc.run()
         # Transform from flo to png
         flow = fz.convert_from_file(flo_file)
         Image.fromarray(flow).save(
             os.path.join(self.of_dir, "{0}.png".format(ind)))
         self.update.emit(ind)
Ejemplo n.º 7
0
    def startBackOf(self):  # A slot takes no params
        """Start backward optical flow calculation
        """

        img_list = utils.listDirectory(self.img_dir)
        for ind in reversed(range(len(img_list) - 1)):
            logging.info("Running back optical flow on: {0} {1}".format(
                img_list[ind], img_list[ind - 1]))
            back_flo_file = os.path.join(self.back_of_dir,
                                         "{0}.flo".format(ind))
            pwc.setupArguments(
                model=self.of_model,
                first_img=img_list[ind],
                second_img=img_list[ind - 1],
                save_path=back_flo_file,
            )
            pwc.run()

            # Transform from flo to png
            flow = fz.convert_from_file(back_flo_file)
            Image.fromarray(flow).save(
                os.path.join(self.back_of_dir, "{0}.png".format(ind)))
            self.update.emit(abs(ind - len(img_list)))
def compute_flow(network, inputs):
    global f
    global initialize
    current_frame = np.array(inputs["input_image"])

    if initialize:
        frame = current_frame
        f.append(current_frame)
        initialize = False

    else:
        f.append(current_frame)
        prev_frame = f.pop(0)
        next_frame = f[0]

        tensorFirst = torch.FloatTensor(
            np.array(prev_frame)[:, :, ::-1].transpose(2, 0, 1).astype(
                np.float32) * (1.0 / 255.0))
        tensorSecond = torch.FloatTensor(
            np.array(next_frame)[:, :, ::-1].transpose(2, 0, 1).astype(
                np.float32) * (1.0 / 255.0))

        tensorOutput = estimate(network, tensorFirst, tensorSecond)

        objectOutput = tempfile.NamedTemporaryFile(suffix=".flo")
        out = str(Path(objectOutput.name))
        np.array([80, 73, 69, 72], np.uint8).tofile(objectOutput)
        np.array(
            [tensorOutput.size(2), tensorOutput.size(1)],
            np.int32).tofile(objectOutput)
        np.array(tensorOutput.numpy().transpose(1, 2, 0),
                 np.float32).tofile(objectOutput)

        frame = flowiz.convert_from_file(out)

    return {"output_image": frame}
Ejemplo n.º 9
0
def save_flow_file(fname: str):
    img = fz.convert_from_file(fname)
    plt.imsave('.'.join(fname.split('.')[:-1] + ['png']), img)
Ejemplo n.º 10
0
import flowiz as fz
import glob
import matplotlib.pyplot as plt
import cv2

files = glob.glob(
    'D:/Study/flownet2-pytorch/out2/inference/run.epoch-0-flow-field/000001.flo'
)
img = fz.convert_from_file(files[0])
# cv2.imshow("",img)
# cv2.waitKey(0)
plt.imshow(img)
plt.show()
def plot_optical_vectors(url, cut_co):
    vis = fz.convert_from_file(url)
    plt.imshow(vis[:, cut_co[0]:cut_co[0] + cut_co[1]])