From 93f7b2a734b5a7338dc0e67757c58ca3aa7e9399 Mon Sep 17 00:00:00 2001 From: liuyebo <1515783401@qq.com> Date: Wed, 10 Jul 2024 13:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AA=E5=AF=B9=E6=B6=82=E6=8A=B9=E7=9A=84?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=BF=9B=E8=A1=8C=E5=A4=87=E4=BB=BD=E5=92=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- photo_mask.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/photo_mask.py b/photo_mask.py index f7f936d..112bf72 100644 --- a/photo_mask.py +++ b/photo_mask.py @@ -230,12 +230,11 @@ def photo_mask(pk_phhd, content): .all() session.close() for phrec in phrecs: - is_copy_success = ucloud.copy_file(BUCKET, phrec.cfjaddress, "drg2015", phrec.cfjaddress) - if not is_copy_success: - continue img_url = ucloud.get_private_url(phrec.cfjaddress) if not img_url: continue + # 是否有涂抹 + is_masked = False # 打开图片 image = open_image(img_url) split_result = split_image(image) @@ -249,6 +248,9 @@ def photo_mask(pk_phhd, content): rotated_img = rotate_image(img["img"], angle) results = get_mask_layout(rotated_img, content) + if results: + is_masked = True + for result in results: height, width = img["img"].shape[:2] center = (width / 2, height / 2) @@ -261,21 +263,29 @@ def photo_mask(pk_phhd, content): ) cv2.rectangle(image, (int(result[0]), int(result[1])), (int(result[2]), int(result[3])), (255, 255, 255), -1, 0) - with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: - cv2.imwrite(temp_file.name, image) - cv2.imwrite(f"./mask_test2/{phrec.cfjaddress}.jpg", image) - try: + + # 如果涂抹了要备份以及更新 + if is_masked: for i in range(3): - is_upload_success = ucloud.upload_file(phrec.cfjaddress, temp_file.name) - if is_upload_success: + is_copy_success = ucloud.copy_file(BUCKET, phrec.cfjaddress, "drg2015", phrec.cfjaddress) + if is_copy_success: break - except Exception as e: - logging.error("上传图片出错", exc_info=e) - finally: + + with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: + cv2.imwrite(temp_file.name, image) + cv2.imwrite(f"./mask_test2/{phrec.cfjaddress}.jpg", image) try: - os.remove(temp_file.name) + for i in range(3): + is_upload_success = ucloud.upload_file(phrec.cfjaddress, temp_file.name) + if is_upload_success: + break except Exception as e: - logging.info(f"删除临时文件 {temp_file.name} 时出错", exc_info=e) + logging.error("上传图片出错", exc_info=e) + finally: + try: + os.remove(temp_file.name) + except Exception as e: + logging.info(f"删除临时文件 {temp_file.name} 时出错", exc_info=e) if __name__ == '__main__':