def delete_collection(): """Delete collection and local images.""" vrs_client = create_vrs_client() collection_id = get_collection_id(vrs_client) vrs_client.delete_collection(collection_id) save_dir = './results/{0}'.format(collection_id) if os.path.exists(save_dir): shutil.rmtree(save_dir)
def remove_face(face_id): """Remove face from face collection and delete local image. :param face_id: id of the face """ vrs_client = create_vrs_client() collection_id = get_collection_id(vrs_client) vrs_client.remove_face(collection_id, face_id) save_dir = './results/{0}/{1}.jpg'.format(collection_id, face_id) if os.path.exists(save_dir): os.remove(save_dir)
def human_detection(pil_img, file_path): """Call human detection API and show the processed image. :param pil_img: image object from PIL.image :param file_path: image file path """ vrs_client = create_vrs_client() draw = ImageDraw.Draw(pil_img) res = vrs_client.detect_humans(file_path) humans_list = res["humans"] for human in humans_list: draw_rectangle(human['location'], draw) if not os.path.exists('./results'): os.mkdir('./results') pil_img.save('./results/human_detection.jpg', quality=90)
def compare_faces(pil_img, file_path, max_results=None): """Compare face to collection. :param pil_img: image object from PIL.Image :param file_path: image file path :param max_results: a value to limit the result to be displayed """ vrs_client = create_vrs_client() collection_id = get_collection_id(vrs_client) response = vrs_client.compare_faces( file_path, collection_id, max_results) source_draw = ImageDraw.Draw(pil_img) draw_rectangle(response['source']['location'], source_draw) pil_img.save( './results/compare_face_to_{0}.jpg'.format(collection_id), quality=90) print(json.dumps(response, indent=4, separators=(',', ': ')))
def add_face(pil_img, file_path): """Add face to face collection. :param pil_img: image object from PIL.Image :param file_path: image file path """ vrs_client = create_vrs_client() draw = ImageDraw.Draw(pil_img) collection_id = get_collection_id(vrs_client) response = vrs_client.add_face(file_path, collection_id) draw_rectangle(response['location'], draw) save_dir = './results/{0}'.format(collection_id) if not os.path.exists(save_dir): os.mkdir(save_dir) pil_img.save('{0}/{1}.jpg'.format(save_dir, response['face_id']), quality=90) print(json.dumps(response, indent=4, separators=(',', ': ')))
def compare_faces(source_pil_img, target_pil_img, source_path, target_path): """Call face recognition API and show the processed image. :param source_pil_img: source image object from PIL.Image :param target_pil_img: target image object from PIL.Image :param source_path: source image file path :param target_path: target image file path """ vrs_client = create_vrs_client() resp = vrs_client.compare_faces(source_path, target_path) source_draw = ImageDraw.Draw(source_pil_img) target_draw = ImageDraw.Draw(target_pil_img) draw_rectangle(resp['source']['location'], source_draw) draw_rectangle(resp['target']['location'], target_draw) if not os.path.exists('./results'): os.mkdir('./results') source_pil_img.save('./results/face_recognition_source.jpg', quality=90) target_pil_img.save('./results/face_recognition_target.jpg', quality=90) print('score: {0}'.format(resp['score']))