5
头图

demand

ID photos are also needed at any time in our lives, such as to apply for a certificate, physical examination, or to take an exam, but not everyone knows the P picture. Sometimes you don’t need to ask for an electronic ID photo to take a photo in a photo studio, so You can use technology to develop an online tool that instantly produces pictures.

ready

This article uses the free portrait segmentation API provided by Baidu AI open platform to achieve matting. Baidu is very conscientious. I have seen a lot of the same API, and others have to spend money. The number of free experience is only a few or dozens of calls. , And Baidu directly is 50,000 times! Moreover, Baidu is more professional in AI, and the results are very good!

image.png

1. Apply for API first
https://ai.baidu.com/tech/body/seg

2. You can get the API Key and Secret Key after creating the application

3. Development documentation
https://ai.baidu.com/ai-doc/BODY/Fk3cpyxua

4. Write code
(1) Obtain access_token
Obtain the access_token document: https://ai.baidu.com/docs#/Auth/top

Code

Get access_token code

<?php
header("Content-type:application/json");
 
//初始化 CURL
$ch = curl_init();
 
//目标服务器地址 
curl_setopt($ch, CURLOPT_URL, 'https://aip.baidubce.com/oauth/2.0/token');
 
//设置上传的文件
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
 'grant_type' => 'client_credentials',
 'client_id' => '填写你的',
 'client_secret' => '填写你的'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
// 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
 
//获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
//请求头数组
$headers[] = "user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36";
//设置请求头
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
 
//发起请求
$result = curl_exec($ch);
echo $result;
 
//关闭请求
curl_close($ch);
 
?>

Image to base64 tool: http://tool.chinaz.com/tools/imgtobase

image.png

Request interface, portrait segmentation code

<?php
header("Content-type:text/html;charset=utf-8");
 
//初始化 CURL
$ch = curl_init();
 
//目标服务器地址 
curl_setopt($ch, CURLOPT_URL, 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg');
 
//设置上传的文件
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
 'access_token' => '上一步获取到的access_token',
 'image' => '这里要用base64去掉图片头的base64代码'
 
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
// 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
 
//获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
//请求头数组
$headers[] = "Content-Type:application/x-www-form-urlencoded";
//设置请求头
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
 
//发起请求
$result = curl_exec($ch);
 
// 解析json
$arr_result = json_decode($result);
$imgurl = $arr_result->foreground;
 
// 输出图片
echo "<img src='./8.jpg' style='background:#f00'/>";
echo "<img src='data:image/png;base64,".$imgurl."' style='background:#f00'/><br/>";
echo "<img src='data:image/png;base64,".$imgurl."' style='background:#39f'/>";
echo "<img src='data:image/png;base64,".$imgurl."' style='background:#fff'/>";
 
//关闭请求
curl_close($ch);
 
?>

Finally, it returns the base64 picture code of the successful matting, also without the picture header, if you need to display the picture, you have to stitch data:image/jpg;base64, and that's it!

effect

Conclusion

This article is just a request process. If you want to make a usable request, you need to complete the image upload and automatically convert it to base64, remove the image header, automatically obtain the access_token and cache it (because of the validity period), and finally return a variety of background colors. Fixed size image json data.

Author

Author:TANKING
Date:2021-04-25
Web:http://www.likeyuns.com
WeChat:sansure2016


TANKING
4.8k 声望493 粉丝

热爱分享,热爱创作,热爱研究。