def save_image(self, image, people, faces, directory): """Save a timestamped image with bounding boxes around targets.""" img = Image.open(io.BytesIO(bytearray(image))).convert("RGB") draw = ImageDraw.Draw(img) for person in people: box = hound.bbox_to_tf_style(person["boundingBox"], self._image_width, self._image_height) draw_box(draw, box, self._image_width, self._image_height, color=RED) for face in faces: age = str(face["age"]) gender = face["gender"] face_description = f"{gender}_{age}" bbox = hound.bbox_to_tf_style(face["boundingBox"], self._image_width, self._image_height) draw_box( draw, bbox, self._image_width, self._image_height, text=face_description, color=RED, ) latest_save_path = directory + "{}_latest.jpg".format(self._name) out_file = open(latest_save_path, "wb") img.save(out_file, format="JPEG") out_file.flush() os.fsync(out_file) out_file.close() self.fire_saved_file_event(latest_save_path) if self._save_timestamped_file: timestamp_save_path = directory + "{} {}.jpg".format( self._name, self._last_detection) out_file = open(timestamp_save_path, "wb") img.save(out_file, format="JPEG") out_file.flush() os.fsync(out_file) out_file.close() self.fire_saved_file_event(timestamp_save_path) _LOGGER.info("Saved %s", timestamp_save_path)
def test_bbox_to_tf_style(): bbox = {"x": 227, "y": 133, "height": 245, "width": 125} img_width = 960 img_height = 480 assert hound.bbox_to_tf_style(bbox, img_width, img_height) == ( 0.27708, 0.23646, 0.7875, 0.36667, )
def fire_person_detected_event(self, person): """Send event with detected total_persons.""" self.hass.bus.fire( EVENT_PERSON_DETECTED, { ATTR_ENTITY_ID: self.entity_id, ATTR_BOUNDING_BOX: hound.bbox_to_tf_style( person["boundingBox"], self._image_width, self._image_height ), }, )
def fire_face_detected_event(self, face): """Send event with detected total_persons.""" self.hass.bus.fire( EVENT_FACE_DETECTED, { ATTR_ENTITY_ID: self.entity_id, ATTR_BOUNDING_BOX: hound.bbox_to_tf_style(face["boundingBox"], self._image_width, self._image_height), ATTR_AGE: face["age"], ATTR_GENDER: face["gender"], }, )
def save_image(self, image, people, directory): """Save a timestamped image with bounding boxes around targets.""" try: img = Image.open(io.BytesIO(bytearray(image))).convert("RGB") except UnidentifiedImageError: _LOGGER.warning("Sighthound unable to process image, bad data") return draw = ImageDraw.Draw(img) for person in people: box = hound.bbox_to_tf_style(person["boundingBox"], self._image_width, self._image_height) draw_box(draw, box, self._image_width, self._image_height) latest_save_path = directory / f"{self._name}_latest.jpg" img.save(latest_save_path) if self._save_timestamped_file: timestamp_save_path = directory / f"{self._name}_{self._last_detection}.jpg" img.save(timestamp_save_path) _LOGGER.info("Sighthound saved file %s", timestamp_save_path)