天天看點

使用face_recognition批量識别圖檔中的人數

#使用face_recognition實作從圖檔中選中人數并分别輸出txt




import face_recognition
import cv2
import os

fin = 'D:\\Users\\a\\Pictures\\test_pho'
# 讀取圖檔并識别人臉
for file in os.listdir(fin):
    file_fullname = fin + '/' + file
    img = face_recognition.load_image_file(file_fullname)
    face_locations = face_recognition.face_locations(img)
    print(face_locations)
    faceNum = len(face_locations)
    print(faceNum)
    num = str(faceNum)
    file_object = open(file_fullname+'.txt', 'w')
    file_object.write(num+ '個人')
    file_object.close()

    # 調用opencv函數顯示圖檔
    # img = cv2.imread("D:\\Users\\a\\Pictures\\test_pho\\3.jpg")
    # cv2.namedWindow("pre_pho")
    # cv2.imshow("pre_pho", img)

#周遊每個人臉,并标注
    # faceNum = len(face_locations)
    # print(faceNum)
    for i in range(0, faceNum):
        top = face_locations[i][0]
        right = face_locations[i][1]
        bottom = face_locations[i][2]
        left = face_locations[i][3]

        start = (left, top)
        end = (right, bottom)

        color = (55, 255, 155)
        thickness = 3
        cv2.rectangle(img, start, end, color, thickness)

# 顯示識别結果
# cv2.namedWindow("rec_pho")
# cv2.imshow("rec_pho", img)

cv2.waitKey(0)
cv2.destroyAllWindows()      
使用face_recognition批量識别圖檔中的人數

繼續閱讀