1. 程式人生 > >基於python Arcface 實現人臉檢測和識別

基於python Arcface 實現人臉檢測和識別

是什麽 rop china -- {} 研究 ceo %s gin

虹軟的人臉識別技術也是很強的,重要的是他免費提供了離線的sdk,還提供了實例,這個是目前幾家研究人臉識別的大公司裏面少有的。識別能力正常用還是可以的。我這個代碼是調用的離線sdk實現的

```
from arcsoft import CLibrary, ASVL_COLOR_FORMAT, ASVLOFFSCREEN,c_ubyte_p,FaceInfo
from arcsoft.utils import BufferInfo, ImageLoader
from arcsoft.AFD_FSDKLibrary import *
from ctypes import *
import traceback
import cv2
import time


APPID = c_char_p(b‘your id‘)
FD_SDKKEY = c_char_p(b‘your key‘)
FD_WORKBUF_SIZE = 20 * 1024 * 1024
MAX_FACE_NUM = 50
bUseYUVFile = False
bUseBGRToEngine = True

def doFaceDetection(hFDEngine, inputImg): #對圖像中的人臉進行定位
faceInfo = []

pFaceRes = POINTER(AFD_FSDK_FACERES)()
ret = AFD_FSDK_StillImageFaceDetection(hFDEngine, byref(inputImg), byref(pFaceRes))
#ret 為0

if ret != 0:
print(u‘AFD_FSDK_StillImageFaceDetection 0x{0:x}‘.format(ret))
return faceInfo
faceRes = pFaceRes.contents
print(‘******‘)

facecont=faceRes.nFace #faceRes 是一個對象所以 輸出會是一個地址值 而他的一個屬性nface是表示的是人臉的個數
print(‘%d 個人臉‘ %facecont)


if faceRes.nFace > 0:
for i in range(0, faceRes.nFace):
rect = faceRes.rcFace[i]
orient = faceRes.lfaceOrient[i]
faceInfo.append(FaceInfo(rect.left,rect.top,rect.right,rect.bottom,orient))


return faceInfo

def loadImage(filePath):

inputImg = ASVLOFFSCREEN()

if bUseBGRToEngine: #true
bufferInfo = ImageLoader.getBGRFromFile(filePath)
inputImg.u32PixelArrayFormat = ASVL_COLOR_FORMAT.ASVL_PAF_RGB24_B8G8R8
inputImg.i32Width = bufferInfo.width
inputImg.i32Height = bufferInfo.height
inputImg.pi32Pitch[0] = bufferInfo.width*3
inputImg.ppu8Plane[0] = cast(bufferInfo.buffer, c_ubyte_p)
inputImg.ppu8Plane[1] = cast(0, c_ubyte_p)
inputImg.ppu8Plane[2] = cast(0, c_ubyte_p)
inputImg.ppu8Plane[3] = cast(0, c_ubyte_p)
else:
bufferInfo = ImageLoader.getI420FromFile(filePath)
inputImg.u32PixelArrayFormat = ASVL_COLOR_FORMAT.ASVL_PAF_I420
inputImg.i32Width = bufferInfo.width
inputImg.i32Height = bufferInfo.height
inputImg.pi32Pitch[0] = inputImg.i32Width
inputImg.pi32Pitch[1] = inputImg.i32Width // 2
inputImg.pi32Pitch[2] = inputImg.i32Width // 2
inputImg.ppu8Plane[0] = cast(bufferInfo.buffer, c_ubyte_p)
inputImg.ppu8Plane[1] = cast(addressof(inputImg.ppu8Plane[0].contents) + (inputImg.pi32Pitch[0] * inputImg.i32Height), c_ubyte_p)
inputImg.ppu8Plane[2] = cast(addressof(inputImg.ppu8Plane[1].contents) + (inputImg.pi32Pitch[1] * inputImg.i32Height // 2), c_ubyte_p)
inputImg.ppu8Plane[3] = cast(0, c_ubyte_p)
inputImg.gc_ppu8Plane0 = bufferInfo.buffer

return inputImg


if __name__ == u‘__main__‘:
t=time.time()
print(u‘#####################################################‘)

# init Engine
pFDWorkMem = CLibrary.malloc(c_size_t(FD_WORKBUF_SIZE))
hFDEngine = c_void_p()
ret = AFD_FSDK_InitialFaceEngine(APPID, FD_SDKKEY, pFDWorkMem, c_int32(FD_WORKBUF_SIZE), byref(hFDEngine), AFD_FSDK_OPF_0_HIGHER_EXT, 32, MAX_FACE_NUM)
#ret 為0
if ret != 0:
CLibrary.free(pFDWorkMem)
print(u‘AFD_FSDK_InitialFaceEngine ret 0x{:x}‘.format(ret))
exit(0)
#--------------------------------以上部分兩個函數以及主函數的幾條語句不變-----------------------------------------------------------

filePath = ‘001.jpg‘
inputImg = loadImage(filePath) #調用loadImage函數 返回一種格式(目前還不知道這種格式是什麽)

frame=cv2.imread(filePath)
# do Face Detect

faceInfos = doFaceDetection(hFDEngine, inputImg) #調用dofaceDetection函數 進行圖像處理檢測人臉
#print(‘faceInfos %s‘% faceInfos[0])

for i in range(0, len(faceInfos)):
rect = faceInfos[i]
print(u‘{} ({} {} {} {}) orient {}‘.format(i, rect.left, rect.top, rect.right, rect.bottom, rect.orient))
cv2.rectangle(frame, (rect.left, rect.top), (rect.right, rect.bottom), (0, 0, 255), 2)
cropimg=frame[rect.top:rect.bottom,rect.left:rect.right]# 使用opencv裁剪照片 把人臉的照片裁剪下來
cv2.imwrite(‘crop-photo/‘+str(i)+‘.jpg‘,cropimg) # 把人臉照片保存下來





AFD_FSDK_UninitialFaceEngine(hFDEngine) # release Engine
cv2.imshow(‘tuxiang‘,frame)
cv2.waitKey(1)
print(‘所用時間為{} ‘.format(time.time()-t)) #不進行保存圖片 0.12s 保存圖片0.16s
time.sleep(1)

CLibrary.free(pFDWorkMem)
print(u‘#####################################################‘)

  


運行結果
![](https://oscimg.oschina.net/oscnet/7fc55619ab96c8fae01e434bb040cb2269d.jpg)

運行時間0.14800000190734863

底層是c寫的所以運行起來還是比較快的 使用的是離線的sdk配置需要動態鏈接庫fd (官網有)

對於虹軟的這個 我只會用 裏面的代碼很大一部分都是不懂的,因為那些函數都被封裝起來了,定義看不到也看不懂。

opencv就是用來顯示照片以及標框 time用來測時間和暫停

對於虹軟的人臉識別,是使用了另一種動態鏈接庫fr,跟這個類似,代碼有些差別,等做出來基於虹軟的實時的人臉識別再分享出來。

基於python Arcface 實現人臉檢測和識別