首页 > 其他分享 >flask部署mtcnn

flask部署mtcnn

时间:2024-06-18 20:31:28浏览次数:16  
标签:right eye img flask mtcnn cv2 部署 mouth left

目录

保存检测结果

浏览器查看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)

效果展示

标签:right,eye,img,flask,mtcnn,cv2,部署,mouth,left
From: https://blog.csdn.net/JIA_NG_FA_N/article/details/139782569

相关文章

  • Stable Diffusion 3 大模型文生图“开源英雄”笔记本部署和使用教程,轻松实现AI绘图自
    备受期待的StableDiffusion3(以下亦简称SD3)如期向公众开源了(StableDiffusion3Medium),作为StabilityAI迄今为止最先进的文本生成图像的开源大模型,SD3在图像质量、文本内容生成、复杂提示理解和资源效率方面有了显著提升,被誉为AI文生图领域的开源英雄。StableDiffusion3Medi......
  • linux 部署jar包
    1.准备工作linux如果没有配置java环境变量的话,具体操作见文末推荐。2.启动jar包linux操作系统下启动jar包的方式和windows操作系统没有区别。用法都是遵循java语法规范。前提:跳转到要启动的jar包所在目录。cdjar包所在绝对路径使用默认jdk启动查看当前jdk版本java-......
  • Linux 部署
    1、安装dotnet运行时输入dotnet--info有输出就按照好运行时了sudoaptinstalldotnet-sdk-6.0https://learn.microsoft.com/zh-cn/dotnet/core/install/linux-ubuntu2、运行dotnet应用https://learn.microsoft.com/zh-cn/aspnet/core/host-and-deploy/docker/building-net-d......
  • Kubernetes 1.18 部署 Traefik2.0
    Kubernetes1.18部署Traefik2.0参考资料:Traefik2.0官方文档:https://doc.traefik.io/traefik/v2.0/Kubernetes1.18.3部署Traefik2.0:https://www.cnblogs.com/heian99/p/14608414.html1.Traefik介绍traefik是一款反向代理、负载均衡服务,使用golang实现的。和n......
  • kubernetes部署dashboard
    kubernetes部署dashboard1.简介Dashboard是基于网页的Kubernetes用户界面。你可以使用Dashboard将容器应用部署到Kubernetes集群中,也可以对容器应用排错,还能管理集群资源。你可以使用Dashboard获取运行在集群中的应用的概览信息,也可以创建或者修改Kubernetes......
  • 部署监控公司电脑桌面并截图保存至阿里云OSS(定时功能)
    importsocketimportuuidimportscheduleimportdatetimeimportpyautoguiimportloggingimportoss2importctypesimportosimporttimeimportshutil#阿里云OSS配置信息access_key_id='your_id'access_key_secret='your_secret'bucket_na......
  • 部署YUM仓库及NFS共享服务
    部署YUM仓库及NFS共享服务部署YUM软件仓库准备网络安装源(服务器端)配置软件仓库位置(客户端)使用yum工具管理软件包查询软件包安装、升级、卸载软件包NFS共享储存服务使用NFS发布共享资源在客户机中访问NFS共享资源部署YUM软件仓库借助于YUM软件仓库,可以......
  • 部署YUM仓库及NFS共享服务
    部署YUM软件仓库借助于YUM软件仓库,可以完成安装、卸载、自动升级rpm软件包等任务,能够自动找并解决rpm 包之间的依赖关系,而无须管理员逐个、手工地去安装每个rpm包,使管理员在维护大量Linux服务器时更加轻松自如。特别是在拥有大量Linux主机的本地网络中,构建一台“源”服务器可......
  • OpenStack一键安装部署与配置(全网最详细)
    一,安装环境准备(步骤比较多,建议搭建合理利用虚拟机快照)下载Linux操作系统CentOS7.9镜像:http://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso;1.创建在VMware中创建实验用的虚拟机实例。CPU2x2,(开启虚拟化引擎),内存4G以上,硬盘80G,NAT网络模式......
  • spring-boot 热部署
    1.引入依赖: <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency>2.修改pom文件中插件 3.setting设置 4.crtl+shift+alt+......