AttributeError: ‘module’ object has no attribute ‘io’ in caffe

我正在尝试制作一个性别识别程序,以下是代码..

import caffeimport osimport numpy as npimport sysimport cv2import time#模型根目录models_path = "./models"#加载均值图像mean_filename=os.path.join(models_path,'./mean.binaryproto')proto_data = open(mean_filename, "rb").read()a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)mean_image  = caffe.io.blobproto_to_array(a)[0]#加载性别网络gender_net_pretrained=os.path.join(models_path,'./gender_net.caffemodel')gender_net_model_file=os.path.join(models_path,'./deploy_gender.prototxt')gender_net = caffe.Classifier(gender_net_model_file,     gender_net_pretrained)#重塑均值输入图像mean_image = np.transpose(mean_image,(2,1,0))#性别标签gender_list=['Male','Female']#cv2 Haar人脸检测器    face_cascade=cv2.CascadeClassifier(os.path.join  (models_path,'haarcascade_frontalface_default.xml')) #从实时摄像头获取预测 cap = cv2.VideoCapture(0) while True:       ret,frame = cap.read()   if ret is True:      start_time = time.time()       frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)       rects = face_cascade.detectMultiScale(frame_gray, 1.3, 5)       #查找最大的人脸       if len(rects) >= 1:           rect_area = [rects[i][2]*rects[i][3] for i in xrange(len(rects))]           rect = rects[np.argmax(rect_area)]           x,y,w,h = rect           cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)           roi_color = frame[y:y+h, x:x+w]           #调整人脸图像大小           crop = cv2.resize(roi_color, (256,256))           #从均值文件中减去           #input_image = crop -mean_image       input_image = rect           #获取预测           start_prediction = time.time()           prediction = gender_net.predict([input_image])            gender = gender_list[prediction[0].argmax()]           print("DeepNet模型处理时间: {}").format(time.time()-start_prediction)           print prediction,gender           cv2.putText(frame,gender,(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2)           print("总处理时间: {}").format(time.time()-start_time)        #显示输出        cv2.imshow("性别检测",frame)        cv2.waitKey(1) #删除对象cap.release()cv2.killAllWindows()

当我运行时,我得到了一个错误:

a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)AttributeError: 'module' object has no attribute 'io'

我该如何解决它?我正在使用cnn_gender_age_prediction模型。我想使用Python和cnn_gender_age模型制作一个实时性别识别程序。


回答:

iocaffe 包中的一个模块。通常,当你输入 ,它不会自动尝试导入 caffe 包中的所有模块,包括 io。有两种解决方案。

第一种:手动导入 caffe.io

第二种:更新到最新的caffe版本,你应该会在 python/caffe 目录下的 __init__.py 文件中找到一行:

from . import io

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注