diff --git a/demo/locales.py b/demo/locales.py
index 61ac1d20..10b15632 100644
--- a/demo/locales.py
+++ b/demo/locales.py
@@ -667,22 +667,22 @@
"label": "포화도 강도",
},
},
- "face_alignment": {
+ "plugin": {
"en": {
- "label": "Face Alignment",
- "choices": ["Open"],
+ "label": "🤖Plugin",
+ "choices": ["Face Alignment", "Layout Photo Crop Line"],
},
"zh": {
- "label": "人脸旋转对齐",
- "choices": ["开启"],
+ "label": "🤖插件",
+ "choices": ["人脸旋转对齐", "排版照裁剪线"],
},
"ja": {
- "label": "顔の整列",
- "choices": ["開始"],
+ "label": "🤖プラグイン",
+ "choices": ["顔の整列", "レイアウト写真の切り取り線"],
},
"ko": {
- "label": "얼굴 정렬",
- "choices": ["시작"],
+ "label": "🤖플러그인",
+ "choices": ["얼굴 정렬", "레이아웃 사진 자르기 선"],
},
},
"template_photo": {
diff --git a/demo/processor.py b/demo/processor.py
index 62663d5e..2f6dbbd1 100644
--- a/demo/processor.py
+++ b/demo/processor.py
@@ -9,7 +9,7 @@
save_image_dpi_to_bytes,
)
from hivision.creator.layout_calculator import (
- generate_layout_photo,
+ generate_layout_array,
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
@@ -61,16 +61,26 @@ def process(
contrast_strength=0,
sharpen_strength=0,
saturation_strength=0,
- face_alignment_option=False,
- ):
+ plugin_option=[],
+ ):
# 初始化参数
top_distance_min = top_distance_max - 0.02
# 得到render_option在LOCALES["render_mode"][language]["choices"]中的索引
render_option_index = LOCALES["render_mode"][language]["choices"].index(
render_option
)
+ # 读取插件选项
+ if LOCALES["plugin"][language]["choices"][0] in plugin_option:
+ face_alignment_option = True
+ else:
+ face_alignment_option = False
+ if LOCALES["plugin"][language]["choices"][1] in plugin_option:
+ layout_photo_crop_line_option = True
+ else:
+ layout_photo_crop_line_option = False
+
idphoto_json = self._initialize_idphoto_json(
- mode_option, color_option, render_option_index, image_kb_options
+ mode_option, color_option, render_option_index, image_kb_options, layout_photo_crop_line_option
)
# 处理尺寸模式
@@ -153,6 +163,7 @@ def _initialize_idphoto_json(
color_option,
render_option,
image_kb_options,
+ layout_photo_crop_line_option,
):
"""初始化idphoto_json字典"""
return {
@@ -162,6 +173,7 @@ def _initialize_idphoto_json(
"image_kb_mode": image_kb_options,
"custom_image_kb": None,
"custom_image_dpi": None,
+ "layout_photo_crop_line_option": layout_photo_crop_line_option,
}
# 处理尺寸模式
@@ -435,7 +447,7 @@ def _generate_image_layout(
if idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]:
return None, False
- typography_arr, typography_rotate = generate_layout_photo(
+ typography_arr, typography_rotate = generate_layout_array(
input_height=idphoto_json["size"][0],
input_width=idphoto_json["size"][1],
)
@@ -446,6 +458,7 @@ def _generate_image_layout(
typography_rotate,
height=idphoto_json["size"][0],
width=idphoto_json["size"][1],
+ crop_line=idphoto_json["layout_photo_crop_line_option"],
)
return result_image_layout, True
diff --git a/demo/ui.py b/demo/ui.py
index 56f9c861..569a2482 100644
--- a/demo/ui.py
+++ b/demo/ui.py
@@ -81,12 +81,7 @@ def create_ui(
value=LOCALES["size_mode"][DEFAULT_LANG]["choices"][0],
min_width=520,
)
- # 人脸对齐
- face_alignment_options = gr.CheckboxGroup(
- label=LOCALES["face_alignment"][DEFAULT_LANG]["label"],
- choices=LOCALES["face_alignment"][DEFAULT_LANG]["choices"],
- interactive=True,
- )
+
# 尺寸列表
with gr.Row(visible=True) as size_list_row:
size_list_options = gr.Dropdown(
@@ -143,6 +138,14 @@ def create_ui(
label=LOCALES["render_mode"][DEFAULT_LANG]["label"],
value=LOCALES["render_mode"][DEFAULT_LANG]["choices"][0],
)
+
+ with gr.Row():
+ # 插件模式
+ plugin_options = gr.CheckboxGroup(
+ label=LOCALES["plugin"][DEFAULT_LANG]["label"],
+ choices=LOCALES["plugin"][DEFAULT_LANG]["choices"],
+ interactive=True,
+ )
# TAB2 - 高级参数 ------------------------------------------------
with gr.Tab(
@@ -525,10 +528,6 @@ def change_language(language):
saturation_option: gr.update(
label=LOCALES["saturation_strength"][language]["label"]
),
- face_alignment_options: gr.update(
- label=LOCALES["face_alignment"][language]["label"],
- choices=LOCALES["face_alignment"][language]["choices"],
- ),
custom_size_width_px: gr.update(
label=LOCALES["custom_size_px"][language]["width"]
),
@@ -547,6 +546,11 @@ def change_language(language):
template_image_accordion: gr.update(
label=LOCALES["template_photo"][language]["label"]
),
+ plugin_options: gr.update(
+ label=LOCALES["plugin"][language]["label"],
+ choices=LOCALES["plugin"][language]["choices"],
+ value=LOCALES["plugin"][language]["choices"][0],
+ ),
}
def change_visibility(option, lang, locales_key, custom_component):
@@ -571,7 +575,7 @@ def change_size_mode(size_option_item, lang):
custom_size_px: gr.update(visible=False),
custom_size_mm: gr.update(visible=True),
size_list_row: gr.update(visible=False),
- face_alignment_options: gr.update(visible=True),
+ plugin_options: gr.update(interactive=True),
}
# 如果选择自定义尺寸px
elif size_option_item == choices[2]:
@@ -579,7 +583,7 @@ def change_size_mode(size_option_item, lang):
custom_size_px: gr.update(visible=True),
custom_size_mm: gr.update(visible=False),
size_list_row: gr.update(visible=False),
- face_alignment_options: gr.update(visible=True),
+ plugin_options: gr.update(interactive=True),
}
# 如果选择只换底,则隐藏所有尺寸组件
elif size_option_item == choices[1]:
@@ -587,7 +591,7 @@ def change_size_mode(size_option_item, lang):
custom_size_px: gr.update(visible=False),
custom_size_mm: gr.update(visible=False),
size_list_row: gr.update(visible=False),
- face_alignment_options: gr.update(visible=False),
+ plugin_options: gr.update(interactive=False),
}
# 如果选择预设尺寸,则隐藏自定义尺寸组件
else:
@@ -595,7 +599,7 @@ def change_size_mode(size_option_item, lang):
custom_size_px: gr.update(visible=False),
custom_size_mm: gr.update(visible=False),
size_list_row: gr.update(visible=True),
- face_alignment_options: gr.update(visible=True),
+ plugin_options: gr.update(interactive=True),
}
def change_image_kb(image_kb_option, lang):
@@ -650,7 +654,7 @@ def change_image_dpi(image_dpi_option, lang):
contrast_option,
sharpen_option,
saturation_option,
- face_alignment_options,
+ plugin_options,
custom_size_width_px,
custom_size_height_px,
custom_size_width_mm,
@@ -669,7 +673,7 @@ def change_image_dpi(image_dpi_option, lang):
custom_size_px,
custom_size_mm,
size_list_row,
- face_alignment_options,
+ plugin_options,
],
)
@@ -731,7 +735,7 @@ def change_image_dpi(image_dpi_option, lang):
contrast_option,
sharpen_option,
saturation_option,
- face_alignment_options,
+ plugin_options,
],
outputs=[
img_output_standard,
diff --git a/deploy_api.py b/deploy_api.py
index 8eab422a..6f4230e2 100644
--- a/deploy_api.py
+++ b/deploy_api.py
@@ -2,7 +2,7 @@
from hivision import IDCreator
from hivision.error import FaceError
from hivision.creator.layout_calculator import (
- generate_layout_photo,
+ generate_layout_array,
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
@@ -173,7 +173,7 @@ async def generate_layout_photos(
size = (int(height), int(width))
- typography_arr, typography_rotate = generate_layout_photo(
+ typography_arr, typography_rotate = generate_layout_array(
input_height=size[0], input_width=size[1]
)
diff --git a/hivision/creator/layout_calculator.py b/hivision/creator/layout_calculator.py
index b79d090a..1637ea5f 100644
--- a/hivision/creator/layout_calculator.py
+++ b/hivision/creator/layout_calculator.py
@@ -75,7 +75,7 @@ def judge_layout(
return layout_mode, centerBlockWidth_1, centerBlockHeight_1
-def generate_layout_photo(input_height, input_width):
+def generate_layout_array(input_height, input_width):
# 1.基础参数表
LAYOUT_WIDTH = 1746
LAYOUT_HEIGHT = 1180
@@ -118,23 +118,63 @@ def generate_layout_photo(input_height, input_width):
def generate_layout_image(
- input_image, typography_arr, typography_rotate, width=295, height=413
+ input_image, typography_arr, typography_rotate, width=295, height=413, crop_line:bool=False,
):
+ # 定义画布的宽度和高度
LAYOUT_WIDTH = 1746
LAYOUT_HEIGHT = 1180
+
+ # 创建一个白色背景的空白画布
white_background = np.zeros([LAYOUT_HEIGHT, LAYOUT_WIDTH, 3], np.uint8)
white_background.fill(255)
+
+ # 如果输入图像的高度不等于指定高度,则调整图像大小
if input_image.shape[0] != height:
input_image = cv2.resize(input_image, (width, height))
+
+ # 如果需要旋转排版,则对图像进行转置和垂直镜像
if typography_rotate:
input_image = cv2.transpose(input_image)
input_image = cv2.flip(input_image, 0) # 0 表示垂直镜像
+ # 交换高度和宽度
height, width = width, height
+
+ # 将图像按照排版数组中的位置放置到白色背景上
for arr in typography_arr:
locate_x, locate_y = arr[0], arr[1]
white_background[locate_y : locate_y + height, locate_x : locate_x + width] = (
input_image
)
+ if crop_line:
+ # 添加裁剪线
+ line_color = (200, 200, 200) # 浅灰色
+ line_thickness = 1
+
+ # 初始化裁剪线位置列表
+ vertical_lines = []
+ horizontal_lines = []
+
+ # 根据排版数组添加裁剪线
+ for arr in typography_arr:
+ x, y = arr[0], arr[1]
+ if x not in vertical_lines:
+ vertical_lines.append(x)
+ if x + width not in vertical_lines:
+ vertical_lines.append(x + width)
+ if y not in horizontal_lines:
+ horizontal_lines.append(y)
+ if y + height not in horizontal_lines:
+ horizontal_lines.append(y + height)
+
+ # 绘制垂直裁剪线
+ for x in vertical_lines:
+ cv2.line(white_background, (x, 0), (x, LAYOUT_HEIGHT), line_color, line_thickness)
+
+ # 绘制水平裁剪线
+ for y in horizontal_lines:
+ cv2.line(white_background, (0, y), (LAYOUT_WIDTH, y), line_color, line_thickness)
+
+ # 返回排版后的图像
return white_background
diff --git a/hivision/creator/photo_adjuster.py b/hivision/creator/photo_adjuster.py
index 325da333..2e966a7e 100644
--- a/hivision/creator/photo_adjuster.py
+++ b/hivision/creator/photo_adjuster.py
@@ -8,7 +8,7 @@
证件照调整
"""
from .context import Context
-from .layout_calculator import generate_layout_photo
+from .layout_calculator import generate_layout_array
import hivision.creator.utils as U
import numpy as np
import math
@@ -111,7 +111,7 @@ def adjust_photo(ctx: Context):
}
# Step7. 排版照参数获取
- typography_arr, typography_rotate = generate_layout_photo(
+ typography_arr, typography_rotate = generate_layout_array(
input_height=standard_size[0], input_width=standard_size[1]
)
diff --git a/inference.py b/inference.py
index 95e5c5cd..ea24805e 100644
--- a/inference.py
+++ b/inference.py
@@ -6,7 +6,7 @@
from hivision.utils import hex_to_rgb, resize_image_to_kb, add_background, save_image_dpi_to_bytes
from hivision import IDCreator
from hivision.creator.layout_calculator import (
- generate_layout_photo,
+ generate_layout_array,
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
@@ -141,7 +141,7 @@
size = (int(args.height), int(args.width))
- typography_arr, typography_rotate = generate_layout_photo(
+ typography_arr, typography_rotate = generate_layout_array(
input_height=size[0], input_width=size[1]
)