用树莓派 + Python + OpenCV识别人脸 拍照并上传到七牛云

栏目: Python · 发布时间: 7年前

内容简介:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cuiran/article/details/85007662

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cuiran/article/details/85007662

这里主要想通过识别人脸并上传到七牛云存储

首先是安装七牛SDK

下载地址

https://github.com/qiniu/python-sdk/releases

直接安装:

pip install qiniu

easy_install qiniu

源码安装:

#从Python SDK 下载地址下载源码
tar xvzf python-sdk-$VERSION.tar.gz
cd python-sdk-$VERSION
python setup.py install

在使用SDK 前,您需要一对有效的 AccessKey 和 SecretKey 签名授权。

可以通过如下步骤获得:

1、开通七牛开发者帐号

2、登录七牛开发者平台,查看 Access Key 和 Secret Key。

获取Access Key 和 Secret Key 后,调用如下两行代码进行初始化对接:

from qiniu import Auth
q = Auth(access_key, secret_key)

上传相关

# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth, put_file, etag
import qiniu.config
#需要填写你的 Access Key 和 Secret Key
access_key = 'Access_Key'
secret_key = 'Secret_Key'
#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
bucket_name = 'Bucket_Name'
#上传到七牛后保存的文件名
key = 'my-python-logo.png'
#生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
#要上传文件的本地路径
localfile = './sync/bbb.jpg'
ret, info = put_file(token, key, localfile)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)

对图片上传

# -*- coding: utf-8 -*-
import cv2
import time
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
import os
#需要填写你的 Access Key 和 Secret Key
access_key = '' #这里的密钥填上刚才我让你记住的密钥对
secret_key = '' #这里的密钥填上刚才我让你记住的密钥对

#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
bucket_name = 'face'


cap=cv2.VideoCapture(0)
i=0
while(1):
    ret ,frame = cap.read()
    k=cv2.waitKey(1)
    if k==27:
        break
    elif k==ord('s'):
        localfile=str(i)+'.jpg'
        cv2.imwrite(localfile,frame)
        #上传到七牛后保存的文件名
        key = '%s_%s_%s_%s_%s_%s.jpg'%(time.localtime()[0],time.localtime()[1],time.localtime()[2],time.localtime()[3],time.localtime()[4],time.localtime()[5])
        #生成上传 Token,可以指定过期时间等
        token = q.upload_token(bucket_name, key, 3600)
        #要上传文件的本地路径
        ret, info = put_file(token, key, localfile)
        print(info)
        assert ret['key'] == key
        assert ret['hash'] == etag(localfile)
        i+=1
    elif k==ord('q'):
        break
    cv2.imshow("capture", frame)
cap.release()
cv2.destroyAllWindows()

给出识别人脸的代码

import cv2

cap = cv2.VideoCapture(0) # 使用第5个摄像头(我的电脑插了5个摄像头)
face_cascade = cv2.CascadeClassifier(r'haarcascade_frontalface_default.xml') # 加载人脸特征库

while(True):
    ret, frame = cap.read() # 读取一帧的图像
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转灰

    faces = face_cascade.detectMultiScale(gray, scaleFactor = 1.15, minNeighbors = 5, minSize = (5, 5)) # 检测人脸
    for(x, y, w, h) in faces:
        cv2.rectangle(gray, (x, y), (x + w, y + h), (0, 255, 0), 2) # 用矩形圈出人脸

    cv2.imshow('Face Recognition', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release() # 释放摄像头
cv2.destroyAllWindows()

以上所述就是小编给大家介绍的《用树莓派 + Python + OpenCV识别人脸 拍照并上传到七牛云》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Developing Large Web Applications

Developing Large Web Applications

Kyle Loudon / Yahoo Press / 2010-3-15 / USD 34.99

As web applications grow, so do the challenges. These applications need to live up to demanding performance requirements, and be reliable around the clock every day of the year. And they need to withs......一起来看看 《Developing Large Web Applications》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试