每天网上各种人工智能的新闻,但是总觉得是某些大神的玩物。昨日偶然在网上看到一个网站--人工智能抠图工具 Remove.bg。
在线人像照片/物品图片背景快速去除工具,你只需要上传图片,它就会利用「人工智能」技术全自动帮你扣掉背景,5 秒钟就能给你一张透明的移除背景后的主体照片。整个过程非常的简单傻瓜,你完完全全不需要花费任何精力去自己抠图。主要是这玩意还全端支持 Win、Mac、Linux 客户端与 PS 插件都有。
上网站注册一个帐户,在个人中心里面会找到api key
登录SSH执行命令就可以愉快的P图了
curl -H 'X-API-Key: YOUR_API_KEY' \
-F 'image_file=@/path/to/file.jpg' \
-f https://api.remove.bg/v1.0/removebg -o no-bg.png
我的目的显然不是只为了就这样意淫一下,于是打开最近开发的网站项目,里面刚好有客户来图处理,加上自动抠图岂不是瞬间高大上了?废话少说,直接开始。
1、扩展后台接口
2、扩展Helper
public function isRemoveBGAvailable()
{
return Mage::getStoreConfig(self::XML_RB_PATH_APIKEY) && Mage::getStoreConfigFlag(self::XML_RB_PATH_ACTIVE);
}
public function getRemoveBGApiKey() {
return Mage::getStoreConfig(self::XML_RB_PATH_APIKEY);
}
3、扩展客户图片裁切方法,这里我直接使用了php的exec执行curl方法并返回图象地址到前台
public function cropAction() {
$crop = new CropAvatar(
isset($_POST['avatar_src']) ? $_POST['avatar_src'] : null,
isset($_POST['crop_data']) ? $_POST['crop_data'] : null,
isset($_FILES['avatar_file']) ? $_FILES['avatar_file'] : null
);
$response = array(
'state' => 200,
'message' => $crop -> getMsg(),
'result' => $crop -> getResult(),
'orginal' => $crop -> getOrginal()
);
if(isset($_POST['removebg']) && Mage::helper('googletag')->isRemoveBGAvailable()){
$key = Mage::helper('googletag')->getRemoveBGApiKey();
$apiKey = 'X-API-Key: '.$key;
$time = date('YmdHis');
$outfileName = $time.'nobg.png';
$path = Mage::getBaseDir('media').'/tmp/tmp/';
$inFile = 'image_file=@'.$crop -> getResult();
$outFile = $path.$outfileName;
$cmd = "curl -H '".$apiKey."' \-F '".$inFile."' \-f https://api.remove.bg/v1.0/removebg -o ".$outFile;
exec($cmd, $output, $return);
$response = array(
'state' => 200,
'message' => $crop -> getMsg(),
'result' => str_replace("/www/wwwroot/site1","",$outFile),
'orginal' => $crop -> getOrginal()
);
}
echo json_encode($response);
}
点击裁切图象就自动扣好了,直接让美工下岗。
写在最后,这个API商用是需要付费的,因为免费帐户抠图数量是有限制的。