记录一下怎么使用pycaffe调用已有的网络模型识别人脸(物体)
来源:互联网 发布:日系化妆品 知乎 编辑:程序博客网 时间:2024/05/16 05:48
我的哲学原理:比较喜欢从结果向前推,有了能做什么、再去学怎么做?
今天就来看看怎么从图片中识别出人脸:
代码很简单,直接上码:
# -*- coding: utf-8 -*import numpy as np import sys,os import cv2caffe_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()#gpunet = 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变为BGRdef preprocess(src): img = cv2.resize(src, (1024,1024)) img = img - 127.5 img = img * 0.007843 return imgdef 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
阅读全文
0 0
- 记录一下怎么使用pycaffe调用已有的网络模型识别人脸(物体)
- 使用pycaffe定义网络
- 10-2 使用已有的模型类(Using Predefined Models)
- mysql:使用已有的记录更新另一条数据
- 如何使用logstash更新已有的elasticsearch记录
- caffe通过python(pycaffe)在已有模型上进行批量预测,批量提取特征
- 需要整理一下已有的资源。。。
- pycaffe使用(第一篇)——训练网络
- pycaffe使用(第二篇)——构造网络结构
- 使用深度学习网络SSD识别自己定义的物体
- 广播接收机(监听网络例子,记录一下方便使用)
- 怎样使用已有的工具训练Tesseract 3.03–3.05来识别新的语言
- 如何使用已有的主题
- 记录一下目前为止有的板子
- 新安装的wampserver怎么使用本机已有的mysql作为数据库
- Caffe学习:使用pycaffe定义网络
- Caffe学习:使用pycaffe绘制网络结构
- Caffe学习:使用pycaffe绘制网络结构
- python 部署pythonweb开发的几种方式
- 语音测试基础以及demo解读
- Linux 搭建 ftp 环境
- YTU.3155: 贪心之Packets
- Git常用命令
- 记录一下怎么使用pycaffe调用已有的网络模型识别人脸(物体)
- intelliJ IDEA 2017.1.3 创建maven web项目
- CSDN Markdown 博客范文代码
- 深度学习高手该怎样炼成?这位拿下阿里天池大赛冠军的中科院博士为你规划了一份专业成长路径
- AR# 54242: Install
- 重磅 阿里云成为CNCF白金会员
- The following untracked working tree files would be overwritten by merge(reset 无效时)
- CSS新单位vw,vh在响应式设计中的应用
- 减少你的重复代码-lombok使用教程-idea集成插件