人脸识别

  首先我想描述一下,在学校的时候一直好奇人脸识别与人脸检测这个技术,之后做了很多实验,曾经使用过很多简单的算法来做人脸相似度对比,比如:夹角余弦算法、判断两个矩阵之间对应位置元素相同来做统计、直方图比对、欧氏距离、绝对值距离等等很多这种低级的实验我都做过,一次次的识别让我感到万分难过。之后我不在最求这种算法的研究了,改成了看别人如何实现的,你也实现出来就好。很多人说这是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. 【多校联合】(HDU6045)Is Derek lying?

    分析 之前没有想到题目解法,看了题解才会,记录一下思考过程. 这条题目的实质是,在满足合法的情况下,有没有a和d的可行解?也就是说,不要仅仅附在表面的思考逻辑条件,而是要思考实际的数学表达. 转化为数 ...

  2. Python目录链接

    第1章 就这么愉快的开始吧 课时1:我和python的第一次亲密接触 一.Python3的下载与安装 二.从IDIE启动Python 三.尝试点新的东西 四.为什么会这样? 五.课时01课后习题及答案 ...

  3. 有向图的强连通分量——kosaraju算法

    一.前人种树 博客:Kosaraju算法解析: 求解图的强连通分量

  4. UITableView性能优化【本文摘自智车芯官网】

    UITableView是个表格视图,可以在表格行空间中添加多个子控件,UITableView继承了UIScrollView,默认状态下可以堆单元格进行滚动,所有的UITableViewControll ...

  5. HDU 1338 Game Prediction

    http://acm.hdu.edu.cn/showproblem.php?pid=1338 Problem Description Suppose there are M people, inclu ...

  6. PHP整数取余返回负数解决办法

    <?php $num1 = 1494313163777; $num2 = 9999; //直接计算取余会出错,出现负数 -8779 //echo $num1 % $num2;exit; //算上 ...

  7. Jboss提示:Server already running on localhost

    最近在做项目中,经常遇到JBoss报如下提示:Server already running on localhost.这时Jboss显示已启动,但页面显示不出来.提示中给出了两种解决办法,运行新的服务 ...

  8. c# 调用 matlab 引发初始化错误 异常

    1. 除了matlab 编译的DLL 意外还需要引用 MWArray.dll 这个dill 在安装了  MCRInstaller.exe(matlab运行环境之后就会有了): 2. 最重要的一点.ne ...

  9. elasticsearch集群及filebeat server和logstash server

    elasticsearch集群及filebeat server和logstash server author:JevonWei版权声明:原创作品blog:http://119.23.52.191/ 实 ...

  10. jQuery插件jquery.fullPage.js

    简介如今我们经常能看到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或者色块做背景,再添加一些简单的内容,显得格外的高端大气上档次,比如 iPone 5C 的介绍页面.QQ浏览器的官方网站.百度史 ...