Face++:
1000个全脸关键点,api,整体效果最好,
请求代码:
import base64
import io
import os
import traceback
from json import JSONDecoder
import PIL.Image as Image
import cv2
import numpy as np
import requests
import base64
from misc import is_url, download_image
API_KEY = "zYVNURhMvDx47H-f5au2YKKQKNoLRKOO"
API_SECRET = "Sme_EoOr1oOl2w1ZLFnloP206mKZrEV8"
def image_to_base64(img, ext='.png'):
retval, buf = cv2.imencode(ext, img)
return base64.b64encode(buf).decode()
def image_from_base64(img_b64):
return cv2.imdecode(np.fromstring(base64.b64decode(img_b64.encode()), np.uint8), cv2.IMREAD_UNCHANGED)
class FacePPAPI(object):
def __init__(self):
pass
def thousandlandmark(self, img):
http_url = 'https://api-cn.faceplusplus.com/facepp/v1/face/thousandlandmark'
data = {"api_key": API_KEY,
"api_secret": API_SECRET,
"return_landmark": "all",
}
files = {"image_file": self.__get_image_data(img)}
resp = requests.post(http_url, data=data, files=files, timeout=5)
req_con = resp.content.decode('utf-8')
data_dict = JSONDecoder().decode(req_con)
return data_dict
@staticmethod
def __get_image_data(img):
image_data = None
if isinstance(img, bytes):
image_data = img
elif isinstance(img, str):
if os.path.exists(img):
img_path = img
with open(img_path, "rb") as image_file:
image_data = image_file.read()
return image_data
elif is_url(img):
img_url = img
return download_image(img_url, timeout=5)
try:
image_data = base64.b64decode(img.encode())
except:
raise RuntimeError("不是base64格式字符串")
return image_data
elif type(img).__module__ == np.__name__:
retval, image_data = cv2.imencode('.jpg', img)
return image_data
elif isinstance(img, Image.Image):
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='jpg')
image_data = img_byte_arr.getvalue()
return image_data
return image_data
facepp_api = FacePPAPI()
if __name__=="__main__":
data_dir = "./images/"
out_dir= "./out/"
radius_lists = ["right_eye_pupil_radius", "left_eye_pupil_radius"]
for name in os.listdir(data_dir):
print(name)
fullname = os.path.join(data_dir, name)
image = cv2.imread(fullname)
data_dict = facepp_api.thousandlandmark(image)
for key, key_value in data_dict["face"]["landmark"].items():
for kkey, value in key_value.items():
if kkey not in radius_lists:
cv2.circle(image, (value["x"], value["y"]), 1, (255,255,255),4)
cv2.imwrite(os.path.join(out_dir,name), image)
import re
import traceback
import requests
import requests.compat
CAPITALS = re.compile('([A-Z])')
def is_url(url):
try:
res = requests.compat.urlparse(url)
if not res.scheme or not res.netloc or not "." in res.netloc:
return False
res = requests.compat.urlparse(requests.compat.urljoin(url, "/"))
if not res.scheme or not res.netloc or not "." in res.netloc:
return False
except:
return False
return True
def download_image(url, timeout=10):
image_file = None
try:
response = requests.get(url, timeout=timeout)
if response.status_code == 200:
image_file = response.content
except:
traceback.print_exc()
return image_file
TengineKit:
424个全脸关键点,c++ so,居然依赖libcurl的库,慎重使用,整体效果中等
FaceLandmark1000:
https://github.com/Single430/FaceLandmark1000
1000个全脸关键点,python,onnx,整体效果最差
最终效果对比: