python - How to convert Mat from opencv to caffe format -


i using opencv crop face camera. , used caffe predict image belongs male or female. have original code load image static image. however, want use image camera it. original code in caffe

    model = caffe.classifier(...)     image_path = './static_image.jpg'     input_image = caffe.io.load_image(image_path )     prediction =model.predict([input_image])  

now, use opencv capture frame , call predict method

  val, image = cap.read()       image = cv2.resize(image, (320,240))   gray = cv2.cvtcolor(image, cv2.color_bgr2gray)   faces = face_cascade.detectmultiscale(gray, 1.3, 5, minsize=(30,30))   f in faces:       x,y,w,h = f       cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,255))                face_image = gray[y:y+h, x:x+w]        resized_img = cv2.resize(face_image, (45,45))/255. 

after having resized_image, conver caffe type such function

def format_frame(self,frame):     img = frame.astype(np.float32)/255.     img = img[...,::-1]     return img 

however, when call function. don't know self. me fix it?

thank help!

you can use cvmattodatum function in caffe.io. more info here: https://github.com/bvlc/caffe/blob/master/src/caffe/util/io.cpp

edit: think can use array_to_datum https://github.com/bvlc/caffe/blob/master/python/caffe/io.py, though might necessary convert mat ndarray first


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -