實(shí)戰(zhàn)45講基礎(chǔ)篇】(task2)日志系統(tǒng)">【MySQL實(shí)戰(zhàn)45講基礎(chǔ)篇】(task2)日志系統(tǒng)
875
2025-04-02
首先我們通過視頻學(xué)習(xí)之后,會有官方給的鏈接,復(fù)制之后打開如下圖一,~因?yàn)閺?fù)制粘貼少了一個符號-
復(fù)制粘貼后時(例子):https://nbviewer.jupyter.org/github/huaweicloud/ModelArtsLab/blob/master/notebook/DL_face_recognition_advanced/face_similarity.ipynb
一
然后打開:
https://nbviewer.jupyter.org/圖二
需要手動修改一下:https://nbviewer.jupyter.org/github/huaweicloud/ModelArts-Lab/blob/master/notebook/DL_face_recognition_advanced/face_similarity.ipynb(成功打開的例子)將手動修改后的鏈接復(fù)制到如下圖,回車即可打開
二
本次實(shí)驗(yàn)鏈接 https://nbviewer.jupyter.org/github/huaweicloud/ModelArts-Lab/blob/master/notebook/DL_face_detector/face_detection.ipynb
具體操作步驟:
登陸ModelArts控制臺https://www.huaweicloud.com/product/modelarts.html -> 開發(fā)環(huán)境 -> Notebook -> 創(chuàng)建
名稱:任意設(shè)置參數(shù):python3-公共資源池-GPU-云硬盤EVS
創(chuàng)建Notebook
可以選擇免費(fèi)的版本,但是免費(fèi)的要排隊(duì)哦~
點(diǎn)擊提交
創(chuàng)建步驟我就直接省略了,直接啟動以及創(chuàng)建好的,初次創(chuàng)建只要選擇好GPU一般都不會出現(xiàn)什么問題,如果選擇cpu可能會出現(xiàn)內(nèi)存耗盡的問題,所以建議選擇GPU~
點(diǎn)擊‘new’選擇 ’tensorflow-1.13.1’
在Notebook中,我們輸入一個簡單的打印語句,然后點(diǎn)擊上方的運(yùn)行按鈕,可以查看語句執(zhí)行的結(jié)果:"如果未輸出結(jié)果,則環(huán)境還沒準(zhǔn)備好,稍等一下在試試,還不能輸出結(jié)果,則重新創(chuàng)建環(huán)境
數(shù)據(jù)和代碼下載
運(yùn)行下面代碼,進(jìn)行數(shù)據(jù)和代碼的下載和解壓
import os from modelarts.session import Session sess = Session() if sess.region_name == 'cn-north-1': bucket_path="modelarts-labs/notebook/DL_face_detector/face_detector.tar.gz" elif sess.region_name == 'cn-north-4': bucket_path="modelarts-labs-bj4/notebook/DL_face_detector/face_detector.tar.gz" else: print("請更換地區(qū)到北京一或北京四") if not os.path.exists('./models/detector.dat'): sess.download_data(bucket_path=bucket_path, path="./face_detector.tar.gz")
if os.path.exists('./face_detector.tar.gz'): # 使用tar命令解壓資源包 os.system("tar -xf ./face_detector.tar.gz") # 清理壓縮包 os.system("rm ./face_detector.tar.gz")
安裝依賴
!pip install dlib==19.17.0
from PIL import Image import dlib
獲取基于神經(jīng)網(wǎng)絡(luò)CNN的人臉區(qū)域檢測器,并加載預(yù)訓(xùn)練模型。
dlib.cnn_face_detection_model_v1()方法介紹: http://dlib.net/cnn_face_detector.py.html
cnn_face_detector = dlib.cnn_face_detection_model_v1("./models/detector.dat")
也可以通過notebook的upload按鈕上傳自己的測試數(shù)據(jù)。,測試數(shù)據(jù)需要是圖片,并且放在test文件夾下
image = dlib.load_rgb_image("./test.jpg")
檢測,得到圖片中人臉的位置。返回人臉區(qū)域左上角和右下角的坐標(biāo)
dets = cnn_face_detector(image, 1)
打印檢測結(jié)果
dets
print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format( i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))
rects = dlib.rectangles() rects.extend([d.rect for d in dets])
rects
import cv2 res_img = cv2.rectangle(image, (rects[0].left(), rects[0].top()), (rects[0].right(), rects[0].bottom()), 0, 1)
Image.fromarray(res_img)
人臉關(guān)鍵點(diǎn)檢測
人臉關(guān)鍵點(diǎn)檢測,依賴人臉區(qū)域檢測輸出的人臉位置,然后在人臉區(qū)域內(nèi)檢測人臉關(guān)鍵點(diǎn)。會輸出68個人臉關(guān)鍵點(diǎn)。
檢測人臉區(qū)域
獲取人臉區(qū)域檢測器
dlib.get_frontal_face_detector()方法返回基于HoG (Histogram of Oriented Gradients)的人臉區(qū)域檢測器。HoG是圖像處理領(lǐng)域用于目標(biāo)檢測的特征描述器。
detector = dlib.get_frontal_face_detector()
dets = detector(image, 1)
檢測人臉關(guān)鍵點(diǎn)
獲取基于神經(jīng)網(wǎng)絡(luò)的人臉關(guān)鍵點(diǎn)檢測模型,并加載預(yù)訓(xùn)練模型
predictor_kp = dlib.shape_predictor("./models/shape_predictor_68_face_landmarks.dat")
人臉關(guān)鍵點(diǎn)檢測,檢測人臉的68個關(guān)鍵點(diǎn)。
for k, d in enumerate(dets): # 遍歷每個人臉框 print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) res_img = cv2.rectangle(res_img,( d.left(), d.top()), (d.right(), d.bottom()), 0, 1) # 檢測人臉關(guān)鍵點(diǎn) shape = predictor_kp(image, d) print("Point 0: {}, Point 1: {} ...".format(shape.part(0), shape.part(1)))
顯示關(guān)鍵點(diǎn)檢測結(jié)果
繪制關(guān)鍵點(diǎn)
for i in range(68): res_img = cv2.circle(res_img,(shape.part(i).x,shape.part(i).y), 1, 255, 4)
顯示人臉區(qū)域和68個關(guān)鍵點(diǎn)的檢測結(jié)果
Image.fromarray(res_img)
視頻人臉檢測
使用OpenCV讀取一段視頻,然后逐幀顯示視頻。
from IPython.display import clear_output, Image, display, HTML import time import cv2 import base64 import numpy as np # 原視頻來源:UCF-101數(shù)據(jù)集 video_name = "./face.avi" def arrayShow(img): _,ret = cv2.imencode('.jpg', img) return Image(data=ret) # 打開一個視頻流 cap = cv2.VideoCapture(video_name) frame_id = 0 while True: try: clear_output(wait=True) # 清除之前的顯示 ret, frame = cap.read() # 讀取一幀圖片 if ret: frame_id += 1 if frame_id > 100: break cv2.putText(frame, str(frame_id), (5, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # 畫frame_id tmp = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 轉(zhuǎn)換色彩模式 img = arrayShow(frame) display(img) # 顯示圖片 time.sleep(0.05) # 線程睡眠一段時間再處理下一幀圖片 else: break except KeyboardInterrupt: cap.release() cap.release()
人臉關(guān)鍵點(diǎn)檢測函數(shù)
使用dlib集成的深度學(xué)習(xí)模型,檢測人臉的68個關(guān)鍵點(diǎn)。
def keypoint_detector(image): global res_img detector = dlib.get_frontal_face_detector() # 獲取人臉區(qū)域檢測器 dets = detector(image, 1) # 檢測人臉區(qū)域 predictor_kp = dlib.shape_predictor("./models/shape_predictor_68_face_landmarks.dat") # 加載人臉關(guān)鍵點(diǎn)檢測模型 for k, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) res_img = cv2.rectangle(image,( d.left(), d.top()), (d.right(), d.bottom()), 0, 1) shape = predictor_kp(image, d) # 檢測人臉關(guān)鍵點(diǎn) for i in range(68): res_img = cv2.circle(image,(shape.part(i).x,shape.part(i).y), 1, 255, 2) return res_img
檢測視頻中的人臉關(guān)鍵點(diǎn),并顯示檢測結(jié)果,由于dlib做人臉關(guān)鍵點(diǎn)檢測比較慢,因此只顯示10幀圖片的檢測結(jié)果
def arrayShow(img): _,ret = cv2.imencode('.jpg', img) return Image(data=ret) cap = cv2.VideoCapture(video_name) frame_id = 0 while True: try: clear_output(wait=True) ret, frame = cap.read() if ret: frame_id += 1 if frame_id > 10: break cv2.putText(frame, str(frame_id), (5, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # 畫frame_id res_img = keypoint_detector(frame) img = arrayShow(res_img) display(img) else: break except KeyboardInterrupt: cap.release() cap.release()
AI OpenCV 云學(xué)院
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。