赞
踩
无聊的发慌打算写个搭建人脸106点关键点检测模型的简易教程,包括数据集准备,模型搭建,训练以及推理测试,保证简单易懂。第一步,就是数据集的准备。我们首先想到的就是开源的人脸关键点数据集300W,WFLW等。但是开源的缺少106点的数据集,所以我们可以调用face++的api标注106关键点作为我们训练的数据集。(虽然不是很准)。
一.下载数据。
我们选用WFLW数据集的图片(商汤开源的人脸98点数据集),下面是下载链接。链接:https://pan.baidu.com/s/1XkUe-mJQmnrVjy84DkjRNQ
提取码:eq90
二.调用face++api标注人脸。
https://www.faceplusplus.com.cn/官网链接。进入官网点击右侧控制台会进入注册页面,注册一下。到控制台应用管理创建一个APIkey。创建好会有API Key和API Secret后面会有用。准备就绪后,下面直接上调用代码。代码里的Key和Secret换上你自己的。还有下载下来的图片路径换上你的路径。
- # -*- coding: utf-8 -*-
- import urllib.request
- import urllib.error
- import time
- import cv2
- import json,os
- import numpy as np
- http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
- key = ""
- secret = ""
- f=open('WFLW_106landmark.txt','a')
- t=1
- dir_path=r"D:\py\datasets\WFLW_ALL"
- img_names=os.listdir(dir_path)
- for img_name in img_names:
- # if t<6551:
- # t=t+1
- # continue
- filepath=os.path.join(dir_path,img_name)
- #filepath = "3.jpg"
-
- boundary = '----------%s' % hex(int(time.time() * 1000))
- data = []
- data.append('--%s' % boundary)
- data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key')
- data.append(key)
- data.append('--%s' % boundary)
- data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret')
- data.append(secret)
- data.append('--%s' % boundary)
- fr = open(filepath, 'rb')
- data.append('Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file')
- data.append('Content-Type: %s\r\n' % 'application/octet-stream')
- data.append(fr.read())
- fr.close()
- data.append('--%s' % boundary)
- data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_landmark')
- data.append('2')
- data.append('--%s' % boundary)
- data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes')
- data.append(
- "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus")
- data.append('--%s--\r\n' % boundary)
-
- for i, d in enumerate(data):
- if isinstance(d, str):
- data[i] = d.encode('utf-8')
-
- http_body = b'\r\n'.join(data)
-
- # build http request
- req = urllib.request.Request(url=http_url, data=http_body)
-
- # header
- req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
-
- try:
- # post data to server
- resp = urllib.request.urlopen(req, timeout=200)
- # get response
- qrcont = resp.read()
- # if you want to load as json, you should decode first,
- # for example: json.loads(qrount.decode('utf-8'))
- #print(qrcont.decode('utf-8'))
- result=json.loads(qrcont.decode('utf-8'))
- #print(1)
- #img=cv2.imread(filepath)
- for i in range(result['face_num']):
- if 'landmark' not in result['faces'][i].keys():
- continue
- landmark=result['faces'][i]['landmark']
- landmark_list=[]
- for dict_key in landmark.keys():
- #print(landmark.keys())
- landmark_list.append([landmark[dict_key]['x'],landmark[dict_key]['y']])
- #cv2.circle(img,(landmark[dict_key]['x'],landmark[dict_key]['y']),2,(0,0,255))
- landmark_array=np.array(landmark_list)
- landmark_str = ' '.join(list(map(str,landmark_array.reshape(-1).tolist())))
- label = '{} {}\n'.format(img_name, landmark_str)
- f.writelines(label)
- # cv2.namedWindow('face++ landmark',0)
- # cv2.imshow('face++ landmark',img)
- # cv2.waitKey(0)
- print(t)
- t=t+1
- except urllib.error.HTTPError as e:
- print(e.read().decode('utf-8'))
-
- f.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。