- Getting Started: Start the Backend Service
- API Function Descriptions
- cURL Request Examples
- Python Request Examples
- Java Request Examples
- JavaScript Request Examples
Before making API requests, please start the backend service
python deploy_api.py
API Name: idphoto
The logic of the Generate ID Photo
API is to send an RGB image and output a standard ID photo and a high-definition ID photo:
- High-definition ID Photo: The ID photo created based on the aspect ratio of
size
, with the filename beingoutput_image_dir
with the_hd
suffix added. - Standard ID Photo: The size is equal to
size
, scaled from the high-definition ID photo, with the filename beingoutput_image_dir
.
It is important to note that both generated photos are transparent (RGBA four-channel images). To generate a complete ID photo, the following Add Background Color
API is needed.
Q: Why is it designed this way?
A: Because in actual products, users often switch background colors to preview effects frequently. Providing a transparent background image allows for a better experience with color composition done by frontend JS code.
API Name: add_background
The logic of the Add Background Color
API is to send an RGBA image and add a background color based on color
, resulting in a JPG image.
API Name: generate_layout_photos
The logic of the Generate Six-Inch Layout Photo
API is to send an RGB image (typically the ID photo after adding background color) and arrange the photos based on size
, resulting in a six-inch layout photo.
API Name: human_matting
The logic of the Human Matting
API is to send an RGB image and output a standard matting portrait and a high-definition matting portrait (with no background fill).
cURL is a command-line tool for transferring data using various network protocols. Below are examples of using cURL to call these APIs.
curl -X POST "http://127.0.0.1:8080/idphoto" \
-F "input_image=@demo/images/test0.jpg" \
-F "height=413" \
-F "width=295" \
-F "human_matting_model=hivision_modnet" \
-F "face_detect_model=mtcnn"
curl -X POST "http://127.0.0.1:8080/add_background" \
-F "[email protected]" \
-F "color=638cce" \
-F "kb=200" \
-F "render=0"
curl -X POST "http://127.0.0.1:8080/generate_layout_photos" \
-F "[email protected]" \
-F "height=413" \
-F "width=295" \
-F "kb=200"
curl -X POST "http://127.0.0.1:8080/human_matting" \
-F "input_image=@demo/images/test0.jpg" \
-F "human_matting_model=hivision_modnet"
import requests
url = "http://127.0.0.1:8080/idphoto"
input_image_path = "images/test0.jpg"
files = {"input_image": open(input_image_path, "rb")}
data = {"height": 413, "width": 295, "human_matting_model": "hivision_modnet", "face_detect_model": "mtcnn"}
response = requests.post(url, files=files, data=data).json()
# response is a JSON formatted dictionary containing status, image_base64_standard, and image_base64_hd
print(response)
import requests
url = "http://127.0.0.1:8080/add_background"
input_image_path = "test.png"
files = {"input_image": open(input_image_path, "rb")}
data = {"color": '638cce', "kb": None, "render": 0}
response = requests.post(url, files=files, data=data).json()
# response is a JSON formatted dictionary containing status and image_base64
print(response)
import requests
url = "http://127.0.0.1:8080/generate_layout_photos"
input_image_path = "test.jpg"
files = {"input_image": open(input_image_path, "rb")}
data = {"height": 413, "width": 295, "kb": 200}
response = requests.post(url, files=files, data=data).json()
# response is a JSON formatted dictionary containing status and image_base64
print(response)
import requests
url = "http://127.0.0.1:8080/human_matting"
input_image_path = "test.jpg"
files = {"input_image": open(input_image_path, "rb")}
data = {"human_matting_model": "hivision_modnet"}
response = requests.post(url, files=files, data=data).json()
# response is a JSON formatted dictionary containing status and image_base64
print(response)
python requests_api.py -u <URL> -t <TYPE> -i <INPUT_IMAGE_DIR> -o <OUTPUT_IMAGE_DIR> [--height <HEIGHT>] [--width <WIDTH>] [-c <COLOR>] [-k <KB>]
-
-u
,--url
- Description: The URL of the API service.
- Default Value:
http://127.0.0.1:8080
-
-t
,--type
- Description: The type of API request.
- Default Value:
idphoto
-
-i
,--input_image_dir
- Description: The path to the input image.
- Required: Yes
- Example:
./input_images/photo.jpg
-
-o
,--output_image_dir
- Description: The path to save the image.
- Required: Yes
- Example:
./output_images/processed_photo.jpg
-
--face_detect_model
- Description: Face detection model
- Default Value: mtcnn
-
--human_matting_model
- Description: Human matting model
- Default Value: hivision_modnet
-
--height
- Description: The height of the output size for the standard ID photo.
- Default Value: 413
-
--width
- Description: The width of the output size for the standard ID photo.
- Default Value: 295
-
-c
,--color
- Description: Add background color to the transparent image, format as Hex (e.g., #638cce), only effective when type is
add_background
- Default Value:
638cce
- Description: Add background color to the transparent image, format as Hex (e.g., #638cce), only effective when type is
-
-k
,--kb
- Description: The KB value of the output photo, only effective when type is
add_background
orgenerate_layout_photos
, no setting when the value is None. - Default Value:
None
- Example: 50
- Description: The KB value of the output photo, only effective when type is
-
-r
,--render
- Description: The rendering method for adding background color to the transparent image, only effective when type is
add_background
orgenerate_layout_photos
- Default Value: 0
- Description: The rendering method for adding background color to the transparent image, only effective when type is
python requests_api.py \
-u http://127.0.0.1:8080 \
-t idphoto \
-i ./photo.jpg \
-o ./idphoto.png \
--height 413 \
--width 295 \
--face_detect_model mtcnn \
--human_matting_model hivision_modnet
python requests_api.py \
-u http://127.0.0.1:8080 \
-t add_background \
-i ./idphoto.png \
-o ./idphoto_with_background.jpg \
-c 638cce \
-k 50 \
-r 0
python requests_api.py \
-u http://127.0.0.1:8080 \
-t generate_layout_photos \
-i ./idphoto_with_background.jpg \
-o ./layout_photo.jpg \
--height 413 \
--width 295 \
-k 200
python requests_api.py \
-u http://127.0.0.1:8080 \
-t human_matting \
-i ./photo.jpg \
-o ./photo_matting.png \
--human_matting_model hivision_modnet
- If more than one face is detected in the photo, the request will fail.
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
/**
* Generate ID Photo (Transparent Background) /idphoto API
* @param inputImageDir File path
* @return
* @throws IOException
*/
public static String requestIdPhoto(String inputImageDir) throws IOException {
String url = BASE_URL+"/idphoto";
// Create file object
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
// Contains status, image_base64_standard, and image_base64_hd
return HttpUtil.post(url, paramMap);
}
/**
* Add Background Color /add_background API
* @param inputImageDir File path
* @return
* @throws IOException
*/
public static String requestAddBackground(String inputImageDir) throws IOException {
String url = BASE_URL+"/add_background";
// Create file object
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("color","638cce");
paramMap.put("kb","200");
// response is a JSON formatted dictionary containing status and image_base64
return HttpUtil.post(url, paramMap);
}
/**
* Generate Six-Inch Layout Photo /generate_layout_photos API
* @param inputImageDir File path
* @return
* @throws IOException
*/
public static String requestGenerateLayoutPhotos(String inputImageDir) throws IOException {
String url = BASE_URL+"/generate_layout_photos";
// Create file object
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
paramMap.put("kb","200");
// response is a JSON formatted dictionary containing status and image_base64
return HttpUtil.post(url, paramMap);
}
/**
* Generate Human Matting Photo /human_matting API
* @param inputImageDir File path
* @return
* @throws IOException
*/
public static String requestHumanMattingPhotos(String inputImageDir) throws IOException {
String url = BASE_URL+"/human_matting";
// Create file object
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
// Contains status and image_base64
return HttpUtil.post(url, paramMap);
}
In JavaScript, we can use the fetch
API to send HTTP requests. Below are examples of how to call these APIs using 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/test0.jpg", 413, 295).then(response => {
console.log(response);
});
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);
});
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);
});
async function uploadImage(inputImagePath) {
const url = "http://127.0.0.1:8080/human_matting";
const formData = new FormData();
formData.append("input_image", new File([await fetch(inputImagePath).then(res => res.blob())], "test.jpg"));
const response = await fetch(url, {
method: 'POST',
body: formData
});
const result = await response.json(); // Assume the response is in JSON format
console.log(result);
return result;
}
// Example call
uploadImage("demo/images/test0.jpg").then(response => {
console.log(response);
});