网站开发gif图太多耗资源吗,世界知名网站,浙江网站备案查询,基于php的动态校园兼职网站设计论文最近看了一篇很有意思的文章 http://matthewearl.github.io/2015/07/28/switching-eds-with-python/ #xff0c;本来想自己复现一下#xff0c;后来发现自己太菜#xff0c;用了一整天只完成了不到一半#xff0c;最近要找工作了#xff0c;看书看的有点烦#xff0c;本…最近看了一篇很有意思的文章 http://matthewearl.github.io/2015/07/28/switching-eds-with-python/ 本来想自己复现一下后来发现自己太菜用了一整天只完成了不到一半最近要找工作了看书看的有点烦本来想写个有趣的代码放松下。哎。开始正文。原作者用的是dlib的库完成关键点检测试着装了一下没装成那就不用了。本来想用自己的库后来想了下自己封装的太烂了还是改用别人的吧这样程序的大小也会小很多训练好的文件还是比较大的。首先去Face注册一个账号然后创建应用(得到访问的密钥)这里直接截图了。我用的是Python 接口去github下载SDK https://github.com/FacePlusPlus/facepp-python-sdk/tree/v2.0修改apikey.cfg的内容可以运行hello.py的历程。注意服务器的地址不要选错还有就是官方的历程里是这样的API_KEY ‘‘,替换直接的密钥时记得把也删掉不然也会报错。SDK的facepp.py文件的350行左右修改一下添加 ‘/detection/landmark‘,这句不然的话人脸关键点检测的接口无法调用。这里稍微吐槽一下Face的SDK写的真不怎么好很多地方不够详细而且程序有时会因为网络问题出现bug。想上传自己的图片也找不到接口官网只给了这么几句感觉解释的太简单了吧SDK里也没有见到有相关的本地图片上传接口。说了这么多展示一下效果然后贴代码。这是人类关键点检测的效果。由于开始选的两个脸对齐的比较好所以换了一张这里完成的效果是把第二幅图片的人脸缩放、旋转和第一张人脸对其。之后的操作就是裁剪、覆盖了。下面贴一下代码只为完成了一半(文章里写的这么多)后面的部分不太熟悉要找工作了没心情写代码。。。这里建议大家自己注册一个账号每个账号的开发者版本同时有3个线程如果我这里修改了密钥程序应该会报错这里可能程序无法运行。#!/usr/bin/env python2# -*- coding: utf-8 -*-# You need to register your App first, and enter you API key/secret.# 您需要先注册一个App并将得到的API key和API secret写在这里。API_KEY de12a860239680fb8aba7c8283efffd9API_SECRET 61MzhMy_j_L8T1-JAzVjlBSsKqy2pUap# Import system libraries and define helper functions# 导入系统库并定义辅助函数import timeimport osimport cv2import numpyimport urllibimport refrom facepp import APIALIGN_POINTS list(range(0,25))OVERLAY_POINTSlist(range(0,25))RIGHT_EYE_POINTS list(range(2, 6))LEFT_EYE_POINTS list(range(4, 8))FEATHER_AMOUNT 11SCALE_FACTOR 1COLOUR_CORRECT_BLUR_FRAC 0.6def encode(obj):if type(obj) is unicode:return obj.encode(utf-8)if type(obj) is dict:return {encode(k): encode(v) for (k, v) in obj.iteritems()}if type(obj) is list:return [encode(i) for i in obj]return objdef getPoints(text):aencode(text)astr(a)#print ax re.findall(r\x\:......,a)for i in range(len(x)):x[i]re.findall(r\d\.\d\d,x[i])y re.findall(r\y\:......,a)for i in range(len(y)):y[i]re.findall(r\d\.\d\d,y[i])xy zip(x,y)return xydef drawPoints(img,xy): #画点用于检测程序运行情况Img imgtmp numpy.array(img)h,w,c tmp.shapefor i,j in xy:xpfloat(i[0])*w/100.ypfloat(j[0])*h/100.point (int(xp),int(yp))cv2.circle(Img,point,1,(0,255,0))return Imgdef get_landmarks(path,tmpPic):result api.detection.detect(url path,mode oneface)ID result[face][0][face_id]points api.detection.landmark(face_idID,type 25p)xy getPoints(points)print downloading the picture....urllib.urlretrieve(path,tmpPic) #为防止图片内容有变化每次都下载一遍调试可以不用tmp cv2.imread(tmpPic)#测试Img drawPoints(tmp,xy)cv2.imwrite(point.jpg,Img)tmp numpy.array(tmp)h,w,c tmp.shapepoints numpy.empty([25,2],dtypenumpy.int16)n0for i,j in xy:xpfloat(i[0])*w/100.ypfloat(j[0])*h/100.points[n][0]int(xp)points[n][1]int(yp)n1return numpy.matrix([[i[0], i[1]] for i in points])#return pointsdef transformation_from_points(points1, points2):Return an affine transformation [s * R | T] such that:sum ||s*R*p1,i T - p2,i||^2is minimized.# Solve the procrustes problem by subtracting centroids, scaling by the# standard deviation, and then using the SVD to calculate the rotation. See# the following for more details:# https://en.wikipedia.org/wiki/Orthogonal_Procrustes_problempoints1 points1.astype(numpy.float64)points2 points2.astype(numpy.float64)c1 numpy.mean(points1, axis0)c2 numpy.mean(points2, axis0)points1 - c1points2 - c2s1 numpy.std(points1)s2 numpy.std(points2)points1 / s1points2 / s2U, S, Vt numpy.linalg.svd(points1.T * points2)# The R we seek is in fact the transpose of the one given by U * Vt. This# is because the above formulation assumes the matrix goes on the right# (with row vectors) where as our solution requires the matrix to be on the# left (with column vectors).R (U * Vt).Treturn numpy.vstack([numpy.hstack(((s2 / s1) * R,c2.T - (s2 / s1) * R * c1.T)),numpy.matrix([0., 0., 1.])])def warp_im(im, M, dshape):output_im numpy.zeros(dshape, dtypeim.dtype)cv2.warpAffine(im,M[:2],(dshape[1], dshape[0]),dstoutput_im,borderModecv2.BORDER_TRANSPARENT,flagscv2.WARP_INVERSE_MAP)return output_imif __name__ __main__:api API(API_KEY, API_SECRET)path1http://www.faceplusplus.com/static/img/demo/17.jpgpath2http://www.faceplusplus.com/static/img/demo/7.jpg#path2http://cimg.163.com/auto/2004/8/28/200408281055448e023.jpgtmp1./tmp1.jpgtmp2./tmp2.jpglandmarks1get_landmarks(path1,tmp1)landmarks2get_landmarks(path2,tmp2)im1 cv2.imread(tmp1,cv2.IMREAD_COLOR)im2 cv2.imread(tmp2,cv2.IMREAD_COLOR)M transformation_from_points(landmarks1[ALIGN_POINTS],landmarks2[ALIGN_POINTS])warped_im2 warp_im(im2, M, im1.shape)cv2.imwrite(wrap.jpg,warped_im2)版权声明本文为博主原创文章未经博主允许不得转载。