class CaraCara: def __init__(self, window_name, image_iterator, cascade, overlay): cv.NamedWindow(window_name, cv.CV_WINDOW_AUTOSIZE) self.window_name = window_name self.image_iterator = image_iterator self.cascade = cv.Load(cascade) self.texts = deque(("What am I gonna do?", "Mark an object with the mouse!", "Tracking...")) self.tracker = ObjectTracker(window_name, overlay) def mainloop(self): fps_buffer = [] group_size = 20 for img in self.image_iterator: try: t = cv.GetTickCount() faces = detect_faces(img, self.cascade) img = self.tracker.track_object(img) #draw_surrounding_rectangles(img, faces) write_text(img, self.texts, faces) cv.ShowImage(self.window_name, img) if cv.WaitKey(10) >= 0: break t = cv.GetTickCount() - t fps_buffer.append((cv.GetTickFrequency() * 1000000.) / t) if len(fps_buffer) == group_size: fps_buffer = [sum(fps_buffer) / group_size] logging.info("%.4f fps" % fps_buffer[0]) self.texts.rotate(-1) except: logging.critical(format_exc()) self.tracker.reset() cv.DestroyWindow(self.window_name)
def __init__(self, window_name, image_iterator, cascade, overlay): cv.NamedWindow(window_name, cv.CV_WINDOW_AUTOSIZE) self.window_name = window_name self.image_iterator = image_iterator self.cascade = cv.Load(cascade) self.texts = deque(("What am I gonna do?", "Mark an object with the mouse!", "Tracking...")) self.tracker = ObjectTracker(window_name, overlay)
def main(): frame_preprocessor = FramePreprocessor() object_detector = ObjectDetector() object_registry = ObjectRegistry() object_tracker = ObjectTracker(object_registry) cap = cv2.VideoCapture('video.avi') fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (800, 480)) while True: # Get the frame from video file ret, frame = cap.read() if ret == True: preprocessed_frame = frame_preprocessor.preprocess(frame) objects_from_bkg_sub = object_detector.find_objects(frame) object_registry.update(objects_from_bkg_sub) objects_from_registry = copy.deepcopy( object_registry.get_objects()) objects_from_tracker = object_tracker.find_objects(frame) bounding_boxes = frame.copy() for obj in objects_from_registry: # Blue display_object(bounding_boxes, obj, (255, 0, 0), 1) for obj in objects_from_tracker: # Red display_object(bounding_boxes, obj, (0, 0, 255), 1) cv2.imshow('Input', frame) cv2.imshow('Preprocessed Frame', preprocessed_frame) cv2.imshow('Bounding Boxes', bounding_boxes) out.write(bounding_boxes) k = cv2.waitKey(30) & 0xff if k == 27: break else: break out.release() cap.release() cv2.destroyAllWindows()