def run(): image_root_path = Path("resources/ocr/images") json_root_path = Path("resources/ocr/json") for image_path, json_path in zip(image_root_path.glob("*"), json_root_path.glob("document_*.json")): print(image_path, json_path) with open(str(json_path), "r") as f: _json = json.load(f) response = json_format.Parse(json.dumps(_json), types.AnnotateImageResponse()) image = Image.open(image_path) bounds, confidences = get_document_bounds(response, feature=FeatureType(5), use_confidence=True) new_image = draw_boxes(image, bounds, 'yellow', confidences) filename = "symbol_{}.jpg".format(image_path.stem) new_image.save(filename)
def main(): args = get_arguments() json_file_path = Path(args.json_path) image_file_path = Path(args.image_path) ts = FeatureType(args.text_structure) with open(json_file_path, "r")as f: _json = json.load(f) # parse json response = json_format.Parse(json.dumps(_json), types.AnnotateImageResponse()) image = Image.open(image_file_path) bounds, confidences = get_document_bounds(response, feature=ts, use_confidence=True) # bounds = get_bounds(response) new_image = draw_boxes(image, bounds, 'yellow', confidences) new_image.save("result.jpg")
def get_faces(self, image_data): cache_key = self.get_cache_key_for_image(image_data) if (self.cache_provider): res = self.cache_provider.get(cache_key) if (res): print(res) cached_faces = types.AnnotateImageResponse() cached_faces.ParseFromString(res) return cached_faces.face_annotations image = types.Image(content=image_data) response = self.vision_client.face_detection(image=image) faces = response.face_annotations if (self.cache_provider): self.cache_provider.set(cache_key, response.SerializeToString()) return faces
# In[50]: # Save response to file f = open('b.txt', 'w') f.write(text_format.MessageToString(response)) f.close() # In[49]: # Read response file f = open('b.txt', 'r') resp = types.AnnotateImageResponse() # replace with your own message text_format.Parse(f.read(), resp) f.close() # In[76]: # In[79]:
def string_to_face(data): cached_faces = types.AnnotateImageResponse() cached_faces.ParseFromString(data) return cached_faces.face_annotations