以下链接来自 @落痕的寒假
GitHub - luohenyueji/OpenCV-Practical-Exercise: OpenCV practical exercise
https://download.csdn.net/download/luohenyj/10993309
import cv2 as cv
import time
import argparsedef getFaceBox(net, frame, conf_threshold=0.7):frameOpencvDnn = frame.copy()frameHeight = frameOpencvDnn.shape[0]frameWidth = frameOpencvDnn.shape[1]blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)net.setInput(blob)detections = net.forward()bboxes = []for i in range(detections.shape[2]):confidence = detections[0, 0, i, 2]if confidence > conf_threshold: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)bboxes.append([x1, y1, x2, y2])cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8)return frameOpencvDnn, bboxesparser = argparse.ArgumentParser(description='Use this script to run age and gender recognition using OpenCV.')
parser.add_argument('--input', help='Path to input image or video file. Skip this argument to capture frames from a camera.')args = parser.parse_args()faceProto = "age_gender/model/opencv_face_detector.pbtxt"
faceModel = "age_gender/model/opencv_face_detector_uint8.pb"ageProto = "age_gender/model/age_deploy.prototxt"
ageModel = "age_gender/model/age_net.caffemodel"genderProto = "age_gender/model/gender_deploy.prototxt"
genderModel = "age_gender/model/gender_net.caffemodel"MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
genderList = ['Male', 'Female']# Load network
ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)# Open a video file or an image file or a camera stream
cap = cv.VideoCapture(args.input if args.input else 0)
padding = 20
while cv.waitKey(1) < 0:# Read framet = time.time()hasFrame, frame = cap.read()if not hasFrame:cv.waitKey()breakframeFace, bboxes = getFaceBox(faceNet, frame)if not bboxes:print("No face Detected, Checking next frame")continuefor bbox in bboxes:# print(bbox)face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),max(0,bbox[0]-padding):min(bbox[2]+padding, frame.shape[1]-1)]blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)genderNet.setInput(blob)genderPreds = genderNet.forward()gender = genderList[genderPreds[0].argmax()]# print("Gender Output : {}".format(genderPreds))print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max()))ageNet.setInput(blob)agePreds = ageNet.forward()age = ageList[agePreds[0].argmax()]print("Age Output : {}".format(agePreds))print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max()))label = "{},{}".format(gender, age)cv.putText(frameFace, label, (bbox[0], bbox[1]-10), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2, cv.LINE_AA)cv.imshow("Age Gender Demo", frameFace)# cv.imwrite("age-gender-out-{}".format(args.input),frameFace)print("time : {:.3f}".format(time.time() - t))
-
导入必要的模块:
cv2
:用于图像处理和显示time
:用于计时argparse
:用于解析命令行参数
-
定义函数
getFaceBox
用于检测人脸框:- 通过 DNN 模型进行人脸检测,筛选出置信度高于阈值的人脸框,并在原图上绘制矩形框。
-
使用
argparse
解析命令行参数:- 支持从图像或视频文件中读取,如果没有指定输入则使用摄像头捕获。
-
定义人脸检测和年龄、性别识别模型的路径:
faceProto
和faceModel
是人脸检测模型的配置文件和权重文件的路径。ageProto
和ageModel
是年龄识别模型的配置文件和权重文件的路径。genderProto
和genderModel
是性别识别模型的配置文件和权重文件的路径。
-
加载模型:
- 使用
cv.dnn.readNet
加载人脸检测、年龄识别和性别识别模型。
- 使用
-
打开视频文件或图像文件或者摄像头流,并设置填充值:
- 使用
cv.VideoCapture
打开视频文件或图像文件或者摄像头流,并设置填充值为 20。
- 使用
-
在循环中处理每帧图像:
- 读取一帧图像,然后调用
getFaceBox
函数检测人脸框。 - 对检测到的人脸框进行处理,提取人脸区域,并使用年龄和性别模型进行识别。
- 将识别结果标记在图像上并显示。
- 读取一帧图像,然后调用
优化:
-
并行处理:如果一帧中有多个面孔,您可以并行处理它们以加快年龄和性别预测。这需要使用线程或多处理。
-
跳帧:对于视频输入,您不需要处理每一帧,特别是在视频流畅且面部变化不快的情况下。您可以处理每第 n 帧以减少计算量。
-
调整帧大小:在人脸检测之前缩小帧的尺寸可以提高速度,因为它减少了要处理的数据量。
-
优化 Blob 创建:您可以对性别和年龄检测模型重复使用 Blob 创建步骤,以避免重复计算。
-
使用更快的模型:如果您的应用程序允许,您可以切换到更快(尽管可能不太准确)的人脸检测模型。
-
资源管理:合理释放视频采集、窗口等资源,避免不必要的资源消耗。
import cv2 as cv
import time
import argparse
from threading import Thread
from queue import Queuedef processFace(faceNet, genderNet, ageNet, frame, bbox, resultsQueue, padding=20):face = frame[max(0, bbox[1] - padding):min(bbox[3] + padding, frame.shape[0] - 1),max(0, bbox[0] - padding):min(bbox[2] + padding, frame.shape[1] - 1)]blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)genderNet.setInput(blob)genderPreds = genderNet.forward()gender = genderList[genderPreds[0].argmax()]ageNet.setInput(blob)agePreds = ageNet.forward()age = ageList[agePreds[0].argmax()]label = "{},{}".format(gender, age)resultsQueue.put((bbox, label))def main():parser = argparse.ArgumentParser(description='Age and gender recognition with OpenCV.')parser.add_argument('--input', help='Path to input image or video file. Skip this argument to capture frames from a camera.')args = parser.parse_args()# [Load models...]cap = cv.VideoCapture(args.input if args.input else 0)frameSkip = 5 # Process every 5th frameframeCount = 0resultsQueue = Queue()while cv.waitKey(1) < 0:hasFrame, frame = cap.read()if not hasFrame:cv.waitKey()breakframeCount += 1if frameCount % frameSkip != 0:continueframeFace, bboxes = getFaceBox(faceNet, frame)if not bboxes:print("No face Detected, Checking next frame")continuefor bbox in bboxes:# Start a new thread for processing each faceThread(target=processFace, args=(faceNet, genderNet, ageNet, frame, bbox, resultsQueue)).start()# Draw results from the queuewhile not resultsQueue.empty():bbox, label = resultsQueue.get()cv.putText(frameFace, label, (bbox[0], bbox[1] - 10), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2, cv.LINE_AA)cv.imshow("Age Gender Demo", frameFace)cap.release()cv.destroyAllWindows()if __name__ == "__main__":main()