flask部署mtcnn

目录

保存检测结果

浏览器查看nginx 

url图片检测人脸 

Flask hello-world

 Flask+mtcnn

python调flask+mtcnn 


示例图片:

打印人脸检测信息 

import cv2
from mtcnn.mtcnn import MTCNN

img = cv2.cvtColor(cv2.imread('./face.png'), cv2.COLOR_BGR2RGB)

detector = MTCNN()
faces = detector.detect_faces(img)

print(faces)

打印结果

[{'box': [283, 295, 43, 49], 'confidence': 0.9999926090240479, 'keypoints': {'left_eye': (297, 311), 'right_eye': (317, 311), 'nose': (308, 322), 'mouth_left': (299, 332), 'mouth_right': (316, 332)}}, {'box': [748, 436, 49, 54], 'confidence': 0.9999852180480957, 'keypoints': {'left_eye': (765, 456), 'right_eye': (786, 456), 'nose': (776, 467), 'mouth_left': (767, 477), 'mouth_right': (783, 477)}}, {'box': [450, 426, 46, 52], 'confidence': 0.9999797344207764, 'keypoints': {'left_eye': (461, 446), 'right_eye': (482, 443), 'nose': (471, 454), 'mouth_left': (467, 467), 'mouth_right': (481, 466)}}, {'box': [729, 246, 45, 49], 'confidence': 0.99997878074646, 'keypoints': {'left_eye': (743, 263), 'right_eye': (763, 263), 'nose': (754, 273), 'mouth_left': (746, 283), 'mouth_right': (761, 283)}}, {'box': [886, 437, 51, 56], 'confidence': 0.9999725818634033, 'keypoints': {'left_eye': (901, 456), 'right_eye': (924, 457), 'nose': (912, 470), 'mouth_left': (903, 480), 'mouth_right': (921, 481)}}, {'box': [968, 275, 43, 53], 'confidence': 0.9999260902404785, 'keypoints': {'left_eye': (979, 296), 'right_eye': (999, 299), 'nose': (987, 307), 'mouth_left': (976, 315), 'mouth_right': (994, 318)}}, {'box': [1076, 116, 53, 65], 'confidence': 0.9999237060546875, 'keypoints': {'left_eye': (1087, 141), 'right_eye': (1110, 139), 'nose': (1095, 150), 'mouth_left': (1089, 167), 'mouth_right': (1108, 165)}}, {'box': [294, 427, 47, 55], 'confidence': 0.9999179840087891, 'keypoints': {'left_eye': (309, 448), 'right_eye': (332, 449), 'nose': (321, 460), 'mouth_left': (310, 470), 'mouth_right': (329, 470)}}, {'box': [1033, 443, 48, 55], 'confidence': 0.9999170303344727, 'keypoints': {'left_eye': (1048, 462), 'right_eye': (1071, 461), 'nose': (1061, 475), 'mouth_left': (1051, 485), 'mouth_right': (1069, 484)}}, {'box': [502, 292, 43, 52], 'confidence': 0.999904990196228, 'keypoints': {'left_eye': (513, 313), 'right_eye': (534, 312), 'nose': (524, 322), 'mouth_left': (516, 333), 'mouth_right': (534, 332)}}, {'box': [599, 434, 49, 57], 'confidence': 0.999863862991333, 'keypoints': {'left_eye': (614, 453), 'right_eye': (636, 454), 'nose': (625, 464), 'mouth_left': (617, 477), 'mouth_right': (631, 478)}}, {'box': [143, 110, 56, 71], 'confidence': 0.9998359680175781, 'keypoints': {'left_eye': (160, 136), 'right_eye': (186, 136), 'nose': (175, 150), 'mouth_left': (163, 162), 'mouth_right': (184, 162)}}, {'box': [626, 251, 47, 55], 'confidence': 0.999794065952301, 'keypoints': {'left_eye': (640, 272), 'right_eye': (660, 272), 'nose': (650, 284), 'mouth_left': (641, 294), 'mouth_right': (657, 294)}}, {'box': [157, 253, 55, 67], 'confidence': 0.999727189540863, 'keypoints': {'left_eye': (175, 278), 'right_eye': (199, 278), 'nose': (188, 291), 'mouth_left': (175, 303), 'mouth_right': (198, 304)}}, {'box': [1192, 197, 56, 79], 'confidence': 0.9995760321617126, 'keypoints': {'left_eye': (1206, 230), 'right_eye': (1231, 230), 'nose': (1217, 247), 'mouth_left': (1206, 257), 'mouth_right': (1231, 256)}}, {'box': [383, 301, 41, 50], 'confidence': 0.9991057515144348, 'keypoints': {'left_eye': (396, 319), 'right_eye': (415, 318), 'nose': (406, 330), 'mouth_left': (397, 339), 'mouth_right': (414, 339)}}, {'box': [861, 272, 45, 54], 'confidence': 0.9945133924484253, 'keypoints': {'left_eye': (874, 293), 'right_eye': (893, 292), 'nose': (884, 305), 'mouth_left': (877, 313), 'mouth_right': (891, 313)}}]

保存检测结果

import cv2
from mtcnn.mtcnn import MTCNN

img = cv2.cvtColor(cv2.imread('./face.png'), cv2.COLOR_BGR2RGB)

detector = MTCNN()
faces = detector.detect_faces(img)

for i in faces:
    x,y,w,h = i['box']
    cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
    cv2.putText(img,'{:.1f}'.format(i['confidence']),(x,y-4),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),2)
    for _,v in i['keypoints'].items():
        cv2.circle(img,(v[0],v[1]),3,(255,0,0),3)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite('result.jpg', img)

效果图

浏览器查看nginx 

url图片检测人脸 

 

import cv2
from mtcnn.mtcnn import MTCNN
import requests
import numpy as np

url = 'http://192.168.31.198:8080/00000125.jpg'
r = requests.get(url)

buffer_np = np.frombuffer(r.content, np.uint8)
img = cv2.imdecode(buffer_np, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

detector = MTCNN()
faces = detector.detect_faces(img)

print(faces)

打印结果

[{'box': [248, 56, 58, 76], 'confidence': 0.9995517134666443, 'keypoints': {'left_eye': (261, 85), 'right_eye': (289, 86), 'nose': (271, 99), 'mouth_left': (262, 115), 'mouth_right': (283, 115)}}]

Flask hello-world

from flask import Flask
import requests
import os

app = Flask(__name__)

@app.route('/')
def hi():
    
    return 'hello world'

app.run(host='0.0.0.0',port=9986)

 Flask+mtcnn

代码示例

from flask import Flask
import requests
import os
import cv2
import numpy as np
from mtcnn import MTCNN

app = Flask(__name__)
model = MTCNN()

def inference(imgName):
    # url = 'http://192.168.31.198:8080/00000125.jpg'
    url = 'http://192.168.31.198:8080/'+imgName
    r = requests.get(url)

    buffer_np = np.frombuffer(r.content, np.uint8)
    img = cv2.imdecode(buffer_np, cv2.IMREAD_COLOR)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    faces = model.detect_faces(img)
    return {'face_result':faces}

@app.route('/<name>')
def hi(name):
    result = inference(name)
    return result

app.run(host='0.0.0.0',port=9986)

python调flask+mtcnn 

import requests

response = requests.get('http://192.168.31.198:9986/00000125.jpg')
print(response.text)

'--------以下是测试人脸检测效果--不写---'
import numpy as np
import cv2
import json

url = 'http://192.168.31.198:8080/00000125.jpg'
r = requests.get(url)
buffer_np = np.frombuffer(r.content, np.uint8)
img = cv2.imdecode(buffer_np, cv2.IMREAD_COLOR)
result = json.loads(response.text)

for i in result['face_result']:
    x,y,w,h = i['box']
    cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
    cv2.putText(img,'{:.1f}'.format(i['confidence']),(x,y-4),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),2)
    for _,v in i['keypoints'].items():
        cv2.circle(img,(v[0],v[1]),3,(255,0,0),3)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite('test.jpg', img)

效果展示

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值