Magic Studio Eraser API使用教程

AI橡皮擦 - 使用网址

Magic Studio的AI橡皮擦功能非常好用,能去除图片中的杂物。但是网页版只支持低分辨率下载,想要原图就得开会员,价格不菲。

不过官网其实提供了API接入方式,并且有100次的免费试用机会

API接入网站

在这里可以查看AI橡皮擦的接入文档

1、先获取API Token

2、调用Magic Eraser API,传入待处理图片、待擦除部位图片(mask_file)、文件名

关于这个mask_file,官网并没有给出详细介绍,但是通过对网页版AI橡皮擦进行抓包可以发现,mask_file是一个二进制流文件

尝试对这个mask_file流文件进行拦截捕获,使用postman、charles、nginx均未实现,最终解决办法:安装chrome插件 Requestly

Requestly插件下载地址

安装完毕后,在Requestly管理页面,创建一条重定向规则

匹配规则为Equals,并将请求:https://ai-api.magicstudio.com/api/magic-erase

转发到:http://127.0.0.1:8000/saveBinary

http://127.0.0.1:8000/saveBinary 是我本地启动的java服务(下面有提供代码),将页面请求转发到本地,从而捕获mask_file流文件内容,并保存到本地

存到本地后,发现其实就是橡皮擦涂抹区域的图片

这样就拿到了mask_file,接下来就可以调用官方API进行原图无损擦除了。

附完整java代码:

package com.yechen.smm.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.io.File;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@CrossOrigin
@RestController
public class AController {

    /**
     * 图片地址
     */
    static String pic_path = "C:\\Users\\Administrator\\Pictures\\百丈漈瀑布.jpg";
    /**
     * mask地址
     */
    static String mask_path = "D:\\Temp\\mask.jpg";

    /**
     * 使用chrome浏览器Requestly插件,创建redirect规则,将:
     * https://ai-api.magicstudio.com/api/magic-erase
     * 转发到:
     * http://127.0.0.1:8000/saveBinary
     * 即可保存mask文件到本地
     *
     * @param mask_file
     * @return
     * @throws IOException
     */
    @PostMapping("/saveBinary")
    public String saveBinary(@RequestParam("mask_file") MultipartFile mask_file) throws IOException {
        FileUtil.writeBytes(mask_file.getBytes(), mask_path);
        return "ok";
    }


    @Test
    public void getToken() {
        JSONObject param = new JSONObject();
        param.set("client_id", "xxx");
        param.set("client_secret", "xxx");
        param.set("expiry_days", 4);
        String res = HttpUtil.post("https://api.magicstudio.com/auth/token", param.toString());
        JSONObject obj = JSONUtil.parseObj(res);
        String token = obj.getStr("token");

        res = HttpRequest.post("https://api.magicstudio.com/magiceraser/erase")
                .header("accessToken", token)
                .form("image_file", new File(pic_path))
                .form("mask_file", new File(mask_path))
                .form("filename", "filename.jpg")
                .execute()
                .body();
        System.out.println(res);
    }


}

需要引入lombok、hutool依赖:

<dependency>    
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.25</version>
</dependency>

只能说这个magic studio真的太坑爹了,用心做AI,用脚做API,要不是冲着100次的免费额度,真不想折腾这么大劲

`iptables` 是Linux系统中用于防火墙规则配置的工具。下面是安装和关闭它的基本步骤: **安装iptables:** 如果你使用的是基于Debian或Ubuntu的发行版,iptables通常已经预装,你可以直接使用包管理器来确认是否已安装。打开终端并输入: ```bash sudo apt update # 更新软件包列表 sudo apt install iptables # 如果未安装,将安装它 ``` 如果是CentOS或Fedora系列,可以使用`dnf`或`yum`命令: ```bash sudo dnf install iptables # 或者 sudo yum install iptables ``` **关闭iptables(仅临时禁用):** 如果你想暂时禁用iptables,可以使用 `iptables -P INPUT DROP` 和 `iptables -F` 来设置默认策略为拒绝所有连接,并清除现有的规则。这样系统不会应用任何新的防火墙规则,直到你再次启用。 ```bash sudo iptables -P INPUT DROP sudo iptables -F ``` 但是请注意,这仅仅是针对当前会话,重启后iptables还会自动启动。 **永久关闭iptables:** 如果你想完全禁止iptables服务开自启,可以在系统级别操作。以Ubuntu为例,在`/etc/init.d`目录下找到iptables脚本(通常是`iptables-persistent`),然后运行: ```bash sudo update-rc.d iptables-persistent disable ``` 这会使iptables在系统启动时不加载。 **开启iptables:** 如果之前禁用了,恢复服务的命令是: ```bash sudo service iptables start ``` 或者在Ubuntu中,可能会是 `sudo systemctl start iptables.service` **相关问题--:** 1. 怎么查看iptables的状态? 2. 如何保存iptables规则到开启动? 3. iptables有哪些主要的链和表?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值