wordpress網(wǎng)站怎么建設(shè)海南百度推廣開戶
結(jié)合以下鏈接中的文章有助于理解此篇案例:
- OpenCV中的 cnn 模塊
- https://blog.csdn.net/weixin_73504499/article/details/142965441?spm=1001.2014.3001.5501
此案例是通過使用OpenCV中的cnn模塊來調(diào)用別人已經(jīng)訓(xùn)練好的深度學(xué)習(xí)模型,此篇案例中用到了人臉檢測(cè)模型、年齡預(yù)測(cè)模型,性別預(yù)測(cè)模型。
-
以下鏈接中是這三種模型所需要的模型文件和配置文件
- 鏈接: https://pan.baidu.com/s/1hzatG5CNVVULCA8TjEegag?pwd=iaeg
- 提取碼: iaeg
-
完整代碼如下:
import cv2 from PIL import Image, ImageDraw, ImageFont import numpy as np# ======模型初始化====== # 模型(網(wǎng)絡(luò)模型/預(yù)訓(xùn)練模型):face/age/gender(臉、年齡、性別) faceProto = "model/opencv_face_detector.pbtxt" faceModel = "model/opencv_face_detector_uint8.pb" ageProto = "model/deploy_age.prototxt" ageModel = "model/age_net.caffemodel" genderProto = "model/deploy_gender.prototxt" genderModel = "model/gender_net.caffemodel"# 加載網(wǎng)絡(luò) ageNet = cv2.dnn.readNet(ageModel, ageProto) # 模型的權(quán)重參數(shù)、模型的配置 genderNet = cv2.dnn.readNet(genderModel, genderProto) faceNet = cv2.dnn.readNet(faceModel, faceProto) # ======年齡初始化====== # 年齡段和性別 共有8個(gè)年齡區(qū)間,區(qū)間范圍可自行更改 ageList = ['0-2歲', '4-6歲', '8-12歲', '15-22歲', '25-32歲', '38-43歲', '48-53歲', '60-100歲'] genderList = ['男性', '女性'] mean = (78.4263377603, 87.7689143744, 114.895847746) # 模型均值# ======自定義函數(shù),獲取人臉包圍框====== def getBoxes(net, frame):frameHeight, frameWidth = frame.shape[:2] # 獲取高度、寬度# 實(shí)現(xiàn)圖像預(yù)處理,從原始圖像構(gòu)建一個(gè)符合人工神經(jīng)網(wǎng)絡(luò)輸入格式的四維塊。blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), [104, 117, 123], True, False)net.setInput(blob) # 調(diào)用網(wǎng)絡(luò)模型,輸入圖片進(jìn)行人臉檢測(cè)detections = net.forward()faceBoxes = [] # 存儲(chǔ)檢測(cè)到的人臉xx = detections.shape[2]for i in range(detections.shape[2]):# confidence中每一行保存了7個(gè)數(shù)據(jù),第3個(gè)數(shù)據(jù)表示置信度,第4,5,6,7分別表示人臉歸一化后的坐標(biāo)位置confidence = detections[0, 0, i, 2]if confidence > 0.7: # 篩選一下,將置信度大于0.7的保留,其余不要了x1 = int(detections[0, 0, i, 3] * frameWidth)y1 = int(detections[0, 0, i, 4] * frameHeight)x2 = int(detections[0, 0, i, 5] * frameWidth)y2 = int(detections[0, 0, i, 6] * frameHeight)faceBoxes.append([x1, y1, x2, y2]) # 人臉框坐標(biāo)# 繪制人臉框cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight / 150)), 6)# 返回繪制了人臉框的幀frame、人臉包圍框faceBoxesreturn frame, faceBoxes""" 向圖片中添加中文 """ def cv2AddChineseText(img, text, position, textColor=(0, 255, 0), textSize=30):if (isinstance(img, np.ndarray)): # 判斷是否是OpenCV圖片類型img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 實(shí)現(xiàn) array 到 image 的轉(zhuǎn)換draw = ImageDraw.Draw(img) # 在img圖片上創(chuàng)建一個(gè)繪圖的對(duì)象# 字體的格式 C 盤中的 Windows/Fonts 中,復(fù)制到此文件夾下可看到文件名fontStyle = ImageFont.truetype("simsun.ttc", textSize, encoding="utf-8")draw.text(position, text, textColor, font=fontStyle) # 繪制文本return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # 轉(zhuǎn)換回 OpenCV 格式""" 打開攝像頭,將每一幀畫面?zhèn)魅肷窠?jīng)網(wǎng)絡(luò)中 """ cap = cv2.VideoCapture(0) # 0-->電腦自帶攝像頭,1-->電腦外接攝像頭while True:_, frame = cap.read()# frame = cv2.flip(frame,1) # 鏡像處理# 獲取人臉包圍框、繪制人臉包圍框(可能多個(gè))frame, faceBoxes = getBoxes(faceNet, frame)if not faceBoxes:print("當(dāng)前鏡頭中沒有人")continue# 遍歷每一個(gè)人臉包圍框for faceBoxe in faceBoxes:# 處理每一幀畫面frame,將其處理為符合DNN輸入的格式x, y, x1, y1 = faceBoxeface = frame[y:y1, x:x1]blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), mean) # 模型輸入為227*277# 調(diào)用模型,預(yù)測(cè)性別genderNet.setInput(blob)genderOuts = genderNet.forward()gender = genderList[genderOuts[0].argmax()]# 調(diào)用模型,預(yù)測(cè)年齡ageNet.setInput(blob)ageOuts = ageNet.forward()age = ageList[ageOuts[0].argmax()]result = "{},{}".format(gender, age) # 格式化文本(年齡、性別)frame = cv2AddChineseText(frame, result, (x, y - 30)) # 輸出中文性別和年齡cv2.imshow("result", frame)if cv2.waitKey(1) == 27: # 按下ESc鍵,退出程序breakcv2.destroyAllWindows() cap.release()