def test_reader(): from matplotlib import pyplot as plt video_fn = '/mnt/ilcompf5d0/user/owang/data/spatialaudiogen/preproc/2YTzoWLAzA4-video.mp4' flow_fn = '/mnt/ilcompf5d0/user/owang/data/spatialaudiogen/preproc/2YTzoWLAzA4-flow.mp4' lims_fn = '/mnt/ilcompf5d0/user/owang/data/spatialaudiogen/preproc/2YTzoWLAzA4-flow.npy' RATE = 5 SEEK = 30 DURATION = 10 flow_reader = FlowReader(flow_fn, lims_fn, rate=RATE, seek=SEEK, duration=DURATION) vid_reader = VideoReader(video_fn, rate=RATE, seek=SEEK, duration=DURATION) f, ax = plt.subplots(3, 1) while True: rgb_frame = vid_reader.get_next_frame() flow_frame = flow_reader.get_next_frame() if flow_frame is None and rgb_frame is None: break flow_frame = (flow_frame / 25. * 255).astype(np.uint8) print flow_frame.min(), flow_frame.max() ax[0].imshow(flow_frame, cmap='gray', vmin=0, vmax=255) ax[1].imshow(rgb_frame) ax[2].imshow(rgb_frame * (flow_frame[:, :, np.newaxis] > 25).astype(np.uint8)) plt.pause(0.1)
def __init__(self, flow_video_fn, flow_lims_fn, rate=None, pad_start=0, seek=None, duration=None, rotation=None, flow_prep=None): self.reader = VideoReader(flow_video_fn, rate=rate, pad_start=pad_start, seek=seek, duration=duration, rotation=rotation) self.lims = np.load(flow_lims_fn) self.fps = self.reader.fps self.duration = self.reader.duration # Seek and store first frame if seek is not None: n = int(seek * self.reader._ifps) self.lims = self.lims[n:] if flow_prep is None: flow_prep = lambda x: x self.flow_prep = flow_prep dummy_img = self.flow_prep( np.zeros(self.reader.frame_shape[:2], dtype=np.float32)) self.frame_shape = dummy_img.shape + (1, ) self.dtype = dummy_img.dtype
def __init__(self, clip, folder): self.clip = clip self.folder = folder self.videoFile = clip.getVideoFile() self.reader = VideoReader(self.videoFile) self.frames = self.reader.getVideoFrames() self.fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case
class VideoWriter: def __init__(self, clip, folder): self.clip = clip self.folder = folder self.videoFile = clip.getVideoFile() self.reader = VideoReader(self.videoFile) self.frames = self.reader.getVideoFrames() self.fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case # Create a video fragment from a clip def createVideo(self, start, end, filename): self.new(self.folder + filename, self.reader.width, self.reader.height, self.reader.fps) startFrame = int(start * self.reader.fps) endFrame = int(end * self.reader.fps) self.insertFrames(self.frames[startFrame:endFrame]) self.writer.release() def new(self, filename, width, height, fps): self.writer = cv2.VideoWriter(filename + '.mp4', self.fourcc, int(fps), (int(width),int(height))) def insertFrames(self, frames): for idx, frame in enumerate(frames): self.writer.write(frame) print 'Done'
def trackParticles(self): video = VideoReader(self.filePath_edit.text()) video.cropVideo(self.minHeight_slider.value(), self.maxHeight_slider.value(), self.minWidth_slider.value(), self.maxWidth_slider.value()) #0,790,300,1190 initialFrame = self.initialFrame_spinBox.value() finalFrame = self.finalFrame_spinBox.value() self.particles = video.detectCircles(initialFrame, finalFrame) model = PandasModel(self.particles) self.tracking_tableView.setModel(model) self.trajectories_button.setDisabled(False) print(self.particles)
def saveAsImg_Process(videoPath, img_folder, crop, verbose, percentSaved): """ Here we define what happens when pressing the save button. This method executes u¡in a new process """ #verbose=False #img_folder = 'D:/imagenes2/' video = VideoReader(videoPath) frameCount = video.frameCount video = cv2.VideoCapture(videoPath) i = 0 while (video.isOpened()): # Leemos el frame actual y lo asignamos a la variable frame ret, frame = video.read() # Recorto el frame a la zona que me interesa (es simplemente operar # con arrays de numpy) frame_crop = frame[crop[0]:crop[1], crop[2]:crop[3]] # Guardo el frame recortado a una imagen path = img_folder + 'img' + "{:06d}".format(i) + '.png' cv2.imwrite(path, frame_crop) i += 1 # Guardamos el porcentaje que llevamos completado en una variable # compartida entre este proceso y el principal percentSaved.value = int(100 * i / frameCount) if verbose == True: percent = " - " + "{:.2f}".format(100 * i / frameCount) + " %" print("Frame nº: " + str(i) + " / " + str(frameCount) + percent) # Cerramos el stream de video video.release()
def updatePlayLength(self): """ This function updates te labels indicating video length when played """ try: video = VideoReader(self.filePath_edit.text()) t = video.frameCount / self.playFps_slider.value() multiplier = '{:.1f}'.format(video.recordingSpeed / self.playFps_slider.value()) self.playLength_label.setText('Video Length: ' + '{:.2f}'.format(t) + ' s (' + multiplier + 'X)') except: print('Incorrect or empty file selected') print(self.filePath_edit.text())
def play(self): try: fps = self.playFps_slider.value() video = VideoReader(self.filePath_edit.text()) #'D:/aire2.cine' video.cropVideo(self.minHeight_slider.value(), self.maxHeight_slider.value(), self.minWidth_slider.value(), self.maxWidth_slider.value()) #0,790,300,1190 video.playVideo(fps) except: print('Incorrect or empty file selected') print(self.filePath_edit.text())
def refreshInfo(self): try: video = VideoReader(self.filePath_edit.text()) self.recSpeed_label.setText('Recording Speed: ' + str(video.recordingSpeed) + ' fps') self.width_label.setText('Width: ' + str(video.width) + ' px') self.height_label.setText('Height: ' + str(video.height) + ' px') self.frameCount_label.setText('Frame Count: ' + str(video.frameCount)) self.realTime_label.setText( 'Recorded Time: ' + '{:.2f}'.format(video.realRecordedTime) + ' s') self.recDate_label.setText( 'Recording Date: ' + video.recordingDate.strftime("%d %b %Y %H:%M")) self.updatePlayLength() self.maxHeight_slider.setMaximum(video.height) self.maxWidth_slider.setMaximum(video.width) self.initialFrame_spinBox.setMaximum(video.frameCount) self.finalFrame_spinBox.setMaximum(video.frameCount) #self.playFps_slider.setMaximum(video.recordingSpeed) except: print('Incorrect or empty file selected') print(self.filePath_edit.text()) # Return values to default self.recSpeed_label.setText('Recording Speed: ') self.width_label.setText('Width: ') self.height_label.setText('Height: ') self.frameCount_label.setText('Frame Count: ') self.realTime_label.setText('Recorded Time: ') self.recDate_label.setText('Recording Date: ') self.playLength_label.setText('Video Length: ')
def getFrames(self): self.videoReader = VideoReader(self.getVideoFile()) return self.videoReader.getFrames(self.interval, self.start)
def hasVideo(self): self.videoReader = VideoReader(self.getVideoFile()) return self.videoReader.success
class Clip: # Init caffenet for evaluation net = CaffeNet() def __init__(self, clip=None, vidFolder=None, thumbFolder=None): if clip != None: self.clipId = clip['id'] self.clip = clip else: raise NameError('No clip(id) supplied') if vidFolder == None: self.vidFolder = 'data/videos/' else: self.vidFolder = vidFolder if thumbFolder == None: self.thumbFolder = 'data/thumbnails/' else: self.thumbFolder = thumbFolder self.interval = 25 self.start = 0 self.__tmpLocation = 'tmp/' self.cache = True self.concepts = None def hasVideo(self): self.videoReader = VideoReader(self.getVideoFile()) return self.videoReader.success def getClip(self): return self.clip def getClipId(self): return self.clipId def getVideoFile(self): return self.vidFolder + self.clipId + '.mp4' def getRawFrames(self): self.videoReader = VideoReader(self.getVideoFile()) return self.videoReader.getFrames(self.interval, self.start, False) def getFrames(self): self.videoReader = VideoReader(self.getVideoFile()) return self.videoReader.getFrames(self.interval, self.start) def getConcepts(self, start=None, end=None, indexes=None): fileName = self.__tmpLocation + 'concepts/' + self.clipId + '_' + str(self.interval) + '.concepts.npy' # Check if a file with concepts with this interval already exists if self.concepts == None: try: self.concepts = np.load(fileName) except (IOError): # No file found, so get concepts and save them frames = self.getFrames() self.concepts = self.net.classify(frames) np.save(fileName, self.concepts) if start != None and end != None and indexes == None: return self.concepts[start:end] if indexes != None: return np.take(self.concepts, indexes, axis=0) return self.concepts # Get concepts for mediaclip thumbnail image with caffenet def getThumbnailConcepts(self): thumbnail = self.getThumbnail() if thumbnail != False: frame = self.net.loadImage(thumbnail) return self.net.classify([frame])[0] else: return False # Returns relevant metadata def getMetadata(self): ret = [] if 'title' in self.clip: ret.append(self.clip['title']) if 'description' in self.clip: ret.append(self.clip['description']) if 'cat' in self.clip: ret.append(' '.join(self.clip['cat'])) return ret # Returns clip title def getTitle(self): if 'title' in self.clip: return self.clip['title'] else: return 'n/a' # Returns relevant metadata as single text blob def getMetadataBlob(self): return ' '.join(self.getMetadata()) def getThumbnailFrame(self): thumbnailUrl = self.getThumbnail() if thumbnailUrl: return cv2.imread(thumbnailUrl, flags=cv2.IMREAD_COLOR) else: return False def getThumbnail(self): if 'thumbnail' in self.clip: ext = os.path.splitext(self.clip['thumbnail']['sourcepath'])[1] fileName = str(self.thumbFolder) + str(self.clipId) + str(ext) if os.path.isfile(fileName) == False: # Does not exist, so fetch it from online self._downloadThumbnail(fileName) return str(self.thumbFolder) + str(self.clipId) + '.jpg' else: return False def setInterval(self, interval): self.interval = int(interval) def setStart(self, start): self.start = int(start) def setCache(self, cache): self.cache = bool(cache) def _downloadThumbnail(self, fileName): thumbnail = urllib.URLopener() # Cloudfront: Gets 403 Forbidden # url = 'https://d2vt09yn8fcn7w.cloudfront.net' + self.clip['thumbnail']['sourcepath'] # pthumbnail: url = 'http://landmark.bbvms.com/mediaclip/' + self.clipId + '/pthumbnail/default/default.jpg' print url thumbnail.retrieve(url, fileName)
def __init__(self, filename, max_frames=None): VideoReader.__init__(self, filename, max_frames) self.flow_max = float(np.loadtxt(filename + ".np"))
class FlowReader: def __init__(self, flow_video_fn, flow_lims_fn, rate=None, pad_start=0, seek=None, duration=None, rotation=None, flow_prep=None): self.reader = VideoReader(flow_video_fn, rate=rate, pad_start=pad_start, seek=seek, duration=duration, rotation=rotation) self.lims = np.load(flow_lims_fn) self.fps = self.reader.fps self.duration = self.reader.duration # Seek and store first frame if seek is not None: n = int(seek * self.reader._ifps) self.lims = self.lims[n:] if flow_prep is None: flow_prep = lambda x: x self.flow_prep = flow_prep dummy_img = self.flow_prep( np.zeros(self.reader.frame_shape[:2], dtype=np.float32)) self.frame_shape = dummy_img.shape + (1, ) self.dtype = dummy_img.dtype def get_next_frame(self): while True: flow = self.reader.get_next_frame() if flow is None: return None # Recover angle and magnitude cur_frame = int(self.reader._it * self.reader._ifps) m_min, m_max = self.lims[cur_frame, 0], self.lims[cur_frame, 1] magnt = flow[:, :, 2] / 255. * (m_max - m_min) + m_min return self.flow_prep(magnt)[:, :, np.newaxis] angle = flow[:, :, 0] / 255. * (2 * np.pi) x_flow = self.flow_prep(magnt * np.cos(angle)) y_flow = self.flow_prep(magnt * np.sin(angle)) return np.stack((x_flow, y_flow), axis=2) def loop_frames(self): while True: frame = self.get_next_frame() if frame is None: break yield frame def get_next_chunk(self, n=1, force_size=False): chunk = [] for i in range(n): frame = self.get_next_frame() if frame is None: break chunk.append(frame) if len(chunk) == 0: return None if force_size and len(chunk) != n: return None return np.stack(chunk, axis=0) if len(chunk) > 1 else np.expand_dims( chunk[0], 0) def loop_chunks(self, n=1, force_size=False): while True: snippet = self.get_next_chunk(n, force_size=force_size) if snippet is None: break yield snippet