|
|
@@ -120,261 +120,54 @@ class Base(object):
|
|
|
class DealOneImage(Base):
|
|
|
def __init__(self, image_data, lock, windows, num):
|
|
|
super().__init__(image_data, lock, windows, num)
|
|
|
- self.image_data = image_data
|
|
|
- self.lock = lock
|
|
|
- self.windows = windows
|
|
|
- self.num = num
|
|
|
+ self.r_pixian = RemoveBgPiXian()
|
|
|
+
|
|
|
+ def run(self, image_data, upload_pic_dict):
|
|
|
self.file_path = image_data["file_path"]
|
|
|
self.file = os.path.split(self.file_path)[1]
|
|
|
self.root_path = image_data["root_path"]
|
|
|
- self.r_pixian = RemoveBgPiXian()
|
|
|
self.file_name = image_data["file_name"]
|
|
|
-
|
|
|
- def run(self):
|
|
|
# 直接调用抠图
|
|
|
# 1、增加获取key,2、key需要加密、3、429报错 重试再来拿一个KEY
|
|
|
self.add_log("开始处理")
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "处理中",
|
|
|
- "info": "",
|
|
|
- }
|
|
|
- )
|
|
|
remaining_times = self.get_online_data.get_cutout_image_times().get("balance")
|
|
|
print("remaining_times", remaining_times)
|
|
|
if remaining_times <= 0:
|
|
|
- # self.send_info(text="次数不足,处理失败", is_success=False)
|
|
|
raise UnicornException("次数不足,处理失败")
|
|
|
- return
|
|
|
|
|
|
# 检查图片上传是否有结束
|
|
|
n = 60
|
|
|
- while 1:
|
|
|
- if self.windows.state != 1:
|
|
|
- # self.show_image_info({"text": "已取消", "info": "", })
|
|
|
- # self.send_info(text="用户主动终止", is_success=False)
|
|
|
- raise UnicornException("用户主动终止")
|
|
|
- return
|
|
|
- n -= 1
|
|
|
- # print(n)
|
|
|
- if self.file_path in self.windows.upload_pic_dict:
|
|
|
- break
|
|
|
-
|
|
|
- else:
|
|
|
- time.sleep(1)
|
|
|
- if n <= 0:
|
|
|
- text = "处理超时"
|
|
|
- self.send_info(text=text, is_success=False)
|
|
|
-
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "处理超时",
|
|
|
- }
|
|
|
- self.show_image_info(_data)
|
|
|
- return
|
|
|
- continue
|
|
|
-
|
|
|
+ if self.file_path not in upload_pic_dict:
|
|
|
+ # 不扣图
|
|
|
+ raise UnicornException("处理失败")
|
|
|
s = time.time()
|
|
|
- # print(self.upload_pic_dict[file_path])
|
|
|
- _flag = self.windows.upload_pic_dict[self.file_path]["flag"]
|
|
|
- if not _flag:
|
|
|
- self.add_log("未查到上传的图片地址")
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "上传错误",
|
|
|
- }
|
|
|
- self.show_image_info(_data)
|
|
|
- # self.send_info(text="上传错误", is_success=False)
|
|
|
- raise UnicornException("上传错误")
|
|
|
- return
|
|
|
-
|
|
|
- image_deal_info = self.windows.upload_pic_dict[self.file_path][
|
|
|
- "image_deal_info"
|
|
|
- ]
|
|
|
- original_im = self.windows.upload_pic_dict[self.file_path]["_im"]
|
|
|
-
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "开始抠图",
|
|
|
- "info": "",
|
|
|
- }
|
|
|
- )
|
|
|
+ image_deal_info = upload_pic_dict[self.file_path]["image_deal_info"]
|
|
|
+ original_im = upload_pic_dict[self.file_path]["_im"]
|
|
|
self.add_log("抠图中")
|
|
|
-
|
|
|
- with self.lock:
|
|
|
- self.windows.is_upload_pic_num -= 1
|
|
|
-
|
|
|
# 抠图
|
|
|
out_root_path = "{}/已扣图".format(self.root_path)
|
|
|
self.check_path(out_root_path)
|
|
|
-
|
|
|
- # 直接调用pixian
|
|
|
- # second_cut_image,_ = self.r_pixian.run_by_image_url(image_url)
|
|
|
-
|
|
|
try:
|
|
|
- balance = self.get_online_data.get_cutout_image_times()["balance"]
|
|
|
+ balance = self.get_online_data.get_cutout_image_times().get("balance")
|
|
|
self.add_log("查询balance:{}成功".format(balance))
|
|
|
if balance <= 0:
|
|
|
self.add_log("次数不足,处理失败")
|
|
|
raise UnicornException("次数不足,处理失败")
|
|
|
- self.send_info(text="次数不足,处理失败", is_success=False)
|
|
|
- return
|
|
|
except:
|
|
|
self.add_log("查询balance失败")
|
|
|
raise UnicornException("查询balance失败")
|
|
|
- self.send_info(text="查询balance失败", is_success=False)
|
|
|
- return
|
|
|
-
|
|
|
n = 0
|
|
|
- while 1:
|
|
|
- # 获取key
|
|
|
- if self.windows.state != 1:
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "已取消",
|
|
|
- "info": "",
|
|
|
- }
|
|
|
- )
|
|
|
- raise UnicornException("用户主动终止")
|
|
|
- # self.send_info(text="用户主动终止", is_success=False)
|
|
|
- return
|
|
|
- n += 1
|
|
|
- data = self.get_online_data.get_key_secret()
|
|
|
- key = (data["api_info"]["api_key"], data["api_info"]["api_serect"])
|
|
|
- self.add_log("查询key成功")
|
|
|
-
|
|
|
- if not key:
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "多次获取key失败",
|
|
|
- }
|
|
|
- self.add_log(text="多次获取key失败")
|
|
|
- self.show_image_info(_data)
|
|
|
- raise UnicornException("处理失败,请联系管理员")
|
|
|
- self.send_info(text="处理失败,请联系管理员", is_success=False)
|
|
|
- return
|
|
|
-
|
|
|
- if self.is_once("sub_point"):
|
|
|
- # 调用扣分
|
|
|
- with self.lock:
|
|
|
- self.refresh_times(cumulative_frequency_times_change=1)
|
|
|
- f = self.dispose_point(_type="sub")
|
|
|
- if not f:
|
|
|
- self.add_log(text="多次获取调用余额扣减失败")
|
|
|
- raise UnicornException("多次获取调用余额扣减失败")
|
|
|
- self.send_info(text="多次获取调用余额扣减失败", is_success=False)
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "多次获取调用余额扣减失败",
|
|
|
- }
|
|
|
- self.show_image_info(_data)
|
|
|
- return
|
|
|
-
|
|
|
- pixian_cutout_data = self.r_pixian.run_by_image_im(original_im, key)
|
|
|
- # pixian_cutout_data = {"status_code":200}
|
|
|
-
|
|
|
- if pixian_cutout_data["status_code"] == 200:
|
|
|
- second_cut_image = pixian_cutout_data["im"]
|
|
|
- # second_cut_image.save("xx.png")
|
|
|
- # second_cut_image = Image.open("xx.png")
|
|
|
- self.add_log(text="调用抠图完成")
|
|
|
- break
|
|
|
-
|
|
|
- elif pixian_cutout_data["status_code"] == 402:
|
|
|
- if n >= 2:
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "多次抠图失败:{}".format(
|
|
|
- pixian_cutout_data["status_code"]
|
|
|
- ),
|
|
|
- }
|
|
|
- self.add_log(
|
|
|
- text="多次抠图失败:{}".format(pixian_cutout_data["status_code"])
|
|
|
- )
|
|
|
- self.show_image_info(_data)
|
|
|
- self.send_info(
|
|
|
- text="处理失败,请联系管理员",
|
|
|
- is_success=False,
|
|
|
- need_point_return=True,
|
|
|
- )
|
|
|
- raise UnicornException("多次获取调用余额扣减失败")
|
|
|
- if self.is_once("余额不足报错"):
|
|
|
- # todo 余额不足报错,钉钉消息通知
|
|
|
- self.get_online_data.send_message(
|
|
|
- "Pixian:{} 余额不足".format(key)
|
|
|
- )
|
|
|
- pass
|
|
|
- return
|
|
|
- self.add_log(
|
|
|
- text="抠图失败:{},延迟6秒".format(
|
|
|
- pixian_cutout_data["status_code"]
|
|
|
- )
|
|
|
- )
|
|
|
- time.sleep(6)
|
|
|
- continue
|
|
|
-
|
|
|
- elif pixian_cutout_data["status_code"] == 429:
|
|
|
- if n >= 2:
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "多次抠图失败:{}".format(
|
|
|
- pixian_cutout_data["status_code"]
|
|
|
- ),
|
|
|
- }
|
|
|
-
|
|
|
- self.show_image_info(_data)
|
|
|
- self.add_log(
|
|
|
- text="多次抠图失败:{}".format(pixian_cutout_data["status_code"])
|
|
|
- )
|
|
|
- self.send_info(
|
|
|
- text="处理失败,请联系管理员",
|
|
|
- is_success=False,
|
|
|
- need_point_return=True,
|
|
|
- )
|
|
|
- return
|
|
|
-
|
|
|
- self.add_log(
|
|
|
- text="抠图失败:{},延迟10秒".format(
|
|
|
- pixian_cutout_data["status_code"]
|
|
|
- )
|
|
|
- )
|
|
|
- time.sleep(10)
|
|
|
- continue
|
|
|
- else:
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": "抠图异常",
|
|
|
- }
|
|
|
- self.show_image_info(_data)
|
|
|
- self.send_info(
|
|
|
- text="处理失败,请联系管理员",
|
|
|
- is_success=False,
|
|
|
- need_point_return=True,
|
|
|
- )
|
|
|
- if "message" in pixian_cutout_data:
|
|
|
- text = "抠图异常,code:{},message:{}".format(
|
|
|
- pixian_cutout_data["status_code"], pixian_cutout_data["message"]
|
|
|
- )
|
|
|
- else:
|
|
|
- text = "抠图异常,code:{}".format(
|
|
|
- pixian_cutout_data["status_code"]
|
|
|
- )
|
|
|
- self.add_log(text)
|
|
|
- return
|
|
|
-
|
|
|
- # 拼接处理
|
|
|
- # print("耗时1:", time.time() - s)
|
|
|
-
|
|
|
+ second_cut_image = self.runPiXian(n, original_im=original_im)
|
|
|
+ if second_cut_image is None:
|
|
|
+ raise UnicornException("抠图失败")
|
|
|
try:
|
|
|
out_path = "{}/{}.png".format(out_root_path, self.file_name)
|
|
|
if image_deal_info["二次抠图是否缩放"]:
|
|
|
- # print("图片尺寸还原")
|
|
|
self.add_log(text="图片尺寸进行还原")
|
|
|
original_im = image_deal_info["抠图扩边后PIL对象"]
|
|
|
second_cut_image = self.picture_resize_to_original(
|
|
|
second_cut_image, original_im
|
|
|
)
|
|
|
- # second_cut_image = second_cut_image.resize(image_deal_info["抠图扩边后图片大小"])
|
|
|
# 创建空白图片并粘贴回去
|
|
|
_img_im = Image.new(
|
|
|
mode="RGBA", size=image_deal_info["原始图片大小"], color=(0, 0, 0, 0)
|
|
|
@@ -393,20 +186,85 @@ class DealOneImage(Base):
|
|
|
text = "{} 图片处理错误,代码44".format(e)
|
|
|
self.add_log(text)
|
|
|
self.send_info(text=text, is_success=False, need_point_return=True)
|
|
|
- _data = {
|
|
|
- "text": "出错/超时",
|
|
|
- "info": text,
|
|
|
- }
|
|
|
- self.show_image_info(_data)
|
|
|
- return
|
|
|
+ _data = {"text": "出错/超时", "info": text, "status": False}
|
|
|
+ return _data
|
|
|
|
|
|
self.add_log(text="本张耗时:{}".format(time.time() - s))
|
|
|
self.send_info(text="抠图已完成", is_success=True)
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "已完成",
|
|
|
- "info": "",
|
|
|
+ result = {"text": "已完成", "info": "", "status": True}
|
|
|
+ return result
|
|
|
+
|
|
|
+ def runPiXian(self, num, original_im):
|
|
|
+ """执行pixian抠图"""
|
|
|
+ num += 1
|
|
|
+ data = self.get_online_data.get_key_secret()
|
|
|
+ key = (data["api_info"]["api_key"], data["api_info"]["api_serect"])
|
|
|
+ self.add_log("查询key成功")
|
|
|
+ if not key:
|
|
|
+ raise UnicornException("处理失败,请联系管理员")
|
|
|
+ if self.is_once("sub_point"):
|
|
|
+ # 调用扣分
|
|
|
+ self.refresh_times(cumulative_frequency_times_change=1)
|
|
|
+ f = self.dispose_point(_type="sub")
|
|
|
+ if not f:
|
|
|
+ self.add_log(text="多次获取调用余额扣减失败")
|
|
|
+ raise UnicornException("多次获取调用余额扣减失败")
|
|
|
+
|
|
|
+ pixian_cutout_data = self.r_pixian.run_by_image_im(original_im, key)
|
|
|
+ if pixian_cutout_data["status_code"] == 200:
|
|
|
+ second_cut_image = pixian_cutout_data["im"]
|
|
|
+ self.add_log(text="调用抠图完成")
|
|
|
+ return second_cut_image
|
|
|
+
|
|
|
+ elif pixian_cutout_data["status_code"] == 402:
|
|
|
+ if num >= 2:
|
|
|
+ self.cutoutFail(pixian_cutout_data)
|
|
|
+ raise UnicornException("多次获取调用余额扣减失败")
|
|
|
+ self.add_log(
|
|
|
+ text="抠图失败:{},延迟6秒".format(pixian_cutout_data["status_code"])
|
|
|
+ )
|
|
|
+ time.sleep(6)
|
|
|
+ self.runPiXian(num, original_im=original_im)
|
|
|
+
|
|
|
+ elif pixian_cutout_data["status_code"] == 429:
|
|
|
+ if num >= 2:
|
|
|
+ self.cutoutFail(pixian_cutout_data)
|
|
|
+ return None
|
|
|
+
|
|
|
+ self.add_log(
|
|
|
+ text="抠图失败:{},延迟10秒".format(pixian_cutout_data["status_code"])
|
|
|
+ )
|
|
|
+ time.sleep(10)
|
|
|
+ self.runPiXian(num, original_im=original_im)
|
|
|
+ else:
|
|
|
+ _data = {
|
|
|
+ "text": "出错/超时",
|
|
|
+ "info": "抠图异常",
|
|
|
}
|
|
|
+ self.show_image_info(_data)
|
|
|
+ self.get_online_data.dispose_point("add")
|
|
|
+ if "message" in pixian_cutout_data:
|
|
|
+ text = "抠图异常,code:{},message:{}".format(
|
|
|
+ pixian_cutout_data["status_code"], pixian_cutout_data["message"]
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ text = "抠图异常,code:{}".format(pixian_cutout_data["status_code"])
|
|
|
+ self.add_log(text)
|
|
|
+ return None
|
|
|
+ return second_cut_image
|
|
|
+
|
|
|
+ def cutoutFail(self, pixian_cutout_data):
|
|
|
+ """处理失败"""
|
|
|
+ _data = {
|
|
|
+ "text": "出错/超时",
|
|
|
+ "info": "多次抠图失败:{}".format(pixian_cutout_data["status_code"]),
|
|
|
+ }
|
|
|
+ self.show_image_info(_data)
|
|
|
+ self.add_log(text="多次抠图失败:{}".format(pixian_cutout_data["status_code"]))
|
|
|
+ self.send_info(
|
|
|
+ text="处理失败,请联系管理员",
|
|
|
+ is_success=False,
|
|
|
+ need_point_return=True,
|
|
|
)
|
|
|
|
|
|
def picture_resize_to_original(self, _img, original_im):
|
|
|
@@ -455,80 +313,20 @@ class DealOneImage(Base):
|
|
|
class DealOneImageBeforehand(Base):
|
|
|
def __init__(self, image_data, lock, windows, num):
|
|
|
super().__init__(image_data, lock, windows, num)
|
|
|
- self.image_data = image_data
|
|
|
- self.lock = lock
|
|
|
- self.windows = windows
|
|
|
- self.get_online_data = GetOnlineData()
|
|
|
- self.num = num
|
|
|
- self.file_path = image_data["file_path"]
|
|
|
- self.file = os.path.split(self.file_path)[1]
|
|
|
- self.root_path = image_data["root_path"]
|
|
|
self.r_ali = RemoveBgALi()
|
|
|
- self.file_name = image_data["file_name"]
|
|
|
|
|
|
- def run(self):
|
|
|
+ def run(self, upload_pic_dict):
|
|
|
# 增加阿里调用报错的重试机制
|
|
|
- # a = 1/0
|
|
|
- # raise "11111111111111111"
|
|
|
- while 1:
|
|
|
- if self.windows.state != 1:
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "已取消",
|
|
|
- "info": "",
|
|
|
- }
|
|
|
- )
|
|
|
- return
|
|
|
- with self.lock:
|
|
|
- if self.windows.is_upload_pic_num >= 4:
|
|
|
- f = False
|
|
|
- else:
|
|
|
- self.windows.is_upload_pic_num += 1
|
|
|
- f = True
|
|
|
- if f:
|
|
|
- break
|
|
|
- else:
|
|
|
- time.sleep(1)
|
|
|
- continue
|
|
|
image_deal_info = {}
|
|
|
try:
|
|
|
- _data = {
|
|
|
- "text": "开始上传",
|
|
|
- "info": "",
|
|
|
- }
|
|
|
- self.show_image_info(_data)
|
|
|
cut_image, image_deal_info = self.get_image_cut()
|
|
|
-
|
|
|
- f = True
|
|
|
- url = ""
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "上传成功",
|
|
|
- "info": "",
|
|
|
- }
|
|
|
- )
|
|
|
except BaseException as e:
|
|
|
- # print(e)
|
|
|
- self.show_image_info(
|
|
|
- {
|
|
|
- "text": "上传出错",
|
|
|
- "info": "{}".format(e),
|
|
|
- }
|
|
|
- )
|
|
|
- f = False
|
|
|
- url = ""
|
|
|
- cut_image = ""
|
|
|
-
|
|
|
- # cut_image.save("1.jpg",format="JPEG")
|
|
|
- # raise 1
|
|
|
-
|
|
|
- with self.lock:
|
|
|
- self.windows.upload_pic_dict[self.file_path] = {
|
|
|
- "url": url,
|
|
|
- "image_deal_info": image_deal_info,
|
|
|
- "flag": f,
|
|
|
- "_im": cut_image,
|
|
|
- }
|
|
|
+ raise UnicornException("上传出错")
|
|
|
+ upload_pic_dict[self.file_path] = {
|
|
|
+ "image_deal_info": image_deal_info,
|
|
|
+ "_im": cut_image,
|
|
|
+ }
|
|
|
+ return upload_pic_dict
|
|
|
|
|
|
def get_image_cut(self):
|
|
|
original_pic = Picture(self.file_path)
|
|
|
@@ -548,7 +346,7 @@ class DealOneImageBeforehand(Base):
|
|
|
cut_image = self.r_ali.get_image_cut(
|
|
|
file_path=None, out_file_path=None, original_im=original_pic.im
|
|
|
)
|
|
|
- # cut_image.save("XX1.png")
|
|
|
+ cut_image.save("XX1.png")
|
|
|
|
|
|
self.add_log("预抠图处理结束")
|
|
|
|
|
|
@@ -593,6 +391,69 @@ class DealOneImageBeforehand(Base):
|
|
|
image_deal_info["二次抠图是否缩放"] = False
|
|
|
return cut_image, image_deal_info
|
|
|
|
|
|
+ def get_image_cut_cloths(self):
|
|
|
+ original_pic = Picture(self.file_path)
|
|
|
+ original_pic.im = self.get_image_orientation(original_pic.im)
|
|
|
+ original_pic.x, original_pic.y = original_pic.im.size
|
|
|
+
|
|
|
+ original_pic.im = original_pic.im.convert("RGB")
|
|
|
+ image_deal_info = {}
|
|
|
+ image_deal_info["原始图片大小"] = (original_pic.x, original_pic.y)
|
|
|
+ self.add_log("开始预抠图处理")
|
|
|
+ remaining_times = self.get_online_data.get_cutout_image_times().get("balance")
|
|
|
+ print("remaining_times", remaining_times)
|
|
|
+ if remaining_times <= 0:
|
|
|
+ raise UnicornException("次数不足,处理失败")
|
|
|
+ self.get_online_data.dispose_point("sub")
|
|
|
+ cut_images = self.r_ali.get_image_cut_cloths(
|
|
|
+ file_path=None, out_file_path=None, original_im=original_pic.im
|
|
|
+ )
|
|
|
+ if cut_images is None:
|
|
|
+ self.get_online_data.dispose_point("add")
|
|
|
+ self.add_log("预抠图处理结束")
|
|
|
+ print("cut_images", cut_images)
|
|
|
+ return
|
|
|
+ x1, y1, x2, y2 = cut_image.getbbox()
|
|
|
+ image_deal_info["鞋子原始位置"] = (x1, y1, x2, y2)
|
|
|
+ o_w, o_h = cut_image.size
|
|
|
+ image_deal_info["鞋子原始抠图后大小"] = (o_w, o_h)
|
|
|
+ # 扩边处理
|
|
|
+ _w, _h = x2 - x1, y2 - y1
|
|
|
+ out_px = 0.025
|
|
|
+ _w, _h = int(out_px * _w), int(out_px * _h)
|
|
|
+ n_x1, n_y1, n_x2, n_y2 = x1 - _w, y1 - _h, x2 + _w, y2 + _h
|
|
|
+ if n_x1 < 0:
|
|
|
+ n_x1 = 0
|
|
|
+ if n_y1 < 0:
|
|
|
+ n_y1 = 0
|
|
|
+ if n_x2 > o_w:
|
|
|
+ n_x2 = o_w
|
|
|
+ if n_y2 > o_h:
|
|
|
+ n_y2 = o_h
|
|
|
+ image_deal_info["抠图扩边后位置"] = (n_x1, n_y1, n_x2, n_y2)
|
|
|
+ cut_image = original_pic.im.crop(image_deal_info["抠图扩边后位置"])
|
|
|
+
|
|
|
+ image_deal_info["抠图扩边后图片大小"] = cut_image.size
|
|
|
+ x, y = image_deal_info["抠图扩边后图片大小"]
|
|
|
+ # max_size = 32000000
|
|
|
+ max_size = 12000000
|
|
|
+ if x * y > max_size:
|
|
|
+ r = max_size / (x * y)
|
|
|
+ size = (int(x * r), int(y * r))
|
|
|
+ self.add_log(
|
|
|
+ text="图片进行压缩,压缩前:{},压缩后:{}".format(
|
|
|
+ image_deal_info["抠图扩边后图片大小"], size
|
|
|
+ )
|
|
|
+ )
|
|
|
+ image_deal_info["抠图扩边后PIL对象"] = copy.deepcopy(cut_image)
|
|
|
+ cut_image = cut_image.resize(size=size)
|
|
|
+ # print(cut_image.size)
|
|
|
+ # print(image_deal_info["抠图扩边后PIL对象"].size)
|
|
|
+ image_deal_info["二次抠图是否缩放"] = True
|
|
|
+ else:
|
|
|
+ image_deal_info["二次抠图是否缩放"] = False
|
|
|
+ return cut_image, image_deal_info
|
|
|
+
|
|
|
def get_image_orientation(self, img):
|
|
|
# 获取EXIF数据
|
|
|
exif = img._getexif()
|