Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add curl and js in api docs #60

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ python inference.py -t generate_layout_photos -i ./idhoto_ab.jpg -o ./idhoto_lay
python deploy_api.py
```


## 请求 API 服务 - Python Request

> 请求方式请参考 [API 文档](docs/api_CN.md),含 [cURL](docs/api_CN.md#curl-请求示例)、[Python](docs/api_CN.md#python-请求示例)、[Java](docs/api_CN.md#java-请求示例)、[Javascript](docs/api_CN.md#javascript-请求示例) 请求示例。

### 1. 证件照制作

输入 1 张照片,获得 1 张标准证件照和 1 张高清证件照的 4 通道透明 png
Expand Down Expand Up @@ -194,8 +197,6 @@ response = requests.post(url, files=files, data=data).json()
print(response)
```

更多请求方式请参考 [API 文档](docs/api_CN.md),含 Python 脚本请求、Python Request 请求、Java 请求。

<br>

# 🐳 Docker 部署
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ python deploy_api.py

## Request API Service - Python Request

> Please refer to the [API documentation](docs/api_EN.md) for the request method, including examples of requests using [cURL](docs/api_EN.md#curl-request-example), [Python](docs/api_EN.md#python-request-example), [Java](docs/api_EN.md#java-request-example), and [Javascript](docs/api_EN.md#javascript-request-example).

### 1. ID Photo Creation

Input 1 photo, receive 1 standard ID photo and 1 high-definition ID photo in 4-channel transparent PNG format.
Expand Down Expand Up @@ -190,8 +192,6 @@ response = requests.post(url, files=files, data=data).json()
print(response)
```

For more request methods, please refer to the [API documentation](docs/api_EN.md), including Python script requests, Python Request requests, and Java requests.

<br>

# 🐳 Docker deployment
Expand Down
119 changes: 119 additions & 0 deletions docs/api_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

- [开始之前:开启后端服务](#开始之前开启后端服务)
- [接口功能说明](#接口功能说明)
- [cURL 请求示例](#curl-请求示例)
- [Python 请求示例](#python-请求示例)
- [Python Requests 请求方法](#1️⃣-python-requests-请求方法)
- [Python 脚本请求方法](#2️⃣-python-脚本请求方法)
- [Java 请求示例](#java-请求示例)
- [Javascript 请求示例](#javascript-请求示例)

## 开始之前:开启后端服务

Expand Down Expand Up @@ -49,6 +51,40 @@ python delopy_api.py

<br>


## cURL 请求示例

cURL 是一个命令行工具,用于使用各种网络协议传输数据。以下是使用 cURL 调用这些 API 的示例。

### 1. 生成证件照(底透明)

```bash
curl -X POST "http://127.0.0.1:8080/idphoto" \
-F "input_image=@demo/images/test.jpg" \
-F "height=413" \
-F "width=295"
```

### 2. 添加背景色

```bash
curl -X POST "http://127.0.0.1:8080/add_background" \
-F "input_image=@test.png" \
-F "color=638cce" \
-F "kb=200"
```

### 3. 生成六寸排版照

```bash
curl -X POST "http://127.0.0.1:8080/generate_layout_photos" \
-F "input_image=@test.jpg" \
-F "height=413" \
-F "width=295" \
-F "kb=200"
```


## Python 请求示例

### 1️⃣ Python Requests 请求方法
Expand Down Expand Up @@ -431,3 +467,86 @@ public class Test {
}

```

## JavaScript 请求示例

在JavaScript中,我们可以使用`fetch` API来发送HTTP请求。以下是如何使用JavaScript调用这些API的示例。

### 1. 生成证件照(底透明)

```javascript
async function generateIdPhoto(inputImagePath, height, width) {
const url = "http://127.0.0.1:8080/idphoto";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.jpg"));
formData.append("height", height);
formData.append("width", width);

const response = await fetch(url, {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result);
return result;
}

// 示例调用
generateIdPhoto("images/test.jpg", 413, 295).then(response => {
console.log(response);
});
```

### 2. 添加背景色

```javascript
async function addBackground(inputImagePath, color, kb) {
const url = "http://127.0.0.1:8080/add_background";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.png"));
formData.append("color", color);
formData.append("kb", kb);

const response = await fetch(url, {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result);
return result;
}

// 示例调用
addBackground("test.png", "638cce", 200).then(response => {
console.log(response);
});
```

### 3. 生成六寸排版照

```javascript
async function generateLayoutPhotos(inputImagePath, height, width, kb) {
const url = "http://127.0.0.1:8080/generate_layout_photos";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.jpg"));
formData.append("height", height);
formData.append("width", width);
formData.append("kb", kb);

const response = await fetch(url, {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result);
return result;
}

// 示例调用
generateLayoutPhotos("test.jpg", 413, 295, 200).then(response => {
console.log(response);
});
```
117 changes: 117 additions & 0 deletions docs/api_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

- [Before You Start: Launch the Backend Service](#before-you-start-launch-the-backend-service)
- [Interface Function Descriptions](#interface-function-descriptions)
- [cURL Request Example](#curl-request-examples)
- [Python Request Example](#python-request-example)
- [Python Requests Method](#1️⃣-python-requests-method)
- [Python Script Method](#2️⃣-python-script-request-method)
- [Java Request Example](#java-request-example)
- [Javascript Request Example](#javascript-request-examples)

## Before You Start: Launch the Backend Service

Expand Down Expand Up @@ -49,6 +51,39 @@ The `Generate 6-inch Layout Photo` interface logic involves sending an RGB image

<br>


## cURL Request Examples

cURL is a command-line tool used to transfer data using various network protocols. Below are examples of how to use cURL to call these APIs.

### 1. Generate ID Photo (Transparent Background)

```bash
curl -X POST "http://127.0.0.1:8080/idphoto" \
-F "input_image=@demo/images/test.jpg" \
-F "height=413" \
-F "width=295"
```

### 2. Add Background Color

```bash
curl -X POST "http://127.0.0.1:8080/add_background" \
-F "input_image=@test.png" \
-F "color=638cce" \
-F "kb=200"
```

### 3. Generate Six-Inch Layout Photo

```bash
curl -X POST "http://127.0.0.1:8080/generate_layout_photos" \
-F "input_image=@test.jpg" \
-F "height=413" \
-F "width=295" \
-F "kb=200"
```

## Python Request Example

### 1️⃣ Python Requests Method
Expand Down Expand Up @@ -430,5 +465,87 @@ public class Test {
}
}
}
```

## JavaScript Request Examples

In JavaScript, we can use the `fetch` API to send HTTP requests. Below are examples of how to call these APIs using JavaScript.

### 1. Generate ID Photo (Transparent Background)

```javascript
async function generateIdPhoto(inputImagePath, height, width) {
const url = "http://127.0.0.1:8080/idphoto";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.jpg"));
formData.append("height", height);
formData.append("width", width);

const response = await fetch(url, {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result);
return result;
}

// Example call
generateIdPhoto("images/test.jpg", 413, 295).then(response => {
console.log(response);
});
```

### 2. Add Background Color

```javascript
async function addBackground(inputImagePath, color, kb) {
const url = "http://127.0.0.1:8080/add_background";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.png"));
formData.append("color", color);
formData.append("kb", kb);

const response = await fetch(url, {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result);
return result;
}

// Example call
addBackground("test.png", "638cce", 200).then(response => {
console.log(response);
});
```

### 3. Generate Six-Inch Layout Photo

```javascript
async function generateLayoutPhotos(inputImagePath, height, width, kb) {
const url = "http://127.0.0.1:8080/generate_layout_photos";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.jpg"));
formData.append("height", height);
formData.append("width", width);
formData.append("kb", kb);

const response = await fetch(url, {
method: 'POST',
body: formData
});

const result = await response.json();
console.log(result);
return result;
}

// Example call
generateLayoutPhotos("test.jpg", 413, 295, 200).then(response => {
console.log(response);
});
```