经过多天学习,终于了解了python如何调用于老师提供的人脸检测动态库,感觉检测速度很快,CPU占用率在50%左右(09年的神舟笔记本),本着开源的精神,与大家坦诚交流的态度,现将源码放出,一起学习,不足之处多指正。
---------------------------------------------------------------------------帅气的分割线---------------------------------------------------------------------------------
def detectFaceC(image):
# 调用Dll动态链接库
import ctypes
DETECT_BUFFER_SIZE = 0x20000 # 设置缓存大小
sBuf = b'a'*DETECT_BUFFER_SIZE # 设置缓存,用于存储识别数据
pStr = ctypes.cast(sBuf, ctypes.POINTER(ctypes.c_short)) # 缓存指针,用于读取缓存数据
dll = ctypes.CDLL('libfacedetect.dll') # 调用脸部识别动太链接库
# 动态链接库功能调用方法,有三种:1. dll.funname() 2. fun=getattr(dll, '?funname@HYY***@@Z') 3. dll[num]
# 不同库可能调用的方法不一样,下面是这个库的调用方法。
# facedetect_front = dll[1] # 脸检测, 无检测角度
# facedetect_frontal_surveillance = dll[2] # 暗光线下检测,无检测角度
# facedetect_multiview = dll[3] # 脸部角度检测
# facedetect_multiview_reinforce = dll[4] # 加强版脸部角度检测
imggray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
row, col = imggray.shape[:2]
doLandmark = 1
result = dll[1](sBuf, imggray.ctypes.data, col, row, col, ctypes.c_float(1.2), 2, 48, 0, doLandmark)
for i in range(pStr[0]):
p = 2 + 142*i
# 显示脸部坐标,neighbors,angle
print('x1:',pStr[p], 'y1:', pStr[p+1], 'x2:', pStr[p]+pStr[p+2], 'y2:', pStr[p+1]+pStr[p+3], 'neighbors:', pStr[p+4], 'angle:', pStr[p+5])
cv2.rectangle(image, (pStr[p], pStr[p+1]), (pStr[p]+pStr[p+2], pStr[p+1]+pStr[p+3]), (0,0,255), 2)
if doLandmark:
for j in range(68):
# circle(result_frontal, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0))
cv2.circle(image, (pStr[p+6+2*j], pStr[p+6+2*j+1]), 2, (0,255,0))
return image