基于python、虹软实现人脸检测,人脸识别
生活随笔
收集整理的這篇文章主要介紹了
基于python、虹软实现人脸检测,人脸识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
虹軟的人臉識別技術也是很強的,重要的是他免費提供了離線的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 timeAPPID = 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 = Truedef doFaceDetection(hFDEngine, inputImg): #對圖像中的人臉進行定位faceInfo = []pFaceRes = POINTER(AFD_FSDK_FACERES)()ret = AFD_FSDK_StillImageFaceDetection(hFDEngine, byref(inputImg), byref(pFaceRes)) #ret 為0if ret != 0:print(u'AFD_FSDK_StillImageFaceDetection 0x{0:x}'.format(ret))return faceInfofaceRes = pFaceRes.contentsprint('******')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 faceInfodef loadImage(filePath):inputImg = ASVLOFFSCREEN()if bUseBGRToEngine: #truebufferInfo = ImageLoader.getBGRFromFile(filePath)inputImg.u32PixelArrayFormat = ASVL_COLOR_FORMAT.ASVL_PAF_RGB24_B8G8R8inputImg.i32Width = bufferInfo.widthinputImg.i32Height = bufferInfo.heightinputImg.pi32Pitch[0] = bufferInfo.width*3inputImg.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_I420inputImg.i32Width = bufferInfo.widthinputImg.i32Height = bufferInfo.heightinputImg.pi32Pitch[0] = inputImg.i32WidthinputImg.pi32Pitch[1] = inputImg.i32Width // 2inputImg.pi32Pitch[2] = inputImg.i32Width // 2inputImg.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.bufferreturn inputImgif __name__ == u'__main__':t=time.time()print(u'#####################################################')# init EnginepFDWorkMem = 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 為0if 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 DetectfaceInfos = 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 Enginecv2.imshow('tuxiang',frame)cv2.waitKey(1)print('所用時間為{} '.format(time.time()-t)) #不進行保存圖片 0.12s 保存圖片0.16stime.sleep(1)CLibrary.free(pFDWorkMem)print(u'#####################################################')運行結果
運行時間0.14800000190734863???
底層是c寫的所以運行起來還是比較快的? 使用的是離線的sdk配置需要動態鏈接庫fd (官網有)
對于虹軟的這個 我只會用 里面的代碼很大一部分都是不懂的,因為那些函數都被封裝起來了,定義看不到也看不懂。
?opencv就是用來顯示照片以及標框? time用來測時間和暫停
對于虹軟的人臉識別,是使用了另一種動態鏈接庫fr,跟這個類似,代碼有些差別,等做出來基于虹軟的實時的人臉識別再分享出來。
轉載于:https://my.oschina.net/u/3970172/blog/3032564
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的基于python、虹软实现人脸检测,人脸识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: server2008r2/2012R2遠
- 下一篇: 「Luogu1552」[APIO2012