人脸识别

  首先我想描述一下,在学校的时候一直好奇人脸识别与人脸检测这个技术,之后做了很多实验,曾经使用过很多简单的算法来做人脸相似度对比,比如:夹角余弦算法、判断两个矩阵之间对应位置元素相同来做统计、直方图比对、欧氏距离、绝对值距离等等很多这种低级的实验我都做过,一次次的识别让我感到万分难过。之后我不在最求这种算法的研究了,改成了看别人如何实现的,你也实现出来就好。很多人说这是it从业者的征兆,这样我彻底相信了,因为作为一个it开发的从业工作者,不能什么东西都需要自己开发,公司的要求是尽快的把项目赶出来,才能到达利益最大化。下面我为大家带来我在学校一直实验的人脸识别实现吧。当然我是在调用别人的接口。

1、导入所需要的包

import requests
from json import JSONDecoder
import cv2

导包

2、定义请求返回数据画出人脸

def drawFace(face_rectangle,img):
width = face_rectangle['width']
top = face_rectangle['top']
left = face_rectangle['left']
height = face_rectangle['height']
start = (left, top)
end = (left + width, top + height)
color = (55, 255, 155)
thickness = 3
cv2.rectangle(img, start, end, color, thickness)

定义函数

3、准备好post请求的地址、key、password。

compare_url = "https://api-cn.faceplusplus.com/facepp/v3/compare"
key = "5Ut_EUtu3dG8Q60UBQdj8_LICgc4KByR"
secret = "cWXtsKOMx62m8zHUx810MG-0oGoOnhSO" faceId1 = "img_test/tong1.jpg" # 图片地址
faceId2 = "img_test/tong2.jpg" # 图片地址 data = {"api_key": key, "api_secret": secret}
files = {"image_file1": open(faceId1, "rb"), "image_file2": open(faceId2, "rb")}
response = requests.post(compare_url, data=data, files=files)

发送请求

4、对http返回数据进行转换提取

req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con) print(req_dict) #置信度,越高说明越像
confindence = req_dict['confidence']
print(confindence) #将人脸框出来
face_rectangle_1 = req_dict['faces1'][0]['face_rectangle']
# print(face_rectangle_1)
face_rectangle_2 = req_dict['faces2'][0]['face_rectangle']

5、画出检测对比的人脸

img1 = cv2.imread(faceId1)
img2 = cv2.imread(faceId2) if confindence>=80:
drawFace(face_rectangle_1,img1)
drawFace(face_rectangle_2,img2) #图片过大,调整下大小
img1 = cv2.resize(img1,(500,500))
img2 = cv2.resize(img2,(500,500)) cv2.imshow("img1",img1)
cv2.imshow("img2",img2) cv2.waitKey(0)
cv2.destroyAllWindows()

6、下面是完整代码:

# coding:utf-8
import requests
from json import JSONDecoder
import cv2 def drawFace(face_rectangle,img):
width = face_rectangle['width']
top = face_rectangle['top']
left = face_rectangle['left']
height = face_rectangle['height']
start = (left, top)
end = (left + width, top + height)
color = (55, 255, 155)
thickness = 3
cv2.rectangle(img, start, end, color, thickness) compare_url = "https://api-cn.faceplusplus.com/facepp/v3/compare"
key = "5Ut_EUtu3dG8Q60UBQdj8_LICgc4KByR"
secret = "cWXtsKOMx62m8zHUx810MG-0oGoOnhSO" faceId1 = "img_test/tong1.jpg" # 图片地址
faceId2 = "img_test/tong2.jpg" # 图片地址 data = {"api_key": key, "api_secret": secret}
files = {"image_file1": open(faceId1, "rb"), "image_file2": open(faceId2, "rb")}
response = requests.post(compare_url, data=data, files=files) req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con) print(req_dict) #置信度,越高说明越像
confindence = req_dict['confidence']
print(confindence) #将人脸框出来
face_rectangle_1 = req_dict['faces1'][0]['face_rectangle']
# print(face_rectangle_1)
face_rectangle_2 = req_dict['faces2'][0]['face_rectangle'] img1 = cv2.imread(faceId1)
img2 = cv2.imread(faceId2) if confindence>=80:
drawFace(face_rectangle_1,img1)
drawFace(face_rectangle_2,img2) #图片过大,调整下大小
img1 = cv2.resize(img1,(500,500))
img2 = cv2.resize(img2,(500,500)) cv2.imshow("img1",img1)
cv2.imshow("img2",img2) cv2.waitKey(0)
cv2.destroyAllWindows()

  

face++ API接口调用的更多相关文章

  1. 【redis】redis实现API接口调用调用次数的限制

    redis实现API接口调用调用次数的限制 参考地址:https://bbs.csdn.net/topics/391856106?page=1 参考地址:https://www.cnblogs.com ...

  2. [转载]android常用的API接口调用

    原文地址:android常用的API接口调用作者:宋耀 显示网页:         Uri uri = Uri.parse("http://www.google.com"); In ...

  3. Nginx api接口调用配置

    1 # Nginx api接口调用配置 2 3 # 什么是跨域同源? 4 # 同源策略:协议(http.https.wss--)+域名+端口=一个完整的网站 5 # 跨域:当前所在的网站post(ge ...

  4. 新浪网易淘宝等IP地区信息查询开放API接口调用方法

    通过IP地址获取对应的地区信息通常有两种方法:1)自己写程序,解析IP对应的地区信息,需要数据库.2)根据第三方提供的API查询获取地区信息. 第一种方法,参见文本<通过纯真IP数据库获取IP地 ...

  5. ajax跨域实现api 接口调用

    背景: 想实现跨域去调用接口, 然后同时支持下次调用,能够带cookie信息过来,同时支持来自多个源头的域名的跨域调用. 1.这样支持来自所有域名的跨域调用: 不支持跨域是,浏览器报错: 在api接口 ...

  6. JAVA的免费天气api接口调用示例

    step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...

  7. java web api接口调用

    Web Services 被W3C进行了标准化定义. Web Services 发布到网上,可以公布到某个全局注册表,自动提供服务URL,服务描述.接口调用要求.参数说明以及返回值说明.比如中国气象局 ...

  8. 小程序API接口调用

    1.在config.js中写入api接口及appkey   2.在HTTP.js中引入config.js,然后新建HTTP.js,在里进行wx.request的封装. 定义一个HTTP的类,来类里定义 ...

  9. .Net RabbitMQ实战指南——HTTP API接口调用

    RabbitMQ Management插件还提供了基于RESTful风格的HTTP API接口来方便调用.一共涉及4种HTTP方法:GET.PUT.DELETE和POST.GET方法一般用来获取如集群 ...

  10. Restful API接口调用的方法总结

    restful 接口调用的方法 https://www.cnblogs.com/taozhiye/p/6704659.html http://www.jb51.net/article/120589.h ...

随机推荐

  1. vue循环绑定v-model

    直接上代码 结构: <repayInput v-if="formData" v-for="(item, index) in formData" :isPw ...

  2. 6.1 python+appium元素定位方式(登录app)

    1.0.0     :常见的十种元素定位方式 .driver.find_element_by_id() #id定位 .driver.find_element_by_name() #name定位(已经凉 ...

  3. python3 安装win32clipboard 和 win32con 报No matching distribution found for win32con错误

    win32con.win32clipboad不能用pip install 安装,也不能够查找到这个包,原来,这个是pypiwin32的一部分,直接安装pypiwin32就可以了 pip install ...

  4. LINQ学习笔记——(3)基本查询操作符

    Select() 作用于uIEnumerable<TSource>类型 public static void Test() { List<string> persons = n ...

  5. [转载] python 解析xml 文件: SAX方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  6. c#form界面情况下显示console窗体

    使用console.write()前后加上AllocConsole()和FreeConsole()方法. 注意:如果在使用之前有console输出(不带有这两个方法),则会无效. 这两个方法: [Dl ...

  7. lintcode-115-不同的路径 II

    115-不同的路径 II "不同的路径" 的跟进问题: 现在考虑网格中有障碍物,那样将会有多少条不同的路径? 网格中的障碍和空位置分别用 1 和 0 来表示. 注意事项 m 和 n ...

  8. SQL Server 性能调优 之执行计划(Execution Plan)调优

    SQL Server 存在三种 Join 策略:Hash Join,Merge Join,Nested Loop Join. Hash Join:用来处理没有排过序/没有索引的数据,它在内存中把 Jo ...

  9. 【PHP】- session_cache_limiter(private,must-revalidate)是什么意思

    session_cache_limiter(private,must-revalidate)是什么意思 表义一: 指定会话页面所使用的缓冲控制方法: 当session_cache_limiter('p ...

  10. grpc deadlines

    最近在将应用的rpc更换为grpc,使用过程中,发现报“rpc error:code=DeadlineExceeded desc = context deadline exceeded”,这是啥?原来 ...