明天清明节了,最近在搞全连接和卷积
些许浮躁
"""
使用Mediapipe的提供的方案
"""
import cv2
import mediapipe as mp
class Face:
def __init__(self):
mpFaceMesh = mp.solutions.face_mesh
# 加载模型
self.face = mpFaceMesh.FaceMesh()
# 当前这种方式使用frame的地方比较多所以定义为成员变量
self.frame = None
def process(self, frame):
"""
识别面部关键点
"""
self.frame = frame
# 获取关键点
multi_face_landmarks = self.getLandmark()
# 没有值不需要继续执行
04-计算纵横比
if not multi_face_landmarks:
return
# 绘制样式
self.drawStyle(multi_face_landmarks)
def getLandmark(self):
"""
获取关键点
"""
face = self.face
frameRGB = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
result = face.process(frameRGB)
multi_face_landmarks = result.multi_face_landmarks
return multi_face_landmarks
def drawStyle(self, multi_face_landmarks):
"""
绘制关键点样式
"""
conn = mp.solutions.face_mesh_connections
pointStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 255, 0),
thickness=1)
lineStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 255, 0),
thickness=1)
for face_landmarks in multi_face_landmarks:
# 参数1:绘制在什么地方
# 参数2:所有的关键点
# 参数3:将那些关键点进行绘制
# 参数4:关键点的样式
# 参数5:连接线的样式
mp.solutions.drawing_utils.draw_landmarks(
self.frame,
face_landmarks,
conn.FACEMESH_RIGHT_EYE | conn.FACEMESH_LEFT_EYE |
conn.FACEMESH_LEFT_EYEBROW,
pointStyle,
lineStyle
)
def do_nms(det, boxes, confs, clss):
drop = False
if len(boxes) <= 0:
boxes.append((det[0],det[1],det[2],det[3]))
confs.append(det[4])
clss.append(det[5])
return boxes, confs, clss
for i in range(0,len(boxes)):
bbox = boxes[i]
xx1 = np.maximum(det[0], bbox[0])
yy1 = np.maximum(det[1], bbox[1])
xx2 = np.minimum(det[2], bbox[2])
yy2 = np.minimum(det[3], bbox[3])
w = np.maximum(0.0, xx2-xx1+1)
h = np.maximum(0.0, yy2-yy1+1)
area_det = (det[2]-det[0]+1)*(det[3]-det[1]+1)
area_bbox = (bbox[2]-bbox[0]+1)*(bbox[3]-bbox[1]+1)
inter = w*h
ovr = inter / (area_det + area_bbox - inter)
if ovr > 0.6 and not drop:
if det[4] > confs[i]:
boxes[i] = ((det[0],det[1],det[2],det[3]))
confs[i] = det[4]
clss[i] = det[5]
drop = True
if not drop:
boxes.append((det[0],det[1],det[2],det[3]))
confs.append(det[4])
clss.append(det[5])
return boxes, confs, clss
def _preprocess_trt(img, shape=(300, 300)):
"""Preprocess an image before TRT SSD inferencing."""
img = cv2.resize(img, shape)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.transpose((2, 0, 1)).astype(np.float32)
img *= (2.0/255.0)
img -= 1.0
return img
def _postprocess_trt(img, output, conf_th, output_layout):
"""Postprocess TRT SSD output."""
img_h, img_w, _ = img.shape
boxes, confs, clss, results = [], [], [],[]
#print(((len(output[1]))/4+1))
#print("len(outputs[0]): "+str(len(output[0]))+" len(outputs[1]): "+str(len(output[1])))
for n in range(0, int((len(output[1]))/4)):
maxScore =跟单网gendan5.com -1000.0000
maxClass = 0
for m in range(0, 4):
score = output[0][n*4+m]
#print(score)
if score < conf_th:
continue
if m <= 0:
continue
if( score > maxScore):
maxScore = score
maxClass = m
#if(maxClass < 0):
# continue
index = int(n)
if maxScore < conf_th:
continue
#print(str(output[1][n*4+0])+" "+str(output[1][n*4+1])+" "+str(output[1][n*4+2])+" "+str(output[1][n*4+3]))
x1 = int(output[1][n*4+0] * img_w)
y1 = int(output[1][n*4+1] * img_h)
x2 = int(output[1][n*4+2] * img_w)
y2 = int(output[1][n*4+3] * img_h)
det = [x1,y1,x2,y2,maxScore,maxClass,n]
boxes, confs, clss = do_nms(det, boxes, confs, clss)
return boxes, confs, clss