|
|
@@ -1,5 +1,7 @@
|
|
|
+import asyncio
|
|
|
import copy
|
|
|
import os.path
|
|
|
+
|
|
|
# from module.other.module_online_data import GetOnlineData
|
|
|
from .online_request.module_online_data import GetOnlineDataHLM
|
|
|
|
|
|
@@ -8,6 +10,7 @@ from logger import logger
|
|
|
|
|
|
import os
|
|
|
from PIL import Image
|
|
|
+
|
|
|
# from module.other.remove_bg_ali import RemoveBgALi
|
|
|
from .remove_bg_pixian import RemoveBgPiXian
|
|
|
from .remove_bg_ali import RemoveBgALi, Picture
|
|
|
@@ -16,13 +19,14 @@ import numpy as np
|
|
|
import settings
|
|
|
import math
|
|
|
|
|
|
+
|
|
|
class Base(object):
|
|
|
- def __init__(self, image_data, lock, windows, num):
|
|
|
+ def __init__(self, image_data, lock, windows, num, token):
|
|
|
self.lock = lock
|
|
|
self.windows = windows
|
|
|
self.image_data = image_data
|
|
|
self.num = num
|
|
|
- self.get_online_data = GetOnlineDataHLM()
|
|
|
+ self.get_online_data = GetOnlineDataHLM(token)
|
|
|
self.file_path = image_data["file_path"]
|
|
|
self.file_name = image_data["file_name"]
|
|
|
self.file = os.path.split(self.file_path)[1]
|
|
|
@@ -30,9 +34,13 @@ class Base(object):
|
|
|
self.logger = logger
|
|
|
|
|
|
def add_log(self, text, _type="info"):
|
|
|
- self.logger.info("第{}个,图片名称:{},内容:{}".format(self.num, self.file, text))
|
|
|
+ self.logger.info(
|
|
|
+ "第{}个,图片名称:{},内容:{}".format(self.num, self.file, text)
|
|
|
+ )
|
|
|
|
|
|
- def send_info(self, text="", is_success=None, _type="show_info", need_point_return=False):
|
|
|
+ def send_info(
|
|
|
+ self, text="", is_success=None, _type="show_info", need_point_return=False
|
|
|
+ ):
|
|
|
with self.lock:
|
|
|
if is_success is not None:
|
|
|
if is_success:
|
|
|
@@ -42,15 +50,23 @@ class Base(object):
|
|
|
if need_point_return:
|
|
|
# 分数返回
|
|
|
if self.is_once("add_point"):
|
|
|
- print("第{}个,图片名称:{},内容:{}".format(self.num, self.file, "扣分返回"))
|
|
|
+ print(
|
|
|
+ "第{}个,图片名称:{},内容:{}".format(
|
|
|
+ self.num, self.file, "扣分返回"
|
|
|
+ )
|
|
|
+ )
|
|
|
self.dispose_point(_type="add")
|
|
|
- self.windows.remaining_times = self.windows.remaining_times + 1
|
|
|
+ self.windows.remaining_times = (
|
|
|
+ self.windows.remaining_times + 1
|
|
|
+ )
|
|
|
|
|
|
if text:
|
|
|
- data = {"_type": _type,
|
|
|
- "data": text,
|
|
|
- }
|
|
|
- self.windows.send_sign(data)
|
|
|
+ data = {
|
|
|
+ "_type": _type,
|
|
|
+ "data": text,
|
|
|
+ }
|
|
|
+ # self.windows.send_sign(data)
|
|
|
+ print("精细化抠图 send info ", data)
|
|
|
|
|
|
def refresh_times(self, cumulative_frequency_times_change):
|
|
|
if cumulative_frequency_times_change > 0:
|
|
|
@@ -83,8 +99,8 @@ class Base(object):
|
|
|
|
|
|
|
|
|
class DealOneImage(Base):
|
|
|
- def __init__(self, image_data, lock, windows, num):
|
|
|
- super().__init__(image_data, lock, windows, num)
|
|
|
+ def __init__(self, image_data, lock, windows, num, token):
|
|
|
+ super().__init__(image_data, lock, windows, num, token)
|
|
|
self.image_data = image_data
|
|
|
self.lock = lock
|
|
|
self.windows = windows
|
|
|
@@ -95,8 +111,9 @@ class DealOneImage(Base):
|
|
|
self.file_name = image_data["file_name"]
|
|
|
self.out_path = image_data["out_path"]
|
|
|
|
|
|
- def run(self):
|
|
|
+ async def run(self):
|
|
|
# 直接调用抠图
|
|
|
+ await asyncio.sleep(0.1)
|
|
|
# 1、增加获取key,2、key需要加密、3、429报错 重试再来拿一个KEY
|
|
|
self.add_log("开始处理")
|
|
|
self.send_info(text="{} 处理中".format(self.file_name))
|
|
|
@@ -113,29 +130,30 @@ class DealOneImage(Base):
|
|
|
n -= 1
|
|
|
if self.file_path in self.windows.upload_pic_dict:
|
|
|
break
|
|
|
-
|
|
|
else:
|
|
|
- time.sleep(1)
|
|
|
+ await asyncio.sleep(1)
|
|
|
if n <= 0:
|
|
|
self.send_info(text="{} 处理超时", is_success=False)
|
|
|
return
|
|
|
continue
|
|
|
-
|
|
|
+ print("图片已上传============》》》》》》")
|
|
|
s = time.time()
|
|
|
- # print(self.upload_pic_dict[file_path])
|
|
|
+ # print(self.windows.upload_pic_dict,self.windows.upload_pic_dict[self.file_path], self.file_path)
|
|
|
_flag = self.windows.upload_pic_dict[self.file_path]["flag"]
|
|
|
if not _flag:
|
|
|
self.add_log("未查到上传的图片地址")
|
|
|
self.send_info(text="{} 上传错误", is_success=False)
|
|
|
return
|
|
|
|
|
|
- image_deal_info = self.windows.upload_pic_dict[self.file_path]["image_deal_info"]
|
|
|
+ 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.add_log("抠图中")
|
|
|
|
|
|
- with self.lock:
|
|
|
- self.windows.is_upload_pic_num -= 1
|
|
|
+ # with self.lock:
|
|
|
+ self.windows.is_upload_pic_num -= 1
|
|
|
|
|
|
try:
|
|
|
balance = self.get_online_data.get_cutout_image_times()["balance"]
|
|
|
@@ -160,19 +178,23 @@ class DealOneImage(Base):
|
|
|
self.add_log("查询key成功")
|
|
|
|
|
|
if not key:
|
|
|
- _data = {"text": "出错/超时",
|
|
|
- "info": "多次获取key失败",
|
|
|
- }
|
|
|
+ _data = {
|
|
|
+ "text": "出错/超时",
|
|
|
+ "info": "多次获取key失败",
|
|
|
+ }
|
|
|
self.add_log(text="多次获取key失败")
|
|
|
- self.send_info(text="{} 处理失败,请联系管理员".format(self.file_name), is_success=False)
|
|
|
+ self.send_info(
|
|
|
+ text="{} 处理失败,请联系管理员".format(self.file_name),
|
|
|
+ is_success=False,
|
|
|
+ )
|
|
|
return
|
|
|
|
|
|
if self.is_once("sub_point"):
|
|
|
# 调用扣分
|
|
|
- with self.lock:
|
|
|
+ # with self.lock:
|
|
|
|
|
|
- self.refresh_times(cumulative_frequency_times_change=1)
|
|
|
- f = self.dispose_point(_type="sub")
|
|
|
+ self.refresh_times(cumulative_frequency_times_change=1)
|
|
|
+ f = self.dispose_point(_type="sub")
|
|
|
if not f:
|
|
|
self.add_log(text="多次获取调用余额扣减失败")
|
|
|
self.send_info(text="多次获取调用余额扣减失败", is_success=False)
|
|
|
@@ -188,33 +210,62 @@ class DealOneImage(Base):
|
|
|
|
|
|
elif pixian_cutout_data["status_code"] == 402:
|
|
|
if n >= 2:
|
|
|
- self.add_log(text="多次抠图失败:{}".format(pixian_cutout_data["status_code"]))
|
|
|
- self.send_info(text="处理失败,请联系管理员", is_success=False, need_point_return=True)
|
|
|
+ self.add_log(
|
|
|
+ text="多次抠图失败:{}".format(pixian_cutout_data["status_code"])
|
|
|
+ )
|
|
|
+ self.send_info(
|
|
|
+ text="处理失败,请联系管理员",
|
|
|
+ is_success=False,
|
|
|
+ need_point_return=True,
|
|
|
+ )
|
|
|
if self.is_once("余额不足报错"):
|
|
|
# 余额不足报错,钉钉消息通知
|
|
|
- self.get_online_data.send_message("Pixian:{} 余额不足".format(key))
|
|
|
+ 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)
|
|
|
+ self.add_log(
|
|
|
+ text="抠图失败:{},延迟6秒".format(
|
|
|
+ pixian_cutout_data["status_code"]
|
|
|
+ )
|
|
|
+ )
|
|
|
+ await asyncio.sleep(6)
|
|
|
continue
|
|
|
|
|
|
elif pixian_cutout_data["status_code"] == 429:
|
|
|
if n >= 2:
|
|
|
- self.add_log(text="多次抠图失败:{}".format(pixian_cutout_data["status_code"]))
|
|
|
- self.send_info(text="处理失败,请联系管理员", is_success=False, need_point_return=True)
|
|
|
+ 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(self.file_name, pixian_cutout_data["status_code"]))
|
|
|
- time.sleep(10)
|
|
|
+ self.add_log(
|
|
|
+ text="{}抠图失败:{},延迟10秒".format(
|
|
|
+ self.file_name, pixian_cutout_data["status_code"]
|
|
|
+ )
|
|
|
+ )
|
|
|
+ await asyncio.sleep(10)
|
|
|
continue
|
|
|
else:
|
|
|
- self.send_info(text="{} 处理失败,请联系管理员".format(self.file_name), is_success=False, need_point_return=True)
|
|
|
+ self.send_info(
|
|
|
+ text="{} 处理失败,请联系管理员".format(self.file_name),
|
|
|
+ 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"])
|
|
|
+ text = "抠图异常,code:{},message:{}".format(
|
|
|
+ pixian_cutout_data["status_code"], pixian_cutout_data["message"]
|
|
|
+ )
|
|
|
else:
|
|
|
- text = "抠图异常,code:{}".format(pixian_cutout_data["status_code"])
|
|
|
+ text = "抠图异常,code:{}".format(
|
|
|
+ pixian_cutout_data["status_code"]
|
|
|
+ )
|
|
|
self.add_log(text)
|
|
|
return
|
|
|
|
|
|
@@ -226,10 +277,20 @@ class DealOneImage(Base):
|
|
|
# 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 = self.picture_resize_to_original(
|
|
|
+ second_cut_image, original_im
|
|
|
+ )
|
|
|
# 创建空白图片并粘贴回去
|
|
|
- _img_im = Image.new(mode="RGBA", size=image_deal_info["原始图片大小"], color=(0, 0, 0, 0))
|
|
|
- _img_im.paste(second_cut_image, box=(image_deal_info["抠图扩边后位置"][0], image_deal_info["抠图扩边后位置"][1]))
|
|
|
+ _img_im = Image.new(
|
|
|
+ mode="RGBA", size=image_deal_info["原始图片大小"], color=(0, 0, 0, 0)
|
|
|
+ )
|
|
|
+ _img_im.paste(
|
|
|
+ second_cut_image,
|
|
|
+ box=(
|
|
|
+ image_deal_info["抠图扩边后位置"][0],
|
|
|
+ image_deal_info["抠图扩边后位置"][1],
|
|
|
+ ),
|
|
|
+ )
|
|
|
_img_im.save(self.out_path)
|
|
|
|
|
|
self.send_info(text="{} 抠图已完成".format(self.file_name), is_success=True)
|
|
|
@@ -257,13 +318,15 @@ class DealOneImage(Base):
|
|
|
|
|
|
# 将抠图结果转成mask
|
|
|
# 将抠图结果放大到原始图大小
|
|
|
- _img = _img.resize(original_im.size,resample=1)
|
|
|
- new_big_mask = Image.new('RGB', _img.size, (0, 0, 0))
|
|
|
- white = Image.new('RGB', _img.size, (255, 255, 255))
|
|
|
+ _img = _img.resize(original_im.size, resample=1)
|
|
|
+ new_big_mask = Image.new("RGB", _img.size, (0, 0, 0))
|
|
|
+ white = Image.new("RGB", _img.size, (255, 255, 255))
|
|
|
new_big_mask.paste(white, mask=_img.split()[3])
|
|
|
|
|
|
# ---------制作选区缩小的mask
|
|
|
- mask = cv2.cvtColor(np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY) # 将PIL 格式转换为 CV对象
|
|
|
+ mask = cv2.cvtColor(
|
|
|
+ np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY
|
|
|
+ ) # 将PIL 格式转换为 CV对象
|
|
|
mask[mask != 255] = 0
|
|
|
# 黑白反转
|
|
|
# mask = 255 - mask
|
|
|
@@ -272,9 +335,11 @@ class DealOneImage(Base):
|
|
|
erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
|
|
|
|
|
|
# -------再进行抠图处理
|
|
|
- mask = Image.fromarray(cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)) # CV 对象转 PIL
|
|
|
- transparent_im = Image.new('RGBA', original_im.size, (0, 0, 0, 0))
|
|
|
- transparent_im.paste(original_im, (0, 0), mask.convert('L'))
|
|
|
+ mask = Image.fromarray(
|
|
|
+ cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)
|
|
|
+ ) # CV 对象转 PIL
|
|
|
+ transparent_im = Image.new("RGBA", original_im.size, (0, 0, 0, 0))
|
|
|
+ transparent_im.paste(original_im, (0, 0), mask.convert("L"))
|
|
|
# 上述抠图结果进行拼接
|
|
|
_img.paste(transparent_im, (0, 0), transparent_im)
|
|
|
|
|
|
@@ -282,33 +347,32 @@ class DealOneImage(Base):
|
|
|
|
|
|
|
|
|
class DealOneImageBeforehand(Base):
|
|
|
- def __init__(self, image_data, lock, windows, num):
|
|
|
- super().__init__(image_data, lock, windows, num)
|
|
|
+ def __init__(self, image_data, lock, windows, num,token):
|
|
|
+ super().__init__(image_data, lock, windows, num,token)
|
|
|
self.image_data = image_data
|
|
|
self.lock = lock
|
|
|
self.windows = windows
|
|
|
- self.get_online_data = GetOnlineDataHLM()
|
|
|
+ self.get_online_data = GetOnlineDataHLM(token)
|
|
|
self.num = num
|
|
|
self.file_path = image_data["file_path"]
|
|
|
self.file = os.path.split(self.file_path)[1]
|
|
|
self.r_ali = RemoveBgALi()
|
|
|
self.file_name = image_data["file_name"]
|
|
|
|
|
|
- def run(self):
|
|
|
+ async def run(self):
|
|
|
while 1:
|
|
|
if self.windows.state != 1:
|
|
|
return
|
|
|
|
|
|
- with self.lock:
|
|
|
- if self.windows.is_upload_pic_num >= 4:
|
|
|
+ if self.windows.is_upload_pic_num >= 4:
|
|
|
f = False
|
|
|
- else:
|
|
|
+ else:
|
|
|
self.windows.is_upload_pic_num += 1
|
|
|
f = True
|
|
|
if f:
|
|
|
break
|
|
|
else:
|
|
|
- time.sleep(1)
|
|
|
+ await asyncio.sleep(1)
|
|
|
continue
|
|
|
image_deal_info = {}
|
|
|
try:
|
|
|
@@ -317,15 +381,17 @@ class DealOneImageBeforehand(Base):
|
|
|
f = True
|
|
|
url = ""
|
|
|
except BaseException as e:
|
|
|
+ print("精细化抠图错误",e)
|
|
|
f = False
|
|
|
url = ""
|
|
|
cut_image = ""
|
|
|
|
|
|
- with self.lock:
|
|
|
- self.windows.upload_pic_dict[self.file_path] = {"url": url,
|
|
|
- "image_deal_info": image_deal_info,
|
|
|
- "flag": f,
|
|
|
- "_im": cut_image, }
|
|
|
+ self.windows.upload_pic_dict[self.file_path] = {
|
|
|
+ "url": url,
|
|
|
+ "image_deal_info": image_deal_info,
|
|
|
+ "flag": f,
|
|
|
+ "_im": cut_image,
|
|
|
+ }
|
|
|
|
|
|
def get_image_cut(self):
|
|
|
original_pic = Picture(self.file_path)
|
|
|
@@ -344,7 +410,9 @@ class DealOneImageBeforehand(Base):
|
|
|
image_deal_info["抠图扩边后位置"] = (0, 0, original_pic.x, original_pic.y)
|
|
|
else:
|
|
|
self.add_log("开始预抠图处理")
|
|
|
- cut_image = self.r_ali.get_image_cut(file_path=None, out_file_path=None, original_im=original_pic.im)
|
|
|
+ cut_image = self.r_ali.get_image_cut(
|
|
|
+ file_path=None, out_file_path=None, original_im=original_pic.im
|
|
|
+ )
|
|
|
|
|
|
self.add_log("预抠图处理结束")
|
|
|
|
|
|
@@ -375,12 +443,16 @@ class DealOneImageBeforehand(Base):
|
|
|
max_size = settings.MAX_PIXIAN_SIZE
|
|
|
if x * y > max_size:
|
|
|
r = math.sqrt(max_size) / math.sqrt(x * y)
|
|
|
- r = r*0.9
|
|
|
+ r = r * 0.9
|
|
|
size = (int(x * r), int(y * r))
|
|
|
# print("图片:{} pixian触发二次缩放,原尺寸{}*{},新尺寸:{}".format(self.file_name, x, y, size))
|
|
|
- self.add_log(text="图片进行压缩,压缩前:{},压缩后:{}".format(image_deal_info["抠图扩边后图片大小"], size))
|
|
|
+ self.add_log(
|
|
|
+ text="图片进行压缩,压缩前:{},压缩后:{}".format(
|
|
|
+ image_deal_info["抠图扩边后图片大小"], size
|
|
|
+ )
|
|
|
+ )
|
|
|
image_deal_info["抠图扩边后PIL对象"] = copy.deepcopy(cut_image)
|
|
|
- cut_image = cut_image.resize(size=size,resample=1)
|
|
|
+ cut_image = cut_image.resize(size=size, resample=1)
|
|
|
# print(cut_image.size)
|
|
|
# print(image_deal_info["抠图扩边后PIL对象"].size)
|
|
|
image_deal_info["二次抠图是否缩放"] = True
|