首页 > 其他分享 >【探花交友】阿里云OSS、百度人脸识别

【探花交友】阿里云OSS、百度人脸识别

时间:2023-01-09 12:35:31浏览次数:54  
标签:人脸识别 String OSS private properties class 探花 public


文章目录

​​1、完善用户信息​​

​​1.1、阿里云OSS​​

​​1.2、百度人脸识别​​

1、完善用户信息

用户在首次登录时需要完善个人信息,包括性别、昵称、生日、城市、头像等。其中,头像数据需要做图片上传,这里采用阿里云的OSS服务作为我们的图片服务器,并且对头像要做人脸识别,非人脸照片不得上传。

【探花交友】阿里云OSS、百度人脸识别_人脸识别

 

  • 首次登录时(手机号码不存在),需要创建用户存入数据库中
  • 客户端检测首次登录需要完善用户信息
  • 填写用户基本信息
  • 上传用户头像(需要人脸认证)

1.1、阿里云OSS

实现图片上传服务,需要有存储的支持,那么我们的解决方案将以下几种:

  1. 直接将图片保存到服务的硬盘(springmvc将的文件上传)
  1. 优点:开发便捷,成本低
  2. 缺点:扩容困难
  1. 使用分布式文件系统进行存储
  1. 优点:容易实现扩容
  2. 缺点:开发复杂度稍大(有成熟的产品可以使用,比如:FastDFS)
  1. 使用第三方的存储服务
  1. 优点:开发简单,拥有强大功能,免维护
  2. 缺点:付费

在本套课程中选用阿里云的OSS服务进行图片存储。

【探花交友】阿里云OSS、百度人脸识别_人脸识别_02

 

1.1.1、概述

对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。

地址:对象存储OSS_云存储服务_企业数据管理_存储-阿里云

 

1.1.2、账号申请

购买服务

使用第三方服务最大的缺点就是需要付费,下面,我们看下如何购买开通服务。

【探花交友】阿里云OSS、百度人脸识别_java_03

【探花交友】阿里云OSS、百度人脸识别_人脸识别_04

 购买下行流量包: (不购买也可以使用,按照流量付费)

【探花交友】阿里云OSS、百度人脸识别_项目_05

 

说明:OSS的上行流量是免费的,但是下行流量是需要购买的。

创建Bucket

使用OSS,首先需要创建Bucket,Bucket翻译成中文是水桶的意思,把存储的图片资源看做是水,想要盛水必须得有桶,就是这个意思了。

进入控制台,阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

【探花交友】阿里云OSS、百度人脸识别_毕设_06

 

选择Bucket后,即可看到对应的信息,如:url、消耗流量等 :

【探花交友】阿里云OSS、百度人脸识别_交友_07

 

文件管理:

【探花交友】阿里云OSS、百度人脸识别_毕设_08

 

查看文件:

【探花交友】阿里云OSS、百度人脸识别_项目_09

 

1.1.3、抽取模板工具

和发送短信类似,阿里云OSS也是采用自定义工具的形式进行封装

OssProperties

tanhua-autoconfig创建配置类


@Data
@ConfigurationProperties(prefix = "tanhua.oss")
public class OssProperties {

private String accessKey;
private String secret;
private String bucketName;
private String url; //域名
private String endpoint;
}


OssTemplate

tanhua-autoconfig创建模板对象


package com.tanhua.autoconfig.template;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.tanhua.autoconfig.properties.OssProperties;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class OssTemplate {

private OssProperties properties;

public OssTemplate(OssProperties properties) {
this.properties = properties;
}

/**
* 文件上传
* 1:文件名称
* 2:输入流
*/
public String upload(String filename, InputStream is) {
//3、拼写图片路径
filename = new SimpleDateFormat("yyyy/MM/dd").format(new Date())
+"/"+ UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));


// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
String endpoint = properties.getEndpoint();
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
String accessKeyId = properties.getAccessKey();
String accessKeySecret = properties.getSecret();

// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);

// 填写Byte数组。
// 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
ossClient.putObject(properties.getBucketName(), filename, is);

// 关闭OSSClient。
ossClient.shutdown();

String url = properties.getUrl() +"/" + filename;
return url;
}
}

TanhuaAutoConfiguration

TanhuaAutoConfiguration加入配置


@EnableConfigurationProperties({
SmsProperties.class,
OssProperties.class
})
public class TanhuaAutoConfiguration {

@Bean
public SmsTemplate smsTemplate(SmsProperties properties) {
return new SmsTemplate(properties);
}

@Bean
public OssTemplate ossTemplate(OssProperties properties) {
return new OssTemplate(properties);
}
}


1.1.4、测试

tanhua-app-server加入配置内容,并测试


tanhua:  
oss:
accessKey: LTAI4GKgob9vZ53k2SZdyAC7
secret: LHLBvXmILRoyw0niRSBuXBZewQ30la
endpoint: oss-cn-beijing.aliyuncs.com
bucketName: tanhua001
url: https://tanhua001.oss-cn-beijing.aliyuncs.com/


编写测试类


@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class OssTest {

@Autowired
private OssTemplate template;

@Test
public void testTemplateUpload() throws FileNotFoundException {
String path = "C:\\Users\\lemon\\Desktop\\课程资源\\02-完善用户信息\\03-资料\\2.jpg";
FileInputStream inputStream = new FileInputStream(new File(path));
String imageUrl = template.upload(path, inputStream);
System.out.println(imageUrl);
}
}


1.2、百度人脸识别

人脸识别(Face Recognition)基于图像或视频中的人脸检测、分析和比对技术,提供对您已获授权前提下的私有数据的人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求

1.2.1、概述

地址:​​人脸识别_人脸识别_准确率99.99%_免费试用-百度AI开放平台​

【探花交友】阿里云OSS、百度人脸识别_项目_10

1.2.2、账号申请

账号登录注册

百度云AI支持百度账号登录,也可以支持云账号。按需注册即可

【探花交友】阿里云OSS、百度人脸识别_人脸识别_11

创建应用

按需创建应用

【探花交友】阿里云OSS、百度人脸识别_交友_12

 

1.2.3、抽取模板工具

AipFaceProperties


@Data
@ConfigurationProperties("tanhua.aip")
public class AipFaceProperties {
private String appId;
private String apiKey;
private String secretKey;

@Bean
public AipFace aipFace() {
AipFace client = new AipFace(appId, apiKey, secretKey);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
}


AipFaceTemplate


package com.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

public class AipFaceTemplate {

@Autowired
private AipFace client;

/**
* 检测图片中是否包含人脸
* true:包含
* false:不包含
*/
public boolean detect(String imageUrl) {
// 调用接口
String imageType = "URL";

HashMap<String, String> options = new HashMap<String, String>();
options.put("face_field", "age");
options.put("max_face_num", "2");
options.put("face_type", "LIVE");
options.put("liveness_control", "LOW");

// 人脸检测
JSONObject res = client.detect(imageUrl, imageType, options);
System.out.println(res.toString(2));

Integer error_code = (Integer) res.get("error_code");

return error_code == 0;
}
}


1.2.4、测试

tanhua-app-server加入百度AI的配置信息


tanhua:
aip:
appId: 24021388
apiKey: ZnMTwoETXnu4OPIGwGAO2H4G
secretKey: D4jXShyinv5q26bUS78xRKgNLnB9IfZh

编写单元测试类


@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class FaceTest {


@Autowired
private AipFaceTemplate template;

@Test
public void detectFace() {
String image = "https://tanhua001.oss-cn-beijing.aliyuncs.com/2021/04/19/a3824a45-70e3-4655-8106-a1e1be009a5e.jpg";
boolean detect = template.detect(image);
}
}

  

标签:人脸识别,String,OSS,private,properties,class,探花,public
From: https://blog.51cto.com/u_15680317/5997076

相关文章

  • 34、商品服务--品牌管理--OSS获取服务端签名
    1、阿里云参考代码位置如下2、我们单独创建一个模块,来编写有关第三方服务的代码2.1)注册中心和配置中心的相关配置2.2)oss的配置编写2.3)controller编写packagecom.......
  • 33、商品服务--品牌管理--OSS整合测试
    1、使用SDK进行上传就到达了如下地址,按照里面步骤来即可https://help.aliyun.com/document_detail/32008.htm?spm=a2c4g.11186623.0.0.6e0340c2gZddUH#conc......
  • 人脸识别的圆框
    <template> <viewclass="camera-box"> <cameraclass="camera-el"frame-size="large"device-position="front" flash="off"@error="error"></camera>......
  • Crossing River
    CrossingRiver题解:贪心经典过河问题:先对过河的人速度降序排序,我们要知道想要使时间最短,有两个地方可以贪心1.去的时候减少时间浪费,就是说让两个速度慢的人一起过河2.......
  • OSS Insight 2022 All In One
    OSSInsight2022AllInOneOpenSourceHighlights:TrendsandInsightsfromGitHub2022|OSSInsighthttps://ossinsight.io/2022/https://live.ossinsight.io/......
  • 集成阿里云oss
     1. 开通“对象存储OSS”服务(1)申请阿里云账号(2)实名认证(3)开通“对象存储OSS”服务(4)进入管理控制台2. 创建Bucket在管理控制台中,新建Bucket,选......
  • Java人脸识别相册分类按时间分类相册按城市分类相册app源码
    简介后台Java,前台mui开发的androidapp;主要是按拍摄时间,人脸,城市进行相册照片的分类。演示视频https://www.bilibili.com/video/BV1XP4y187rA/?share_source=copy_web&v......
  • 基于隐式反馈的推荐算法MFLogLoss
    论文:ChristopherC.Johnson.Logisticmatrixfactorizationforimplicitfeedbackdata[C].In:ProceedingsoftheWorkshoponDistributedMachineLearningandM......
  • [Typescript] Extract the Result From Several Possible Function Shapes
    Let'simagineyou'rebuildingatypehelpertoextractoutthevaluefromseveraldifferent'parsers'.Hereareafewdifferentexamplesofwhataparsercanb......
  • aliyun oss操作汇总
    STS介绍阿里云STS(SecurityTokenService)是为阿里云账号(或RAM用户)提供短期访问权限管理的云服务。通过STS,您可以为联盟用户(您的本地账号系统所管理的用户)颁发一个自定义......