def should_append_the_returned_focal_points_to_context_request(self, topic): focal_point1_repr = FocalPoint.from_square(1, 2, 3, 4).to_dict() focal_point2_repr = FocalPoint.from_square(5, 6, 7, 8).to_dict() calls = topic.context.request.focal_points.append.call_args_list first_call_arg_repr = calls[0][0][0].to_dict() secon_call_arg_repr = calls[1][0][0].to_dict() expect(first_call_arg_repr).to_equal(focal_point1_repr) expect(secon_call_arg_repr).to_equal(focal_point2_repr)
def features_to_focal_points(cls, features): focal_points = [] for (left, top, width, height), neighbors in features: top = cls.add_hair_offset(top, height) focal_points.append( FocalPoint.from_square(left, top, width, height, origin="Face Detection")) return focal_points
def detect(self, context): features = self.get_features(context) if features: for (left, top, width, height), neighbors in features: context['focal_points'].append(FocalPoint.from_square(left, top, width, height)) else: self.next(context)
async def detect(self): features = self.get_features() if features: for square, _ in features: self.context.request.focal_points.append( FocalPoint.from_square(*square)) else: await self.next() # pylint: disable=not-callable
def focal_point(self, overall_width, overall_height): bbox = self.api_resp['BoundingBox'] x = int(bbox['Left'] * overall_width) y = int(bbox['Top'] * overall_height) w = int(bbox['Width'] * overall_width) h = int(bbox['Height'] * overall_height) return FocalPoint.from_square(x, y, w, h, origin='RekognitionDetector')
def detect(self, callback): features = self.get_features() if features: for square, neighbors in features: self.context.request.focal_points.append(FocalPoint.from_square(*square)) callback() else: self.next(callback)
def config_context(context): image_w, image_h = expected.size[0], expected.size[1] point = FocalPoint.from_square(50, 50, image_w - 100, image_h - 100, origin="Face Detection") context.request.focal_points = [point]
def features_to_focal_points(cls, features): focal_points = [] for (left, top, width, height), neighbors in features: top = cls.add_hair_offset(top, height) focal_points.append( FocalPoint.from_square(left, top, width, height, origin='Face Detection')) return focal_points
def detect(self, callback): features = self.get_features() if features: for (left, top, width, height), neighbors in features: top = self.__add_hair_offset(top, height) self.context.request.focal_points.append( FocalPoint.from_square(left, top, width, height, origin="Face Detection") ) callback() else: self.next(callback)
def extract_focal(self): parts = self.parse_url(self.context.request.image_url) if parts: image, top, right, left, bottom = parts top, right, left, bottom = int(top), int(right), int(left), int(bottom) width = right - left height = bottom - top self.context.request.focal_points.append( FocalPoint.from_square(left, top, width, height, origin="Original Extraction") ) self.context.request.image_url = image
def focal(self, focal_string): parsed = self.focal_regex.match(focal_string) if parsed: left, top, right, bottom = parsed.groups() left, top, right, bottom = int(left), int(top), int(right), int(bottom) width = right - left height = bottom - top if width and height: self.context.request.focal_points.append( FocalPoint.from_square(left, top, width, height, origin="Explicit") )
def detect(self, callback): try: features = self.get_features() except Exception: logger.warn('Error during face detection; skipping to next detector') self.next(callback) return if features: for (left, top, width, height), neighbors in features: top = self.__add_hair_offset(top, height) self.context.request.focal_points.append( FocalPoint.from_square(left, top, width, height, origin="Face Detection") ) callback() else: self.next(callback)
def detect(self, context): size = context['engine'].size image_header = cv.CreateImageHeader(size, cv.IPL_DEPTH_8U, 3) cv.SetData(image_header, Image.open(StringIO(context['buffer'])).tostring()) grayscale = cv.CreateImage(size, 8, 1) cv.CvtColor(image_header, grayscale, cv.CV_BGR2GRAY) cv.EqualizeHist(grayscale, grayscale) faces = cv.HaarDetectObjects(grayscale, Detector.cascade, cv.CreateMemStorage(), 1.1, 3, cv.CV_HAAR_DO_CANNY_PRUNING, (30, 30)) if faces: for face in faces: left, top, width, height = face[0] top = self.__add_hair_offset(top, height) context['focal_points'].append(FocalPoint.from_square(left, top, width, height)) else: self.next(context)
async def detect(self): try: features = self.get_features() except Exception as error: logger.exception(error) logger.warning("Error during face detection; skipping to next detector") return await self.next() if features: for (left, top, width, height), _ in features: top = self.__add_hair_offset(top, height) self.context.request.focal_points.append( FocalPoint.from_square( left, top, width, height, origin="Face Detection" ) ) return await self.next()
async def detect(self): if not self.verify_cv(): await self.next() return features = self.get_features() if not features: await self.next() # pylint: disable=not-callable for (left, top, width, height), _ in features: offset = self.get_detection_offset(left, top, width, height) self.context.request.focal_points.append( FocalPoint.from_square( left + offset.get("left", 0.0), top + offset.get("top", 0.0), width + offset.get("right", 0.0), height + offset.get("bottom", 0.0), origin=self.get_origin(), ))
def test_new_point_square_point(self): point = FocalPoint.from_square(x=350, y=50, width=110, height=110) expect(point.x).to_equal(405) expect(point.y).to_equal(105) expect(point.weight).to_equal(12100)
def config_context(context): image_w, image_h = expected.size[0], expected.size[1] point = FocalPoint.from_square(50, 50, image_w - 100, image_h - 100, origin='Face Detection') context.request.focal_points = [point]
def topic(self): return FocalPoint.from_square(0, 300, 300, 300)
def topic(self): return FocalPoint.from_square(x=350, y=50, width=110, height=110)
def topic(self): return FocalPoint.from_square(350, 50, 110, 110)
def test_from_square(): point = FocalPoint.from_square(x=10.0, y=20.0, width=100, height=200) assert point.x == 55.0 assert point.y == 110.0 assert point.weight == 20000.0