(self, as_client: httpx.AsyncClient, image_url: str = "", image_content: str = "",
params_template="default")
| 34 | self._load_api(creds, headers) |
| 35 | |
| 36 | async def detect_faces(self, as_client: httpx.AsyncClient, image_url: str = "", image_content: str = "", |
| 37 | params_template="default") -> Tuple[bool, bool, VisionFaceDetection]: |
| 38 | endpoint_name = inspect.currentframe().f_code.co_name |
| 39 | |
| 40 | # image_url can cause errors with vision_api, so we prefer using image_content |
| 41 | # See => https://cloud.google.com/vision/docs/detecting-faces?#detect_faces_in_a_remote_image |
| 42 | |
| 43 | verb = "POST" |
| 44 | base_url = "/v1/images:annotate" |
| 45 | data_type = "json" # json, data or None |
| 46 | params_templates = { |
| 47 | "default": { |
| 48 | "requests":[ |
| 49 | { |
| 50 | "features": [ |
| 51 | { |
| 52 | "maxResults":100, |
| 53 | "type":"FACE_DETECTION" |
| 54 | } |
| 55 | ], |
| 56 | "image": {} |
| 57 | } |
| 58 | ] |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if not params_templates.get(params_template): |
| 63 | raise GHuntParamsTemplateError(f"The asked template {params_template} for the endpoint {endpoint_name} wasn't recognized by GHunt.") |
| 64 | |
| 65 | # Inputs checks |
| 66 | if image_url and image_content: |
| 67 | raise GHuntParamsInputError("[Vision API faces detection] image_url and image_content can't be both put at the same time.") |
| 68 | elif not image_url and not image_content: |
| 69 | raise GHuntParamsInputError("[Vision API faces detection] Please choose at least one parameter between image_url and image_content.") |
| 70 | |
| 71 | if image_url: |
| 72 | params_templates["default"]["requests"][0]["image"] = { |
| 73 | "source": { |
| 74 | "imageUri": image_url |
| 75 | } |
| 76 | } |
| 77 | elif image_content: |
| 78 | params_templates["default"]["requests"][0]["image"] = { |
| 79 | "content": image_content |
| 80 | } |
| 81 | |
| 82 | self._load_endpoint(endpoint_name) |
| 83 | req = await self._query(as_client, verb, endpoint_name, base_url, None, params_templates[params_template], data_type) |
| 84 | rate_limited = req.status_code == 429 # API Explorer sometimes rate-limit because they set their DefaultRequestsPerMinutePerProject to 1800 |
| 85 | |
| 86 | vision_face_detection = VisionFaceDetection() |
| 87 | if rate_limited: |
| 88 | return rate_limited, False, vision_face_detection |
| 89 | |
| 90 | # Parsing |
| 91 | data = json.loads(req.text) |
| 92 | if not data["responses"][0]: |
| 93 | return rate_limited, False, vision_face_detection |
no test coverage detected