FM/cutbgG.py

54 lines
1.6 KiB
Python

# coding:utf-8
from __future__ import print_function
import sys
import base64
from volcengine.visual.VisualService import VisualService
def read_image_base64(file_path):
with open(file_path, "rb") as file:
image_bytes = file.read()
encoded_image = base64.b64encode(image_bytes)
return encoded_image.decode('utf-8')
if __name__ == '__main__':
visual_service = VisualService()
# call below method if you don't set ak and sk in $HOME/.volc/config
visual_service.set_ak('AKLTY2YyYmI5MmQwZGNhNDhjZGIxMGJhNTJjYzQ4OTk1NTg')
visual_service.set_sk('Wmpoa1ptSTBabU0zWXprek5HRXpZV0l3TVRabU5qRXpZVEV3TVRNMk1qYw==')
if len(sys.argv) < 3:
sys.exit(1)
image_file_path = sys.argv[1]
output_file_path = sys.argv[2]
# Read image file and encode to base64
image_base64 = read_image_base64(image_file_path)
#with open("debug.txt", "w") as file:
# file.write(image_base64)
form = {
"image_base64": image_base64,
"return_foreground_image": 1
}
resp = visual_service.general_segment(form)
#print(resp)
#with open("r321.txt", "w") as file:
# file.write(str(resp))
# Check if response is successful
if resp['code'] == 10000:
# Extract and print the 'content' part
content = resp['data']['foreground_image']
# Write content to file
with open(output_file_path, "wb") as file:
file.write(base64.b64decode(content))
#with open("result.txt", "w") as file:
# file.write(content)
else:
with open("debug_cutbg.txt", "w") as file:
file.write(str(resp))