您的位置:首页 > 理论基础 > 计算机网络

记录一下怎么使用pycaffe调用已有的网络模型识别人脸(物体)

2017-12-08 14:37 561 查看
我的哲学原理:比较喜欢从结果向前推,有了能做什么、再去学怎么做?

今天就来看看怎么从图片中识别出人脸:



代码很简单,直接上码:

# -*- coding: utf-8 -*
import numpy as np
import sys,os
import cv2
caffe_root = 'E:/bigdata/workspace/caffe-ssd-microsoft/'
sys.path.insert(0, caffe_root + 'python')
import caffe
import time;

net_file= 'faceboxes_deploy.prototxt'
caffe_model='FaceBoxes_1024x1024.caffemodel'
test_dir = "images"

#
if not os.path.exists(caffe_model):
print("FaceBoxes_deploy.caffemodel does not exist,")
print("use merge_bn.py to generate it.")
exit()
#
caffe.set_mode_cpu()#gpu
net = caffe.Net(net_file,caffe_model,caffe.TEST) #加载network和model

#分类类别
CLASSES = ('background',
'face')

#设定图片的shape格式(1,3,1024,1024)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) #改变维度的顺序,由原始图片(H,W,C)变为(C,H,W)
transformer.set_mean('data', np.array([104,117,123])) # mean pixel
#transformer.set_raw_scale('data', 255); # 缩放到[0,255]之间
#transformer.set_channel_swap('data', (2,1,0)); #交换通道,将图片由RGB变为BGR

def preprocess(src):
img = cv2.resize(src, (1024,1024))
img = img - 127.5
img = img * 0.007843
return img

def postprocess(img, out):
h = img.shape[0]
w = img.shape[1]
box = out['detection_out'][0,0,:,3:7] * np.array([w, h, w, h])

cls = out['detection_out'][0,0,:,1]
conf = out['detection_out'][0,0,:,2]
return (box.astype(np.int32), conf, cls)

def detect_ok(imgfile):

frame = cv2.imread(imgfile)
print(frame.shape, frame.ndim, frame.size,frame.dtype, frame.itemsize)
if frame.ndim<0:
return False
#cv2.imshow("org", frame)
#cv2.waitKey(1000)

# image shape info
#rows, cols, channels = frame.shape
height, width, channels = frame.shape
res = cv2.resize(frame, (1024, 1024), 0.0, 0.0, interpolation=cv2.INTER_CUBIC)
#cv2.imshow("org", res)
#cv2.waitKey(1000)

#转成caffe能识别的图片格式
#res=cv2.cvtColor(res,cv2.COLOR_BGR2RGB)
res=res/255.

# net input shape info
print(net.blobs['data'].data.shape)#input 1,3,1024,1024
transformed_image = transformer.preprocess('data', res)#frame
#cv2.imshow("org", transformed_image)
#cv2.waitKey(1000)

# 执行上面设置的图片预处理操作,并将图片载入到blob中
net.blobs['data'].data[...] = transformed_image

time_start=time.time()
out = net.forward() #运行前向网络预测
time_end=time.time()
print(time_end-time_start,"s")

#显示结果
box, conf, cls = postprocess(frame, out)

for i in range(len(box)):
p1 = (box[i][0], box[i][1])
p2 = (box[i][2], box[i][3])
cv2.rectangle(frame, p1, p2, (0,255,0), 2)
p3 = (max(p1[0], 15), max(p1[1], 15))
title = "%s:%.2f" % (CLASSES[int(cls[i])], conf[i])
cv2.putText(frame, title, p3, cv2.FONT_ITALIC, 0.6, (0, 255, 0), 2)
#
frame = cv2.resize(frame, (int(width/2), int(height/2)), interpolation=cv2.INTER_CUBIC)
cv2.imshow("org", frame)
cv2.waitKey(2000)
return True

#test here!!!
for f in os.listdir(test_dir):
if detect_ok(test_dir + "/" + f) == False:
break


模型文件参见:
https://github.com/zeusees/FaceBoxes
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐