Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save the output vedios using matplotlib in this program #71

Open
RahulRNandan opened this issue Jan 5, 2023 · 0 comments
Open

Comments

@RahulRNandan
Copy link

import argparse
from operator import sub
import os
import esim_torch
import numpy as np
import glob
import cv2
import tqdm
import torch
import matplotlib.pyplot as plt

def is_valid_dir(subdirs, files):
return len(subdirs) == 1 and len(files) == 1 and "timestamps.txt" in files and "imgs" in subdirs

#def close_event():

plt.close() #timer calls this function after 3 seconds and closes the window

def process_dir(outdir, indir, args):
print(f"Processing folder {indir}... Generating events in {outdir}")
os.makedirs(outdir, exist_ok=True)

# constructor
esim = esim_torch.ESIM(args.contrast_threshold_negative,
                                       args.contrast_threshold_positive,
                                       args.refractory_period_ns)

timestamps = np.genfromtxt(os.path.join(indir, "timestamps.txt"), dtype="float64")
timestamps_ns = (timestamps * 1e9).astype("int64")
timestamps_ns = torch.from_numpy(timestamps_ns).cuda()

image_files = sorted(glob.glob(os.path.join(indir, "imgs", "*.png")))

pbar = tqdm.tqdm(total=len(image_files)-1)
num_events = 0

#fig = plt.figure()
fig, ax = plt.subplots()
#timer = fig.canvas.new_timer(interval = 300) #creating a timer object and setting an interval of 3000 milliseconds
#timer.add_callback(close_event)

counter = 0
ims = []
for image_file, timestamp_ns in zip(image_files, timestamps_ns):
    image = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
    log_image = np.log(image.astype("float32") / 255 + 1e-5)
    log_image = torch.from_numpy(log_image).cuda()

    sub_events = esim.forward(log_image, timestamp_ns)

    # for the first image, no events are generated, so this needs to be skipped
    if sub_events is None:
        continue

          
    print("Plotting")
    first_few_events = {k: v.cpu().numpy() for k,v in sub_events.items()}
    image_color = np.stack([image,image,image],-1)
    image_color[first_few_events['y'], first_few_events['x'], :] = 0
    image_color[first_few_events['y'], first_few_events['x'], first_few_events['p']] = 255
    #plt.imshow(image_color)
    
    #timer.start()
    #plt.show()
    
    im = ax.imshow(image_color, animated=True)
 		
    ims.append([im])
    
    sub_events = {k: v.cpu() for k, v in sub_events.items()}    
    num_events += len(sub_events['t'])

    # do something with the events
    np.savez(os.path.join(outdir, "%010d.npz" % counter), **sub_events)
    pbar.set_description(f"Num events generated: {num_events}")
    pbar.update(1)
    counter += 1

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,repeat_delay=100)

plt.show()

if name == "main":
parser = argparse.ArgumentParser("""Generate events from a high frequency video stream""")
parser.add_argument("--contrast_threshold_negative", "-cn", type=float, default=0.2)
parser.add_argument("--contrast_threshold_positive", "-cp", type=float, default=0.2)
parser.add_argument("--refractory_period_ns", "-rp", type=int, default=0)
parser.add_argument("--input_dir", "-i", default="", required=True)
parser.add_argument("--output_dir", "-o", default="", required=True)
args = parser.parse_args()

print(f"Generating events with cn={args.contrast_threshold_negative}, cp={args.contrast_threshold_positive} and rp={args.refractory_period_ns}")

for path, subdirs, files in os.walk(args.input_dir):
    if is_valid_dir(subdirs, files):
        output_folder = os.path.join(args.output_dir, os.path.relpath(path, args.input_dir))

        process_dir(output_folder, path, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant