记录一下怎么使用pycaffe调用已有的网络模型识别人脸(物体)
2017-12-08 14:37
561 查看
我的哲学原理:比较喜欢从结果向前推,有了能做什么、再去学怎么做?
今天就来看看怎么从图片中识别出人脸:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201712/28f48c98ea338f031350a55ccce283a8)
代码很简单,直接上码:
# -*- 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
今天就来看看怎么从图片中识别出人脸:
代码很简单,直接上码:
# -*- 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
相关文章推荐
- python多进程,使用用微软RENET模型的视频人脸识别程序
- 《炼数成金》第八课 保存和载入模型,使用Google的图像识别网络inception-v3进行图像识别
- CMU提出对抗生成网络:可实现对人脸识别模型的神经网络攻击
- java调用dll进行人脸识别(JNI的使用)
- 《炼数成金》第八课 保存和载入模型,使用Google的图像识别网络inception-v3进行图像识别。
- (更新视频教程)Tensorflow object detection API 搭建属于自己的物体识别模型(2)——训练并使用自己的模型
- 使用深度学习网络SSD识别自己定义的物体
- 【深度学习】笔记3_caffe自带的第一个例子,Mnist手写数字识别所使用的LeNet网络模型的详细解释
- java调用dll进行人脸识别(JNI的使用)
- 腾讯AI Lab刷新人脸检测与识别两大测评国际记录,技术日调用超六亿
- FastDFS分布式文件系统点滴记录3 -- 网络模型、libevent框架使用
- OpenCV学习记录(一):使用haar分类器进行人脸识别 标签: opencv脸部识别c++ 2017-07-03 15:59 26人阅读
- OpenCV学习记录(一):使用haar分类器进行人脸识别
- TensorFlow使用object detection训练自己的模型用于物体识别
- 单样本学习:使用孪生神经网络进行人脸识别
- 广播接收机(监听网络例子,记录一下方便使用)
- 使用jsp调用tag文件时浏览器运行结果不识别中文怎么办?
- 学习使用VS2005+ORACLE,感觉乱糟糟的,在此记录一下
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况