2

I am trying to do a gender recognition program, below is the code..

import caffe
import os
import numpy as np
import sys
import cv2
import time

#Models root folder
models_path = "./models"

#Loading the mean image
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]

#Loading the gender network
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)

#Reshaping mean input image
mean_image = np.transpose(mean_image,(2,1,0))

#Gender labels
gender_list=['Male','Female']

#cv2 Haar Face detector
    face_cascade=cv2.CascadeClassifier(os.path.join
  (models_path,'haarcascade_frontalface_default.xml'))

 #Getting prediction from live camera
 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)

       #Finding the largest face
       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]

           #Resizing the face image
           crop = cv2.resize(roi_color, (256,256))

           #Subtraction from mean file
           #input_image = crop -mean_image
       input_image = rect
           #Getting the prediction
           start_prediction = time.time()
           prediction = gender_net.predict([input_image]) 
           gender = gender_list[prediction[0].argmax()]
           print("Time taken by DeepNet model: {}").format(time.time()-start_prediction)
           print prediction,gender
           cv2.putText(frame,gender,(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2)

           print("Total Time taken to process: {}").format(time.time()-start_time)
        #Showing output
        cv2.imshow("Gender Detection",frame)
        cv2.waitKey(1) 

#Delete objects
cap.release()
cv2.killAllWindows()

When I am running the I am getting an error:

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

How Can I solve it. I am using cnn_gender_age_prediction model. I want to make a real time gender recognition program using python and cnn_gender_age model.

8
  • The Error speaks for itself. Try using dir() on the module. Commented Oct 19, 2016 at 11:07
  • you should have io.py file in $CAFFE_ROOT/python/caffe folder. If not, then something is wrong with the caffe you got. Try re-install the package. Commented Oct 19, 2016 at 13:15
  • @Shai Hi I've seen the folder and I got io.py Commented Oct 19, 2016 at 16:14
  • can you please type the content of $CAFFE_ROOT/python/caffe/__init__.py? Commented Oct 19, 2016 at 16:16
  • @SSj.Luffy I did dir( ) and I got: >>> dir(caffe) ['AdaDeltaSolver', 'AdaGradSolver', 'AdamSolver', 'Classifier', 'Detector', 'Layer', 'NesterovSolver', 'Net', 'NetSpec', 'RMSPropSolver', 'SGDSolver', 'TEST', 'TRAIN', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_caffe', 'classifier', 'detector', 'get_solver', 'io', 'layer_type_list', 'layers', 'net_spec', 'params', 'proto', 'pycaffe', 'set_device', 'set_mode_cpu', 'set_mode_gpu', 'set_random_seed', 'to_proto'] >>> Commented Oct 19, 2016 at 16:16

1 Answer 1

1

io is a module in caffe package. Basically when you type import caffe, it will not automatically try to import all modules in caffe package including io. There are two solutions.

First one: import caffe.io manually

import caffe
import caffe.io

Second one: update to the latest caffe version, in which you should find a line in __init__.py under python/caffe directory:

from . import io
Sign up to request clarification or add additional context in comments.

1 Comment

if you read the comments on the question you will see that the OP has the latest version with the proper import in caffe init

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.