def _find_frame_vectors(self): if self.get_image1_luminance() and self.get_image2_luminance(): klog("Evaluating image with size: %dx%d" %(self.get_image1_luminance().width(), self.get_image2_luminance().height() )) comp = ImageComparator(self.image_1_luminance) start_time = time.time() self.vectors = comp.get_motion_vectors(self.image_2_luminance, self.searcher()) elasped_time = time.time() - start_time self.ui.searchElapsedTimeLabel.setText("%.2f seconds" %elasped_time) self._draw_compressed_frame2()
def compared_frames_statuses(self, motion_threshold, MAD_threshold): holden_frames = [0] #start with the first frame discarded_frames = [] i = 0 j = 1 while i < self._video.frames_count()-2 and j < self._video.frames_count()-1: #controllo il frame successivo per vedere se sono sotto la soglia #si lo sono, posso aggiungere il frame alla lista di quelli da non considerare #no non lo sono, il frame e necessario if i is j: print "CYCLE COMPARISON ERROR" print "\nComparing frame #%d with frame #%d" %(i, j) frame_1 = self._video.frames[i].grayscaled_image() frame_2 = self._video.frames[j].grayscaled_image() comp = ImageComparator(frame_1) vectors = comp.get_motion_vectors(frame_2, self._searcher, MAD_threshold) longest_vector, max_distance = ImageComparator.longest_motion_vector(vectors) print "Max distance found: %f" %max_distance print "Longest vector is: "+ str(longest_vector) if max_distance < motion_threshold: print "Frame #%d discared.. :-) " %j discarded_frames.append(j) #the compared frame contains only short motion vectors, so I can discard that frame j += 1 #the I frame is same, the frame to be compared is the j+1 so the search continue else: print "Frame #%d holden... :-(" %j holden_frames.append(j) #the compared frame contains a very long motion vector, so the frame will be rendered as frame I i = j j = i+1 holden_frames.append(self._video.frames_count()-1) #keep the last frame return holden_frames, discarded_frames
def interpolate(self, discarded_frames, searcher, block_size, MAD_threshold): new_video_path = "/tmp/%s.interpolated/" % self._oldvideo.videoname() if os.path.exists(new_video_path): shutil.rmtree(new_video_path) os.makedirs(new_video_path) i=0 while i < self._oldvideo.frames_count(): if not i in discarded_frames: #The frame must me copied in the folder frame = self._oldvideo.frames[i] shutil.copyfile(frame.path(), "%s/Frame_%07d.png" %(new_video_path, i)) i+=1 else: #the frame must be interpolated #check which other adiacent frames must me interpolated new_frames = [i] for j in range(i+1, self._oldvideo.frames_count()): if j in discarded_frames: new_frames.append(j) else: #The adiacent frame is an I frame, so stop searching i = j break #interpolate the frames print "I have to interpolate these frames: "+ str(new_frames) left_frame_index = new_frames[0] - 1 right_frame_index = new_frames[-1] + 1 print "Left I frame: %s" %self._oldvideo.frames[left_frame_index].path() print "Right I frame: %s" %self._oldvideo.frames[right_frame_index].path() frame_1 = self._oldvideo.frames[left_frame_index] #the frame on the LEFT of the first frame to be interpolated frame_2 = self._oldvideo.frames[right_frame_index] #the frame on the RIGHT of the last frame to be interpolated comp = ImageComparator(frame_1.grayscaled_image()) vectors = comp.get_motion_vectors(frame_2.grayscaled_image(), searcher, MAD_threshold) subvectors = VideoInterpolator.sub_motion_vectors(vectors, len(new_frames)) #TODO: check len(subvectors) MUST be equal to len(new_frames) for k in range(len(new_frames)): frame_num = new_frames[k] frame_path = "%s/Frame_%07d.png" % (new_video_path, frame_num) print "Devo interpolare %d" % frame_num image1 = frame_1.image() image_new = image1.copy() #image_new.paste((0,0,0)) #First copy the background from the frame_2 to the image_new #image2 = frame_2.image() #for v in subvectors[k]: # background_block_image = image2.crop( (v["x"], v["y"], v["x"]+block_size, v["y"]+block_size) ) # image_new.paste(background_block_image, (v["x"], v["y"], v["x"]+block_size, v["y"]+block_size) ) #Then copy the moved blocks print "------------" for v in subvectors[k]: #print "Copying the block (%d, %d) from frame: %d to (%d, %d) to the new frame %d" %(v["x"], v["y"], left_frame_index, v["to_x"], v["to_y"], frame_num) moved_block_image = image1.crop( (v["x"], v["y"], v["x"]+block_size, v["y"]+block_size) ) image_new.paste(moved_block_image, (v["to_x"], v["to_y"], v["to_x"]+block_size, v["to_y"]+block_size) ) #image_new.paste((255, 0, 0), (v["to_x"], v["to_y"], v["to_x"]+block_size, v["to_y"]+block_size) ) image_new.save(frame_path) #print "Saved new frame: "+ frame_path #print "---" return new_video_path[:len(new_video_path)-1] #remove the trailing slash