主页: 智云视图
FaceBoxes 完整复现.
论文: FaceBoxes: A CPU Real-time Face Detector with High Accuracy - 2018
1. FaceBoxes 测试
Caffemodel - FaceBoxes_1024x1024.caffemodel (3.6M)
Prototxt - faceboxes_deploy.prototxt
Caffe - [caffe-ssd]()
# -*- coding: utf-8 -*
import numpy as np
import matplotlib.pyplot as plt
import sys,os
import cv2
caffe_root = '/path/to/caffe-ssd/'
sys.path.insert(0, caffe_root + 'python')
import caffe
import time
net_file= 'faceboxes_deploy.prototxt'
caffe_model='FaceBoxes_1024x1024.caffemodel'
caffe.set_mode_cpu() # cpu
# caffe.set_mode_gpu() # gpu
net = caffe.Net(net_file,caffe_model,caffe.TEST)
CLASSES = ('background', 'face')
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.array([104, 117, 123])) # mean pixel
def preprocess(src):
img = cv2.resize(src, (1024,1024))
img = img - 127.5
img = img * 0.007843
return img
def postprocess(img, out):
h = img.shape[0]
w = img.shape[1]
box = out['detection_out'][0,0,:,3:7] * np.array([w, h, w, h])
cls = out['detection_out'][0,0,:,1]
conf = out['detection_out'][0,0,:,2]
return (box.astype(np.int32), conf, cls)
def detect(imgfile):
origimg = cv2.imread(imgfile)
transformed_image = transformer.preprocess('data', origimg)
net.blobs['data'].data[...] = transformed_image
time_start = time.time()
out = net.forward()
print("[INFO]timecost: ", time.time() - time_start)
box, conf, cls = postprocess(origimg, out)
for i in range(len(box)):
p1 = (box[i][0], box[i][1])
p2 = (box[i][2], box[i][3])
cv2.rectangle(origimg, p1, p2, (0, 255, 0))
p3 = (max(p1[0], 15), max(p1[1], 15))
title = "%s:%.2f" % (CLASSES[int(cls[i])], conf[i])
cv2.putText(origimg, title, p3, cv2.FONT_ITALIC, 0.6, (0, 255, 0), 1)
plt.imshow(origimg[:,:,::-1])
plt.axis("off")
plt.show()
return True
if __name__ == '__main__':
test_dir = "/path/to/images"
for img_name in os.listdir(test_dir):
img_file = os.path.join(test_dir, img_name)
result = detect(img_file)
print("[INFO]Done.")
如(会出现漏检):
('[INFO]cpu timecost: ', 0.1766371726989746)
('[INFO]cpu timecost: ', 0.13269805908203125)
('[INFO]gpu timecost: ', 0.024658203125)
('[INFO]gpu timecost: ', 0.01454019546508789)