利用caffe和mxnet 打开摄像头,进行人脸检测

# -*- coding:utf-8 -*-
import random
import mxnet as mx
import numpy as np
from sklearn import preprocessing
import base64
import cv2
import time
from easydict import EasyDict as edict
import os
import sys
from scipy import misc
from mtcnn_detector import MtcnnDetector
import numpy as np
caffe_root = '../../caffe-ssd/python'  # 设置当前的工作环境在caffe下
sys.path.insert(0, caffe_root)
import caffe
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src', 'common'))  #放入preprocess路径
import face_preprocess

caffe.set_device(0)
caffe.set_mode_gpu()
model_def = '../models/sfd/deploy.prototxt'
model_weights = '../models/sfd/SFD.caffemodel'
net = caffe.Net(model_def, model_weights, caffe.TEST)    #把上面添加的两个变量都作为参数构造一个Net


#********************************SFD***********************************************************************
def sfd_detection(frame, threshold, mode):
    # start = time.time()
    image = frame
    height = image.shape[0]
    width = image.shape[1]

    # 适用于长宽不对等的场景,进行长宽等比例变化
    if mode == 'reg':
        im_shrink = 80.0 / max(image.shape[0], image.shape[1])
        # print(im_shrink)
    # waitkey()
    elif mode == 'global':
        im_shrink = 320.0 / max(image.shape[0], image.shape[1])
    elif mode == 'cached':
        im_shrink = 160.0 / max(image.shape[0], image.shape[1])
    image = cv2.resize(image, None, None, fx=im_shrink, fy=im_shrink, interpolation=cv2.INTER_LINEAR)    #调整输入样本大小以便输入神经网络呦 (3维) 320*320*3
    # cv2.imshow('iamge',image)
    # cv2.waitKey(0)
    # print image.shape[0]
    # print image.shape[1]
    net.blobs['data'].reshape(1, 3, image.shape[0], image.shape[1])

#由于matplotlib加载的image像素(0-1)之间,图片:RGB 而caffe需要像素(0-255),图片:BGR ,因此要转换
    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

#channel通道提前
    transformer.set_transpose('data', (2, 0, 1))
    transformer.set_mean('data', np.array([104, 117, 123]))
  
#像素放大,通道RGB->BGR 
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2, 1, 0))
#处理加载的图片
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image 
#前向传
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值