python3+虹軟2.0的所有功能整合測試完成,並對虹軟所有功能進行了封裝,現提供demo
主要功能,
1.人臉識別
2.人臉特徵提取
3.特徵比對
4.特徵資料儲存與比對
其他特徵沒有新增
face_class.py
複製程式碼 from ctypes import * #人臉框 class MRECT(Structure): _fields_=[(u`left1`,c_int32),(u`top1`,c_int32),(u`right1`,c_int32),(u`bottom1`,c_int32)] #版本資訊 版本號,構建日期,版權說明 class ASF_VERSION(Structure): _fields_=[(`Version`,c_char_p),(`BuildDate`,c_char_p),(`CopyRight`,c_char_p)] #單人人臉資訊 人臉狂,人臉角度 class ASF_SingleFaceInfo(Structure): _fields_=[(`faceRect`,MRECT),(`faceOrient`,c_int32)] #多人人臉資訊 人臉框陣列,人臉角度陣列,人臉數 class ASF_MultiFaceInfo(Structure): # _fields_=[(`faceRect`,POINTER(MRECT)),(`faceOrient`,POINTER( c_int32)),(`faceNum`,c_int32)] _fields_=[(u`faceRect`,POINTER(MRECT)),(u`faceOrient`,POINTER(c_int32)),(u`faceNum`, c_int32)] # _fields_=[(u`faceRect`,MRECT*50),(u`faceOrient`,c_int32*50),(u`faceNum`,c_int32)] #人臉特徵 人臉特徵,人臉特徵長度 class ASF_FaceFeature(Structure): _fields_=[(`feature`,c_void_p),(`featureSize`,c_int32)] #自定義圖片類 class IM: def __init__(self): self.filepath=None self.date=None self.width=0 self.height=0 複製程式碼
face_dll.py
複製程式碼 from ctypes import * from face_class import * wuyongdll=CDLL(`d:pythonTestFacelibX64libarcsoft_face.dll`) dll=CDLL(`d:pythonTestFacelibX64libarcsoft_face_engine.dll`) dllc=cdll.msvcrt ASF_DETECT_MODE_VIDEO = 0x00000000 ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF c_ubyte_p = POINTER(c_ubyte) #啟用 jihuo=dll.ASFActivation jihuo.restype = c_int32 jihuo.argtypes = (c_char_p,c_char_p) #初始化 chushihua=dll.ASFInitEngine chushihua.restype=c_int32 chushihua.argtypes=(c_long,c_int32,c_int32,c_int32,c_int32,POINTER(c_void_p)) #人臉識別 shibie=dll.ASFDetectFaces shibie.restype=c_int32 shibie.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_MultiFaceInfo)) #特徵提取 tezheng=dll.ASFFaceFeatureExtract tezheng.restype=c_int32 tezheng.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_SingleFaceInfo),POINTER(ASF_FaceFeature)) #特徵比對 bidui=dll.ASFFaceFeatureCompare bidui.restype=c_int32 bidui.argtypes=(c_void_p,POINTER(ASF_FaceFeature),POINTER(ASF_FaceFeature),POINTER(c_float)) malloc = dllc.malloc free = dllc.free memcpy = dllc.memcpy malloc.restype = c_void_p malloc.argtypes = (c_size_t, ) free.restype = None free.argtypes = (c_void_p, ) memcpy.restype = c_void_p memcpy.argtypes = (c_void_p, c_void_p, c_size_t) 複製程式碼
face_function.py
複製程式碼 import face_dll,face_class from ctypes import * import cv2 from io import BytesIO # from Main import * Handle=c_void_p() c_ubyte_p = POINTER(c_ubyte) # 啟用函式 def JH(appkey,sdkey): ret=face_dll.jihuo(appkey,sdkey) return ret # 初始化函式 def CSH():# 1:視訊或圖片模式,2角度,3最小人臉尺寸推薦16,4最多人臉數最大50,5功能,6返回啟用控制程式碼 ret=face_dll.chushihua(0xFFFFFFFF,0x1,16,50,5,byref(Handle)) # Main.Handle=Handle return ret,Handle # cv2記載圖片並處理 def LoadImg(im): img=cv2.imread(im.filepath) sp=img.shape img=cv2.resize(img,(sp[1]//4*4,sp[0]//4*4)) sp=img.shape im.data=img im.width=sp[1] im.height=sp[0] return im def RLSB(im): faces=face_class.ASF_MultiFaceInfo() img=im.data imgby=bytes(im.data) imgcuby=cast(imgby,c_ubyte_p) ret=face_dll.shibie(Handle,im.width,im.height,0x201,imgcuby,byref(faces)) return ret,faces # 顯示人臉識別圖片 def showimg(im,faces): for i in range(0,faces.faceNum): ra=faces.faceRect[i] cv2.rectangle(im.data,(ra.left1,ra.top1),(ra.right1,ra.bottom1),(255,0,0,),2) cv2.imshow(`faces`,im.data) cv2.waitKey(0) #提取人臉特徵 def RLTZ(im,ft): detectedFaces=face_class.ASF_FaceFeature() img=im.data imgby=bytes(im.data) imgcuby=cast(imgby,c_ubyte_p) ret=face_dll.tezheng(Handle,im.width,im.height,0x201,imgcuby,ft,byref(detectedFaces)) if ret==0: retz=face_class.ASF_FaceFeature() retz.featureSize=detectedFaces.featureSize #必須操作記憶體來保留特徵值,因為c++會在過程結束後自動釋放記憶體 retz.feature=face_dll.malloc(detectedFaces.featureSize) face_dll.memcpy(retz.feature,detectedFaces.feature,detectedFaces.featureSize) # print(`提取特徵成功:`,detectedFaces.featureSize,mem) return ret,retz else: return ret #特徵值比對,返回比對結果 def BD(tz1,tz2): jg=c_float() ret=face_dll.bidui(Handle,tz1,tz2,byref(jg)) return ret,jg.value #單人特徵寫入檔案 def writeFTFile(feature,filepath): f = BytesIO(string_at(feature.feature,feature.featureSize)) a=open(filepath,`wb`) a.write(f.getvalue()) a.close() #從多人中提取單人資料 def getsingleface(singleface,index): ft=face_class.ASF_SingleFaceInfo() ra=singleface.faceRect[index] ft.faceRect.left1=ra.left1 ft.faceRect.right1=ra.right1 ft.faceRect.top1=ra.top1 ft.faceRect.bottom1=ra.bottom1 ft.faceOrient=singleface.faceOrient[index] return ft #從檔案獲取特徵值 def ftfromfile(filepath): fas=face_class.ASF_FaceFeature() f=open(`d:/1.dat`,`rb`) b=f.read() f.close() fas.featureSize=b.__len__() fas.feature=face_dll.malloc(fas.featureSize) face_dll.memcpy(fas.feature,b,fas.featureSize) return fas 複製程式碼
複製程式碼 import face_dll,face_class from ctypes import * import cv2 import face_function as fun Appkey=b`` SDKey=b`` # 啟用 ret=fun.JH(Appkey,SDKey) if ret==0 or ret==90114: print(`啟用成功:`,ret) else: print(`啟用失敗:`,ret) pass # 初始化 ret=fun.CSH() if ret[0]==0: print(`初始化成功:`,ret,`控制程式碼`,fun.Handle) else: print(`初始化失敗:`,ret) # 載入圖片 im=face_class.IM() im.filepath=`e:/2.jpg` im=fun.LoadImg(im) print(im.filepath,im.width,im.height) # cv2.imshow(`im`,im.data) # cv2.waitKey(0) print(`載入圖片完成:`,im) ret=fun.RLSB(im) if ret[0]==-1: print(`人臉識別失敗:`,ret) pass else: print(`人臉識別成功:`,ret) # 顯示人臉照片 # showimg(im,ret) #提取單人1特徵 ft=fun.getsingleface(ret[1],0) tz1=fun.RLTZ(im,ft)[1] #提取單人2特徵 ft=fun.getsingleface(ret[1],1) tz2=fun.RLTZ(im,ft)[1] #特徵儲存到檔案 # fun.writeFTFile(tz1,`d:/1.dat`) # fun.writeFTFile(tz2,`d:/2.dat`) #檔案獲取特徵 tz=fun.ftfromfile(`d:/1.dat`) jg=fun.BD(tz1,tz) print(jg[1]) #結果比對 # jg=fun.BD(tz1,tz2) # print(jg[1]) 複製程式碼