這里openmv必須插入sd卡
這里使用LBP特征分辨人臉
LBP是Local Binary Pattern(局部二值模式)的縮寫,具有灰度不變性和旋轉不變性等顯著優點
http://blog.sina.com.cn/s/blog_4bdbec750101ekuh.html
https://1043693084-qq-com.iteye.com/blog/2245828
https://blog.csdn.net/heli200482128/article/details/79204008
首先采集人臉數據,可以從不同角度,用不同的表情
1 # Snapshot Example 2 # 3 # Note: You will need an SD card to run this example. 4 # 5 # You can use your OpenMV Cam to save image files. 6 7 import sensor, image, pyb 8 9 RED_LED_PIN = 1 10 BLUE_LED_PIN = 3 11 12 sensor.reset() # Initialize the camera sensor. 13 sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE 14 sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others) 15 sensor.set_windowing((92,112)) 16 sensor.skip_frames(10) # Let new settings take affect. 17 sensor.skip_frames(time = 2000) 18 19 num = 1 #設置被拍攝者序號,第一個人的圖片保存到s1文件夾,第二個人的圖片保存到s2文件夾,以此類推。每次更換拍攝者時,修改num值。 20 21 n = 20 #設置每個人拍攝圖片數量。 22 23 #連續拍攝n張照片,每間隔3s拍攝一次。 24 while(n): 25 #紅燈亮 26 pyb.LED(RED_LED_PIN).on() 27 sensor.skip_frames(time = 3000) # Give the user time to get ready.等待3s,准備一下表情。 28 29 #紅燈滅,藍燈亮 30 pyb.LED(RED_LED_PIN).off() 31 pyb.LED(BLUE_LED_PIN).on()a 32 33 #保存截取到的圖片到SD卡 34 print(n) 35 sensor.snapshot().save("face/s%s/%s.pgm" % (num, n) ) # or "example.bmp" (or others) 36 37 n -= 1 38 39 pyb.LED(BLUE_LED_PIN).off() 40 print("Done! Reset the camera to see the saved image.") 41 42 print("finished!")
num代表要存儲的第幾個人, n代表多少張人臉圖像
首先要在openmv插入的sd卡中創建文件夾
存儲路徑為例如:face/s1/1.pgm
采集完樣本后,要在openmvIDE中工具欄->重置OpenMv Cam才能在文件中看到樣本圖像
人臉識別代碼:
1 # Face recognition with LBP descriptors. 2 # See Timo Ahonen's "Face Recognition with Local Binary Patterns". 3 # 4 # Before running the example: 5 # 1) Download the AT&T faces database http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip 6 # 2) Exract and copy the orl_faces directory to the SD card root. 7 8 9 import sensor, time, image, pyb 10 11 sensor.reset() # Initialize the camera sensor. 12 sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE 13 sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others) 14 sensor.set_windowing((92,112)) 15 sensor.skip_frames(10) # Let new settings take affect 16 sensor.skip_frames(time = 5000) #等待5s 17 18 19 20 #SUB = "s1" 21 NUM_SUBJECTS = 2 #圖像庫中不同人數,一共2人 22 NUM_SUBJECTS_IMGS = 20 #每人有20張樣本圖片 23 24 # 拍攝當前人臉。 25 img = sensor.snapshot() 26 #img = image.Image("singtown/%s/1.pgm"%(SUB)) 27 d0 = img.find_lbp((0, 0, img.width(), img.height())) 28 # d0為當前人臉的lbp特征 29 img = None 30 pmin = 999999 31 num=0 32 33 def min(pmin, a, s): 34 global num 35 if a<pmin: 36 pmin=a 37 num=s 38 return pmin 39 40 for s in range(1, NUM_SUBJECTS+1): 41 dist = 0 42 for i in range(1, NUM_SUBJECTS_IMGS+1): 43 img = image.Image("face/s%d/%d.pgm"%(s, i)) 44 d1 = img.find_lbp((0, 0, img.width(), img.height())) 45 # d1為第s文件夾中的第i張圖片的lbp特征 46 dist += image.match_descriptor(d0, d1)# 計算d0 d1即樣本圖像與被檢測人臉的特征差異度。 47 print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS)) 48 pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)# 特征差異度越小,被檢測人臉與此樣本更相似更匹配。 49 print(pmin) 50 51 print(num) # num為當前最匹配的人的編號。
s為第幾個庫
i為第幾張樣本圖像
image.
find_lbp
(roi)
從ROI元組(x, y, w, h)中提取LBP(局部二值模式)鍵點。您可以使用 image.match_descriptor
函數來比較兩組關鍵點,以獲取匹配距離。
roi
是感興趣區域的矩形元組(x,y,w,h)。如果未指定,ROI即整個圖像的圖像矩形。 操作范圍僅限於 roi
區域內的像素。
僅支持灰度圖像。
image.
match_descriptor
(descritor0, descriptor1[, threshold=70[, filter_outliers=False]])
對於LBP描述符來說,這個函數返回的是一個體現兩個描述符之間區別的整數。這一距離測度尤為必要。這個距離是對相似度的一個度量。這個測度值越接近0,LBPF特征點匹配得就越好。
對於ORB描述符來說,這個函數返回的是kptmatch對象。見上。
threshold
是用來為ORB鍵點過濾不明確匹配服務的。
一個較低的 threshold
值將緊扣關鍵點匹配算法。 threshold
值位於0-100 (int)。默認值為70。
filter_outliers
是用來為ORB鍵點過濾異常值服務的。 特征點允許用戶提高 threshold
值。默認設置為False。
dist賦值為被檢測人臉和20張樣本圖像的特征差異度和
自定義函數min()的作用是選擇最小的 平均樣本特征差異度和對應的圖像庫(每個樣本圖像庫和被檢測人臉 )
pmin設置為最大
下圖可以看到在左下角的串行終端中打印出的最匹配的樣本庫和特征差異度