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__':