Method to realize object detection and intelligent classification of pictures using PHP and Qiniu cloud storage interface

王林
Release: 2023-07-05 20:06:01
Original
905 people have browsed it

Method of using PHP and Qiniu cloud storage interface to implement object detection and intelligent classification of pictures

Extracting useful information from pictures, such as object detection and intelligent classification, is an important task in the field of computer vision. Qiniu Cloud Storage, as a cloud service provider with powerful storage and computing capabilities, provides developers with convenient interfaces to achieve these tasks. In this article, we will introduce how to use PHP and Qiniu cloud storage interface to implement object detection and intelligent classification of pictures.

First, we need to create a Qiniu Cloud Storage account and obtain the access key (Access Key and Secret Key). These keys will be used for authentication and access control.

Next, we need to install the PHP SDK, which provides interactive functions with the Qiniu cloud storage interface. You can download the SDK from the Qiniu Cloud official website and extract it to your project directory.

In the code, we first need to introduce the SDK and configure the access key:

require_once 'path/to/sdk/autoload.php';
use QiniuAuth;
use QiniuStorageUploadManager;

// 需要填写你的 Access Key 和 Secret Key
$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';

// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
Copy after login

Next, we can use the SDK interface to upload images to Qiniu Cloud Storage and obtain the Image address:

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

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

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

// 获取上传后的图片地址
$imageUrl = $ret['key'];
Copy after login

Now, we can use the Qiniu cloud storage interface for image processing. Qiniu Cloud Storage provides a wealth of image processing functions, including image scaling, cropping, rotation, etc. Here, we will focus on how to perform object detection and intelligent classification.

For object detection, Qiniu Cloud Storage provides AI-based image processing capabilities. We can use the interface of Qiniu Cloud Storage to send a request to perform object detection on the uploaded pictures and obtain the detection results:

// 构建物体检测的请求参数
$requestUrl = 'http://ai.qiniuapi.com/v1/image/realtime?detectType=3&returnLabels=1';

// 发送请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $requestUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['url' => $imageUrl]));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($curl);
curl_close($curl);

// 解析返回的结果
$resultArray = json_decode($result, true);
$labels = $resultArray['result'][0]['labels'];

// 打印物体标签
foreach ($labels as $label) {
    echo $label['name'] . "
";
}
Copy after login

For intelligent classification, Qiniu Cloud Storage can also intelligently classify pictures based on the image content. . We can use the Qiniu Cloud Storage interface to send requests and obtain classification results:

// 构建智能分类的请求参数
$requestUrl = 'http://ai.qiniuapi.com/v1/image/censor?scenes=';
$scenes = ['pulp', 'terror', 'politician', 'ads', 'live', 'sensitive'];
$requestUrl .= implode(',', $scenes);

// 发送请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $requestUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['url' => $imageUrl]));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($curl);
curl_close($curl);

// 解析返回的结果
$resultArray = json_decode($result, true);
$censorResults = $resultArray['result'];

// 打印分类结果
foreach ($censorResults as $scene => $censorResult) {
    echo $scene . ': ' . ($censorResult['suggestion'] == 0 ? '正常' : '不确定') . "
";
}
Copy after login

Through the above code examples, we can use PHP and the Qiniu Cloud Storage interface to perform object detection and intelligent classification of images. You can adjust the code to suit your project based on actual needs. Hope this article is helpful to you!

The above is the detailed content of Method to realize object detection and intelligent classification of pictures using PHP and Qiniu cloud storage interface. For more information, please follow other related articles on the PHP Chinese website!

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