def startpointCompare(queryVidName, dbVidName, dbStart): """ Startpoint compare take a frame number worth pursuing, and calculates the average rmse value for the duration of the video starting at that point """ #Create the FFMPEG class variables dbVid = FFMPEG_VideoReader(dbVidName) queryVid = FFMPEG_VideoReader(queryVidName) length = queryVid.nframes # Skip to the startpoint frame dbVid.skip_frames(dbStart) frameD = dbVid.read_frame() frameQ = queryVid.read_frame() runAvg = 0 # Calculate the RMSE for each frame for i in xrange(0, length): runAvg += frame_rmse(frameD, frameQ) frameQ = queryVid.read_frame() frameD = dbVid.read_frame() # Return the average RMSE score return runAvg / length
def comparechunk(queryVidName, dbVidName, dbStart, dbEnd, thresh): """ This function takes the names of the files to compare, and where in the comparison file to begin checking from. Threshold is a user determined value that they chose qualitatively, and the GUI turned into a quantitative number Args: path to query video, path to comparison video, start frame number, endframe number, callback to process results, RMSE threshold (a number from 0-255, realistically should be between 10-50) """ # Create the FFMPEG class variables dbVid = FFMPEG_VideoReader(dbVidName) queryVid = FFMPEG_VideoReader(queryVidName) # Skip to the correct frames in the video frameQ = queryVid.get_frame(0) dbVid.skip_frames(dbStart) frameD = dbVid.read_frame() scores = [] # Compare the first frame in the query video to every frame in the chunk belowThresh = [] for i in range(dbStart, dbEnd): score = frame_rmse(frameQ, frameD) # Immediately look at startframes below the threshold if frame_rmse(frameQ, frameD) < thresh: print "Found a frame below the threshold. Scanning sequence..." score = startpointCompare(queryVidName, dbVidName, i) if score < thresh and score is not None: scores.append({ "Video Name": dbVidName, "Timestamp": secondsToTimestamp(i / dbVid.fps), "Frame Number": i, "Score": score }) return scores else: print "A sequence had a poor score of", score, ". Moving on..." frameD = dbVid.read_frame() return scores
reader.fps * 2, audiofile=arguments_strVideoAudio) else: writer = FFMPEG_VideoWriter(arguments_strVideoOut, reader.size, reader.fps * 2) if exists: # Write frames that were already completed print("Re-writing processed frames...") printProgressBar(0, readerCont.nframes) for x in range(0, readerCont.nframes): writer.write_frame(readerCont.read_frame()) printProgressBar(x + 1, readerCont.nframes) reader.skip_frames(startFrame) readerCont.close() print("Deleting temporary file(s)...") shutil.rmtree(tempDir) print("Processing resumed!") print("") totalFrames = reader.nframes nextFrame = reader.read_frame() startedTime = datetime.now() for x in range(startFrame, reader.nframes): start = datetime.now() firstFrame = nextFrame
def __getitem__(self, index): # Find the video first. vid = np.histogram(index, self.sums) assert (np.sum(vid[0]) == 1) vid = np.where(vid[0] > 0)[0][0] v_fmax = len(self.gt_list[vid]) vframe = index - self.sums[vid] #vframes = [min(vframe + i, len(self.gt_list[vid])- 1) for i in np.arange(0, 1 +10*self.multi_frame, 10)] #vframes = [min(vframe, len(self.gt_list[vid])- 1)] #cap = self.video_list[vid] imgs = [] if self.prod_Img: cap = FFMPEG_VideoReader(self.video_list[vid]) cap.initialize() for i in range(self.multi_frame): if i == 0: img = cap.get_frame(vframe / cap.fps) else: cap.skip_frames(n=9) img = cap.read_frame() img = Image.fromarray(img) if self.transform is not None: img = self.transform(img) imgs.append(img) ''' for v in vframes: #cap = cv2.VideoCapture(self.video_list[vid]) #assert cap.isOpened() == True, 'The Video cannot be read:{}'.format(self.video_list[vid]) #cap.set(1, v) #ret, frame= cap.read() img = cap.get_frame(v) #assert ret == True, 'Cannot Load this frame{}:{}'.format(self.video_list[vid], v) #cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = Image.fromarray(img) if self.transform is not None: img = self.transform(img) imgs.append(img) ''' imgs = [img.unsqueeze(0) for img in imgs] imgs = torch.cat(imgs, 0) text = [] if self.prod_Text: text = self.text_list[vid][ min(vframe + self.text_delay, len(self.text_list[vid]) ):min(vframe + self.text_window + self.text_delay, len(self.text_list[vid]))] text = '\n'.join(text) gt = self.gt_list[vid][vframe] if (len(text) == 0): text = ' ' #text = text_util.lineToTensor(text) return imgs, text, gt
def extract_frames(video_data,video_root, destination, num_frames, seed=0): """Extracts [num_frames] images from a video and stores """ np.random.seed(seed) file = h5py.File(destination,'w') X = None for n, (video, metadata) in enumerate(video_data.items()): crop = list(map(int, metadata['crop'].split(','))) if crop is not None: x1, y1, x2, y2 = crop else: x1,y1 = [0,0] x2,y2 = [-1,-1] video_path = video_root/Path(video) print('Extracting {} frames with seed {} from {}'.format(num_frames, seed, video_path.name)) print(crop) video_path = Path(video_path).resolve() if not video_path.exists(): raise FileNotFoundError('Video "{}" not found.'.format(video_path)) # img_path = Path(destination).absolute() # TODO: Exception handling for faulty videos # Warning: Handles to VideoClip object are fickle, and getting them stuck is pretty easy. # Best always handle them with a context manager. clip = FFMPEG_VideoReader(str(video_path))# as clip: print('Video duration: {} s, {} fps, uncropped frame size: {}'.format(clip.duration, clip.fps, clip.size)) # Crop frames # print('Cropping box {}->{}'.format((x1, y1), (x2, y2))) # clip = clip.crop(x1, y1, x2, y2) num_frames_clip = int(clip.duration * clip.fps) #padding = int(math.ceil(math.log10(num_frames_clip))) # file name padding # print('Storing images in {}'.format(img_path)) # Grab frames from video and store as png file frame_indices = sorted(np.random.randint(0, max(1,num_frames_clip-11), num_frames)) for idx in tqdm(np.diff([0]+frame_indices)): print(idx) image = [] clip.skip_frames(idx) for i in range(10): frame = (clip.read_frame())#idx+i / clip.fps)) # print(image) image.append(frame[y1:y2,x1:x2,:]) if X is None: X = file.create_dataset('X',(1,10,y2-y1,x2-x1,3), chunks=(1,10,y2-y1,x2-x1,3), maxshape=(None,10,y2-y1,x2-x1,3), compression="gzip", dtype = 'i') else: X.resize((X.shape[0]+1,)+X.shape[1:]) X[-1,:,:,:,:]=image # print(X[-1,:,:,:,:]) file.close()