当前位置:网站首页>Mtcnn face detection
Mtcnn face detection
2022-07-06 20:44:00 【gmHappy】
demo.py
import cv2
from detection.mtcnn import MTCNN
# Detect the face in the picture
def test_image(imgpath):
mtcnn = MTCNN('./mtcnn.pb')
img = cv2.imread(imgpath)
bbox, landmarks, scores = mtcnn.detect_faces(img)
print('total box:', len(bbox))
for box, pts in zip(bbox, landmarks):
box = box.astype('int32')
img = cv2.rectangle(img, (box[1], box[0]), (box[3], box[2]), (255, 0, 0), 3)
pts = pts.astype('int32')
for i in range(5):
img = cv2.circle(img, (pts[i + 5], pts[i]), 1, (0, 255, 0), 2)
cv2.imshow('image', img)
cv2.waitKey()
# Detect faces in the video
def test_camera():
mtcnn = MTCNN('./mtcnn.pb')
cap = cv2.VideoCapture('rtsp://admin:[email protected]/Streaming/Channels/1')
while True:
ret, img = cap.read()
if not ret:
break
bbox, landmarks, scores = mtcnn.detect_faces(img)
print('total box:', len(bbox), scores)
for box, pts in zip(bbox, landmarks):
box = box.astype('int32')
img = cv2.rectangle(img, (box[1], box[0]), (box[3], box[2]), (255, 0, 0), 3)
pts = pts.astype('int32')
for i in range(5):
img = cv2.circle(img, (pts[i], pts[i + 5]), 1, (0, 255, 0), 2)
cv2.imshow('img', img)
cv2.waitKey(1)
if __name__ == '__main__':
# test_image()
test_camera()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
mtcnn.py
import tensorflow as tf
from detection.align_trans import get_reference_facial_points, warp_and_crop_face
import numpy as np
import cv2
import detection.face_preprocess as face_preprocess
class MTCNN:
def __init__(self, model_path, min_size=40, factor=0.709, thresholds=[0.7, 0.8, 0.8]):
self.min_size = min_size
self.factor = factor
self.thresholds = thresholds
graph = tf.Graph()
with graph.as_default():
with open(model_path, 'rb') as f:
graph_def = tf.GraphDef.FromString(f.read())
tf.import_graph_def(graph_def, name='')
self.graph = graph
config = tf.ConfigProto(
allow_soft_placement=True,
intra_op_parallelism_threads=4,
inter_op_parallelism_threads=4)
config.gpu_options.allow_growth = True
self.sess = tf.Session(graph=graph, config=config)
self.refrence = get_reference_facial_points(default_square=True)
# Face detection
def detect_faces(self, img):
feeds = {
self.graph.get_operation_by_name('input').outputs[0]: img,
self.graph.get_operation_by_name('min_size').outputs[0]: self.min_size,
self.graph.get_operation_by_name('thresholds').outputs[0]: self.thresholds,
self.graph.get_operation_by_name('factor').outputs[0]: self.factor
}
fetches = [self.graph.get_operation_by_name('prob').outputs[0],
self.graph.get_operation_by_name('landmarks').outputs[0],
self.graph.get_operation_by_name('box').outputs[0]]
prob, landmarks, box = self.sess.run(fetches, feeds)
return box, landmarks, prob
# Align to get a single face
def align_face(self, img):
ret = self.detect_faces(img)
if ret is None:
return None
bbox, landmarks, prob = ret
if bbox.shape[0] == 0:
return None
landmarks_copy = landmarks.copy()
landmarks[:, 0:5] = landmarks_copy[:, 5:10]
landmarks[:, 5:10] = landmarks_copy[:, 0:5]
# print(landmarks[0, :])
bbox = bbox[0, 0:4]
bbox = bbox.astype(int)
bbox = bbox[::-1]
bbox_copy = bbox.copy()
bbox[0:2] = bbox_copy[2:4]
bbox[2:4] = bbox_copy[0:2]
# print(bbox)
points = landmarks[0, :].reshape((2, 5)).T
# print(points)
'''
face_img = cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 6)
for i in range(5):
pts = points[i, :]
face_img = cv2.circle(face_img, (pts[0], pts[1]), 2, (0, 255, 0), 2)
cv2.imshow('img', face_img)
if cv2.waitKey(100000) & 0xFF == ord('q'):
cv2.destroyAllWindows()
'''
warped_face = face_preprocess.preprocess(img, bbox, points, image_size='112,112')
'''
cv2.imshow('face', warped_face)
if cv2.waitKey(100000) & 0xFF == ord('q'):
cv2.destroyAllWindows()
'''
# warped_face = cv2.cvtColor(warped_face, cv2.COLOR_BGR2RGB)
# aligned = np.transpose(warped_face, (2, 0, 1))
# return aligned
return warped_face
# Align to get multiple faces
def align_multi_faces(self, img, limit=None):
boxes, landmarks, _ = self.detect_faces(img)
if limit:
boxes = boxes[:limit]
landmarks = landmarks[:limit]
landmarks_copy = landmarks.copy()
landmarks[:, 0:5] = landmarks_copy[:, 5:10]
landmarks[:, 5:10] = landmarks_copy[:, 0:5]
# print('landmarks', landmark)
faces = []
for idx in range(len(landmarks)):
'''
landmark = landmarks[idx, :]
facial5points = [[landmark[j], landmark[j + 5]] for j in range(5)]
warped_face = warp_and_crop_face(np.array(img), facial5points, self.refrence, crop_size=(112, 112))
faces.append(warped_face)
'''
bbox = boxes[idx, 0:4]
bbox = bbox.astype(int)
bbox = bbox[::-1]
bbox_copy = bbox.copy()
bbox[0:2] = bbox_copy[2:4]
bbox[2:4] = bbox_copy[0:2]
# print(bbox)
points = landmarks[idx, :].reshape((2, 5)).T
# print(points)
warped_face = face_preprocess.preprocess(img, bbox, points, image_size='112,112')
cv2.imshow('faces', warped_face)
# warped_face = cv2.cvtColor(warped_face, cv2.COLOR_BGR2RGB)
# aligned = np.transpose(warped_face, (2, 0, 1))
faces.append(warped_face)
# print('faces',faces)
# print('boxes',boxes)
return faces, boxes, landmarks
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
边栏推荐
- Spiral square PTA
- 01 基础入门-概念名词
- 知识图谱构建流程步骤详解
- 1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
- Application layer of tcp/ip protocol cluster
- Variable star --- article module (1)
- Comment faire une radio personnalisée
- Continuous test (CT) practical experience sharing
- Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
- Spark SQL chasing Wife Series (initial understanding)
猜你喜欢
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
[DSP] [Part 1] start DSP learning
02 basic introduction - data package expansion
Maximum likelihood estimation and cross entropy loss
请问sql group by 语句问题
Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model
Utilisation de l'écran OLED
How does kubernetes support stateful applications through statefulset? (07)
Error analysis ~csdn rebound shell error
【每周一坑】计算100以内质数之和 +【解答】输出三角形
随机推荐
Unity load AB package
2022 portal crane driver registration examination and portal crane driver examination materials
看过很多教程,却依然写不好一个程序,怎么破?
recyclerview gridlayout 平分中间空白区域
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
Value of APS application in food industry
【微信小程序】運行機制和更新機制
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
小孩子學什麼編程?
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
2110 summary of knowledge points and common problems in redis class
Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
解剖生理学复习题·VIII血液系统
How does kubernetes support stateful applications through statefulset? (07)
8086 instruction code summary (table)
Boder radius has four values, and boder radius exceeds four values
C language operators