smart-interactive-display/Assets/StreamingAssets/MergeFace/test.py

67 lines
2.3 KiB
Python
Raw Normal View History

2024-06-27 01:08:10 -07:00
import requests
import base64
2024-06-21 01:20:01 -07:00
2024-06-27 01:08:10 -07:00
def download_image_from_url(url, output_file_path):
"""
Downloads an image from a URL and saves it to a local file.
:param url: The URL of the image to download.
:param output_file_path: The path where the downloaded image will be saved.
"""
try:
# Send a GET request to the URL
image_response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Save the image data to a file
with open(output_file_path, "wb") as image_file:
image_file.write(image_response.content)
print(f"Image saved as {output_file_path}")
else:
print(f"Failed to download image. HTTP status code: {image_response.status_code}")
except Exception as e:
print(f"An error occurred: {e}")
swap_url = "https://faceswap3.p.rapidapi.com/faceswap/v1/image"
payload = ("-----011000010111000001101001\r\nContent-Disposition: form-data; "
"name=\"target_url\"\r\n\r\nhttps://i.ibb.co/kh2XYLh/vnpdtvnvsu22vn-27122020-x-108-jpg.jpg\r\n"
"-----011000010111000001101001\r\nContent-Disposition: form-data; "
"name=\"swap_url\"\r\n\r\nhttps://storage.gadgame.com/play4promo/sid/final_image.jpg\r\n"
"-----011000010111000001101001--\r\n\r\n")
headers = {
"x-rapidapi-key": "34c3c2de43msh9d754fc788c3d36p15c896jsn8796ea559bce",
"x-rapidapi-host": "faceswap3.p.rapidapi.com",
"Content-Type": "multipart/form-data; boundary=---011000010111000001101001"
}
response = requests.post(swap_url, data=payload, headers=headers)
result = response.json()
request_id = result["image_process_response"]["request_id"]
result_url = "https://faceswap3.p.rapidapi.com/result/"
payload = ("-----011000010111000001101001\r\nContent-Disposition: form-data; "
f"name=\"request_id\"\r\n\r\n{request_id}\r\n-----011000010111000001101001--\r\n\r\n")
headers = {
"x-rapidapi-key": "34c3c2de43msh9d754fc788c3d36p15c896jsn8796ea559bce",
"x-rapidapi-host": "faceswap3.p.rapidapi.com",
"Content-Type": "multipart/form-data; boundary=---011000010111000001101001"
}
response = requests.post(result_url, data=payload, headers=headers)
url_image = response.json()["image_process_response"]["result_url"]
print(url_image)
download_image_from_url(url_image, "rapapi.jpg")