def ir(self, frames, subjects): # search for subjects in frames matches = [] create_dir(self.job.matches_dir) info('searching for ' + str(len(subjects)) + ' subjects in ' + str(len(frames)) + ' frames...') frame_index = 0 subject_index = 0 total = len(subjects) * len(frames) for frame in frames: frame_index = frame_index + 1 for subject in subjects: progress(subject_index, total) subject_index = subject_index + 1 match = self.search_frame_ir(frame, subject, frame_index) if match: matches.append(match) progress(subject_index, total) for result in matches: highlight('found subject ' + result.get('subject') + ' (' + str( result.get('subject_size')) + 'px) in frame ' + result.get('frame')) if len(matches) == 0: warn('found nothing :/') return matches
def download_subjects(self): if os.path.exists(self.job.subjects_dir): success('subjects exists') return create_dir(self.job.subjects_dir) # delegate to handler self.job.handler.download_subjects(self.job)
def extract_frames(self, area_key, output_dir): # extract frames info('extracting frames from ' + area_key + ' area ...') # area definitions crop_areas = { 'rt': ('(in_w/4)*3', '0', 'in_w/4', 'in_h/3'), 'rb': ('(in_w/4)*3', '(in_h/3)*2', 'in_w/4', 'in_h/3'), 'lt': ('0', '0', 'in_w/4', 'in_h/3'), 'lb': ('0', '(in_h/3)*2', 'in_w/4', 'in_h/3'), } crop_area = crop_areas.get(area_key) create_dir(output_dir) # for area_key, crop_area in crop_areas.items(): try: ( ffmpeg .input(self.job.video_source_path) .crop(*crop_area) .filter('fps', '1/' + str(self.job.frame_interval_seconds)) .output(os.path.join(output_dir, '%05d.jpg')) .run(quiet=True, capture_stderr=True) ) except ffmpeg.Error as err: # decode error message and print to stdout # from: https://stackoverflow.com/a/37059682/580651 error(codecs.escape_decode(err.stderr)[0].decode("utf-8")) success('stored ' + str(len(glob.glob(os.path.join(output_dir, '*.jpg')))) + ' frames to ' + output_dir)
def download_video(self): if os.path.isfile(self.job.video_source_path): success('source video exists') return info('downloading "' + self.job.video_url + '"...') create_dir(self.job.source_dir) if self.job.video_type == 'youtube': # from: https://stackoverflow.com/a/46562895/580651 YouTube(self.job.video_url).streams.first() \ .download(output_path=self.job.source_dir, filename=self.job.job_id) if self.job.video_type == 'local': extension = Path(self.job.video_url).suffix copy(self.job.video_url, os.path.join(self.job.source_dir, self.job.video_source_name + extension)) success('stored to ' + self.job.source_dir)
def __init__(self, video_url, handler, frame_interval_seconds=5, icon_on_screen_size=64): self.video_url = video_url self.job_id = hashlib.sha1(self.video_url.encode('utf-8')).hexdigest() self.job_dir = os.path.join(os.getcwd(), 'jobs', self.job_id) self.video_type = self.determine_video_type(video_url) self.video_source_name = self.job_id self.handler = handler self.frame_interval_seconds = frame_interval_seconds self.icon_on_screen_size = icon_on_screen_size self.source_dir = os.path.join(self.job_dir, source_folder_name) self.frames_dir = os.path.join(self.job_dir, frames_folder_name) self.subjects_dir = os.path.join(self.job_dir, subjects_folder_name) self.matches_dir = os.path.join(self.job_dir, matches_folder_name) self.source_file = os.path.join(self.job_dir, 'source.json') self.subjects_file = os.path.join(self.job_dir, 'subjects.json') self.matches_file = os.path.join(self.job_dir, 'matches.json') self.results_file = os.path.join(self.job_dir, 'results.json') self.result_html_file = os.path.join(self.job_dir, 'index.html') self.video_source_path = os.path.join(self.source_dir, self.video_source_name + '.mp4') self.video_source_webm_path = os.path.join( self.source_dir, self.video_source_name + '.webm') self.video_poster_path = os.path.join(self.source_dir, self.video_source_name + '.jpg') create_dir(self.job_dir) section('[job]') info('id: ' + self.job_id) info('handler: ' + self.handler.type) info('video: ' + self.video_type) section_end()