flask部署mtcnn

ops/2024/10/19 7:32:36/

目录

 打印人脸检测信息

输出结果

 保存检测结果

nginx%EF%BC%88nginx%E9%85%8D%E7%BD%AE%E8%BF%99%E9%87%8C%E5%B0%B1%E4%B8%8D%E5%A4%9A%E4%BB%8B%E7%BB%8D%E4%BA%86%EF%BC%89-toc" style="margin-left:0px;">浏览器查看nginxnginx配置这里就不多介绍了)

 url图片检测人脸

输出结果

Flask hello-world

Flask+mtcnn

python%E8%B0%83flask%2Bmtcnn-toc" style="margin-left:0px;">pythonflask+mtcnn


 打印人脸检测信息

import cv2
from mtcnn.mtcnn import MTCNNimg = 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 MTCNNimg = 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%EF%BC%88nginx%E9%85%8D%E7%BD%AE%E8%BF%99%E9%87%8C%E5%B0%B1%E4%B8%8D%E5%A4%9A%E4%BB%8B%E7%BB%8D%E4%BA%86%EF%BC%89">浏览器查看nginxnginx配置这里就不多介绍了)

 url图片检测人脸

import cv2
from mtcnn.mtcnn import MTCNN
import requests
import numpy as npurl = '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 osapp = 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 MTCNNapp = Flask(__name__)
model = MTCNN()def inference(imgName):# url = 'http://192.168.31.198:8080/00000125.jpg'url = 'http://192.168.31.198:8080/'+imgNamer = 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 resultapp.run(host='0.0.0.0',port=9986)

python%E8%B0%83flask%2Bmtcnn">pythonflask+mtcnn

import requestsresponse = requests.get('http://192.168.31.198:9986/00000125.jpg')
print(response.text)import numpy as np
import cv2
import jsonurl = '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)


http://www.ppmy.cn/ops/48667.html

相关文章

jenkins使用注意问题

1.在编写流水线时并不知道当前处在哪个目录&#xff0c;导致名使用不当&#xff0c;以及文件位置不清楚 流水线任务默认路径是&#xff0c;test4_mvn为jenkins任务名 [Pipeline] sh (hide)pwd /var/jenkins_home/workspace/test4_mvn maven任务也是&#xff0c;看来是一样的…

2024高校网络安全管理运维赛题目--复现+题目+wp

比赛官网 比赛官网 部分writeup Signin 如图所示GIF提取&#xff0c;然后简单的ROT13 flag{welcome-to-signin-quiz} 邮件 ----如图所示简单的base64 邮件flag{WeLCoMeto} 邮箱flag{phishHUntInG} Babyre 解析&#xff1a;放到IDA分析&#xff0c;看伪代码 得到AncsA6g…

Qt系统相关

本文目录 1.Qt事件事件的处理标签事件鼠标事件滚轮事件按键事件定时器事件窗口事件事件派发器 2.Qt文件操作QFile的基本使用 3.Qt多线程使用线程线程锁connect的第五个参数 条件变量和信号量 4.Qt网络编程UDP SocketTCP SocketQTcpServerQTcpSocket HTTP的编写 5.QT多媒体播放音…

springboot-自动配置

一、自动配置的原理 Spring Boot 的自动配置基于以下几个核心概念&#xff1a; 条件注解 (Conditional Annotations)&#xff1a;Conditional 系列注解用于根据特定条件判断是否加载某个配置类或 Bean。 自动配置类 (Auto-configuration Classes)&#xff1a;这些类通过 META-…

【计算机网络】个人学习笔记——第六章 应用层:域名系统DNSWWW万维网FTPHTTPSMTPPOP3DHCP

文章目录 第六章 应用层一、应用层概述1.应用层的功能2.应用层的重要协议3.应用层的常见模型 二、域名系统DNS【重点】1.DNS域名解析系统概述2.域名结构3.域名类型4.域名服务器5.域名解析方式6.域名查询过程的理解 三、文件传输协议【重点】1.文本传送协议FTP2.简单文件传送协议…

背景渐变动画登录页

b站视频演示效果: 效果图: 完整代码: <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>背景…

list容器的基本使用

目录 前言一&#xff0c;list的介绍二&#xff0c;list的基本使用2.1 list的构造2.2 list迭代器的使用2.3 list的头插&#xff0c;头删&#xff0c;尾插和尾删2.4 list的插入和删除2.5 list 的 resize/swap/clear 前言 list中的接口比较多&#xff0c;与string和vector类似&am…

如何在 Windows 10/11 上编辑 PDF [4 种简单方法]

PDF 在大多数设备上都易于查看&#xff0c;但由于其设计用于查看&#xff0c;因此编辑起来可能比较棘手。编辑 PDF 可能比编辑 Microsoft Office 文档更具挑战性。 不用担心&#xff0c;我们已经为你做好了准备。无论你是想添加、删除还是插入文本或图片&#xff0c;你都可以使…