Esempio n. 1
0
	def run(self, img, step=None, debug=False, print_time=False, filename='unknown'):
		self.debug_out_list = []
		self.source = img
		funcs_to_run = self.functions[:step]
		output = self.source
		stopwatch = Stopwatch()
		log_string = '{}\t{: <16}\t{:.04f}s   {:.02f} fps'
		for func in funcs_to_run:
			if debug:
				output, output_debug = func.run(debug, input=output)
				self.debug_out_list.append(output_debug)
			else:
				output = func.run(debug, input=output)
			t = stopwatch.round()
			if print_time:
				fps = 1 / t if t != 0 else 999
				print(log_string.format(filename, str(func), t, fps))
			
		t = stopwatch.total()
		fps = 1 / t if t != 0 else 999
		print(Fore.YELLOW + log_string.format(filename, 'TOTAL', t, fps) + Fore.RESET)
		print() if print_time else None

		if debug:
			return self.debug_out_list[-1]
		else:
			return output
def compute_video(video_file, print_time=True):
    import cv2
    cap = cv2.VideoCapture(video_file)

    fps_input = cap.get(cv2.CAP_PROP_FPS)
    if fps_input < 15:
        # this is because sometimes cv2.CAP_PROP_FPS doesn't work properly
        fps_input = 15

    # initializing the output
    _, video_filename = os.path.split(video_file)
    out_path = os.path.join(app.config['VIDEO_OUTPUTS_FOLDER'], video_filename)
    out = None

    detection = PeopleDetection()
    labeler = PeopleLocalization(people_detection=detection, video_mode=True)

    curr_frame = 1
    total_t = 0
    frame_count = None
    # reading the input video
    while cap.isOpened():
        ret, frame = cap.read()

        frame_count = int(cap.get(
            cv2.CAP_PROP_FRAME_COUNT)) if frame_count is None else frame_count

        if frame is None:
            break

        stopwatch = Stopwatch()

        # processing the frame

        labeled = labeler.run(image=frame)
        # end processing the frame

        t = stopwatch.round()
        total_t += t
        time_remaining = (total_t / curr_frame) * (frame_count - curr_frame)

        if print_time:
            fps = 1 / t if t != 0 else 999
            print(
                f"[{curr_frame}/{frame_count}] time remaining {time_remaining:.0f}s\ttime single frame {t:.02}s\tfps {fps:.03}"
            )
        curr_frame += 1

        if labeled is None:
            break

        height, width, layers = labeled.shape
        size = (width, height)

        # if this is the first read we can set the output "size" and then use the same VideoWriter instance
        if out is None:
            out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*'DIVX'),
                                  fps_input, size)

        # write the labeled (output) to the output video
        out.write(labeled)

    cap.release()
    out.release()
    return out_path