国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python 人臉檢測(cè)與人臉識(shí)別

這篇具有很好參考價(jià)值的文章主要介紹了python 人臉檢測(cè)與人臉識(shí)別。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

'''
安裝庫(kù)文件: pip install dlib face_recognition
'''

import dlib
import face_recognition
import cv2
from PIL import Image, ImageDraw

# 判斷運(yùn)行環(huán)境 cpu or gpu
def check_env():
    print(dlib.DLIB_USE_CUDA)
    print(dlib.cuda.get_num_devices())

# 判斷人臉在圖片當(dāng)中的位置
def get_face_location(image_flle):
    image_fr = face_recognition.load_image_file(image_flle)
    face_locations = face_recognition.face_locations(image_fr)
    print(face_locations)

    # 標(biāo)記人臉的位置
    image_cv = cv2.imread(image_flle)
    for location in face_locations:
        y0, x1, y1, x0 = location
        cv2.rectangle(image_cv,(x0,y0),(x1,y1),(0,0,255),4)
    cv2.imwrite(image_flle + '.new_image.jpg', image_cv)

    return face_locations


# 提取人臉畫(huà)面保存到本地
def extract_face(image_file):
    image_cv = cv2.imread(image_file)
    face_recognitions = get_face_location(image_file)
    for i, location in enumerate(face_recognitions):
        y0,x1,y1,x0 = location
        face_image = image_cv[y0:y1,x0:x1]
        cv2.imwrite(f"{image_file}.face_{i}.jpg",face_image)


# 把人臉信息編碼為一個(gè)128維的向量
def encode_face(image_file):
    image_fr = face_recognition.load_image_file(image_file)
    face_recognitions = face_recognition.face_locations(image_fr)
    face_encodings = face_recognition.face_encodings(image_fr,face_recognitions)
    return face_encodings[0]
    
# 判斷2個(gè)人臉是否為同一個(gè)人
def compare_face(image_file1, image_file2):
    face_encoding1 = encode_face(image_file1)
    face_encoding2 = encode_face(image_file2)
    ret = face_recognition.compare_faces([face_encoding1],face_encoding2)
    return ret

# 標(biāo)記人臉局部和標(biāo)識(shí)
def mark_face(image_file):
    image_fr = face_recognition.load_image_file(image_file)
    face_marks = face_recognition.face_landmarks(image_fr)
    
    image_pil = Image.fromarray(image_fr)
    image_draw = ImageDraw.Draw(image_pil)
    for face_mark in face_marks:
        for facial_feature in face_mark.keys():
            image_draw.line(face_mark[facial_feature],width=5)
    image_pil.save(f"{image_file}_face_mark.jpg")


# 人臉補(bǔ)扮
def beautify_face(image_file):
    image_fr = face_recognition.load_image_file(image_file)
    face_marks = face_recognition.face_landmarks(image_fr)
    image_pil = Image.fromarray(image_fr)
    for i, face_mark in enumerate(face_marks):
        image_draw = ImageDraw.Draw(image_pil)
        # 眉毛
        image_draw.polygon(face_mark['left_eyebrow'],fill=(68,54,39,128))
        image_draw.polygon(face_mark['right_eyebrow'],fill=(68,54,39,128))
        image_draw.line(face_mark['left_eyebrow'],fill=(68,54,39,150),width=2)
        image_draw.line(face_mark['right_eyebrow'],fill=(68,54,39,150),width=2)
        # 嘴唇
        image_draw.polygon(face_mark['top_lip'],fill=(150,0,0,60))
        image_draw.polygon(face_mark['bottom_lip'],fill=(150,0,0,60))
        image_draw.line(face_mark['top_lip'],fill=(150,0,0,20),width=2)
        image_draw.line(face_mark['bottom_lip'],fill=(150,0,0,20),width=2)
        # 眼睛
        image_draw.polygon(face_mark['left_eye'],fill=(255,255,255,20))
        image_draw.polygon(face_mark['right_eye'],fill=(255,255,255,20))
        image_draw.line(face_mark['left_eye'] + [face_mark['left_eye'][0]],fill=(0,0,0,50),width=2)
        image_draw.line(face_mark['right_eye'] + [face_mark['right_eye'][0]],fill=(0,0,0,50),width=2)

    image_pil.save(f"{image_file}.beautify_face.png")


def main():
    check_env()

    face_locations = get_face_location('1.webp')
    print(face_locations)

    extract_face('3.jpg')

    face_encodings = encode_face('1.webp.face_0.jpg')
    print(face_encodings)

    ret = compare_face('1.webp.face_1.jpg','3.jpg.face_1.jpg')
    print(ret)

    mark_face('1.webp')

    beautify_face('1.webp')


if __name__=="__main__":
    main()

文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-783329.html

到了這里,關(guān)于python 人臉檢測(cè)與人臉識(shí)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包