Method of using PHP and Qiniu cloud storage interface to realize artificial intelligence analysis and automatic label generation of pictures

WBOY
Release: 2023-07-06 17:38:01
Original
1004 people have browsed it

Using PHP and Qiniu Cloud Storage Interface to Realize Artificial Intelligence Analysis and Automatic Tag Generation of Pictures

Introduction:
With the development of artificial intelligence, image recognition and understanding technology has been used in various fields. Has been widely used. Among them, the use of artificial intelligence technology to automatically generate tags for images can greatly improve the efficiency of image management and retrieval. This article will introduce how to use PHP and Qiniu cloud storage interface to implement artificial intelligence analysis and automatic label generation of images, and come with code examples.

Preparation:

  1. Register a Qiniu cloud storage account and obtain the Access Key and Secret Key.
  2. Install the PHP SDK of Qiniu Cloud Storage. It can be installed through composer, using the following command:

    composer require qiniu/php-sdk
    Copy after login

Step 1: Upload images to Qiniu Cloud Storage
First, we need to upload images to Qiniu Cloud Storage. This step can be easily achieved using the PHP SDK of Qiniu Cloud Storage.

<?php
require_once '/path/to/autoload.php';

use QiniuAuth;
use QiniuStorageUploadManager;

// 构建鉴权对象
$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';
$auth = new Auth($accessKey, $secretKey);

// 生成上传Token
$bucket = 'YOUR_BUCKET';
$token = $auth->uploadToken($bucket);

//要上传文件的本地路径
$filePath = '/path/to/image.jpg';

// 上传到七牛云存储
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, null, $filePath);

if ($err !== null) {
    echo '图片上传失败:' . $err->message();
} else {
    $imageUrl = $ret['key'];  // 上传成功后的图片地址
    echo '图片上传成功,图片地址:' . $imageUrl;
}
?>
Copy after login

Step 2: Call the artificial intelligence analysis interface of Qiniu Cloud Storage
Qiniu Cloud Storage provides a rich artificial intelligence analysis interface, including image classification, image tags, image content review, etc. Here we take the image tag interface as an example to analyze the newly uploaded image and generate automatic tags.

<?php
require_once '/path/to/autoload.php';

use QiniuAuth;
use QiniuStorageBucketManager;

// 构建鉴权对象
$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';
$auth = new Auth($accessKey, $secretKey);

// 初始化BucketManager类
$bucketManager = new BucketManager($auth);

// 要分析的图片地址
$imageUrl = 'http://YOUR_DOMAIN/' . $imageUrl;

// 调用图像标签接口
$analysis = $bucketManager->apiCall('/image/v2/pulp', 'POST', [
    'url' => $imageUrl  // 图片地址
]);

if ($analysis[0] === null) {
    echo '图片分析失败:' . $analysis[1];
} else {
    $labels = $analysis[0]['labels'];  // 分析结果中的标签数组
    echo '图片分析结果:';
    var_dump($labels);
}
?>
Copy after login

Step 3: Implement automatic tag generation
In step 2, we have obtained the analysis results of the image. Now we can use these results to generate automatic tags and add them to the metadata of the image. .

<?php
// 在上面的代码中,我们已经获取到了分析结果的标签数组$labels

// 将标签数组转换为以逗号分隔的字符串,方便存储
$tagString = implode(',', $labels);

// 将自动标签添加到图片的元数据中
$bucketManager->setImageInfo($bucket, $imageUrl, [
    'x-qn-meta-tags' => $tagString
]);

echo '自动标签生成并添加成功!';
?>
Copy after login

Summary:
This article introduces how to use PHP and Qiniu cloud storage interface to implement artificial intelligence analysis and automatic label generation of images. By uploading pictures to Qiniu Cloud Storage, calling the artificial intelligence interface for image analysis, and generating automatic tags from the analysis results and adding them to the metadata of the pictures, the efficiency of picture management and retrieval can be greatly improved. Using Qiniu Cloud Storage's PHP SDK, we can easily implement these functions. Hope this article is helpful to everyone.

The above is the detailed content of Method of using PHP and Qiniu cloud storage interface to realize artificial intelligence analysis and automatic label generation of pictures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!