有谁知道如何解决这个奇怪的cv2.error:OpenCV(4.1.0)?

时间:2019-05-29 22:26:47

标签: python opencv compiler-errors cv2

好的。我的老师给我做了“简单”的作业。作业只是将命令通道从python.exe。\ AgeGender.py --input。\ sample.jpg更改为python.exe。\ AgeGender.py --input。\ test.jpg

链接到文件库:https://drive.google.com/file/d/1gvIhxmFPvEcbDcPoyjnkcjuxNiiXD1FY/view?usp=sharing

从开始。老师说我要做的就是在任何终端上运行。事实证明,经过2个小时的战斗并尝试运行此AgeGender.py,我不得不学习如何从cmd运行脚本。当然在开始时没有办法从Windows命令promt运行python.exe,所以我不得不找到一种将所有文件放入pycharm脚本文件夹的方法,只是要找出脚本写错了,而且我必须找到一个修复它的方法...

import cv2 as cv
import math
import time
import argparse

def 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, 4] * 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, bboxes


parser = 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 = "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"

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']

ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)

cap = cv.VideoCapture(args.input if args.input else 0)
padding = 20
while cv.waitKey(1) < 0:
# Read frame
t = time.time()
hasFrame, frame = cap.read()
if not hasFrame:
    cv.waitKey()
    break

frameFace, bboxes = getFaceBox(faceNet, frame)
if not bboxes:
    print("No face Detected, Checking next frame")
    continue

for 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)

因此,有人可以告诉我如何解决此问题?请

Traceback (most recent call last):
  File ".\AgeGender.py", line 47, in <module>
    ageNet = cv.dnn.readNet(ageModel, ageProto)
cv2.error: OpenCV(4.1.0) C:\projects\opencv- 
python\opencv\modules\dnn\src\caffe\caffe_io.cpp:1132: error: 
(-2:Unspecified error) FAILED: fs.is_open(). Can't open 
"age_net.caffemodel" in function 'cv::dnn::ReadProtoFromBinaryFile'

我只希望脚本正常运行而没有任何错误,所以我可以完成这场噩梦

2 个答案:

答案 0 :(得分:0)

事实证明服务器过载,因此下载的文件未完成。 下载完“ FULL”程序包后,一切开始起作用。我确实丢失了1 MB的1个文件,不幸的是,这对于整个脚本的正常运行至关重要。

感谢您的帮助,对不起。

答案 1 :(得分:0)

在此公共区域之间提供的文件路径可能存在错误

ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)

我希望它会有所帮助:)