1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Python+OpenCV+百度智能云

        共 13319字,需瀏覽 27分鐘

         ·

        2021-09-27 00:00

        點(diǎn)擊下方卡片,關(guān)注“新機(jī)器視覺”公眾號

        視覺/圖像重磅干貨,第一時(shí)間送達(dá)

        轉(zhuǎn)自 | 馬少爺


        一、人臉識別——人數(shù)識別+標(biāo)記

        1.導(dǎo)入模塊


        import timeimport base64import cv2import requests


        time模塊:計(jì)時(shí)


        base64模塊:由于某些系統(tǒng)中只能使用ASCII字符。Base64就是用來將非ASCII字符的數(shù)據(jù)轉(zhuǎn)換成 ASCII字符的一種方法。


        cv2模塊:本程序中要用到opencv的框選模塊


        requests模塊:使得python進(jìn)行網(wǎng)絡(luò)請求時(shí),變得人性化,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作


        2.獲取token

        這里首先要在百度智能云獲取到自己的APP_ID、API_KEY、SECRET_KEY,具體方法:獲取token

        def gettoken():    token = ""    if token == "":        APP_ID = '*****'  # 你的APP_ID        API_KEY = '*****'  # 你的API_KEY        SECRET_KEY = '*****'  # 你的SECRET_KEY
        # client_id 為官網(wǎng)獲取的AK, client_secret 為官網(wǎng)獲取的SK host = 'https://aip.baidubce.com/oaut/2.0/token?grant_type=client_credentials' + '&client_id=' + API_KEY + '&client_secret=' + SECRET_KEY # print(host) response = requests.get(host) # if response: # for item in response.json().items(): # 逐項(xiàng)遍歷response.json()----字典 # print(item) token = response.json()["access_token"] print(">>成功獲取到token")        return token

        需要注意的是:這里獲取的token你可以自己輸出一下,如果是24.*****(一長串)就說明對了。


        3.圖片讀取

        name為圖片名稱,以“rb”方式打開圖片

        def read_pic(name):    f = open(name, "rb")    base64_data = base64.b64encode(f.read())    s = base64_data.decode()    print(">>圖片讀取成功")    return s


        4.人臉檢測與屬性分析

        首先調(diào)用我們獲取到的token,將我們需要訪問的url通過字符串拼接完整,通過post方式訪問url,并通過json()函數(shù)轉(zhuǎn)換成python支持的dict。最后返回response 供后面我們框選人臉使用。

        # 人臉檢測與屬性分析def face_nums(picture):    token = gettoken()    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"    params = {"image": picture,              "image_type": "BASE64",              "quality_control": "NORMAL",              "max_face_num": 10}    request_url = request_url + "?access_token=" + token    headers = {'content-type': 'application/json'}    response = requests.post(request_url, data=params, headers=headers).json()    if response:        print(">>成功獲取到response")        # for item in response.items():       # 遍歷response        #     print(item)    else:        print("response == NULL")
        if response["error_code"] == 0: num = response["result"]["face_num"] print("圖中人數(shù)為:%d" % num) print(">>人數(shù)檢測完成\n") else: print("出錯(cuò)了")    return response


        5.人臉標(biāo)記
        得到上一步的response(其為字典結(jié)構(gòu)),我們試著將其輸出看看:

                for item in response.items():       # 遍歷response            print(item)

        得到輸出結(jié)果:

        ('error_code', 0)('error_msg', 'SUCCESS')('log_id', 579201101157)('timestamp', 1612106758)('cached', 0)('result', {'face_num': 4, 'face_list': [{'face_token': 'fa26f3aa9227ef72c65dccc2cef4e09b', 'location': {'left': 331.56, 'top': 158.35, 'width': 101, 'height': 101, 'rotation': -20}, 'face_probability': 1, 'angle': {'yaw': 14.84, 'pitch': 0.53, 'roll': -19.51}}, {'face_token': '2f0747b582fa60572fa7340c3bdf081c', 'location': {'left': 245.17, 'top': 136.09, 'width': 70, 'height': 70, 'rotation': -23}, 'face_probability': 1, 'angle': {'yaw': 19.23, 'pitch': 11.06, 'roll': -28.17}}, {'face_token': '19ff570779608d83977781f2f20dfe25', 'location': {'left': 172.71, 'top': 95.36, 'width': 52, 'height': 52, 'rotation': 0}, 'face_probability': 1, 'angle': {'yaw': 0.81, 'pitch': 2.76, 'roll': -4.04}}, {'face_token': 'ad478dbf67ec8ca57657d41b520e09ae', 'location': {'left': 121.06, 'top': 49.85, 'width': 53, 'height': 47, 'rotation': 9}, 'face_probability': 1, 'angle': {'yaw': -0.72, 'pitch': 8.11, 'roll': 7.21}}]})

        我們仔細(xì)分析一下,不要覺得他很復(fù)雜,其實(shí)就是一個(gè)字典里套字典的形式。其result鍵下就是我們需要的的信息,包括人數(shù),人臉坐標(biāo),還有旋度,俯仰角等。所以我們把需要的人數(shù)和人臉坐標(biāo)提取出來,通過cv2.rectangle()函數(shù)將人臉框選出來(注意,此函數(shù)里的所有參數(shù)只能為int類型,我們從字典中得到的數(shù)字為字符串類型,所以需要轉(zhuǎn)換一下)。


        實(shí)現(xiàn)代碼:

        def face_load(response, frame):    num = response["result"]["face_num"]    load = response["result"]["face_list"]    # print(load)    # for item in load.items():    #     print(item)    for i in range(num):                # 使用遍歷把所有的人臉都標(biāo)出框        location = load[i]['location']  # 獲取人臉坐標(biāo)        # print(location)        cv2.rectangle(frame, (int(location['left']), int(location['top'])),                      (int(location['width'] + location['left']), int(location['height'] + location['top'])),                      (0, 0, 255), 2)   # opencv的標(biāo)框函數(shù)        cv2.imshow('人臉檢測', frame)        cv2.waitKey(1)                  # 刷新界面 不然只會呈現(xiàn)灰色        print('運(yùn)行時(shí)間是{}'.format(time.time() - t1))        time.sleep(3)                   # 暫停3秒  展示圖片

        6.主函數(shù)

        輸入圖片,調(diào)用函數(shù),完成

        def main():    # p1 = read_pic("we.jpeg")    p1 = read_pic("Hero.jpg")    response = face_nums(p1)    frame = cv2.imread("Hero.jpg")    # frame = cv2.imread("we.jpeg")    cv2.waitKey(0)    # print("frame為:\n", frame)    face_load(response, frame)

        7.運(yùn)行結(jié)果


        8.總結(jié)

        1、官方人臉檢測API文檔

        https://cloud.baidu.com/doc/FACE/s/yk37c1u4t

        2.官方常見問題及排查API文檔

        https://cloud.baidu.com/doc/FACE/s/Zk5eyn5x3

        3.官方常見錯(cuò)誤碼API文檔

        https://cloud.baidu.com/doc/FACE/s/5k37c1ujz

        4.注意事項(xiàng)&解決方案:

        (1)post參數(shù)中,body中image和image_type為必要參數(shù),其他根據(jù)自己的需要添加

        (2)請求體格式化:Content-Type為application/json,通過json格式化請求體。

        (3)Base64編碼:請求的圖片需經(jīng)過Base64編碼,圖片的base64編碼指將圖片數(shù)據(jù)編碼成一串字符串,使用該字符串代替圖像地址。您可以首先得到圖片的二進(jìn)制,然后用Base64格式編碼即可。需要注意的是,圖片的base64編碼是不包含圖片頭的,如data:image/jpg;base64,

        (4)圖片格式:現(xiàn)支持PNG、JPG、JPEG、BMP,不支持GIF圖片

        (5)cv2.rectangle()函數(shù)里的所有參數(shù)只能為int類型,我們從字典中得到的數(shù)字為字符串類型,所以需要轉(zhuǎn)換一下

        (6)cv2.imshow()之后必須加cv2.waitKey(),否則打開時(shí)間過短會報(bào)錯(cuò)

        例如:

        cv2.imshow('人臉檢測', frame)cv2.waitKey(1)           



        二、人臉融合

        1.導(dǎo)入模塊

        import base64import jsonimport requests

        base64模塊:由于某些系統(tǒng)中只能使用ASCII字符。Base64就是用來將非ASCII字符的數(shù)據(jù)轉(zhuǎn)換成 ASCII字符的一種方法。

        json模塊:JSON的數(shù)據(jù)格式其實(shí)就是python里面的字典格式,里面可以包含方括號括起來的數(shù)組,也就是python里面的列表。

        requents模塊:進(jìn)行網(wǎng)絡(luò)請求時(shí),變得人性化,模仿瀏覽器的訪問網(wǎng)頁并獲得反執(zhí)等

        2.獲取token

        def gettoken():    token = ""    if token == "":        APP_ID = '【】'  # 你的APP_ID        API_KEY = '【】'  # 你的API_KEY        SECRET_KEY = '【】'  # 你的SECRET_KEY
        # client_id 為官網(wǎng)獲取的AK, client_secret 為官網(wǎng)獲取的SK host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' + \ '&client_id=' + API_KEY + \ '&client_secret=' + SECRET_KEY # print(host) response = requests.get(host) # if response: # for item in response.json().items(): # 逐項(xiàng)遍歷response.json()----字典 # print(item) token = response.json()["access_token"] print(">>成功獲取到token")        return token

        這里首先要在百度智能云獲取到自己的APP_ID、API_KEY、SECRET_KEY


        3.獲取圖片base64

        # 3.獲取圖片base64 -- base64是圖片的一種格式,所以要先打開圖片,然后轉(zhuǎn)成base64編碼才能用def read_pic(name):    f = open(name, "rb")    base64_data = base64.b64encode(f.read())    s = base64_data.decode()    print(">>圖片讀取成功")    return s

        打開圖片,對圖片通過base64模塊進(jìn)行編碼并保存

        4.保存base64到本機(jī)

        # 4.保存base64到本機(jī)def save_pic(data):    # image_data = base64.decode()    # f = open("mix.jpg","wb")    # f.write(image_data)
        imagedata = base64.b64decode(data) file = open('E:\\python_Work\\face_know\\mix.jpg', "wb") file.write(imagedata)
            print(">>圖片保存完成")

        將圖片數(shù)據(jù)保存到本地,在本地新建mix.jpg(即最終圖片融合完成要保存的圖)
        這里的路徑應(yīng)該與項(xiàng)目路徑相同

        5.圖片人臉融合

        # 5.圖片融合def mix(template, target):    token = gettoken()    url = "https://aip.baidubce.com/rest/2.0/face/v1/merge"    request_url = url + '?access_token=' + token    params = {        "image_template": {            "image": template,            "image_type": "BASE64",            "quality_control": "NORMAL"        },        "image_target": {            "image": target,            "image_type": "BASE64",            "quality_control": "NORMAL"        },        "merge_degree": "HIGH"    }    params = json.dumps(params)    headers = {"content-type": "application/json"}    result = requests.post(request_url, data=params, headers=headers).json()    if result["error_code"] == 0:        res = result["result"]["merge_image"]        save_pic(res)        print(">>人臉融合完成")    else:        print(str(result[' error_code ']) + result['error_msg'])

        根據(jù)官方文檔要求,訪問方式為post,先設(shè)置訪問請求頭(headers)和主要內(nèi)容(body),
        請求地址在這塊:

        注意:一共有500次調(diào)用限制,謹(jǐn)慎使用,其他調(diào)通了在來這塊訪問,不然等程序調(diào)完,次數(shù)也不多了
        完成訪問后,將返回的信息,通過json()模塊進(jìn)行格式化,轉(zhuǎn)換成python里的字典格式,然后保存

        6.功能測試

        # 6.功能測試
        # 主函數(shù)def main(): image1 = read_pic("Picture1.jpg") image2 = read_pic("Picture2.jpg") mix(image2, image1)

        if __name__ == "__main__": try: main() print(">>程序執(zhí)行完成") except Exception as result: print("出錯(cuò)原因:%s" % result)

        調(diào)用上述函數(shù),進(jìn)行測試

        7.效果展示
        Picture1.jpg(自己的一張證件照)

        Picture2.jpg(大家都愛的千璽,侵刪)


        合成效果如圖:


        8.總結(jié)

        注意事項(xiàng)&解決方案:

        (1)post參數(shù)中,body中image和image_type為必要參數(shù),其他根據(jù)自己的需要添加

        (2)請求體格式化:Content-Type為application/json,通過json格式化請求體。

        (3)Base64編碼:請求的圖片需經(jīng)過Base64編碼,圖片的base64編碼指將圖片數(shù)據(jù)編碼成一串字符串,使用該字符串代替圖像地址。您可以首先得到圖片的二進(jìn)制,然后用Base64格式編碼即可。需要注意的是,圖片的base64編碼是不包含圖片頭的,如data:image/jpg;base64;

        (4)一共有500次調(diào)用限制,謹(jǐn)慎使用,其他調(diào)通了在來這塊訪問,不然等程序調(diào)完,次數(shù)也不多了


        三、人臉識別——視頻人臉鎖定



        1.導(dǎo)入模塊

        import sysimport cv2


        sys模塊:該模塊提供對解釋器使用或維護(hù)的一些變量的訪問,以及與解釋器強(qiáng)烈交互的函數(shù)。
        cv2模塊:opencv接口模塊

        2.讀取視頻

        def videocapture():    cap = cv2.VideoCapture(0)  # 生成讀取攝像頭對象    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))  # 獲取視頻的寬度    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  # 獲取視頻的高度    fps = cap.get(cv2.CAP_PROP_FPS)  # 獲取視頻的幀率    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))  # 視頻的編碼    # 定義視頻對象輸出    writer = cv2.VideoWriter("video_result.mp4", fourcc, fps, (width, height))    while cap.isOpened():        ret, frame = cap.read()  # 讀取攝像頭畫面        cv2.imshow('teswell', frame)  # 顯示畫面        key = cv2.waitKey(24)        writer.write(frame)  # 視頻保存        # 按Q退出        if key == ord('q'):            break    cap.release()  # 釋放攝像頭    cv2.destroyAllWindows()  # 釋放所有顯示圖像窗口


        如果本地沒視頻的話,用這個(gè)函數(shù)調(diào)用自己的攝像頭,并錄制一段視頻,保存下來,如果有本地視頻,后面主函數(shù)中可以把這個(gè)函數(shù)注釋掉,用本地視頻

        3.人臉檢測

        def CatchUsbVideo(window_name, camera_idx):    cv2.namedWindow(window_name)
        # 視頻來源,可以來自一段已存好的視頻,也可以直接來自USB攝像頭 cap = cv2.VideoCapture("video.mp4")
        # 告訴OpenCV使用人臉識別分類器 #classfier = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt2.xml") classfier = cv2.CascadeClassifier(r"F:\OpenCV\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml")

        # 識別出人臉后要畫的邊框的顏色,RGB格式 color = (0, 255, 0)
        while cap.isOpened(): ok, frame = cap.read() # 讀取一幀數(shù)據(jù) if not ok: break
        # 將當(dāng)前幀轉(zhuǎn)換成灰度圖像 grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # 人臉檢測,1.2和2分別為圖片縮放比例和需要檢測的有效點(diǎn)數(shù) faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) if len(faceRects) > 0: # 大于0則檢測到人臉 for faceRect in faceRects: # 單獨(dú)框出每一張人臉 x, y, w, h = faceRect cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)
        # 顯示圖像 cv2.imshow(window_name, frame) c = cv2.waitKey(10) if c & 0xFF == ord('q'): break
        # 釋放攝像頭并銷毀所有窗口 cap.release()    cv2.destroyAllWindows()

        這里需要注意,haarcascade_frontalface_alt2.xml文件的路徑,在自己的安裝路徑里,在下圖這個(gè)位置,記得替換為自己的路徑


        設(shè)置完人臉識別分類器,將視頻一幀一幀轉(zhuǎn)為圖片,并進(jìn)行灰度處理,調(diào)用函數(shù)進(jìn)行人臉檢測,框選,執(zhí)行完將圖片顯示出來,最后釋放攝像頭

        4.程序測試

        if __name__ == '__main__':    #videocapture()    try:        if len(sys.argv) != 1:            print("Usage:%s camera_id\r\n" % (sys.argv[0]))        else:            CatchUsbVideo("識別人臉區(qū)域",0)    except Exception as Error:        print(Error)


        主函數(shù),調(diào)用上面所寫函數(shù)進(jìn)行測試

        5.運(yùn)行結(jié)果


        唯一問題是,最后顯示名稱因?yàn)槭侵形?,會出現(xiàn)如圖亂碼,暫時(shí)不知道解決方案


        6.總結(jié)

        本設(shè)計(jì)需要注意的是:

        1.是否需要調(diào)用本地?cái)z像頭,如果不用,本地應(yīng)該有需要檢測的視頻(我試了下,gif也可以)

        2.haarcascade_frontalface_alt2.xml文件路徑要設(shè)置好


        四、人臉識別——人臉相似度對比


        1.導(dǎo)入模塊

        from aip import AipFaceimport base64import matplotlib.pyplot as plt


        Aipface模塊:調(diào)用百度AipFace做人臉檢測
        base64模塊:對圖片數(shù)據(jù)進(jìn)行base64格式轉(zhuǎn)碼
        plt模塊:“plt”的設(shè)計(jì)是為了在不了解python的情況下使用matplotlib輕松繪制科學(xué)圖形。當(dāng)前實(shí)現(xiàn)將使用json配置文件。

        2.網(wǎng)頁請求

        """ 你的APPID,API_KEY和SECRET_KEY """APP_ID = '【】'  # 你的APP_IDAPI_KEY = '【】'  # 你的API_KEYSECRET_KEY = '【】'  # 你的SECRET_KEY

        # 封裝成函數(shù),返回獲取的client對象def get_client(APP_ID, API_KEY, SECRET_KEY): """ 返回client對象 :param APP_ID: :param API_KEY: :param SECRET_KEY: :return: """ return AipFace(APP_ID, API_KEY, SECRET_KEY)


        這塊直接調(diào)用AipFace()函數(shù),直接獲取網(wǎng)絡(luò)應(yīng)答。

        3.解析網(wǎng)絡(luò)回執(zhí)

        pic1 = "T.jpg"pic2 = "Y.jpg"client = get_client(APP_ID, API_KEY, SECRET_KEY)result = client.match([    {        'image': str(base64.b64encode(open(pic1, 'rb').read()), 'utf-8'),        'image_type': 'BASE64',    },    {        'image': str(base64.b64encode(open(pic2, 'rb').read()), 'utf-8'),        'image_type': 'BASE64',    }])print(result)
        if result['error_msg'] == 'SUCCESS': score = result['result']['score'] print('兩張圖片相似度:', score)else: print('錯(cuò)誤信息:', result['error_msg'])
        pc1 = plt.imread(pic1)pc2 = plt.imread(pic2)plt.imshow(pc1)plt.show()plt.imshow(pc2)plt.show()

        4、結(jié)果測試



        參考文獻(xiàn):

        https://blog.csdn.net/weixin_42670502


        —版權(quán)聲明—

        僅用于學(xué)術(shù)分享,版權(quán)屬于原作者。

        若有侵權(quán),請聯(lián)系微信號:yiyang-sy 刪除或修改!


        —THE END—
        瀏覽 65
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产另类自拍 | 午夜精品一区二区三区视频 | 中文乱片A片AAA毛片 | 波多野结衣一区二区三区Av高清 | 资源av | 免费观看人成视频 | 成人乱妇无码AV在线 | 国产精品无码7777777 | 377人体粉嫩噜噜噜 | 国产久精品 |