Skip to content

Commit 48428b2

Browse files
authored
feat: custom size mm (#136)
* partial ui * update mm * update readme
1 parent 177d4b6 commit 48428b2

File tree

7 files changed

+153
-29
lines changed

7 files changed

+153
-29
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
- 在线体验: [![SwanHub Demo](https://img.shields.io/static/v1?label=Demo&message=SwanHub%20Demo&color=blue)](https://swanhub.co/ZeYiLin/HivisionIDPhotos/demo)[![Spaces](https://img.shields.io/badge/🤗-Open%20in%20Spaces-blue)](https://huggingface.co/spaces/TheEeeeLin/HivisionIDPhotos)[![][modelscope-shield]][modelscope-link]
5757

58-
- 2024.09.16: Gradio Demo增加**人脸旋转对齐**功能
58+
- 2024.09.16: Gradio Demo增加**人脸旋转对齐**功能,自定义尺寸输入支持**毫米**单位
5959
- 2024.09.14: Gradio Demo增加**自定义DPI**功能,增加日语和韩语支持,增加**调整亮度、对比度、锐度**功能
6060
- 2024.09.12: Gradio Demo增加**美白**功能 | API接口增加**加水印****设置照片KB值大小****证件照裁切**
6161
- 2024.09.11: Gradio Demo增加**透明图显示与下载**功能

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ English / [中文](README.md) / [日本語](README_JP.md) / [한국어](README_K
5353

5454
- Online Experience: [![SwanHub Demo](https://img.shields.io/static/v1?label=Demo&message=SwanHub%20Demo&color=blue)](https://swanhub.co/ZeYiLin/HivisionIDPhotos/demo)[![Spaces](https://img.shields.io/badge/🤗-Open%20in%20Spaces-blue)](https://huggingface.co/spaces/TheEeeeLin/HivisionIDPhotos)[![][modelscope-shield]][modelscope-link]
5555

56-
- 2024.09.16: Gradio Demo adds **Face Rotation Alignment** feature
56+
- 2024.09.16: Gradio Demo adds **Face Rotation Alignment** feature, custom size input supports **millimeters**
5757
- 2024.09.14: Gradio Demo adds **Custom DPI** feature, adds Japanese and Korean support, adds **Adjust Brightness, Contrast, Sharpness** feature
5858
- 2024.09.12: Gradio Demo adds **Whitening** feature | API interface adds **Watermark**, **Set Photo KB Size**, **ID Photo Cropping**
5959
- 2024.09.11: Added **transparent image display and download** feature to Gradio Demo.

README_JP.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
- オンライン体験: [![SwanHub Demo](https://img.shields.io/static/v1?label=Demo&message=SwanHub%20Demo&color=blue)](https://swanhub.co/ZeYiLin/HivisionIDPhotos/demo)[![Spaces](https://img.shields.io/badge/🤗-Open%20in%20Spaces-blue)](https://huggingface.co/spaces/TheEeeeLin/HivisionIDPhotos)[![][modelscope-shield]][modelscope-link]
5353

54-
- 2024.09.16: Gradioデモに**顔回転対応**機能を追加
54+
- 2024.09.16: Gradioデモに**顔回転対応**機能を追加、カスタムサイズ入力に**ミリメートル**をサポート
5555
- 2024.09.14: Gradioデモに**カスタムDPI**機能を追加、日本語と韓国語を追加,**明るさ、コントラスト、鮮明度の調整**機能を追加
5656
- 2024.09.12: Gradioデモに**ホワイトニング**機能を追加 | APIインターフェースに**ウォーターマーク追加****写真のKBサイズ設定****証明写真のトリミング**を追加
5757
- 2024.09.11: Gradioデモに**透過画像表示とダウンロード**機能を追加しました。

README_KO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
- 온라인 체험: [![SwanHub Demo](https://img.shields.io/static/v1?label=Demo&message=SwanHub%20Demo&color=blue)](https://swanhub.co/ZeYiLin/HivisionIDPhotos/demo)[![Spaces](https://img.shields.io/badge/🤗-Open%20in%20Spaces-blue)](https://huggingface.co/spaces/TheEeeeLin/HivisionIDPhotos)[![][modelscope-shield]][modelscope-link]
5353

54-
- 2024.09.16: Gradio Demo에 **얼굴 회전 정렬** 기능 추가
54+
- 2024.09.16: Gradio Demo에 **얼굴 회전 정렬** 기능 추가, 커스텀 사이즈 입력에 **밀리미터** 단위 추가
5555
- 2024.09.14: Gradio Demo에 **커스텀 DPI** 기능 추가, 일본어와 한국어 추가, **밝기, 대비, 선명도 조절** 기능 추가
5656
- 2024.09.12: Gradio 데모에 **미백** 기능 추가 | API 인터페이스에 **워터마크 추가**, **사진 KB 크기 설정**, **증명사진 자르기** 추가
5757
- 2024.09.11: Gradio Demo에 **투명 이미지 표시 및 다운로드** 기능 추가

demo/locales.py

+51-4
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,76 @@
8787
"size_mode": {
8888
"en": {
8989
"label": "ID photo size options",
90-
"choices": ["Size List", "Only Change Background", "Custom Size"],
90+
"choices": [
91+
"Size List",
92+
"Only Change BG",
93+
"Custom(px)",
94+
"Custom(mm)",
95+
],
9196
"custom_size_eror": "The width should not be greater than the length; the length and width should not be less than 100, and no more than 1800.",
9297
},
9398
"zh": {
9499
"label": "证件照尺寸选项",
95-
"choices": ["尺寸列表", "只换底", "自定义尺寸"],
100+
"choices": ["尺寸列表", "只换底", "自定义(px)", "自定义(mm)"],
96101
"custom_size_eror": "宽度不应大于长度;长度和宽度不应小于100,不大于1800。",
97102
},
98103
"ja": {
99104
"label": "証明写真サイズオプション",
100105
"choices": [
101106
"サイズリスト",
102107
"背景のみ変更",
103-
"カスタムサイズ",
108+
"カスタムサイズ(px)",
109+
"カスタムサイズ(mm)",
104110
],
105111
"custom_size_eror": "幅は長さより大きくしないでください。長さと幅は100以上1800以下にしてください。",
106112
},
107113
"ko": {
108114
"label": "증명사진 크기 옵션",
109-
"choices": ["크기 목록", "배경만 변경", "사용자 지정 크기"],
115+
"choices": [
116+
"크기 목록",
117+
"배경만 변경",
118+
"사용자 지정(px)",
119+
"사용자 지정(mm)",
120+
],
110121
"custom_size_eror": "너비는 길이보다 크지 않아야 합니다; 길이와 너비는 100 이상 1800 이하여야 합니다.",
111122
},
112123
},
124+
"custom_size_px": {
125+
"en": {
126+
"height": "Height(px)",
127+
"width": "Width(px)",
128+
},
129+
"zh": {
130+
"height": "高度(px)",
131+
"width": "宽度(px)",
132+
},
133+
"ja": {
134+
"height": "高さ(px)",
135+
"width": "幅(px)",
136+
},
137+
"ko": {
138+
"height": "높이(px)",
139+
"width": "너비(px)",
140+
},
141+
},
142+
"custom_size_mm": {
143+
"en": {
144+
"height": "Height(mm)",
145+
"width": "Width(mm)",
146+
},
147+
"zh": {
148+
"height": "高度(mm)",
149+
"width": "宽度(mm)",
150+
},
151+
"ja": {
152+
"height": "高さ(mm)",
153+
"width": "幅(mm)",
154+
},
155+
"ko": {
156+
"height": "높이(mm)",
157+
"width": "너비(mm)",
158+
},
159+
},
113160
"size_list": {
114161
"en": {
115162
"label": "Size list",

demo/processor.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def process(
3333
custom_color_B,
3434
custom_size_height,
3535
custom_size_width,
36+
custom_size_height_mm,
37+
custom_size_width_mm,
3638
custom_image_kb,
3739
language,
3840
matting_model_option,
@@ -72,6 +74,8 @@ def process(
7274
size_list_option,
7375
custom_size_height,
7476
custom_size_width,
77+
custom_size_height_mm,
78+
custom_size_width_mm,
7579
)
7680
if isinstance(size_result, list):
7781
return size_result # 返回错误信息
@@ -159,21 +163,40 @@ def _process_size_mode(
159163
size_list_option,
160164
custom_size_height,
161165
custom_size_width,
166+
custom_size_height_mm,
167+
custom_size_width_mm,
162168
):
163169
"""处理尺寸模式"""
170+
# 如果选择了尺寸列表
164171
if idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][0]:
165172
idphoto_json["size"] = LOCALES["size_list"][language]["develop"][
166173
size_list_option
167174
]
168-
elif idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][2]:
169-
id_height, id_width = int(custom_size_height), int(custom_size_width)
175+
# 如果选择了自定义尺寸(px或mm)
176+
elif (
177+
idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][2]
178+
or idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][3]
179+
):
180+
# 如果选择了自定义尺寸(px)
181+
if (
182+
idphoto_json["size_mode"]
183+
== LOCALES["size_mode"][language]["choices"][2]
184+
):
185+
id_height, id_width = int(custom_size_height), int(custom_size_width)
186+
# 如果选择了自定义尺寸(mm)
187+
else:
188+
# 将mm转换为px
189+
id_height = int(custom_size_height_mm / 25.4 * 300)
190+
id_width = int(custom_size_width_mm / 25.4 * 300)
191+
# 检查尺寸像素是否在100到1800之间
170192
if (
171193
id_height < id_width
172194
or min(id_height, id_width) < 100
173195
or max(id_height, id_width) > 1800
174196
):
175197
return self._create_error_response(language)
176198
idphoto_json["size"] = (id_height, id_width)
199+
# 如果选择了只换底
177200
else:
178201
idphoto_json["size"] = (None, None)
179202

demo/ui.py

+73-19
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def create_ui(
7878
choices=LOCALES["size_mode"][DEFAULT_LANG]["choices"],
7979
label=LOCALES["size_mode"][DEFAULT_LANG]["label"],
8080
value=LOCALES["size_mode"][DEFAULT_LANG]["choices"][0],
81-
min_width=500,
81+
min_width=520,
8282
)
8383
face_alignment_options = gr.CheckboxGroup(
8484
label=LOCALES["face_alignment"][DEFAULT_LANG]["label"],
@@ -92,12 +92,27 @@ def create_ui(
9292
value=LOCALES["size_list"][DEFAULT_LANG]["choices"][0],
9393
elem_id="size_list",
9494
)
95-
with gr.Row(visible=False) as custom_size:
96-
custom_size_height = gr.Number(
97-
value=413, label="height", interactive=True
95+
with gr.Row(visible=False) as custom_size_px:
96+
custom_size_height_px = gr.Number(
97+
value=413,
98+
label=LOCALES["custom_size_px"][DEFAULT_LANG]["height"],
99+
interactive=True,
100+
)
101+
custom_size_width_px = gr.Number(
102+
value=295,
103+
label=LOCALES["custom_size_px"][DEFAULT_LANG]["width"],
104+
interactive=True,
98105
)
99-
custom_size_width = gr.Number(
100-
value=295, label="width", interactive=True
106+
with gr.Row(visible=False) as custom_size_mm:
107+
custom_size_height_mm = gr.Number(
108+
value=35,
109+
label=LOCALES["custom_size_mm"][DEFAULT_LANG]["height"],
110+
interactive=True,
111+
)
112+
custom_size_width_mm = gr.Number(
113+
value=25,
114+
label=LOCALES["custom_size_mm"][DEFAULT_LANG]["width"],
115+
interactive=True,
101116
)
102117

103118
color_options = gr.Radio(
@@ -498,6 +513,18 @@ def change_language(language):
498513
label=LOCALES["face_alignment"][language]["label"],
499514
choices=LOCALES["face_alignment"][language]["choices"],
500515
),
516+
custom_size_width_px: gr.update(
517+
label=LOCALES["custom_size_px"][language]["width"]
518+
),
519+
custom_size_height_px: gr.update(
520+
label=LOCALES["custom_size_px"][language]["height"]
521+
),
522+
custom_size_width_mm: gr.update(
523+
label=LOCALES["custom_size_mm"][language]["width"]
524+
),
525+
custom_size_height_mm: gr.update(
526+
label=LOCALES["custom_size_mm"][language]["height"]
527+
),
501528
}
502529

503530
def change_visibility(option, lang, locales_key, custom_component):
@@ -512,24 +539,35 @@ def change_color(colors, lang):
512539

513540
def change_size_mode(size_option_item, lang):
514541
choices = LOCALES["size_mode"][lang]["choices"]
515-
# 如果选择自定义尺寸,则隐藏预设尺寸列表
516-
if size_option_item == choices[2]:
542+
# 如果选择自定义尺寸mm
543+
if size_option_item == choices[3]:
544+
return {
545+
custom_size_px: gr.update(visible=False),
546+
custom_size_mm: gr.update(visible=True),
547+
size_list_row: gr.update(visible=False),
548+
face_alignment_options: gr.update(visible=True),
549+
}
550+
# 如果选择自定义尺寸px
551+
elif size_option_item == choices[2]:
517552
return {
518-
custom_size: gr.update(visible=True),
553+
custom_size_px: gr.update(visible=True),
554+
custom_size_mm: gr.update(visible=False),
519555
size_list_row: gr.update(visible=False),
520556
face_alignment_options: gr.update(visible=True),
521557
}
522558
# 如果选择只换底,则隐藏所有尺寸组件
523559
elif size_option_item == choices[1]:
524560
return {
525-
custom_size: gr.update(visible=False),
561+
custom_size_px: gr.update(visible=False),
562+
custom_size_mm: gr.update(visible=False),
526563
size_list_row: gr.update(visible=False),
527564
face_alignment_options: gr.update(visible=False),
528565
}
529566
# 如果选择预设尺寸,则隐藏自定义尺寸组件
530567
else:
531568
return {
532-
custom_size: gr.update(visible=False),
569+
custom_size_px: gr.update(visible=False),
570+
custom_size_mm: gr.update(visible=False),
533571
size_list_row: gr.update(visible=True),
534572
face_alignment_options: gr.update(visible=True),
535573
}
@@ -588,27 +626,41 @@ def change_image_dpi(image_dpi_option, lang):
588626
sharpen_option,
589627
saturation_option,
590628
face_alignment_options,
629+
custom_size_width_px,
630+
custom_size_height_px,
631+
custom_size_width_mm,
632+
custom_size_height_mm,
591633
],
592634
)
593635

636+
# ---------------- 设置隐藏/显示交互效果 ----------------
637+
# 尺寸模式
638+
mode_options.input(
639+
change_size_mode,
640+
inputs=[mode_options, language_options],
641+
outputs=[
642+
custom_size_px,
643+
custom_size_mm,
644+
size_list_row,
645+
face_alignment_options,
646+
],
647+
)
648+
649+
# 颜色
594650
color_options.input(
595651
change_color,
596652
inputs=[color_options, language_options],
597653
outputs=[custom_color],
598654
)
599655

600-
mode_options.input(
601-
change_size_mode,
602-
inputs=[mode_options, language_options],
603-
outputs=[custom_size, size_list_row, face_alignment_options],
604-
)
605-
656+
# 图片kb
606657
image_kb_options.input(
607658
change_image_kb,
608659
inputs=[image_kb_options, language_options],
609660
outputs=[custom_image_kb_size],
610661
)
611662

663+
# 图片dpi
612664
image_dpi_options.input(
613665
change_image_dpi,
614666
inputs=[image_dpi_options, language_options],
@@ -627,8 +679,10 @@ def change_image_dpi(image_dpi_option, lang):
627679
custom_color_R,
628680
custom_color_G,
629681
custom_color_B,
630-
custom_size_height,
631-
custom_size_width,
682+
custom_size_height_px,
683+
custom_size_width_px,
684+
custom_size_height_mm,
685+
custom_size_width_mm,
632686
custom_image_kb_size,
633687
language_options,
634688
matting_model_options,

0 commit comments

Comments
 (0)