github下载:DFace
代码中错误修改:
- 1.dface\core\models.py文件
其中PNet、RNet、ONet的 x = self.pre_layer(x),改为 x = self.pre_layer(x.float()) - 2.dface\core\image_tools.py文件
第20行的return transform(image),改为return transform(image)/255 - 3.警告
版本不同的警告
运行代码:
import cv2
from dface.core.detect import create_mtcnn_net, MtcnnDetector
import dface.core.vision as vision
def display(im_array, dets, landmarks=None):
im_array = cv2.cvtColor(im_array, cv2.COLOR_RGB2BGR)
for i in range(dets.shape[0]):
bbox = dets[i,:4]
cv2.rectangle(im_array, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255, 0, 0),2)
if landmarks is not None:
for i in range(landmarks.shape[0]):
landmarks_one = landmarks[i, :]
landmarks_one = landmarks_one.reshape((5, 2))
for j in range(5):
cv2.circle(im_array, (int(landmarks_one[j][0]),int(landmarks_one[j][1])), 2, (0,255,0), 2)
cv2.imshow('demo',im_array)
cv2.imwrite('demo.jpg',im_array)
cv2.waitKey(0)
if __name__ == '__main__':
pnet, rnet, onet = create_mtcnn_net(p_model_path="./model_store/pnet_epoch.pt", r_model_path="./model_store/rnet_epoch.pt", o_model_path="./model_store/onet_epoch.pt", use_cuda=False)
mtcnn_detector = MtcnnDetector(pnet=pnet, rnet=rnet, onet=onet, min_face_size=44)
img = cv2.imread("./000016.jpg")
w,h,c = img.shape
max_wh = max(w,h)
if max_wh >3000:
scale = max_wh/1920
img = cv2.resize(img,(int(w/scale), int(h/scale)))
img_bg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#b, g, r = cv2.split(img)
#img2 = cv2.merge([r, g, b])
bboxs, landmarks = mtcnn_detector.detect_face(img)
print(bboxs)
print(landmarks)
display(img_bg,bboxs,landmarks)
输出效果:
注: 关键点的准确性