FaceSwap API
Last updated
Last updated
The FaceSwap API allows users to seamlessly swap faces between two images. The API processes two input images: the target
image where the swap will occur, and the swap
image whose face will replace the selected face on the target.
https://fapihub.com/v2/faceswap/image/
- Image Swapping UrlPOST
token
: A valid API token is required to authenticate the request.
Parameter
Type
Description
Required
target_url
string
The URL of the target image where the face will be swapped.
Yes
swap_url
string
The URL of the image containing the face to swap.
Yes
target_face_index
integer
The index of the face in the target image to be replaced (useful for images with multiple faces). In deafault, it is 0
No
Response Parameters:
Parameter
Type
Description
request_id
string
Unique ID for the API request.
status
string
Status of the request (e.g., OK
).
description
string
Additional information or null if no description.
err_code
string
Error code (0
indicates no error).
https://fapihub.com/v2/faceswap/video/
- Video Swapping UrlPOST
token
: A valid API token is required to authenticate the request.
Video FaceSwapping processess only first 4 seconds of the video and below, there is a way to process the whole video
Parameter
Type
Description
Required
target_url
string
The URL of the target video where the face will be swapped.
Yes
swap_url
string
The URL of the image containing the face to swap.
Yes
target_face_index
integer
The index of the face in the target video to be replaced (useful for images with multiple faces). In default, it is 0
No
Response Parameters:
Parameter
Type
Description
request_id
string
Unique ID for the API request.
status
string
Status of the request (e.g., OK
).
description
string
Additional information or null if no description.
err_code
string
Error code (0
indicates no error).
https://fapihub.com/v2/faceswap/result/
- Get ResultPOST
faceswap/result/
is not counted as a request usage. However, on the graph, result/
endpoint usages are added for success
and errors
line.
token
: A valid API token is required to authenticate the request.
Parameter
Type
Description
Required
request_id
string
The unique ID of the face swap request to retrieve results for.
Yes
Response Parameters:
Parameter
Type
Description
request_id
string
Unique ID for the original face swap request.
status
string
Status of the request (e.g., OK
).
duration
string
Time taken to process the face swap request.
total_duration
string
Total time including network and processing.
result_url
string
URL of the final processed image.
masks
null
Reserved for future use.
answers
null
Reserved for future use.
When your target image contains multiple people, you may want to swap the face of a specific individual, such as the person in the middle. Is this possible with the FaceSwap API? Absolutely!
To achieve this, the API provides the target_face_index
parameter, available exclusively in the image-swapping endpoint. This parameter allows you to specify the face to be replaced by its index.
Key Points:
Indexing Starts at 0: The first face in the image is assigned an index of 0
, the second face is 1
, and so on.
Default Value: If no value is specified, the API automatically uses 0
(the first detected face).
Ease of Selection: Selecting the correct face might seem tricky, but visualizing indices makes it straightforward.
Example:
In the image below, faces are indexed for clarity. For instance:
To swap the face of the person in the middle (Index 2
in the first image), set target_face_index=2
.
For the second example, the person with Index 3
can be targeted similarly.
This API processes only the first 4 seconds of any uploaded video. For instance, if you upload a 35-second video, only the first 4 seconds will be processed.
How to Process an Entire Video?
To process the whole video, you need to split it into smaller 4-second segments and upload each segment individually. While this approach increases the number of API requests, it allows for faster processing by leveraging concurrent requests using threading or asynchronous methods.
Once all segments are processed, you can combine the results to cover the entire video. Video splitting can be done using various programming languages. Below is an example in Python using the moviepy
library:
Key Points to Consider:
Concurrency: Use threading or asynchronous techniques to send multiple API requests simultaneously, speeding up the processing.
Recombination: After obtaining results for all segments, you can recombine them to create the final processed video.
Compatibility: Ensure your videos are properly formatted and compatible with the API requirements.
By following this approach, you can efficiently process videos of any length while utilizing the API's capabilities to their fullest.
Publicly Accessible URLs: Ensure that target_url
and swap_url
are publicly accessible
Multiple Faces: For images with multiple detected faces, use the target_face_index
parameter to specify which face to replace. Indexing begins at 0
.
High-Quality Images: For the best results, upload clear, well-lit images. Avoid blurry or obscured faces.
Video Processing: This API processes up to 4 seconds of video by default. To handle longer videos, split them into 4-second clips and upload them in parallel for faster processing.
Output Formats: Ensure your input and output files match supported formats (e.g., .jpg
, .png
, .mp4
).
Error Handling: Use the API’s response codes to manage errors effectively. For example, a 400
status indicates a bad request, while 500
denotes a server-side issue and server errors are also not counted as a request usage
Threading for Performance: To optimize request handling for multiple images or videos, implement threading or asynchronous processing in your code.