class AVSplit: # sift # lbp # hog def __init__(self, video_dir): self.videoclip = VideoFileClip(video_dir) self.audioclip = self.videoclip.audio def get_frames(self): return [frame for frame in self.videoclip.iter_frames()] def get_video_clip(self): return self.videocip def get_audio_clip(self): return self.audioclip def save_frames(self, save_video_dir): dir = tools.ensure_dir(save_video_dir) num_frame = int(self.videoclip.fps * self.videoclip.duration) for i, frame in enumerate(self.videoclip.iter_frames()): Image.fromarray(frame).save('{0}/{1}.jpeg'.format(dir, i)) print('saving frame {0}/{1}'.format(i, num_frame - 1)) with open(config.frames_output_dir+'/fps.txt', 'w') as f: f.write(str(self.videoclip.fps)) def save_audio(self, save_audio_dir, save_audio_name): dir = tools.ensure_dir(save_audio_dir) if self.audioclip is None: print('Video has no audio') else: self.audioclip.write_audiofile(os.path.join(save_audio_dir, save_audio_name), nbytes=2, codec='pcm_s16le', bitrate='1000k', verbose=True)
def get_video_array(video_path, label_path, set, video_name): clip = VideoFileClip(video_path, target_resolution=(225, None)) clip_width = clip.get_frame(0).shape[1] assert (clip_width > 224) offset = int((clip_width - 224) / 2) labels_video = loadmat(label_path) try: label_list = (labels_video['IF23'][0]) except: label_list = (labels_video['IF'][0]) start_frame = 0 while True: try: print(start_frame) frames = [] for frame in clip.iter_frames(): frames.append(frame) if len( frames ) == _FRAMES: # here you have 16 frames in your frames array np_frames = np.array(frames) np_frames = np_frames[:, :224, offset:224 + offset, :] np_frames = np_frames.transpose((3, 0, 1, 2)) #print(np_frames.shape) labels = label_list[int(start_frame):int(start_frame) + _FRAMES] label = Counter(labels).most_common(1)[0][0] dict = {"frames": np_frames, 'label': label} file_name = os.path.join( set, video_name + str(start_frame) + ".pkl") pickle.dump(dict, open(file_name, "wb")) frames = [] # empty to start over start_frame += _FRAMES except Exception as e: print(e) for pair in enumerate(clip.iter_frames()): if pair[0] in range(start_frame, -1): frames.append(pair[1]) labels = label_list[int(start_frame):-1] if labels != []: label = Counter(labels).most_common(1)[0][0] f = np.array(frames) print(len(f)) f = pad_frames(f) print(len(f)) dict = {"frames": np_frames, 'label': label} file_name = os.path.join( set, video_name + str(start_frame) + ".pkl") pickle.dump(dict, open(file_name, "wb")) break else: break return print('got video blocks')
def vid2npy(fileName): clip = VideoFileClip(fileName) n_frames = sum(1 for x in clip.iter_frames()) s = None for frame in clip.iter_frames(): s = frame.shape break X = np.zeros((1, n_frames, s[0], s[1], s[2])) for i, frame in enumerate(clip.iter_frames()): X[0, i, :, :, :] = frame return X
def get_vid(path, rgb=False): clip = VideoFileClip(path) # n_frames = clip.fps * clip.duration # print n_frames vid = [] if rgb: for i, frame in enumerate(clip.iter_frames()): vid.append(cv2.cvtColor(frame, cv2.cv.CV_RGB2GRAY)) else: for i, frame in enumerate(clip.iter_frames()): vid.append(frame[..., 0]) v = np.array(vid, dtype="uint8") return v
def video_frames_visualization(self): infile = 'project_video.mp4' start_time, end_time = 41, None clip = VideoFileClip(infile).subclip(start_time, end_time) # y_start, y_stop, scale configurations configs = [[400, 556, 1.5], [400, 656, 2.0], [400, 496, 1.0]] hmaps = [] fproc = [] for idx, frame in enumerate(clip.iter_frames()): if idx > 5: # only few frames is sufficient break car_boxes = [] box_confidences = [] for config in configs: boxes, c = self._find_cars(frame, *config) car_boxes.extend(boxes) box_confidences.extend(c) # draw bounding boxes around detected cars fproc.append(self._draw_boxes(frame, car_boxes, thick=3)) # reduce false positives car_boxes, heat_map = self._reduce_false_positives( car_boxes, box_confidences) heat_map = plt.get_cmap('hot')(heat_map / heat_map.max())[..., :3] hmaps.append(heat_map) fig, ax = plt.subplots(5, 2) for i in range(5): ax[i, 0].imshow(fproc[i]) ax[i, 1].imshow(hmaps[i]) ax[0, 0].set_title('Frames') ax[0, 1].set_title('Heatmap') plt.subplots_adjust(wspace=0) plt.show()
def save_vid_clip(path): clip = VideoFileClip(path) clip_dat = np.array([x for x in clip.iter_frames()]).astype(np.uint8) name = path.split('/')[-1] task = name.split('-')[1] case = name.split('-')[-1].split('.')[0] mode = name.split('-')[4].split('_')[-1] if task == 'GW': put_title = 'Grid World' mode = name.split('_')[-1].split('.')[0] elif task == 'BS': put_title = 'Shape Stacking' mode = name.split('_')[-1].split('.')[0] else: put_title = 'CREATE ' + task if case == 'success': put_subtitle = ' Success Examples' elif case == 'failure': put_subtitle = ' Failure Examples' else: case = 'reg' put_subtitle = ''
def process_video(self, video_path): self.paused = 0 self.video_path = video_path logger.info(video_path) clip = VideoFileClip(video_path) for i, rgb_frame in enumerate(clip.iter_frames()): self.process_frame(i, rgb_frame)
def main(argv): clip1 = VideoFileClip(argv[1]).without_audio().set_fps(1) index = 0 csvFile = open('info.csv', 'w') writer = csv.writer(csvFile) for image_np in clip1.iter_frames(1, False, True, None): # save origin picture for chopping origin_np = copy(image_np) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] # image_np_expanded = np.expand_dims(image_np, axis=0) # Actual detection. output_dict = run_inference_for_single_image(image_np, detection_graph) img_size = [len(image_np[0]), len(image_np)] for j in range(0, 10): if output_dict['detection_scores'][j] < 0.5: break p1 = (output_dict['detection_boxes'][j][1] * img_size[0]).round() p2 = (output_dict['detection_boxes'][j][0] * img_size[1]).round() p3 = (output_dict['detection_boxes'][j][3] * img_size[0]).round() p4 = (output_dict['detection_boxes'][j][2] * img_size[1]).round() region_np = origin_np[int(p2):int(p4), int(p1):int(p3)] img = Image.fromarray(region_np) relativePath = 'results/' + '1-' + str(index + 1) + '-' + str(j) + '.jpg' resultFileName = os.getcwd() + '/' + relativePath writer.writerow( [index + 1, '1-' + str(index + 1) + '-' + str(j) + '.jpg']) writer.writerow( [index + 1, '1-' + str(index + 1) + '-' + str(j) + '.jpg']) img.save(resultFileName) index = index + 1 csvFile.close()
def main(): resultFrames = [] clipFileName = input('Введите имя видеофайла для обработки: ') if not os.path.isfile(clipFileName): print('Указанный файл не найден') return clip = VideoFileClip(clipFileName) depth = 5 margin = 100 fillerWidth = 320 windowSplit = 2 winCount = 18 searchPortion = 1. pipAlpha = .7 pipScaleRatio = .35 pipParams = {'alpha': pipAlpha, 'scaleRatio': pipScaleRatio} ld = Detector(imgMarginWidth=fillerWidth, historyDepth=depth, margin=margin, windowSplit=windowSplit, winCount=winCount, searchPortion=searchPortion, veHiDepth=45, pointSize=64, groupThrd=10, groupDiff=.1, confidenceThrd=.5) for frame in tqdm(clip.iter_frames()): dst = ld.getEmbedDetections(src=frame, pipParams=pipParams) resultFrames.append(dst) resultClip = ImageSequenceClip(resultFrames, fps=25, with_mask=False) resultFileName = clipFileName.split('.')[0] resultFileName = '{}_out_{}.mp4'.format(resultFileName, aux.timeStamp()) resultClip.write_videofile(resultFileName, progress_bar=True)
def main(): car_detector = CarDetector(5) car_detector.trian_classifier('vehicles', 'non-vehicles') # image_files = glob.glob(r'./test_images/test*.jpg') # subplot_text = '23' # i=0 # for image_file in image_files: # i+=1 # img = cv2.imread(image_file) # draw_image= car_detector.find_cars(img) # plt.subplot(subplot_text+str(i)) # draw_image=cv2.cvtColor(draw_image,cv2.COLOR_BGR2RGB) # plt.imshow(draw_image) # plt.title('Example '+str(i)) # plt.show() input_clip_file = 'project_video.mp4' input_clip = VideoFileClip(input_clip_file) output_clip_file = 'output_project_video.mp4' output_frame_list = [] for frame in input_clip.iter_frames(): #The algorithms were developed for BGR not RGB frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) output_frame = car_detector.find_cars(frame) output_frame = cv2.cvtColor(output_frame, cv2.COLOR_BGR2RGB) output_frame_list.append(output_frame) output_clip = ImageSequenceClip(output_frame_list, input_clip.fps) output_clip.write_videofile(output_clip_file)
def load_frames(video_path="project_video.mp4", start_frame=None, end_frame=None): from moviepy.editor import VideoFileClip import cv2 from util import printProgressBar # The file referenced in clip1 is the original video before anything has been done to it input = VideoFileClip(video_path) #vid_clip = input.fl_image(process_image) len_frames = int(input.fps * input.duration) len_frames = len_frames if end_frame == None or end_frame > len_frames else end_frame i = 0 # Initial call to print 0% progress printProgressBar(0, len_frames, 'Loading frames') frames = [] for frame in input.iter_frames(): if start_frame == None or i > start_frame: frames.append(frame) # Update Progress Bar printProgressBar(i + 1, len_frames, 'Loading frames') if i - 1 >= len_frames: break i = i + 1 return frames, input.fps
def displayImage(f, duration=0, start=0): if display: stdscr = curses.initscr() curses.cbreak() curses.noecho() curses.curs_set(0) stdscr.keypad(1) pass try: if f.endswith('gif') or f.endswith('mp4'): clip = VideoFileClip(f) duration = duration or clip.duration clip = clip.subclip(start, min(start + duration, clip.duration - start)) for frame in clip.iter_frames(): img = Image.fromarray(frame) renderImageASCII(stdscr, img) time.sleep(1.0 / clip.fps) elif f.endswith('png') or f.endswith('jpg') or f.endswith('jpeg'): img = Image.open(f) renderImageASCII(stdscr, img) time.sleep(duration or 3) finally: if display: curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin()
def process_video(video_file, clf, scaler, params): ''' create bounding box for each frame in a video file ''' video_clip = VideoFileClip(video_file) qsize = 6 heatmap_threshold = 3 old_frames = Queue() new_frames = Queue() image_sequence = [] for i, f, in enumerate(video_clip.iter_frames()): print ('processing frame ', i) fig = plot_bounding_box(f, clf, scaler, params) if old_frames.qsize() < qsize: old_frames.put(fig) else: average_frame, old_frames = get_frame_aggregate(old_frames) _ = old_frames.get() old_frames.put(fig) heatmap = ro.apply_threshold(average_frame, heatmap_threshold) labels = label(heatmap) print(labels[1], 'cars found') draw_img = ro.draw_labeled_bboxes(np.copy(f), labels) image_sequence.append(draw_img) #plt.figure() #plt.imshow(draw_img) #plt.show() clip = ImageSequenceClip(image_sequence, fps=video_clip.fps) clip.write_videofile("drive_n_%d_t_%d.mp4" %(qsize, heatmap_threshold), audio=False)
def process_video(video_info): video_path = video_info[0] video_cateogory = video_info[1] output_frames = video_info[2] h = video_info[3] w = video_info[4] vidcap = VideoFileClip(video_path) tot_count = int(vidcap.fps * vidcap.duration) output_interval = tot_count // output_frames count, frame_count = 0, 0 video_selected_frames = list() for frame in vidcap.iter_frames(): count += 1 if count % output_interval == 0: # per second # check the number of output frames if frame_count == output_frames: break frame_count += 1 img = Image.fromarray(frame, 'RGB') processed_img = resize_method(img, h, w) video_selected_frames.append(processed_img) del vidcap return (video_cateogory, video_selected_frames)
def codeTemplatesForSubClip(moviePath,startTime,endTime,fps,algoInfo:AlgorithmInfo,templateOutputFolder=None): templateOutputPathForAlgorithm = os.path.join(templateOutputFolder,algoInfo.algorithmName) if not os.path.exists(templateOutputPathForAlgorithm): os.makedirs(templateOutputPathForAlgorithm) libraryLoader = FRVTLibraryLoader() libraryLoader.loadLibrary(algoInfo.libName,libDir=algoInfo.libDir) wrapper = FRVTWrapper(libraryLoader) wrapper.initializeTemplateCreation() clip = VideoFileClip(moviePath).subclip(startTime,endTime) startFrameNumber = int(fps * startTime) currentFrameNumber = startFrameNumber for currentFrame in clip.iter_frames(fps): filename_template = str(currentFrameNumber).zfill(5)+".template" filename_eyes = str(currentFrameNumber).zfill(5)+".eyes" fullFilename_template = os.path.join(templateOutputPathForAlgorithm,filename_template) fullFilename_eyes = os.path.join(templateOutputPathForAlgorithm,filename_eyes) #moviepy gives us the image in RGB, so we dont have to switch frvtImage = FRVTImage(libraryLoader,currentFrame,switchColorChannelsToRGB=False) multiFace = FRVTMultiface(libraryLoader,frvtImage) (retCode,templateData,isLeftAssigned,isRightAssigned,leftX,rightX,leftY,rightY) = wrapper.encodeTemplate(multiFace) if retCode == 0: fileHandle_eyes = open(fullFilename_eyes,"w") fileHandle_eyes.write(f"{isLeftAssigned} {isRightAssigned} {leftX} {rightX} {leftY} {rightY}") fileHandle_eyes.close() templateData.tofile(fullFilename_template) currentFrameNumber +=1
def read_frames(gif_path): clip = VideoFileClip(gif_path) frames = [] for frame in clip.iter_frames(): ## Convert to PIL frame_pil = PIL.Image.fromarray(frame) frames.append(frame_pil) return frames
def detect(self, source, destination): clip, frames = VideoFileClip(source), [] for frame in clip.iter_frames(): items = self.__imgdet.annotations(frame) frame = self.__imgdet.label_image(frame, items) frames.append(frame) clip = ImageSequenceClip(frames, fps=clip.fps) clip.write_videofile(destination, audio=False)
def extract_faces(path): clip = VideoFileClip(path) num_frames = len(list(clip.iter_frames())) print(num_frames) video_fps = 10.0 for frame_number in range(num_frames): print(frame_number) pipeline(clip.get_frame(frame_number / video_fps), frame_number)
def convert(data_path): # iterate through the data splits for data_split in ['train', 'val', 'test']: print('Converting ' + data_split) os.makedirs(os.path.join(data_path, data_split)) split_person_ids = person_ids[data_split] # iterate through the ids, actions, and settings for this split for person_id in split_person_ids: print(' Converting person' + person_id) for action in kth_actions_dict['person' + person_id]: for setting in kth_actions_dict['person' + person_id][action]: frame_nums = kth_actions_dict['person' + person_id][action][setting] if len(frame_nums) > 0: start_frames = [ frame_pair[0] for frame_pair in frame_nums ] end_frames = [ frame_pair[1] for frame_pair in frame_nums ] # load the video file_name = 'person' + person_id + '_' + action + '_' + setting + '_uncomp.avi' print(file_name) video = VideoFileClip( os.path.join(data_path, action, file_name)) # write each sequence to a directory sequence_frame_index = 0 sequence_index = 0 sequence_name = '' in_sequence = False for frame_index, frame in enumerate( video.iter_frames()): if frame_index + 1 in start_frames: # start a new sequence in_sequence = True sequence_frame_index = 0 sequence_name = 'person' + person_id + '_' + action + '_' + setting + '_' + str( sequence_index) os.makedirs( os.path.join(data_path, data_split, sequence_name)) if frame_index + 1 in end_frames: # end the current sequence in_sequence = False sequence_index += 1 if frame_index + 1 == max(end_frames): break if in_sequence: # write frame to the current sequence frame = frame.astype('float32') / 255. imsave( os.path.join( data_path, data_split, sequence_name, str(sequence_frame_index) + '.png'), frame) sequence_frame_index += 1 del video.reader del video
def main(): """ Runs when invoking directly from command line :return: """ resultFrames = [] clipFileName = input('Enter video file name: ') if not os.path.isfile(clipFileName): print('No such file. Exiting.') return clip = VideoFileClip(clipFileName) # depth = aux.promptForInt(message='Enter history depth in frames: ') # detectionPointSize = aux.promptForInt(message='Enter Search Margin: ') # fillerWidth = aux.promptForInt(message='Enter filler width: ') # windowSplit = aux.promptForInt(message='Enter Window Split: ') # winCount = aux.promptForInt(message='Enter Window Count for Box Search: ') # searchPortion = aux.promptForFloat(message='Enter the Search portion (0.0 - 1.0): ') # pipAlpha = aux.promptForFloat(message='Enter Picture-in-picture alpha: (0.0 - 1.0): ') # pipScaleRatio = aux.promptForFloat(message='Enter Picture-in-picture scale (0.0 - 1.0): ') depth = 5 margin = 100 fillerWidth = 320 windowSplit = 2 winCount = 18 searchPortion = 1. pipAlpha = .7 pipScaleRatio = .35 pipParams = {'alpha': pipAlpha, 'scaleRatio': pipScaleRatio} print('Total frames: {}'.format(clip.duration * clip.fps)) ld = Detector(imgMarginWidth=fillerWidth, historyDepth=depth, margin=margin, windowSplit=windowSplit, winCount=winCount, searchPortion=searchPortion, veHiDepth=45, pointSize=64, groupThrd=10, groupDiff=.1, confidenceThrd=.5) for frame in tqdm(clip.iter_frames()): dst = ld.embedDetections(src=frame, pipParams=pipParams) resultFrames.append(dst) resultClip = ImageSequenceClip(resultFrames, fps=25, with_mask=False) resultFileName = clipFileName.split('.')[0] resultFileName = '{}_out_{}.mp4'.format(resultFileName, aux.timeStamp()) resultClip.write_videofile(resultFileName)
class MPGenerator: def __init__(self, videoPth, saveImgFolder='', duration=None): """ @para videoPth path of video @saveImgFolder img save path """ self.video = VideoFileClip(videoPth) if duration is not None: self.video = self.video.subclip(duration[0], duration[1]) self._saveImgFolder = saveImgFolder def generate(self, y1, y2, fileName, mean=True): """ this generate two files mainly, an image for viewing convieniently @para y1 lower-bound 采样的下边界 @para y2 upper-bound 采样的上边界 @para fileName name of image to be saved eg."o1.jpg" 生成运动轮廓图的保存文件名 @para mean boolean, compute mean value of belt or not 布尔值,是否计算采样带的均值 """ w = self.video.size[0] frame_count = self.video.duration * self.video.fps if mean: blank_img = np.zeros((int(frame_count)+1, w, 3), np.uint8) idx = 0 for frame in self.video.iter_frames(): blank_img[idx, :, 0] = np.mean(frame[y1:y2, :, 0], axis=0) blank_img[idx, :, 1] = np.mean(frame[y1:y2, :, 1], axis=0) blank_img[idx, :, 2] = np.mean(frame[y1:y2, :, 2], axis=0) idx += 1 else: belt_width = y2 - y1 blank_img = np.zeros(((int(frame_count) + 1) * belt_width, w, 3), np.uint8) idx = 0 for frame in self.video.iter_frames(): blank_img[idx:idx+belt_width, :, :] = frame[y1:y2, :, :] idx += belt_width cv.imwrite(os.path.join(self._saveImgFolder, fileName), blank_img) return blank_img
def get_vid(path): clip = VideoFileClip(path) # n_frames = clip.fps * clip.duration # print n_frames vid = [] for i, frame in enumerate(clip.iter_frames()): vid.append(frame) v = np.array(vid, dtype="uint8") return v
def get_video_summary(filename): clip = VideoFileClip(filename) scenes = get_scene_list(filename) frame_list = [x[0].frame_num for x in scenes] frames = [ Image.fromarray(f).resize((120, 120)) for i, f in enumerate(clip.iter_frames()) if i in frame_list] return np.hstack(frames)
def __getitem__(self, idx): clip = VideoFileClip(self.video_files[idx]) if not clip.fps: clip.set_fps(25) if clip.end < (self.seq_len+1)/clip.fps: raise VideoTooShortException clip = clip.subclip(0, (self.seq_len+1)/clip.fps) frames = [resize(f, (200, 200)) for f in clip.iter_frames()] return np.array(frames[:-1]), np.array(frames[1:])
def GetMovieIterator(fileNameBase): fileExtIn = ".mp4" dirIn = "ImagesIn/VideosIn/" fileNameIn = dirIn + fileNameBase + fileExtIn #movieClipIn = VideoFileClip(fileNameIn).subclip(39, 43) difficult bridge for project_video movieClipIn = VideoFileClip(fileNameIn) imageIter = movieClipIn.iter_frames() return imageIter
def processor(task_info): video_info, root_path, video_time, output_frames = task_info # get video information category = video_info['category'] idx = video_info['video_id'] start_time = video_info['start time'] # normalize videos datasets by unifying the clip time end_time = start_time + video_time oname = os.path.join(root_path, 'videos', idx + '.mp4') tname = os.path.join(root_path, 'processed', 'processed_' + idx + ".mp4") ffmpeg_extract_subclip(oname, start_time, end_time, targetname=tname) vidcap = VideoFileClip(tname) tot_count = 0 for frame in vidcap.iter_frames(): # count total frames tot_count += 1 #tot_count = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) output_interval = tot_count // output_frames # how many frames per second frame_rate = tot_count // video_time vidcap = VideoFileClip(tname) count, frame_count = 0, 0 directory = os.path.join(root_path, 'frames', idx) for frame in vidcap.iter_frames(): count += 1 if count % output_interval == 0: # per second # check the number of output frames if frame_count == output_frames: break frame_count += 1 img = Image.fromarray(frame, 'RGB') img.save(os.path.join(directory, "frame%d.jpg" % frame_count)) logging.info('Finish processing Video {0}'.format(idx)) return (idx, category)
def main(): """ Runs when invoking directly from command line :return: """ resultFrames = [] clipFileName = input('Enter video file name: ') if not os.path.isfile(clipFileName): print('No such file. Exiting.') return clip = VideoFileClip(clipFileName) # depth = aux.promptForInt(message='Enter history depth in frames: ') # detectionPointSize = aux.promptForInt(message='Enter Search Margin: ') # fillerWidth = aux.promptForInt(message='Enter filler width: ') # windowSplit = aux.promptForInt(message='Enter Window Split: ') # winCount = aux.promptForInt(message='Enter Window Count for Box Search: ') # searchPortion = aux.promptForFloat(message='Enter the Search portion (0.0 - 1.0): ') # pipAlpha = aux.promptForFloat(message='Enter Picture-in-picture alpha: (0.0 - 1.0): ') # pipScaleRatio = aux.promptForFloat(message='Enter Picture-in-picture scale (0.0 - 1.0): ') depth = 5 margin = 100 fillerWidth = 320 windowSplit = 2 winCount = 18 searchPortion = 1. count =0 pipAlpha = .7 pipScaleRatio = .35 pipParams = {'alpha': pipAlpha, 'scaleRatio': pipScaleRatio} print('Total frames: {}'.format(clip.duration * clip.fps)) ld = Detector(imgMarginWidth=fillerWidth, historyDepth=depth, margin=margin, windowSplit=windowSplit, winCount=winCount, searchPortion=searchPortion, veHiDepth=45, pointSize=64, groupThrd=10, groupDiff=.1, confidenceThrd=.5) for frame in tqdm(clip.iter_frames()): dst = ld.embedDetections(src=frame, pipParams=pipParams) count= count+1 play=str(count) cv2.putText(dst,play,bottomLeftCornerOfText,font,fontScale, fontColor,lineType) cv2.imshow('hala',dst) if cv2.waitKey(25) & 0xFF == ord('q'): break resultFrames.append(dst) cv2.destroyAllWindows()
def extract_frames(movie, imgdir): clip = VideoFileClip(movie) frano = 0 for frames in clip.iter_frames(): imgpath = os.path.join(imgdir, '{}.png'.format(str(frano).zfill(5))) cv2.imwrite(imgpath, cv2.cvtColor(frames, cv2.COLOR_RGB2BGR)) print("Saved to: ", imgpath) frano = frano +1
def iterate_through_video(self): original_video = VideoFileClip(self.video_path) count = 0 new_frames = [] i = 0 for frame in original_video.iter_frames(): i += 1 # if i % 3 == 0: self.get_classified_frame(frame, i) print(count)
def predict_video(config, video): """Predict joint locations for video frames.""" config_path = Path(config).resolve() assert config_path.exists() video_path = Path(video).resolve() assert video_path.exists() project_path = config_path.parents[3] training_path = config_path.parent print(project_path) print(video_path) print('Loading test config...') cfg = load_config(config_path) print('Looking for latest snapshot...') snapshots = [ s.with_suffix('').name for s in training_path.glob('snapshot-*.index') ] latest_snapshot_id = max([int(s[len('snapshot-'):]) for s in snapshots]) latest_snapshot = 'snapshot-{}'.format(latest_snapshot_id) snapshot_path = training_path / latest_snapshot print('Using snapshot {} at "{}'.format(latest_snapshot_id, snapshot_path)) cfg['init_weights'] = str(snapshot_path) sess, inputs, outputs = ptf_predict.setup_pose_prediction(cfg) pdindex = pd.MultiIndex.from_product( [['reichler'], cfg['all_joints_names'], ['x', 'y', 'likelihood']], names=['scorer', 'bodyparts', 'coords']) clip = VideoFileClip(str(video_path)) n_frames_approx = math.ceil(clip.duration * clip.fps) predictions = np.zeros((n_frames_approx, 3 * len(cfg['all_joints_names']))) print('Starting pose estimation...') for n, frame in enumerate( tqdm(clip.iter_frames(dtype='uint8'), total=n_frames_approx)): image_batch = data_to_input(frame) # skimage.color.gray2rgb(image) outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref = ptf_predict.extract_cnn_output(outputs_np, cfg) pose = ptf_predict.argmax_pose_predict(scmap, locref, cfg.stride) predictions[n, :] = pose.flatten() print('Storing results') df = pd.DataFrame(predictions[:n, :], columns=pdindex, index=range(n)) df.to_hdf(video_path.with_suffix('.h5'), 'df_with_missing', format='table', mode='w') df.to_csv(video_path.with_suffix('.csv'))
def average_video(filepath, outpath, start=None, end=None, sample_every=1): """Calculate average of video frames""" # Load video vid = VideoFileClip(filepath, audio=False) width = vid.w height = vid.h if start is None and end is None: frame_generator = vid.iter_frames(progress_bar=True, dtype=np.uint8) else: if start is None: start = 0 if end is None: end = vid.duration # compute time increment for sampling by frames sample_inc = sample_every / vid.fps frame_generator = tqdm(vid.get_frame(f) for f in frange(start, end, sample_inc)) # create starting matrix of zeros sum_fs = np.zeros(shape=(height, width, 3), dtype=int) ma_sum_fs = np.zeros(shape=(height, width, 3), dtype=int) prev_f = np.zeros(shape=(height, width, 3), dtype=int) sum_delta_fs = np.zeros(shape=(height, width, 3), dtype=int) n_frames = 0 for f in frame_generator: delta = f - prev_f sum_delta_fs += delta sum_fs += f ma_sum_fs += f if divmod(n_frames, 100)[1] == 0 and n_frames > 0: ma_f = ma_sum_fs / 100 Image.fromarray(ma_f.astype(np.uint8))\ .save(os.path.join(outpath, 'movavg_{}.png'.format(n_frames))) ma_sum_fs = np.zeros(shape=(height, width, 3), dtype=int) n_frames += 1 prev_f = f # average out the values for each frame average_delta_f = sum_delta_fs / n_frames average_f = sum_fs / n_frames # Create images delta_img = Image.fromarray(average_delta_f.astype(np.uint8)) delta_img.save(os.path.join(outpath, 'average_delta.png')) final_img = Image.fromarray(average_f.astype(np.uint8)) final_img.save(os.path.join(outpath, 'average.png'))
def average_video(filepath, outpath, start=None, end=None, sample_every=1): """Calculate average of video frames""" # Load video vid = VideoFileClip(filepath, audio=False).resize(width=66) width = vid.w height = vid.h if start is None and end is None: frame_generator = vid.iter_frames(progress_bar=True, dtype=np.uint8) else: if start is None: start = 0 if end is None: end = vid.duration # compute time increment for sampling by frames sample_inc = sample_every / vid.fps frame_generator = tqdm(vid.get_frame(f) for f in frange(start, end, sample_inc)) # create starting matrix of zeros sum_fs = np.zeros(shape=(height, width, 3), dtype=int) ma_sum_fs = np.zeros(shape=(height, width, 3), dtype=int) prev_f = np.zeros(shape=(height, width, 3), dtype=int) sum_delta_fs = np.zeros(shape=(height, width, 3), dtype=int) n_frames = 0 for f in frame_generator: #delta = f - prev_f #sum_delta_fs += delta #sum_fs += f #ma_sum_fs += f #if divmod(n_frames, 100)[1] == 0 and n_frames > 0: # ma_f = ma_sum_fs / 100 # Image.fromarray(ma_f.astype(np.uint8))\ # .save(os.path.join(outpath, 'movavg_{}.png'.format(n_frames))) # ma_sum_fs = np.zeros(shape=(height, width, 3), dtype=int) #n_frames += 1 #prev_f = f print len(f) time.sleep(1.0/float(sample_every))
def run_moving_crash(args, target, outfile): """Runs a moving crash based on moving (gif/mp4) inputs""" video = VideoFileClip(target) img = video.get_frame(t=0) # first frame of the video bounds = foreground.get_fg_bounds(img.shape[1], args.max_depth) max_depth = bounds.max_depth crash_params = crash.CrashParams( max_depth, args.threshold, args.bg_value, args.rgb_select) options = _options(args.reveal_foreground, args.reveal_background, args.crash, args.reveal_quadrants, args.bg_value) frames = video.iter_frames(fps=video.fps) def make_frame(_): frame = next(frames) fg, bounds = foreground.find_foreground(frame, crash_params) return _process_img(frame, fg, bounds, options) output_video = VideoClip( make_frame, duration=video.duration-(4/video.fps)) # trim last 4 frms output_video.write_videofile( outfile, preset=args.compression, fps=video.fps, threads=args.in_parallel)
def extract_features(input_dir, output_dir, model_type='inceptionv3', batch_size=32): """ Extracts features from a CNN trained on ImageNet classification from all videos in a directory. Args: input_dir (str): Input directory of videos to extract from. output_dir (str): Directory where features should be stored. model_type (str): Model type to use. batch_size (int): Batch size to use when processing. """ input_dir = os.path.expanduser(input_dir) output_dir = os.path.expanduser(output_dir) if not os.path.isdir(input_dir): sys.stderr.write("Input directory '%s' does not exist!\n" % input_dir) sys.exit(1) # Load desired ImageNet model # Note: import Keras only when needed so we don't waste time revving up # Theano/TensorFlow needlessly in case of an error model = None input_shape = (224, 224) if model_type.lower() == 'inceptionv3': from keras.applications import InceptionV3 model = InceptionV3(include_top=True, weights='imagenet') elif model_type.lower() == 'xception': from keras.applications import Xception model = Xception(include_top=True, weights='imagenet') elif model_type.lower() == 'resnet50': from keras.applications import ResNet50 model = ResNet50(include_top=True, weights='imagenet') elif model_type.lower() == 'vgg16': from keras.applications import VGG16 model = VGG16(include_top=True, weights='imagenet') elif model_type.lower() == 'vgg19': from keras.applications import VGG19 model = VGG19(include_top=True, weights='imagenet') else: sys.stderr.write("'%s' is not a valid ImageNet model.\n" % model_type) sys.exit(1) if model_type.lower() == 'inceptionv3' or model_type.lower() == 'xception': shape = (299, 299) # Get outputs of model from layer just before softmax predictions from keras.models import Model model = Model(model.inputs, output=model.layers[-2].output) # Create output directories visual_dir = os.path.join(output_dir, 'visual') # RGB features #motion_dir = os.path.join(output_dir, 'motion') # Spatiotemporal features #opflow_dir = os.path.join(output_dir, 'opflow') # Optical flow features for directory in [visual_dir]:#, motion_dir, opflow_dir]: if not os.path.exists(directory): os.makedirs(directory) # Find all videos that need to have features extracted def is_video(x): return x.endswith('.mp4') or x.endswith('.avi') or x.endswith('.mov') vis_existing = [x.split('.')[0] for x in os.listdir(visual_dir)] #mot_existing = [os.path.splitext(x)[0] for x in os.listdir(motion_dir)] #flo_existing = [os.path.splitext(x)[0] for x in os.listdir(opflow_dir)] video_filenames = [x for x in sorted(os.listdir(input_dir)) if is_video(x) and os.path.splitext(x)[0] not in vis_existing] # Go through each video and extract features from keras.applications.imagenet_utils import preprocess_input for video_filename in tqdm(video_filenames): # Open video clip for reading try: clip = VideoFileClip( os.path.join(input_dir, video_filename) ) except Exception as e: sys.stderr.write("Unable to read '%s'. Skipping...\n" % video_filename) sys.stderr.write("Exception: {}\n".format(e)) continue # Sample frames at 1fps fps = int( np.round(clip.fps) ) frames = [scipy.misc.imresize(crop_center(x.astype(np.float32)), shape) for idx, x in enumerate(clip.iter_frames()) if idx % fps == fps//2] n_frames = len(frames) frames_arr = np.empty((n_frames,)+shape+(3,), dtype=np.float32) for idx, frame in enumerate(frames): frames_arr[idx,:,:,:] = frame frames_arr = preprocess_input(frames_arr) features = model.predict(frames_arr, batch_size=batch_size) name, _ = os.path.splitext(video_filename) feat_filepath = os.path.join(visual_dir, name+'.npy') with open(feat_filepath, 'wb') as f: np.save(f, features)
def average_video(filepath, outpath, start=None, end=None, sample_every=1): global sb1 global sb2 global sb3 global sb4 global sb5 global sb6 global sb7 global sb8 global sb9 global sb10 global sb11 global sb12 global sb13 global sb14 global sb15 global sb16 global sb17 global sb18 global sb19 global sb20 global sb21 global sb22 global sb23 global sb24 global sb25 global sb26 global sb27 global sb28 global sb29 global sb30 global sb31 global sb32 global sb33 global sb34 global sb35 global sb36 global sb37 global sb38 global sb39 global sb40 global sb41 global sb42 global sb43 global sb44 global sb45 global sb46 global sb47 global sb48 global sb49 global sb50 global sb51 global sb52 global sb53 global sb54 global sb55 global sb56 global sb57 global sb58 global sb59 global sb60 global sb61 global sb62 global sb63 global sb64 global sb65 global sb66 """Calculate average of video frames""" # Load video vid = VideoFileClip(filepath, audio=False).resize(width=66) width = vid.w height = vid.h if start is None and end is None: frame_generator = vid.iter_frames(progress_bar=True, dtype=np.uint8) else: if start is None: start = 0 if end is None: end = vid.duration # compute time increment for sampling by frames sample_inc = sample_every / vid.fps frame_generator = tqdm(vid.get_frame(f) for f in frange(start, end, sample_inc)) # create starting matrix of zeros sum_fs = np.zeros(shape=(height, width, 3), dtype=int) ma_sum_fs = np.zeros(shape=(height, width, 3), dtype=int) prev_f = np.zeros(shape=(height, width, 3), dtype=int) sum_delta_fs = np.zeros(shape=(height, width, 3), dtype=int) n_frames = 0 for f in frame_generator: #delta = f - prev_f #sum_delta_fs += delta #sum_fs += f #ma_sum_fs += f #if divmod(n_frames, 100)[1] == 0 and n_frames > 0: # ma_f = ma_sum_fs / 100 # Image.fromarray(ma_f.astype(np.uint8))\ # .save(os.path.join(outpath, 'movavg_{}.png'.format(n_frames))) # ma_sum_fs = np.zeros(shape=(height, width, 3), dtype=int) #n_frames += 1 #prev_f = f #print len(f[0]) for x in range(1,len(f[0])+1): for y in range(0,len(f)): if y < 41: r = f[y][x-1][0] g = f[y][x-1][1] b = f[y][x-1][2] if x == 1: sb1 += pack('BBBB', r, g, b, y) elif x == 2: sb2 += pack('BBBB', r, g, b, y) elif x == 3: sb3 += pack('BBBB', r, g, b, y) elif x == 4: sb4 += pack('BBBB', r, g, b, y) elif x == 5: sb5 += pack('BBBB', r, g, b, y) elif x == 6: sb6 += pack('BBBB', r, g, b, y) elif x == 7: sb7 += pack('BBBB', r, g, b, y) elif x == 8: sb8 += pack('BBBB', r, g, b, y) elif x == 9: sb9 += pack('BBBB', r, g, b, y) elif x == 10: sb10 += pack('BBBB', r, g, b, y) elif x == 11: sb11 += pack('BBBB', r, g, b, y) elif x == 12: sb12 += pack('BBBB', r, g, b, y) elif x == 13: sb13 += pack('BBBB', r, g, b, y) elif x == 14: sb14 += pack('BBBB', r, g, b, y) elif x == 15: sb15 += pack('BBBB', r, g, b, y) elif x == 16: sb16 += pack('BBBB', r, g, b, y) elif x == 17: sb17 += pack('BBBB', r, g, b, y) elif x == 18: sb18 += pack('BBBB', r, g, b, y) elif x == 19: sb19 += pack('BBBB', r, g, b, y) elif x == 20: sb20 += pack('BBBB', r, g, b, y) elif x == 21: sb21 += pack('BBBB', r, g, b, y) elif x == 22: sb22 += pack('BBBB', r, g, b, y) elif x == 23: sb23 += pack('BBBB', r, g, b, y) elif x == 24: sb24 += pack('BBBB', r, g, b, y) elif x == 25: sb25 += pack('BBBB', r, g, b, y) elif x == 26: sb26 += pack('BBBB', r, g, b, y) elif x == 27: sb27 += pack('BBBB', r, g, b, y) elif x == 28: sb28 += pack('BBBB', r, g, b, y) elif x == 29: sb29 += pack('BBBB', r, g, b, y) elif x == 30: sb30 += pack('BBBB', r, g, b, y) elif x == 31: sb31 += pack('BBBB', r, g, b, y) elif x == 32: sb32 += pack('BBBB', r, g, b, y) elif x == 33: sb33 += pack('BBBB', r, g, b, y) elif x == 34: sb34 += pack('BBBB', r, g, b, y) elif x == 35: sb35 += pack('BBBB', r, g, b, y) elif x == 36: sb36 += pack('BBBB', r, g, b, y) elif x == 37: sb37 += pack('BBBB', r, g, b, y) elif x == 38: sb38 += pack('BBBB', r, g, b, y) elif x == 39: sb39 += pack('BBBB', r, g, b, y) elif x == 40: sb40 += pack('BBBB', r, g, b, y) elif x == 41: sb41 += pack('BBBB', r, g, b, y) elif x == 42: sb42 += pack('BBBB', r, g, b, y) elif x == 43: sb43 += pack('BBBB', r, g, b, y) elif x == 44: sb44 += pack('BBBB', r, g, b, y) elif x == 45: sb45 += pack('BBBB', r, g, b, y) elif x == 46: sb46 += pack('BBBB', r, g, b, y) elif x == 47: sb47 += pack('BBBB', r, g, b, y) elif x == 48: sb48 += pack('BBBB', r, g, b, y) elif x == 49: sb49 += pack('BBBB', r, g, b, y) elif x == 50: sb50 += pack('BBBB', r, g, b, y) elif x == 51: sb51 += pack('BBBB', r, g, b, y) elif x == 52: sb52 += pack('BBBB', r, g, b, y) elif x == 53: sb53 += pack('BBBB', r, g, b, y) elif x == 54: sb54 += pack('BBBB', r, g, b, y) elif x == 55: sb55 += pack('BBBB', r, g, b, y) elif x == 56: sb56 += pack('BBBB', r, g, b, y) elif x == 57: sb57 += pack('BBBB', r, g, b, y) elif x == 58: sb58 += pack('BBBB', r, g, b, y) elif x == 59: sb59 += pack('BBBB', r, g, b, y) elif x == 60: sb60 += pack('BBBB', r, g, b, y) elif x == 61: sb61 += pack('BBBB', r, g, b, y) elif x == 62: sb62 += pack('BBBB', r, g, b, y) elif x == 63: sb63 += pack('BBBB', r, g, b, y) elif x == 64: sb64 += pack('BBBB', r, g, b, y) elif x == 65: sb65 += pack('BBBB', r, g, b, y) elif x == 66: sb66 += pack('BBBB', r, g, b, y) time.sleep(1.0/float(sample_every))
#RUNS ON PYTHON 2 import numpy as np import csv from moviepy.editor import VideoFileClip #FILE = 'videos/sample_red.MP4' #FILE = 'videos/sample_red_with_mod.MP4' FILE = "D:/Users/Rafael/Videos/iPhone/IMG_0537.mov" clip = VideoFileClip(FILE) data = clip.iter_frames(fps=None, with_times=True, progress_bar=True) rgb_list = [] times_list = [] for time, rgb in data: times_list.append(time) rgb_list.append(rgb) with open('csv/framedata.csv','wb') as fp: a = csv.writer(fp, delimiter=',') data = ['time','RED', 'GREEN','BLUE'] a.writerow(data) frame_averages = [] for time, frame in zip(times_list, rgb_list): #print(time)
if args.length is not None: clip = clip.set_duration(args.length) print "Read clip, duration = %.1fs, FPS = %.1f" % (clip.duration, clip.fps) #------------------------------------------------------------------------ # Non-integer frame rates (e.g. 29.97) cause problems when retrieving # offsets. Round to an integer. #------------------------------------------------------------------------ clip.fps = round(clip.fps) #------------------------------------------------------------------------ # Transform brightnesses into a list of (offset_seconds, brightness) #------------------------------------------------------------------------ print "Analysing brightness ...." brightnesses = [ (index / clip.fps, round(np.mean(frame) / 255.0, args.round)) for index, frame in enumerate(clip.iter_frames()) ] #------------------------------------------------------------------------ # Sort ascending #------------------------------------------------------------------------ brightnesses = sorted(brightnesses, key = lambda x: ((-1 if args.reverse else 1) * x[1], x[0])) #------------------------------------------------------------------------ # Transform into pairs of (origin_offset, destination_offset) #------------------------------------------------------------------------ brightnesses = dict([ ("%.2f" % (index / clip.fps), value[0]) for index, value in enumerate(brightnesses) ]) print "Found %f frames" % (clip.fps * clip.duration) #------------------------------------------------------------------------ # Filter function. # Need two cases, for video (scalar offsets) and audio (arrays).