|
|
@@ -4,7 +4,7 @@ from PIL import Image
|
|
|
from .remove_bg_ali import RemoveBgALi
|
|
|
import requests
|
|
|
from io import BytesIO
|
|
|
-
|
|
|
+import settings
|
|
|
|
|
|
class Segment(object):
|
|
|
def __init__(self):
|
|
|
@@ -36,20 +36,41 @@ class Segment(object):
|
|
|
else:
|
|
|
return None, response.content
|
|
|
|
|
|
- def get_no_bg_goods_by_url(self, url):
|
|
|
+ def get_no_bg_goods_by_url(self, url,key):
|
|
|
+ if key:
|
|
|
+ # 切换key
|
|
|
+ auth = key
|
|
|
+ # auth = (self.k, self.s)
|
|
|
+ else:
|
|
|
+ auth = (self.k, self.s)
|
|
|
response = requests.post(
|
|
|
- 'https://api.pixian.ai/api/v2/remove-background',
|
|
|
+ 'https://3api.valimart.net/api/v2/remove-background',
|
|
|
data={
|
|
|
'image.url': url
|
|
|
},
|
|
|
- auth=(self.k, self.s)
|
|
|
+ auth=auth
|
|
|
)
|
|
|
|
|
|
- if response.status_code == requests.codes.ok:
|
|
|
- return response.content, ""
|
|
|
- else:
|
|
|
- print("response.status_code:", response.status_code)
|
|
|
- return None, response.content
|
|
|
+ data = {"im": None,
|
|
|
+ "status_code": response.status_code, }
|
|
|
+
|
|
|
+ try:
|
|
|
+ if response.status_code == requests.codes.ok:
|
|
|
+ data["im"] = Image.open(BytesIO(response.content))
|
|
|
+ return data
|
|
|
+ else:
|
|
|
+ print("response.status_code:", response.status_code)
|
|
|
+ data = {"im": None,
|
|
|
+ "status_code": "time_out",
|
|
|
+ "message":"处理失败"
|
|
|
+ }
|
|
|
+ return data
|
|
|
+ except BaseException as e:
|
|
|
+ data = {"im": None,
|
|
|
+ "status_code": "time_out",
|
|
|
+ "message":"{}".format(e)
|
|
|
+ }
|
|
|
+ return data
|
|
|
|
|
|
def get_no_bg_goods(self, file_path=None, _im=None, key=None):
|
|
|
im = _im
|
|
|
@@ -70,7 +91,7 @@ class Segment(object):
|
|
|
|
|
|
try:
|
|
|
response = requests.post(
|
|
|
- 'https://api.pixian.ai/api/v2/remove-background',
|
|
|
+ 'https://3api.valimart.net/api/v2/remove-background',
|
|
|
files={'image': img},
|
|
|
data={
|
|
|
# Add more upload options here
|
|
|
@@ -150,9 +171,40 @@ class RemoveBgPiXian(object):
|
|
|
return _img_im, None
|
|
|
else:
|
|
|
return None, _
|
|
|
-
|
|
|
+ def upload_image_by_io(self, image_pil:Image) -> str:
|
|
|
+ # post_headers = {"Authorization": settings.Authorization}
|
|
|
+ im = image_pil
|
|
|
+ img = BytesIO()
|
|
|
+ try:
|
|
|
+ im.save(img, format='JPEG') # format: PNG or JPEG
|
|
|
+ except:
|
|
|
+ im.save(img, format='PNG') # format: PNG or JPEG
|
|
|
+ img.seek(0) # rewind to the start
|
|
|
+ try:
|
|
|
+ url = settings.DOMAIN + "/api/upload"
|
|
|
+ resultData = requests.post(
|
|
|
+ url, files={"file": img},
|
|
|
+ timeout=100
|
|
|
+ ).json()
|
|
|
+ return resultData["data"]["url"]
|
|
|
+ except Exception as e:
|
|
|
+ print("upload_image_by_io error:", e)
|
|
|
+ return None
|
|
|
def run_by_image_im(self, im, key):
|
|
|
- return self.segment.get_no_bg_goods(_im=im, key=key)
|
|
|
+ image_url = self.upload_image_by_io(im)
|
|
|
+ if image_url is None:
|
|
|
+ data = {"im": None,
|
|
|
+ "status_code": "time_out",
|
|
|
+ "message":"图片上传失败"
|
|
|
+ }
|
|
|
+ return data
|
|
|
+ # image_url
|
|
|
+ # 把image_url中的ossimg.valimart.net
|
|
|
+ # 替换为img-cuts.valimart.net
|
|
|
+ print("image_url 1:", image_url)
|
|
|
+ image_url = image_url.replace('ossimg.valimart.net', 'img-cuts.valimart.net')
|
|
|
+ print("image_url 2:", image_url)
|
|
|
+ return self.segment.get_no_bg_goods_by_url(url=image_url,key=key)
|
|
|
|
|
|
def get_image_cut(self, file_path, out_file_path=None, original_im=None, image_preprocessing=False, is_test=False):
|
|
|
if original_im:
|