def main(task): json_file_path = 'data_preprocessing/MUSIC_dataset/' data_folder_path = 'data/pojha/research-data/Sound-of-Pixels/' for file in os.listdir(json_file_path): if file.endswith('.json'): file_path = os.path.join(json_file_path, file) #print(file_path) json_data = json.load(open(file_path)) for currVideos in json_data['videos']: #print(currVideos) if task == "youtube_download": downloader = youtube_video_downloader() video_directory = data_folder_path + 'frames/' + currVideos + '/' audio_directory = data_folder_path + 'audio/' + currVideos + '/' for videoID in json_data['videos'][currVideos]: print(videoID) downloader.saveYoutubeVideos(video_directory, audio_directory, videoID) if task == "convert_video_to_frame": video_frames = video_to_frames() video_directory = data_folder_path + 'frames/' + currVideos + '/' for videoID in json_data['videos'][currVideos]: full_video_file_path = os.path.join( video_directory, videoID + ".mp4") #full_video_file_path = '"' + full_video_file_path + '"' if os.path.exists(full_video_file_path): #print(full_video_file_path) video_frames.save_to_frames( full_video_file_path, 8) else: print("video file does not exist - " + full_video_file_path) video_file_with_path = full_video_file_path path_to_video, video_file_name = os.path.split( video_file_with_path) video_file_name = Path(video_file_with_path).stem frame_files_path = os.path.join( path_to_video, video_file_name) if os.path.exists(frame_files_path): jpg_files = glob.glob(frame_files_path + "/" + '*.jpg') if not len(jpg_files) > 0: os.rmdir(frame_files_path) if task == "convert_video_mp4_to_audio_mp3": audio_mp3_converter = video_mp4_to_audio_mp3_converter() video_directory = data_folder_path + 'frames/' + currVideos + '/' audio_directory = data_folder_path + 'audio/' + currVideos + '/' for videoID in json_data['videos'][currVideos]: full_video_file_path = os.path.join( video_directory, videoID + ".mp4") if os.path.exists(full_video_file_path): audio_mp3_converter.save_to_audio_mp3( audio_directory, full_video_file_path)
from video_to_frames import video_to_frames video_to_frames('VID_20180913_155407.mp4')
import cv2 from find_best import find_best import video_to_frames import find_rectangle video = video_to_frames.path_to_video("vid.mp4") frames = video_to_frames.video_to_frames(video) key_images = [] for frame in frames: success, key_image, _ = find_rectangle.process(frame) if success: key_images.append(key_image) results = find_best(5, key_images) for image in results: cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows()
def per_video(): # Get a list of videos to process if os.path.exists(os.path.normpath(FLAGS.videos_dir)): videos = os.listdir(FLAGS.videos_dir) logging.info("Will process {} videos from {}".format( len(videos), os.path.normpath(FLAGS.videos_dir))) else: logging.info("videos_dir does not exist: {}".format( os.path.normpath(FLAGS.videos_dir))) return for i, video in enumerate(videos): print("Video ({}) {} of {}".format(video, i + 1, len(videos))) video_to_frames(os.path.join(os.path.normpath(FLAGS.videos_dir), video), FLAGS.frames_dir, FLAGS.stats_dir, overwrite=False, every=FLAGS.detect_every) with open( os.path.join(os.path.normpath(FLAGS.stats_dir), video[:-4] + '.txt'), 'r') as f: video_id, width, height, length = f.read().rstrip().split(',') frame_paths = list() for frame in range(0, int(length), FLAGS.detect_every): frame_path = os.path.join(os.path.normpath(FLAGS.frames_dir), video, "{:010d}.jpg".format(frame)) if not os.path.exists(frame_path): logging.warning( "{} Frame image file doesn't exist. Probably because you extracted frames at " "a higher 'every' value than the 'detect_every' value specified" .format(frame_path)) logging.warning( "Will re-extract frames, you have 10 seconds to cancel") time.sleep(10) video_to_frames(os.path.join( os.path.normpath(FLAGS.videos_dir), video), os.path.normpath(FLAGS.frames_dir), os.path.normpath(FLAGS.stats_dir), overwrite=True, every=FLAGS.detect_every) else: frame_paths.append(frame_path) if 'yolo' in FLAGS.model: model_path = 'models/0001/yolo3_mobilenet1_0_cycle_best.params' else: model_path = 'models/0002/faster_rcnn_best.params' FLAGS.batch_size = 1 FLAGS.gpus = '0' ctx = [mx.gpu(int(i)) for i in FLAGS.gpus.split(',') if i.strip()] ctx = ctx if ctx else [mx.cpu()] net, transform = prep_net(os.path.normpath(model_path), FLAGS.batch_size, ctx) dataset, loader = prep_data(frame_paths, transform, FLAGS.batch_size, FLAGS.num_workers) detect(net, dataset, loader, ctx, FLAGS.detections_dir, FLAGS.save_detection_threshold) track([video[:-4] + '.txt'], FLAGS.detections_dir, FLAGS.stats_dir, FLAGS.tracks_dir, FLAGS.track_detection_threshold, FLAGS.max_age, FLAGS.min_hits) visualise(os.path.join(os.path.normpath(FLAGS.videos_dir), video), FLAGS.frames_dir, FLAGS.detections_dir, FLAGS.tracks_dir, FLAGS.stats_dir, FLAGS.vis_dir, FLAGS.img_snapshots_dir, FLAGS.vid_snapshots_dir, FLAGS.around, FLAGS.start_buffer, FLAGS.end_buffer, FLAGS.display_tracks, FLAGS.display_detections, FLAGS.display_trails, FLAGS.save_static_trails, FLAGS.generate_image_snapshots, FLAGS.generate_video_snapshots, FLAGS.summary, FLAGS.full)
def per_process(): # Get a list of videos to process if os.path.exists(os.path.normpath(FLAGS.videos_dir)): videos = os.listdir(os.path.normpath(FLAGS.videos_dir)) logging.info("Will process {} videos from {}".format( len(videos), os.path.normpath(FLAGS.videos_dir))) else: logging.info("videos_dir does not exist: {}".format( os.path.normpath(FLAGS.videos_dir))) return # generate frames for video in tqdm(videos, desc='Generating frames'): video_to_frames(os.path.join(os.path.normpath(FLAGS.videos_dir), video), os.path.normpath(FLAGS.frames_dir), os.path.normpath(FLAGS.stats_dir), every=FLAGS.detect_every) # make a frame list to build a detection dataset frame_paths = list() for video in videos: with open( os.path.join(os.path.normpath(FLAGS.stats_dir), video[:-4] + '.txt'), 'r') as f: video_id, width, height, length = f.read().rstrip().split(',') frame_paths = list() for frame in range(0, int(length), FLAGS.detect_every): frame_path = os.path.join(os.path.normpath(FLAGS.frames_dir), video, "{:010d}.jpg".format(frame)) if not os.path.exists(frame_path): logging.warning( "{} Frame image file doesn't exist. Probably because you extracted frames at " "a higher 'every' value than the 'detect_every' value specified" .format(frame_path)) logging.warning( "Will re-extract frames, you have 10 seconds to cancel") time.sleep(10) video_to_frames(os.path.join( os.path.normpath(FLAGS.videos_dir), video), os.path.normpath(FLAGS.frames_dir), os.path.normpath(FLAGS.stats_dir), overwrite=True, every=FLAGS.detect_every) else: frame_paths.append(frame_path) # testing contexts ctx = [mx.gpu(int(i)) for i in FLAGS.gpus.split(',') if i.strip()] ctx = ctx if ctx else [mx.cpu()] net, transform = prep_net(os.path.normpath(FLAGS.model_path), FLAGS.batch_size, ctx) dataset, loader = prep_data(frame_paths, transform, FLAGS.batch_size, FLAGS.num_workers) detect(net, dataset, loader, ctx, FLAGS.detections_dir, FLAGS.save_detection_threshold) # Get a list of detections to process if os.path.exists(os.path.normpath(FLAGS.detections_dir)): detections = os.listdir(FLAGS.detections_dir) logging.info("Will process {} detections files from {}".format( len(detections), os.path.normpath(FLAGS.detections_dir))) else: logging.info("detections_dir does not exist: {}".format( os.path.normpath(FLAGS.detections_dir))) return track(detections, FLAGS.detections_dir, FLAGS.stats_dir, FLAGS.tracks_dir, FLAGS.track_detection_threshold, FLAGS.max_age, FLAGS.min_hits) # visualise for video in videos: visualise(os.path.join(os.path.normpath(FLAGS.videos_dir), video), FLAGS.frames_dir, FLAGS.detections_dir, FLAGS.tracks_dir, FLAGS.stats_dir, FLAGS.vis_dir, FLAGS.img_snapshots_dir, FLAGS.vid_snapshots_dir, FLAGS.around, FLAGS.start_buffer, FLAGS.end_buffer, FLAGS.display_tracks, FLAGS.display_detections, FLAGS.display_trails, FLAGS.save_static_trails, FLAGS.generate_image_snapshots, FLAGS.generate_video_snapshots, FLAGS.summary, FLAGS.full)
S = 0.45 * 0.3 # bot horizontal cross sectional area m^2 os.chdir(os.path.dirname(os.path.realpath(__file__))) # change cwd to file location video_loc = './Data/190619_3/' file_ext = '.jpg' fps = 50 time_between_img = 0.5 # seconds plume_absorbance_thres = (0.0, 0.15) # Not needed but saves changing the prep_background imgs func with open(f'{video_loc[:7]}cam_mtx.pickle', 'rb') as pickle_in: camera_params = pickle.load(pickle_in) if CAPTURE_FRAMES == 1: # create jpgs frames video_to_frames(video_loc, '00000.MTS', image_ext=file_ext, video_fps=fps, spacing=time_between_img, start_time=10) # Get list of file names file_ids = raw_img.get_image_fid(video_loc, file_ext) FNAMES = file_ids[file_ext[1:]] # Get background images BG_IDS = raw_img.get_image_fid(video_loc + 'BG/', file_ext) BG_FNAMES = BG_IDS[file_ext[1:]] (BG, CROP_POS, box_dims) = raw_img.prep_background_imgs( [raw_img.raw_img(video_loc + 'BG/', f, file_ext) for f in BG_FNAMES], camera_params) for count, f in enumerate(FNAMES): # Preprocess the image ######################################### img = raw_img.raw_img(video_loc, f, file_ext) img.get_experiment_conditions(get_g_ss = True)
def subclip(video_path, detections_dir, tracks_dir, stats_dir, clip_dir, around='detections', display_detections=False, display_tracks=False, start_buffer=100, end_buffer=50): video_path = os.path.normpath(video_path) detections_dir = os.path.normpath(detections_dir) tracks_dir = os.path.normpath(tracks_dir) stats_dir = os.path.normpath(stats_dir) clip_dir = os.path.normpath(clip_dir) assert around == 'detections' or around == 'tracks' colors = dict() for i in range(200): colors[i] = (int(256 * random.random()), int(256 * random.random()), int(256 * random.random())) os.makedirs(clip_dir, exist_ok=True) video_path, video_filename = os.path.split(video_path) txt_filename = video_filename[:-4] + '.txt' if not os.path.exists(os.path.join(stats_dir, txt_filename)): logging.info( "Stats file {} does not exist so will make it first...".format( os.path.join(stats_dir, txt_filename))) video_to_frames(os.path.join(os.path.normpath(FLAGS.videos_dir), video_filename), os.path.normpath(FLAGS.frames_dir), os.path.normpath(FLAGS.stats_dir), overwrite=False, every=FLAGS.detect_every) with open(os.path.join(stats_dir, txt_filename), 'r') as f: video_id, width, height, length = f.read().rstrip().split(',') if not os.path.exists(os.path.join(detections_dir, txt_filename)): logging.info( "Detections file {} does not exist so will make it first...". format(os.path.join(detections_dir, txt_filename))) detect_wrapper([video_filename]) with open(os.path.join(detections_dir, txt_filename), 'r') as f: detections = [line.rstrip().split(',') for line in f.readlines()] width = int(width) height = int(height) length = int(length) mult = True detections_ = dict() for d in detections: if mult: d_ = [ int(d[1]), float(d[2]), float(d[3]) * width, float(d[4]) * height, float(d[5]) * width, float(d[6]) * height ] else: d_ = [ int(d[1]), float(d[2]), float(d[3]), float(d[4]), float(d[5]), float(d[6]) ] if int(d[0]) in d: detections_[int(d[0])].append(d_) else: detections_[int(d[0])] = [d_] detections = detections_ if not os.path.exists(os.path.join(tracks_dir, txt_filename)): logging.info( "Tracks file {} does not exist so will make it first...".format( os.path.join(tracks_dir, txt_filename))) track([txt_filename], os.path.normpath(FLAGS.detections_dir), os.path.normpath(FLAGS.stats_dir), os.path.normpath(FLAGS.tracks_dir), FLAGS.track_detection_threshold, FLAGS.max_age, FLAGS.min_hits) with open(os.path.join(tracks_dir, txt_filename), 'r') as f: tracks = [line.rstrip().split(',') for line in f.readlines()] tracks_ = dict() for t in tracks: if mult: t_ = [ int(t[1]), float(t[2]), float(t[3]) * width, float(t[4]) * height, float(t[5]) * width, float(t[6]) * height ] else: t_ = [ int(t[1]), float(t[2]), float(t[3]), float(t[4]), float(t[5]), float(t[6]) ] if int(t[0]) in tracks_: tracks_[int(t[0])].append(t_) else: tracks_[int(t[0])] = [t_] tracks = tracks_ capture = cv2.VideoCapture(os.path.join(video_path, video_filename)) # Get the total number of frames total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) # Might be a problem if video has no frames if total < 1: print("Check your opencv + ffmpeg installation, can't read videos!!!\n" "\nYou may need to install open cv by source not pip") return None assert total == length - 1 or total == length or total == length + 1 full_out_video = cv2.VideoWriter( "%s_shortened.mp4" % os.path.join(clip_dir, video_filename[:-4]), cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 25, (width, height)) track_trails = queue.Queue(maxsize=50) since = 0 out_count = 0 capture.set(1, 0) while_safety = 0 for current in tqdm(range(length), desc="Shortening video: {}".format(video_filename)): # capture.set(1, current) # flag, frame = capture.read() while True: while_safety += 1 flag, frame = capture.read() # if flag != 0 and frame is not None: if frame is not None: break if while_safety > 1000: break if frame is None: # should only occur at the end of a video break v_height, v_width, _ = frame.shape assert v_height == height assert v_width == width frame[-50:, -250:, :] = (0, 0, 0) cv2.putText(frame, '{}'.format(current), (v_width - 240, v_height - 12), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) out_frame = frame.copy() if display_tracks: if current in tracks: out_frame = cv_plot_bbox( out_path=None, img=out_frame, bboxes=[t[2:] for t in tracks[current]], scores=[t[1] for t in tracks[current] ], # todo should do in tracking code labels=[t[0] for t in tracks[current]], thresh=0, colors=colors, class_names=[]) track_trails_frame = {} for t in tracks[current]: # make trails per track track_trails_frame[t[0]] = (int(t[2] + ((t[4] - t[2]) / 2)), int(t[5])) # put in the queue that exists over frames if track_trails.full(): track_trails.get() track_trails.put(track_trails_frame) # draw the trail as dots that fade with time for i, trails in enumerate(list(track_trails.queue)): alpha = math.pow(i / len(list(track_trails.queue)), 2) overlay = out_frame.copy() for tid, dot in trails.items(): cv2.circle(overlay, dot, 2, colors[tid], -1) out_frame = cv2.addWeighted(overlay, alpha, out_frame, 1 - alpha, 0) if display_detections: if current in detections: out_frame = cv_plot_bbox( out_path=None, img=out_frame, bboxes=[d[2:] for d in detections[current]], scores=[d[1] for d in detections[current]], labels=[d[0] for d in detections[current]], thresh=0, colors={0: (1, 255, 1)}, class_names=['cyclist']) forward_buffer = False if around == 'tracks': if current not in tracks: since += 1 else: since = 0 for check_forward in range(current, current + start_buffer): if check_forward in tracks: forward_buffer = True break elif around == 'detections': if current not in detections: since += 1 else: since = 0 for check_forward in range(current, current + start_buffer): if check_forward in detections: forward_buffer = True break else: ValueError() if forward_buffer or since < end_buffer: out_count += 1 full_out_video.write(out_frame) if full_out_video is not None: full_out_video.release() logging.info( "\n\nOriginal video length: {}\nNew video length: {} ({}% of original)" .format(length, out_count, int(100 * float(out_count) / length)))