Python Web开发:人脸识别本地脚本测试

来自CloudWiki
跳转至: 导航搜索

实训目的

为了测试上节课Python Web开发:人脸识别后台搭建的有效性

我们在本地搭建一个测试脚本。

实训步骤

安装库

pip install requests

编写测试脚本

在项目根目录下新建test文件夹,并新建文件faceDetectDemo.py:

import cv2, requests
url = "http://localhost:8000/serviceApp/facedetect/"  # web地址(http://localhost:8000)+访问接口(facedetect)

# 上传图像并检测
tracker = None
imgPath = "face.jpg"  #图像路径
files = {
    "image": ("filename2", open(imgPath, "rb"), "image/jpeg"),
}

req = requests.post(url, data=tracker, files=files).json()
print("获取信息: {}".format(req))

# 将检测结果框显示在图像上
img = cv2.imread(imgPath)
for (w, x, y, z) in req["faces"]:
    cv2.rectangle(img, (w, x), (y, z), (0, 255, 0), 2)

cv2.imshow("face detection", img)
cv2.waitKey(0)

验证

本地运行这个脚本,

出现画面,说明测试成功:

Python21060101.png