日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python+opnecv+dlib+face_recognition跟隨視頻

系統 1936 0

這次算是對之前幾篇的聯合使用,雖然使用效果并不是很理想,但是至少是有個思路??!!對都是小細節不允許細究!
首先展示下我用來測試的視頻與識別的圖像,視頻是從抖音提取出來的無水印視頻,因為感覺抖音上面的視頻時間短易獲得易測試還高清,這也是我為什么不看抖音卻還在手機上留著它的原因,它就是我的玩物!至于怎么提取抖音無水印視頻呢,很簡單,只要把鏈接發到我的微信上,就會自動回復給你啦,如圖



所以我又在給我的微信推廣告??

好了說正事,我在這里使用opencv主要是打開目錄下的視頻文件,在視頻中的人臉畫框,在人臉上寫字,控制視頻文件開關的操作。
下面說一下使用到的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)#將識別到的人臉圖片存入目錄下,以便識別讀取
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的作用主要就是檢測出框,當然dlib也可以不使用,用face_recognition也行,下面兩個的代碼都會貼出來,dlib的代碼就這幾行,用來查找人臉與位置

            
              dets = detector(frame_new, 1)
    print("人臉數: {}".format(len(dets)))
    # 查找臉部位置
    for i, face in enumerate(dets):

            
          

下面這個是動圖…只是我錄著卡

最后是識別的face_recognition的方法,和之前寫的用法一樣,獲取人臉,然后一行識別,但也因為這樣簡單,所以我們的識別效率并不會很高,碰到側臉或是很模糊的時候就感覺不是很行了

            
              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)  # 將人臉進行比對

            
          

像下面圖的測試結果,很明顯看出開始的時候雖然臉本來也看不太清,識別不出來也算正常?但是我總覺得很不完美,得臉露出多點才能識別清楚,以后用這個我就能有理地說:“你臉呢!”

以上的圖都是加上dlib的效果圖。
下面是完整代碼

            
              import dlib
import cv2
import face_recognition
detector = dlib.get_frontal_face_detector()#檢測器
camera = cv2.VideoCapture('1.mp4')
while True:
    ret, frame = camera.read()
    frame_new = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    dets = detector(frame_new, 1)#檢測圖像中的人臉
    print("人臉數: {}".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")#導出參考的迪麗熱巴圖片
        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)  # 將
            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為保存的人臉圖像(運行時會不斷更新),1.mp4為測試的視頻,dlrb1.jpg為作為對比測試的原圖
python+opnecv+dlib+face_recognition跟隨視頻對視頻中的人臉進行識別_第1張圖片
然后是只使用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("人臉數: {}".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的識別換成了使用face_recognition。下圖是用face_recognition的測試效果,感覺沒差
python+opnecv+dlib+face_recognition跟隨視頻對視頻中的人臉進行識別_第2張圖片
測試下來并沒有想象中的很好的結果,測試用起來沒關系,要是得用在別的地方還是需要很多優化,多訓練模型什么的,有外界影響時識別效果就沒那么精確,但是我覺得大概可以使用這個思路。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 海阳市| 宁晋县| 明光市| 桐柏县| 庆安县| 吴堡县| 神农架林区| 衡东县| 麻江县| 灵川县| 四川省| 永福县| 马鞍山市| 民权县| 永康市| 鹿泉市| 台南市| 松潘县| 重庆市| 平原县| 论坛| 通山县| 尼勒克县| 江安县| 扎兰屯市| 普宁市| 大宁县| 东阿县| 阳谷县| 含山县| 荔浦县| 万安县| 蒙自县| 达孜县| 雷山县| 凤台县| 偃师市| 建瓯市| 玉门市| 仁布县| 开化县|