1.年齢検出
論文アドレス:論文「畳み込みニューラル ネットワークを使用した年齢と性別の分類」
の著者は、合計 8 つの年齢層を学習した AlexNet に似た単純なネットワーク構造を提案しました。
- 0-2
- 4-6
- 8-12
- 15-20
- 25-32
- 38-43
- 48-53
- 60~100
注: これらの年齢グループは連続していません.
まず, 回帰または分類を使用して年齢を検出するのが適切かどうかを理解する必要があります.
栗を与える:
1.返品
2.分類
年齢の予測は、顔の外見に基づいています. よく維持され、若く見える人もいますが、実際の年齢は測定されたものとは異なります. ネットワーク モデルが他の関連情報を推測せずに実際の実年齢を予測することは困難です。回帰問題と見なすと、モデルが画像内の年齢の正確な値を予測することは困難ですが、分類問題として、年齢層を予測することは、モデルのトレーニングが比較的容易であり、回帰よりも高い精度が得られます。 .
2. 考え方
年齢のステップを自動的に識別します。
1. 入力画像またはビデオで顔を検出する
2. 顔の関心領域 (ROI) を抽出する
3. 年齢検出器を使用して人物の年齢を予測する
4. 結果を返す
顔を検出する分類子の場合:
分類器 | 長所と短所 |
---|---|
ハールカスケード | 高速で、組み込みデバイスで実行できますが、精度は低くなります |
HOG + 線形 SVM | ハールカスケードに比べて精度は高いが速度は遅く、オクルージョンや顔の角度変化の検出効果は良くない |
ディープラーニング検出器 | 上記の2つと比較すると、効果は最高ですが、より多くのコンピューティングリソースを消費する必要があります |
3. コードの実装
環境:
- win10
- パイチャーム
- アナコンダ3
- python3.7
- opencv4.2.0
OpenCVについては、最新バージョンを使用してみてください。コマンド ライン 1 つだけで、このシンプルで迅速なインストールを参照できます。
ファイル構造:
3.1 単一画像検出コード
import numpy as np
import cv2
"""
#图片年龄预测
执行:
python test_age.py
"""
# 检测年龄段
AGE_LIST = ["(0-2)","(4-6)","(8-12)","(15-20)","(25-32)","(38-43)","(48-53)","(60-100)"]
# 人脸检测模型路径
prototxtPathF ="./models/face_detector/face_deploy.prototxt"
weightsPathF = "./models/face_detector/res10_300x300_ssd_iter_140000.caffemodel"
# 加载人脸模型
faceNet = cv2.dnn.readNet(prototxtPathF,weightsPathF)
# 年龄检测模型
prototxtPathA ="./models/age_detector/age_deploy.prototxt"
weightsPathA = "./models/age_detector/age_net.caffemodel"
#加载模型
ageNet = cv2.dnn.readNet(prototxtPathA,weightsPathA)
#获取图像
image = cv2.imread("./input/test01.jpg")
src = image.copy()
(h,w)= image.shape[:2]
# 构造blob
blob = cv2.dnn.blobFromImage(image,1.0,(300,300),
(104,177,123))
# 送入网络计算
faceNet.setInput(blob)
detect = faceNet.forward()
# 检测
for i in range(0,detect.shape[2]):
confidence = detect[0,0,i,2]
# 过滤掉小的置信度,计算坐标,提取面部roi,构造面部blob特征
if confidence > 0.5:
box = detect[0,0,i,3:7]*np.array([w,h,w,h])
(startX,startY,endX,endY) = box.astype("int")
face = image[startY:endY,startX:endX]
faceBlob = cv2.dnn.blobFromImage(face, 1.0, (227, 227),
(78.4263377603, 87.7689143744, 114.895847746),
swapRB=False)
# 预测年龄
ageNet.setInput(faceBlob)
predictions = ageNet.forward()
i = predictions[0].argmax()
age = AGE_LIST[i]
ageConfidence = predictions[0][i]
#显示打印
text = "age{}:{:.2f}%".format(age,ageConfidence*100)
print(text)
#绘制显示框
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
cv2.imshow("Result",image)
cv2.waitKey(0)
3.2 ビデオストリーム検出コード
import numpy as np
import cv2
import imutils
"""
#视频流年龄预测
执行:
python test_video_age.py
"""
def detect_age(frame,faceNet,ageNet,minConfidence=0.5):
# 检测年龄段
AGE_LIST = ["(0-2)","(4-6)","(8-12)","(15-20)","(25-32)","(38-43)","(48-53)","(60-100)"]
#定义空列表存放结果
results = []
(h,w)= frame.shape[:2]
# 构造blob
blob = cv2.dnn.blobFromImage(frame,1.0,(300,300),
(104,177,123))
# 送入网络计算
faceNet.setInput(blob)
detect = faceNet.forward()
# 检测
for i in range(0,detect.shape[2]):
confidence = detect[0,0,i,2]
# 过滤掉小的置信度,计算坐标,提取面部roi,
if confidence > minConfidence:
box = detect[0,0,i,3:7]*np.array([w,h,w,h])
(startX,startY,endX,endY) = box.astype("int")
face = frame[startY:endY,startX:endX]
# 过滤干扰
if face.shape[0]<20 or face.shape[1]<20:
continue
# 构造面部blob特
faceBlob = cv2.dnn.blobFromImage(face, 1.0, (227, 227),
(78.4263377603, 87.7689143744, 114.895847746),
swapRB=False)
# 预测年龄
ageNet.setInput(faceBlob)
predictions = ageNet.forward()
i = predictions[0].argmax()
age = AGE_LIST[i]
ageConfidence = predictions[0][i]
# 构造字典存放结果
dicts = {
"location":(startX,startY,endX,endY),
"age":(age,ageConfidence)
}
results.append(dicts)
return results
# 人脸检测模型路径
prototxtPathF ="./models/face_detector/face_deploy.prototxt"
weightsPathF = "./models/face_detector/res10_300x300_ssd_iter_140000.caffemodel"
# 加载人脸模型
faceNet = cv2.dnn.readNet(prototxtPathF,weightsPathF)
# 年龄检测模型
prototxtPathA ="./models/age_detector/age_deploy.prototxt"
weightsPathA = "./models/age_detector/age_net.caffemodel"
#加载模型
ageNet = cv2.dnn.readNet(prototxtPathA,weightsPathA)
#获取视频图像
videoPath = "./input/test1.mp4"
vs = cv2.VideoCapture(videoPath)
#处理视频流
while True:
(grabbed,frame) = vs.read()
# 判断是否结束
if not grabbed:
print("无视频读取...")
break
frame = imutils.resize(frame,width=720)
#调用上面函数计算
results = detect_age(frame,faceNet,ageNet,minConfidence=0.5)
for i in results:
#显示信息
text = "age{}:{:.2f}%".format(i["age"][0], i["age"][1] * 100)
(startX,startY,endX,endY) = i["location"]
# 绘制显示框
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(frame, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(frame, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
# 显示
cv2.imshow("Result", frame)
key = cv2.waitKey(1) & 0xFF
# 按q键退出循环
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.release()
4. 試験結果
4.1 単一画像テスト
仮想環境でのコマンド ライン入力:
python test_age.py
効果 1:
効果 2:
効果 3:
4.2 ビデオストリーム検出
深層学習における年齢検出
動画アドレス: https://www.bilibili.com/video/BV16g4y187iQ/
参考:
1.https://talhassner.github.io/home/publication/2015_CVPR
2.https://github.com/dpressel/rude-carnie
3.https://github.com/GilLevi/AgeGenderDeepLearning