這次算是對(duì)之前幾篇的聯(lián)合使用,雖然使用效果并不是很理想,但是至少是有個(gè)思路??!!對(duì)都是小細(xì)節(jié)不允許細(xì)究!
首先展示下我用來測(cè)試的視頻與識(shí)別的圖像,視頻是從抖音提取出來的無水印視頻,因?yàn)楦杏X抖音上面的視頻時(shí)間短易獲得易測(cè)試還高清,這也是我為什么不看抖音卻還在手機(jī)上留著它的原因,它就是我的玩物!至于怎么提取抖音無水印視頻呢,很簡(jiǎn)單,只要把鏈接發(fā)到我的微信上,就會(huì)自動(dòng)回復(fù)給你啦,如圖
所以我又在給我的微信推廣告??
好了說正事,我在這里使用opencv主要是打開目錄下的視頻文件,在視頻中的人臉畫框,在人臉上寫字,控制視頻文件開關(guān)的操作。
下面說一下使用到的cv代碼
camera = cv2.VideoCapture('1.mp4')#打開攝像頭
cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 1)#畫框
cv2.imwrite('1.jpg',img1)#將識(shí)別到的人臉圖片存入目錄下,以便識(shí)別讀取
cv2.putText(frame, name, (face.left() - 10, face.top() - 10), cv2.FAST_FEATURE_DETECTOR_NONMAX_SUPPRESSION, 2,(255, 0, 0), 2)#在人臉上方寫字,若相同,則輸出名字,不同則輸出unknow
cv2.resizeWindow("Camera", 640, 550)#控制視頻窗口大小
cv2.imshow("Camera", frame)#展示視頻窗口
dlib的作用主要就是檢測(cè)出框,當(dāng)然dlib也可以不使用,用face_recognition也行,下面兩個(gè)的代碼都會(huì)貼出來,dlib的代碼就這幾行,用來查找人臉與位置
dets = detector(frame_new, 1)
print("人臉數(shù): {}".format(len(dets)))
# 查找臉部位置
for i, face in enumerate(dets):
下面這個(gè)是動(dòng)圖…只是我錄著卡
最后是識(shí)別的face_recognition的方法,和之前寫的用法一樣,獲取人臉,然后一行識(shí)別,但也因?yàn)檫@樣簡(jiǎn)單,所以我們的識(shí)別效率并不會(huì)很高,碰到側(cè)臉或是很模糊的時(shí)候就感覺不是很行了
face_image = face_recognition.load_image_file(r"dlrb1.jpg")
face_image1 = face_recognition.load_image_file(r"1.jpg")
face_encondings = face_recognition.face_encodings(face_image) # 遍歷人臉
face_encondings1 = face_recognition.face_encodings(face_image1)
face_locations = face_recognition.face_locations(face_image) # 人臉位置
face_locations1 = face_recognition.face_locations(face_image1)
face1 = face_encondings[0]
for i in range(len(face_encondings1)):
face2 = face_encondings1[i]
result = face_recognition.compare_faces([face1], face2, tolerance=0.5) # 將人臉進(jìn)行比對(duì)
像下面圖的測(cè)試結(jié)果,很明顯看出開始的時(shí)候雖然臉本來也看不太清,識(shí)別不出來也算正常?但是我總覺得很不完美,得臉露出多點(diǎn)才能識(shí)別清楚,以后用這個(gè)我就能有理地說:“你臉呢!”
以上的圖都是加上dlib的效果圖。
下面是完整代碼
import dlib
import cv2
import face_recognition
detector = dlib.get_frontal_face_detector()#檢測(cè)器
camera = cv2.VideoCapture('1.mp4')
while True:
ret, frame = camera.read()
frame_new = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
dets = detector(frame_new, 1)#檢測(cè)圖像中的人臉
print("人臉數(shù): {}".format(len(dets)))
# 查找臉部位置
for i, face in enumerate(dets):
cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 1)
img1=frame[face.top():face.bottom(),face.left():face.right()]
cv2.imwrite('1.jpg',img1)#將獲取的人臉圖片保存
face_image = face_recognition.load_image_file(r"dlrb1.jpg")#導(dǎo)出參考的迪麗熱巴圖片
face_image1 = face_recognition.load_image_file(r"1.jpg")#導(dǎo)入保存的人臉圖片
face_encondings = face_recognition.face_encodings(face_image) # 遍歷人臉
face_encondings1 = face_recognition.face_encodings(face_image1)
face_locations = face_recognition.face_locations(face_image) # 人臉位置
face_locations1 = face_recognition.face_locations(face_image1)
face1 = face_encondings[0]
for i in range(len(face_encondings1)):
face2 = face_encondings1[i]
result = face_recognition.compare_faces([face1], face2, tolerance=0.5) # 將
print(result)
print(type(result))
if result[0] == True:
print('1')
name = 'dilireba'#若為相同,則在人像上打印出dilireba,下同
cv2.putText(frame, name, (face.left() - 10, face.top() - 10), cv2.FAST_FEATURE_DETECTOR_NONMAX_SUPPRESSION, 2,(255, 0, 0), 2)
else:
print('0')
name = 'unknow'
cv2.putText(frame, name, (face.left() - 10, face.top() - 10),cv2.FAST_FEATURE_DETECTOR_NONMAX_SUPPRESSION, 2,(255, 0, 0), 2)
cv2.resizeWindow("Camera", 640, 550)
cv2.imshow("Camera", frame)
m = cv2.waitKey(1)
1jpg為保存的人臉圖像(運(yùn)行時(shí)會(huì)不斷更新),1.mp4為測(cè)試的視頻,dlrb1.jpg為作為對(duì)比測(cè)試的原圖
然后是只使用face_recognition不使用dlib的代碼
import dlib
import cv2
import face_recognition
camera = cv2.VideoCapture('1.mp4')
while True:
ret, frame = camera.read()
frame_new = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_encondings1 = face_recognition.face_encodings(frame) # 遍歷人臉
print("人臉數(shù): {}".format(len(face_encondings1)))
face_image = face_recognition.load_image_file(r"dlrb1.jpg")
face_encondings = face_recognition.face_encodings(face_image) # 遍歷人臉
face_locations1 = face_recognition.face_locations(frame)
face1 = face_encondings[0]
for i in range(len(face_encondings1)):
face2 = face_encondings1[i]
result = face_recognition.compare_faces([face1], face2, tolerance=0.5) # 將
print(result)
print(type(result))
if result[0] == True:
print('1')
name = 'yes'
else:
print('0')
name = 'unknow'
face_enconding = face_encondings1[i]
face_location = face_locations1[i]
top, right, bottom, left = face_location
img1=frame[top:bottom,left:right]
cv2.imwrite('1.jpg', img1)
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left - 10, top - 10), cv2.FAST_FEATURE_DETECTOR_NONMAX_SUPPRESSION, 2,
(255, 0, 0), 2)
face_image_rgb1 = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
cv2.resizeWindow("Camera", 640, 480)
cv2.imshow("Camera", frame)
m = cv2.waitKey(1)
原理同上,只不過是將dlib的識(shí)別換成了使用face_recognition。下圖是用face_recognition的測(cè)試效果,感覺沒差
測(cè)試下來并沒有想象中的很好的結(jié)果,測(cè)試用起來沒關(guān)系,要是得用在別的地方還是需要很多優(yōu)化,多訓(xùn)練模型什么的,有外界影響時(shí)識(shí)別效果就沒那么精確,但是我覺得大概可以使用這個(gè)思路。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
