通过 Python 进行人脸检测程序中的 CV2 错误
Posted
技术标签:
【中文标题】通过 Python 进行人脸检测程序中的 CV2 错误【英文标题】:CV2 error in face detection program via Python 【发布时间】:2021-12-30 03:19:46 【问题描述】:所以,我正在制作一个人脸检测程序,一切正常,但是一旦我运行它,它就会向我显示错误消息:
line 40, in <module>
faceNet=cv2.dnn.readNet(faceModel, faceProto)
cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\caffe\caffe_io.cpp:1138: error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open "opencv_face_detector_uint8.pb" in function 'cv::dnn::ReadProtoFromBinaryFile'
代码是:
import cv2
import math
import argparse
def highlightFace(net, frame, conf_thershold=0.7):
frameOpencvDnn=frame.copy()
frameHight=frameOpencvDnn.shape[0]
frameWidth=frameOpencvDnn.shape[1]
blob=cv2.dnnblobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)
net.setInput(blob)
detections=net.forword()
faceBoxes=[]
for i in range(detections.shape[2]):
confidence=detections[0,0,1,2]
if confidence>conf_thershold:
x1=int(detections[0,0,i,3]*frameWidth)
y1=int(detections[0,0,i,4]*frameHight)
x2=int(detections[0,0,i,5]*frameWidth)
y2=int(detections[0,0,i,6]*frameHight)
faceBoxes.append([x1,y1,x2,y2])
cv2.rectangel(frameOpencvDnn, (x1,y1), (x2,y2), (0,225,0), int(round(frameHight/150)), 8)
return frameOpencvDnn,faceBoxes
parser=argparse.ArgumentParser()
parser.add_argument('--image')
args=parser.parse_args()
faceProto="opencv_face_detector.pbtxt"
faceModel="opencv_face_detector_uint8.pb"
ageProto="age_deploy.prototxt"
ageModel="age_net.caffemodel"
genderProto="gender_deploy.prototxt"
genderModel="gender_net.caffmodel"
MODEL_MEAN_VALUES=(78.4263377603, 87.7689143744, 144.895847746)
ageList=['(0-2)', '(4-6)', '(8-12)','(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
genderList=['Male','Female']
faceNet=cv2.dnn.readNet(faceModel, faceProto)
ageNet=cv2.dnn.readNet(ageModel, ageProto)
genderNet=cv2.dnn.readNet(genderModel, genderProto)
video=cv2.VideoCapture(args.image if args.image else 0)
padding=20
while cv2.waitKey(1)<0 :
hasFrame,frame=video.read()
if not hasFrame:
cv2.waitKey()
break
resultImg,faceBoxes=highlightFace(faceNet,frame)
if not faceBoxes:
print("No Face is being Detected")
for faceBox in faceBoxes:
face=frame[max(0,faceBox[1]-padding):
min(faceBox[3]+padding,frame.shape[0]-1),max(0,faceBox[0]-padding)
:min(faceBox[2]+padding, frame.shape[1]-1)]
blob=cv2.dnn.blobFromImage(face, 1.0, (227.227), MODEL_MEAN_VALUES, swapRB=False)
genderNet.setInput(blob)
genderPreds=genderNet.forword()
gender=genderList[genderPreds[0].argmax()]
print(f'Gender: gender')
ageNet.setInput(blob)
agePreds=ageNet.forword()
age=ageList[agePreds[0].argmax()]
print(f'Age: age[1:-1] years')
cv2.putText(resultImg, f'gender, age', (faceBox[0], faceBox[1]-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,225,225), 2, cv2.LINE_AA)
cv2.imshow("Detecting age and gender", resultImg)
【问题讨论】:
为什么要标记 Java 和 CSS?请不要使用与您的问题无关的标签。 【参考方案1】:代码有点狡猾,它不显示视频。试试这个。
import cv2
import math
import argparse
""" Identification """
faceProto = "opencv_face_detector.pbtxt"
faceModel = "opencv_face_detector_uint8.pb"
ageProto = "age_deploy.prototxt"
ageModel = "age_net.caffemodel"
genderProto = "gender_deploy.prototxt"
genderModel = "gender_net.caffemodel"
faceNet=cv2.dnn.readNet(faceModel, faceProto)
ageNet=cv2.dnn.readNet(ageModel,ageProto)
genderNet=cv2.dnn.readNet(genderModel,genderProto)
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']
padding=20
""" Face highliting """
def faceBox(faceNet, frames):
frameHeight=frames.shape[0]
frameWidth=frames.shape[1]
blob=cv2.dnn.blobFromImage(frames, 1.0, (300,300), [104,117,123], swapRB=False)
faceNet.setInput(blob)
detection=faceNet.forward()
bboxs=[]
for i in range(detection.shape[2]):
confidence=detection[0,0,i,2]
if confidence>0.7:
x1=int(detection[0,0,i,3]*frameWidth)
y1=int(detection[0,0,i,4]*frameHeight)
x2=int(detection[0,0,i,5]*frameWidth)
y2=int(detection[0,0,i,6]*frameHeight)
bboxs.append([x1,y1,x2,y2])
cv2.rectangle(frames, (x1,y1),(x2,y2),(0,255,0), 1)
return frames, bboxs
""" Video display """
def DisplayVid():
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('testvideo', fourcc, 20.0, (640, 480))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
while (True):
ret, frame = cap.read()
frameFace, bboxes = faceBox(faceNet, frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
for bbox in bboxes:
face = frame[bbox[1]:bbox[3], bbox[0]:bbox[2]]
blob = cv2.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)
cv2.rectangle(frameFace, (bbox[0], bbox[1] - 30), (bbox[2], bbox[1]), (0, 255, 0), -1)
cv2.putText(frameFace, label, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.imshow("Age-Gender", frameFace)
k = cv2.waitKey(1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if not (cap.isOpened()):
print("Could not open video device")
cap.release()
out.release()
cv2.destroyAllWindows()
DisplayVid()
【讨论】:
以上是关于通过 Python 进行人脸检测程序中的 CV2 错误的主要内容,如果未能解决你的问题,请参考以下文章