对象存储 OSS:人脸检测

对象存储 OSS:人脸检测

Java要求使用3.17.4及以上版本的Java SDK。

import com.aliyun.oss.ClientBuilderConfiguration;

import com.aliyun.oss.OSS;

import com.aliyun.oss.OSSClientBuilder;

import com.aliyun.oss.common.auth.CredentialsProviderFactory;

import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

import com.aliyun.oss.common.comm.SignVersion;

import com.aliyun.oss.model.OSSObject;

import com.aliyun.oss.model.GetObjectRequest;

import com.aliyuncs.exceptions.ClientException;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

public class Demo1 {

public static void main(String[] args) throws ClientException, ClientException {

// yourEndpoint填写Bucket所在地域对应的Endpoint。

String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

// 填写Endpoint对应的Region信息,例如cn-hangzhou。

String region = "cn-hangzhou";

// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。

EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

// 指定Bucket名称。

String bucketName = "examplebucket";

// 如果图片位于Bucket根目录,则直接填写图片名称。如果图片不在Bucket根目录,需携带图片完整路径,例如exampledir/example.jpg。

String key = "example.jpg";

// 创建OSSClient实例。

// 当OSSClient实例不再使用时,调用shutdown方法以释放资源。

ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();

clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);

OSS ossClient = OSSClientBuilder.create()

.endpoint(endpoint)

.credentialsProvider(credentialsProvider)

.clientConfiguration(clientBuilderConfiguration)

.region(region)

.build();

try {

// 构建人脸检测的处理指令。

GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);

getObjectRequest.setProcess("image/faces");

// 使用getObject方法,并通过process参数传入处理指令。

OSSObject ossObject = ossClient.getObject(getObjectRequest);

// 读取并打印信息。

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int bytesRead;

while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {

baos.write(buffer, 0, bytesRead);

}

String imageFaces = baos.toString("UTF-8");

System.out.println("Image Faces:");

System.out.println(imageFaces);

} catch (IOException e) {

System.out.println("Error: " + e.getMessage());

} finally {

// 关闭OSSClient。

ossClient.shutdown();

}

}

}Python要求使用Python SDK 2.18.4及以上版本。

# -*- coding: utf-8 -*-

import oss2

from oss2.credentials import EnvironmentVariableCredentialsProvider

# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。

auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。

endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'

# 填写阿里云通用Region ID。

region = 'cn-hangzhou'

bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# 如果图片位于Bucket根目录,则直接填写图片名称。如果图片不在Bucket根目录,需携带图片完整路径,例如exampledir/example.jpg。

key = 'example.jpg'

# 构建人脸检测的处理指令。

process = 'image/faces'

try:

# 使用get_object方法,并通过process参数传入处理指令。

result = bucket.get_object(key, process=process)

# 读取并打印信息。

image_faces = result.read().decode('utf-8')

print("Image Faces:")

print(image_faces)

except oss2.exceptions.OssError as e:

print("Error:", e)Go要求使用Go SDK 3.0.2及以上版本。

package main

import (

"fmt"

"io"

"os"

"github.com/aliyun/aliyun-oss-go-sdk/oss"

)

func main() {

// 从环境变量中获取临时访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。

provider, err := oss.NewEnvironmentVariableCredentialsProvider()

if err != nil {

fmt.Println("Error:", err)

os.Exit(-1)

}

// 创建OSSClient实例。

// yourEndpoint填写Bucket对应的Endpoint,以华东1(杭州)为例,填写为https://oss-cn-hangzhou.aliyuncs.com。其他Region请按实际情况填写。

// yourRegion指定阿里云通用Region ID,例如cn-hangzhou。

client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))

if err != nil {

fmt.Println("Error:", err)

os.Exit(-1)

}

// 指定Bucket名称,例如examplebucket。

bucketName := "examplebucket"

bucket, err := client.Bucket(bucketName)

if err != nil {

fmt.Println("Error:", err)

os.Exit(-1)

}

// 如果图片位于Bucket根目录,则直接填写图片名称。如果图片不在Bucket根目录,需携带图片完整路径,例如exampledir/example.jpg。

// 通过oss.Process方法构建人脸检测的处理指令。

body, err := bucket.GetObject("example.jpg", oss.Process("image/faces"))

if err != nil {

fmt.Println("Error:", err)

os.Exit(-1)

}

defer body.Close()

data, err := io.ReadAll(body)

if err != nil {

fmt.Println("Error:", err)

os.Exit(-1)

}

fmt.Println("data:", string(data))

}PHP要求使用PHP SDK 2.7.0及以上版本。

if (is_file(__DIR__ . '/../autoload.php')) {

require_once __DIR__ . '/../autoload.php';

}

if (is_file(__DIR__ . '/../vendor/autoload.php')) {

require_once __DIR__ . '/../vendor/autoload.php';

}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;

use OSS\OssClient;

try {

// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。

$provider = new EnvironmentVariableCredentialsProvider();

// 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。

$endpoint = 'https://oss-cn-hangzhou.aliyuncs.com';

// 填写Bucket名称,例如examplebucket。

$bucket = 'examplebucket';

// 如果图片位于Bucket根目录,则直接填写图片名称。如果图片不在Bucket根目录,需携带图片完整路径,例如exampledir/example.jpg。

$key = 'example.jpg';

$config = array(

"provider" => $provider,

"endpoint" => $endpoint,

"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,

// 填写阿里云通用Region ID。

"region" => "cn-hangzhou"

);

$ossClient = new OssClient($config);

// 构建人脸检测的处理指令。

$options[$ossClient::OSS_PROCESS] = "image/faces";

$result = $ossClient->getObject($bucket,$key,$options);

var_dump($result);

} catch (OssException $e) {

printf($e->getMessage() . "\n");

return;

}

相关推荐

motiveIELTS重点词汇
必发365手机版下载

motiveIELTS重点词汇

📅 06-27 ⭐ 9836
IGBT如何选择,你真的了解吗?
365bet足球即时比分网

IGBT如何选择,你真的了解吗?

📅 07-10 ⭐ 8877
世界杯赛场上女球迷们人体彩绘,你能看出来美女没穿衣服吗?
最经典的餐厅经营游戏
365bat提现

最经典的餐厅经营游戏

📅 07-18 ⭐ 2503
一个房间应该装多少个筒灯?ilin教你做选择
必发365手机版下载

一个房间应该装多少个筒灯?ilin教你做选择

📅 07-03 ⭐ 9619
7 个最适合挖矿、最有利可图的加密货币
推荐阅读 ❤️