def contain_image(self, image_path: str = None, image_object: np.ndarray = None, *args, **kwargs) -> typing.Dict: assert image_path or image_object, 'should fill image_path or image_object' if image_path: logger.debug(f'found image path, use it first: {image_path}') assert os.path.isfile( image_path), f'image {image_path} not existed' image_object = toolbox.imread(image_path) image_object = toolbox.turn_grey(image_object) # TODO use client or itself..? fi = FindIt(engine=['template']) fi_template_name = 'default' fi.load_template(fi_template_name, pic_object=image_object) with toolbox.video_capture(self.video.path) as cap: target_id = self.pick(*args, **kwargs)[0] frame = toolbox.get_frame(cap, target_id) frame = toolbox.turn_grey(frame) result = fi.find(str(target_id), target_pic_object=frame) return result['data'][fi_template_name]['TemplateEngine']
def contain_image(self, image_path: str = None, image_object: np.ndarray = None, threshold: float = None, *args, **kwargs): assert image_path or image_object, 'should fill image_path or image_object' if not threshold: threshold = 0.99 if image_path: logger.debug(f'found image path, use it first: {image_path}') assert os.path.isfile( image_path), f'image {image_path} not existed' image_object = cv2.imread(image_path) image_object = toolbox.turn_grey(image_object) # TODO use client or itself..? fi = FindIt(engine=['template']) fi_template_name = 'default' fi.load_template(fi_template_name, pic_object=image_object) with toolbox.video_capture(self.video.path) as cap: target_id = self.pick(*args, **kwargs)[0] frame = toolbox.get_frame(cap, target_id) frame = toolbox.turn_grey(frame) result = fi.find(str(target_id), target_pic_object=frame) find_result = result['data'][fi_template_name]['TemplateEngine'] position = find_result['target_point'] sim = find_result['target_sim'] logger.debug(f'position: {position}, sim: {sim}') return sim > threshold
def contain_image(self, image_path: str = None, image_object: np.ndarray = None, *args, **kwargs) -> typing.Dict: assert image_path or image_object, "should fill image_path or image_object" if image_path: logger.debug(f"found image path, use it first: {image_path}") assert os.path.isfile( image_path), f"image {image_path} not existed" image_object = toolbox.imread(image_path) image_object = toolbox.turn_grey(image_object) # TODO use client or itself..? fi = FindIt(engine=["template"]) fi_template_name = "default" fi.load_template(fi_template_name, pic_object=image_object) target_id = self.pick(*args, **kwargs)[0] operator = self.video.get_operator() frame = operator.get_frame_by_id(target_id) result = fi.find(str(target_id), target_pic_object=frame.data) return result["data"][fi_template_name]["TemplateEngine"]
def contain_image(self, *, image_path: str = None, image_object: np.ndarray = None, **kwargs) -> typing.Dict[str, typing.Any]: assert image_path or ( image_object is not None), "should fill image_path or image_object" if image_path: logger.debug(f"found image path, use it first: {image_path}") return toolbox.match_template_with_path(image_path, self.data, **kwargs) image_object = toolbox.turn_grey(image_object) return toolbox.match_template_with_object(image_object, self.data, **kwargs)
def do(self, frame: VideoFrame, *_, **__) -> typing.Optional[VideoFrame]: super().do(frame, *_, **__) # you can get frame_id and frame data here # and use them to custom your own function # add your code here # ... # for example, i want to turn grey, and save size of each frames frame.data = toolbox.turn_grey(frame.data) self.result[frame.frame_id] = frame.data.shape # if you are going to change the origin frame # just return the changed frame # and set 'overwrite' to 'True' when you are calling __init__ return frame
def do( self, frame_id: int, frame: np.ndarray, *_, **__ ) -> typing.Optional[np.ndarray]: super().do(frame_id, frame, *_, **__) return toolbox.turn_grey(frame)
def test_turn_grey(): image = toolbox.imread(IMAGE_PATH) toolbox.turn_grey(image)
def test_turn_blur(): image = toolbox.imread(IMAGE_PATH) grey = toolbox.turn_grey(image) toolbox.turn_blur(grey)
def init(cls, cap: cv2.VideoCapture, frame: np.ndarray) -> "VideoFrame": frame_id = toolbox.get_current_frame_id(cap) timestamp = toolbox.get_current_frame_time(cap) grey = toolbox.turn_grey(frame) logger.debug(f"new a frame: {frame_id}({timestamp})") return VideoFrame(frame_id, timestamp, grey)
def init(cls, cap: cv2.VideoCapture, frame: np.ndarray) -> "VideoFrame": frame_id = toolbox.get_current_frame_id(cap) timestamp = toolbox.get_current_frame_time(cap) grey = toolbox.turn_grey(frame) return VideoFrame(frame_id, timestamp, grey)
def do(self, frame: VideoFrame, *_, **__) -> typing.Optional[VideoFrame]: super().do(frame, *_, **__) frame.data = toolbox.turn_grey(frame.data) return frame