TheEeeeLin commited on
Commit
09383f1
1 Parent(s): 9b2289b
Files changed (5) hide show
  1. demo/assets/title.md +11 -0
  2. demo/images/resize.py +0 -9
  3. demo/locales.py +55 -8
  4. demo/processor.py +26 -3
  5. demo/ui.py +78 -24
demo/assets/title.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div style="display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; font-size: 40px;">
2
+ <div style="display: flex; align-items: center;">
3
+ <b>HivisionIDPhotos</b>
4
+ </div>
5
+ <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
6
+ <a href="https://github.com/xiaolin199912/HivisionIDPhotos"><img alt="Github" src="https://img.shields.io/static/v1?label=GitHub&message=GitHub&color=black"></a> &ensp;
7
+ <a href="https://docs.qq.com/doc/DUkpBdk90eWZFS2JW" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/WeChat-微信-4cb55e"></a> &ensp;
8
+ <a href="https://github.com/xiaolin199912/HivisionIDPhotos/stargazers"><img alt="GitHub stars" src="https://img.shields.io/github/stars/zeyi-lin/hivisionidphotos?color=ffcb47&labelColor=black&style=flat-square"></a> &ensp;
9
+ <a href="https://github.com/Zeyi-Lin/HivisionIDPhotos/blob/master/docs/api_EN.md" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/API_Docs-API文档-315bce"></a>
10
+ </div>
11
+ </div>
demo/images/resize.py DELETED
@@ -1,9 +0,0 @@
1
- import cv2
2
-
3
- input_image = cv2.imread("./test4.jpg")
4
-
5
- # resize成长宽的一半
6
- cv2.imwrite(
7
- "./test4_resized_half.jpg",
8
- cv2.resize(input_image, (input_image.shape[1] // 2, input_image.shape[0] // 2)),
9
- )
 
 
 
 
 
 
 
 
 
 
demo/locales.py CHANGED
@@ -87,12 +87,17 @@ LOCALES = {
87
  "size_mode": {
88
  "en": {
89
  "label": "ID photo size options",
90
- "choices": ["Size List", "Only Change Background", "Custom Size"],
 
 
 
 
 
91
  "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.",
92
  },
93
  "zh": {
94
  "label": "证件照尺寸选项",
95
- "choices": ["尺寸列表", "只换底", "自定义尺寸"],
96
  "custom_size_eror": "宽度不应大于长度;长度和宽度不应小于100,不大于1800。",
97
  },
98
  "ja": {
@@ -100,16 +105,58 @@ LOCALES = {
100
  "choices": [
101
  "サイズリスト",
102
  "背景のみ変更",
103
- "カスタムサイズ",
 
104
  ],
105
  "custom_size_eror": "幅は長さより大きくしないでください。長さと幅は100以上1800以下にしてください。",
106
  },
107
  "ko": {
108
  "label": "증명사진 크기 옵션",
109
- "choices": ["크기 목록", "배경만 변경", "사용자 지정 크기"],
 
 
 
 
 
110
  "custom_size_eror": "너비는 길이보다 크지 않아야 합니다; 길이와 너비는 100 이상 1800 이하여야 합니다.",
111
  },
112
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  "size_list": {
114
  "en": {
115
  "label": "Size list",
@@ -623,19 +670,19 @@ LOCALES = {
623
  "face_alignment": {
624
  "en": {
625
  "label": "Face Alignment",
626
- "value": "Open",
627
  },
628
  "zh": {
629
  "label": "人脸旋转对齐",
630
- "value": "开启",
631
  },
632
  "ja": {
633
  "label": "顔の整列",
634
- "value": "開始",
635
  },
636
  "ko": {
637
  "label": "얼굴 정렬",
638
- "value": "시작",
639
  },
640
  },
641
  }
 
87
  "size_mode": {
88
  "en": {
89
  "label": "ID photo size options",
90
+ "choices": [
91
+ "Size List",
92
+ "Only Change BG",
93
+ "Custom(px)",
94
+ "Custom(mm)",
95
+ ],
96
  "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.",
97
  },
98
  "zh": {
99
  "label": "证件照尺寸选项",
100
+ "choices": ["尺寸列表", "只换底", "自定义(px)", "自定义(mm)"],
101
  "custom_size_eror": "宽度不应大于长度;长度和宽度不应小于100,不大于1800。",
102
  },
103
  "ja": {
 
105
  "choices": [
106
  "サイズリスト",
107
  "背景のみ変更",
108
+ "カスタムサイズ(px)",
109
+ "カスタムサイズ(mm)",
110
  ],
111
  "custom_size_eror": "幅は長さより大きくしないでください。長さと幅は100以上1800以下にしてください。",
112
  },
113
  "ko": {
114
  "label": "증명사진 크기 옵션",
115
+ "choices": [
116
+ "크기 목록",
117
+ "배경만 변경",
118
+ "사용자 지정(px)",
119
+ "사용자 지정(mm)",
120
+ ],
121
  "custom_size_eror": "너비는 길이보다 크지 않아야 합니다; 길이와 너비는 100 이상 1800 이하여야 합니다.",
122
  },
123
  },
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
+ },
160
  "size_list": {
161
  "en": {
162
  "label": "Size list",
 
670
  "face_alignment": {
671
  "en": {
672
  "label": "Face Alignment",
673
+ "choices": ["Open"],
674
  },
675
  "zh": {
676
  "label": "人脸旋转对齐",
677
+ "choices": ["开启"],
678
  },
679
  "ja": {
680
  "label": "顔の整列",
681
+ "choices": ["開始"],
682
  },
683
  "ko": {
684
  "label": "얼굴 정렬",
685
+ "choices": ["시작"],
686
  },
687
  },
688
  }
demo/processor.py CHANGED
@@ -33,6 +33,8 @@ class IDPhotoProcessor:
33
  custom_color_B,
34
  custom_size_height,
35
  custom_size_width,
 
 
36
  custom_image_kb,
37
  language,
38
  matting_model_option,
@@ -72,6 +74,8 @@ class IDPhotoProcessor:
72
  size_list_option,
73
  custom_size_height,
74
  custom_size_width,
 
 
75
  )
76
  if isinstance(size_result, list):
77
  return size_result # 返回错误信息
@@ -159,14 +163,32 @@ class IDPhotoProcessor:
159
  size_list_option,
160
  custom_size_height,
161
  custom_size_width,
 
 
162
  ):
163
  """处理尺寸模式"""
 
164
  if idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][0]:
165
  idphoto_json["size"] = LOCALES["size_list"][language]["develop"][
166
  size_list_option
167
  ]
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  if (
171
  id_height < id_width
172
  or min(id_height, id_width) < 100
@@ -174,6 +196,7 @@ class IDPhotoProcessor:
174
  ):
175
  return self._create_error_response(language)
176
  idphoto_json["size"] = (id_height, id_width)
 
177
  else:
178
  idphoto_json["size"] = (None, None)
179
 
@@ -341,7 +364,7 @@ class IDPhotoProcessor:
341
  ):
342
  """生成排版照片"""
343
  if idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]:
344
- return gr.update(visible=False)
345
 
346
  typography_arr, typography_rotate = generate_layout_photo(
347
  input_height=idphoto_json["size"][0],
 
33
  custom_color_B,
34
  custom_size_height,
35
  custom_size_width,
36
+ custom_size_height_mm,
37
+ custom_size_width_mm,
38
  custom_image_kb,
39
  language,
40
  matting_model_option,
 
74
  size_list_option,
75
  custom_size_height,
76
  custom_size_width,
77
+ custom_size_height_mm,
78
+ custom_size_width_mm,
79
  )
80
  if isinstance(size_result, list):
81
  return size_result # 返回错误信息
 
163
  size_list_option,
164
  custom_size_height,
165
  custom_size_width,
166
+ custom_size_height_mm,
167
+ custom_size_width_mm,
168
  ):
169
  """处理尺寸模式"""
170
+ # 如果选择了尺寸列表
171
  if idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][0]:
172
  idphoto_json["size"] = LOCALES["size_list"][language]["develop"][
173
  size_list_option
174
  ]
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之间
192
  if (
193
  id_height < id_width
194
  or min(id_height, id_width) < 100
 
196
  ):
197
  return self._create_error_response(language)
198
  idphoto_json["size"] = (id_height, id_width)
199
+ # 如果选择了只换底
200
  else:
201
  idphoto_json["size"] = (None, None)
202
 
 
364
  ):
365
  """生成排版照片"""
366
  if idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]:
367
+ return None, gr.update(visible=False)
368
 
369
  typography_arr, typography_rotate = generate_layout_photo(
370
  input_height=idphoto_json["size"][0],
demo/ui.py CHANGED
@@ -43,7 +43,7 @@ def create_ui(
43
  demo = gr.Blocks(title="HivisionIDPhotos")
44
 
45
  with demo:
46
- gr.HTML(load_description(os.path.join(root_dir, "assets/title.md")))
47
  with gr.Row():
48
  # ------------------------ 左半边 UI ------------------------
49
  with gr.Column():
@@ -78,26 +78,41 @@ def create_ui(
78
  choices=LOCALES["size_mode"][DEFAULT_LANG]["choices"],
79
  label=LOCALES["size_mode"][DEFAULT_LANG]["label"],
80
  value=LOCALES["size_mode"][DEFAULT_LANG]["choices"][0],
81
- min_width=500,
82
  )
83
  face_alignment_options = gr.CheckboxGroup(
84
  label=LOCALES["face_alignment"][DEFAULT_LANG]["label"],
85
- choices=[LOCALES["face_alignment"][DEFAULT_LANG]["value"]],
86
  interactive=True,
87
  )
88
  with gr.Row(visible=True) as size_list_row:
89
  size_list_options = gr.Dropdown(
90
  choices=LOCALES["size_list"][DEFAULT_LANG]["choices"],
91
- label="预设尺寸",
92
  value=LOCALES["size_list"][DEFAULT_LANG]["choices"][0],
93
  elem_id="size_list",
94
  )
95
- with gr.Row(visible=False) as custom_size:
96
- custom_size_height = gr.Number(
97
- value=413, label="height", interactive=True
 
 
 
 
 
 
 
98
  )
99
- custom_size_width = gr.Number(
100
- value=295, label="width", interactive=True
 
 
 
 
 
 
 
 
101
  )
102
 
103
  color_options = gr.Radio(
@@ -367,7 +382,7 @@ def create_ui(
367
  elem_id="hd_photo_png",
368
  )
369
 
370
- # ---------------- 设置隐藏/显示组件 ----------------
371
  def change_language(language):
372
  return {
373
  face_detect_model_options: gr.update(
@@ -496,7 +511,19 @@ def create_ui(
496
  ),
497
  face_alignment_options: gr.update(
498
  label=LOCALES["face_alignment"][language]["label"],
499
- value=LOCALES["face_alignment"][language]["value"],
 
 
 
 
 
 
 
 
 
 
 
 
500
  ),
501
  }
502
 
@@ -512,24 +539,35 @@ def create_ui(
512
 
513
  def change_size_mode(size_option_item, lang):
514
  choices = LOCALES["size_mode"][lang]["choices"]
515
- # 如果选择自定义尺寸,则隐藏预设尺寸列表
516
- if size_option_item == choices[2]:
 
 
 
 
 
 
 
 
517
  return {
518
- custom_size: gr.update(visible=True),
 
519
  size_list_row: gr.update(visible=False),
520
  face_alignment_options: gr.update(visible=True),
521
  }
522
  # 如果选择只换底,则隐藏所有尺寸组件
523
  elif size_option_item == choices[1]:
524
  return {
525
- custom_size: gr.update(visible=False),
 
526
  size_list_row: gr.update(visible=False),
527
  face_alignment_options: gr.update(visible=False),
528
  }
529
  # 如果选择预设尺寸,则隐藏自定义尺寸组件
530
  else:
531
  return {
532
- custom_size: gr.update(visible=False),
 
533
  size_list_row: gr.update(visible=True),
534
  face_alignment_options: gr.update(visible=True),
535
  }
@@ -588,27 +626,41 @@ def create_ui(
588
  sharpen_option,
589
  saturation_option,
590
  face_alignment_options,
 
 
 
 
591
  ],
592
  )
593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
  color_options.input(
595
  change_color,
596
  inputs=[color_options, language_options],
597
  outputs=[custom_color],
598
  )
599
 
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
-
606
  image_kb_options.input(
607
  change_image_kb,
608
  inputs=[image_kb_options, language_options],
609
  outputs=[custom_image_kb_size],
610
  )
611
 
 
612
  image_dpi_options.input(
613
  change_image_dpi,
614
  inputs=[image_dpi_options, language_options],
@@ -627,8 +679,10 @@ def create_ui(
627
  custom_color_R,
628
  custom_color_G,
629
  custom_color_B,
630
- custom_size_height,
631
- custom_size_width,
 
 
632
  custom_image_kb_size,
633
  language_options,
634
  matting_model_options,
 
43
  demo = gr.Blocks(title="HivisionIDPhotos")
44
 
45
  with demo:
46
+ gr.HTML(load_description(os.path.join(root_dir, "demo/assets/title.md")))
47
  with gr.Row():
48
  # ------------------------ 左半边 UI ------------------------
49
  with gr.Column():
 
78
  choices=LOCALES["size_mode"][DEFAULT_LANG]["choices"],
79
  label=LOCALES["size_mode"][DEFAULT_LANG]["label"],
80
  value=LOCALES["size_mode"][DEFAULT_LANG]["choices"][0],
81
+ min_width=520,
82
  )
83
  face_alignment_options = gr.CheckboxGroup(
84
  label=LOCALES["face_alignment"][DEFAULT_LANG]["label"],
85
+ choices=LOCALES["face_alignment"][DEFAULT_LANG]["choices"],
86
  interactive=True,
87
  )
88
  with gr.Row(visible=True) as size_list_row:
89
  size_list_options = gr.Dropdown(
90
  choices=LOCALES["size_list"][DEFAULT_LANG]["choices"],
91
+ label=LOCALES["size_list"][DEFAULT_LANG]["label"],
92
  value=LOCALES["size_list"][DEFAULT_LANG]["choices"][0],
93
  elem_id="size_list",
94
  )
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,
105
  )
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,
116
  )
117
 
118
  color_options = gr.Radio(
 
382
  elem_id="hd_photo_png",
383
  )
384
 
385
+ # ---------------- 多语言切换函数 ----------------
386
  def change_language(language):
387
  return {
388
  face_detect_model_options: gr.update(
 
511
  ),
512
  face_alignment_options: gr.update(
513
  label=LOCALES["face_alignment"][language]["label"],
514
+ choices=LOCALES["face_alignment"][language]["choices"],
515
+ ),
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
  ),
528
  }
529
 
 
539
 
540
  def change_size_mode(size_option_item, lang):
541
  choices = LOCALES["size_mode"][lang]["choices"]
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]:
552
  return {
553
+ custom_size_px: gr.update(visible=True),
554
+ custom_size_mm: gr.update(visible=False),
555
  size_list_row: gr.update(visible=False),
556
  face_alignment_options: gr.update(visible=True),
557
  }
558
  # 如果选择只换底,则隐藏所有尺寸组件
559
  elif size_option_item == choices[1]:
560
  return {
561
+ custom_size_px: gr.update(visible=False),
562
+ custom_size_mm: gr.update(visible=False),
563
  size_list_row: gr.update(visible=False),
564
  face_alignment_options: gr.update(visible=False),
565
  }
566
  # 如果选择预设尺寸,则隐藏自定义尺寸组件
567
  else:
568
  return {
569
+ custom_size_px: gr.update(visible=False),
570
+ custom_size_mm: gr.update(visible=False),
571
  size_list_row: gr.update(visible=True),
572
  face_alignment_options: gr.update(visible=True),
573
  }
 
626
  sharpen_option,
627
  saturation_option,
628
  face_alignment_options,
629
+ custom_size_width_px,
630
+ custom_size_height_px,
631
+ custom_size_width_mm,
632
+ custom_size_height_mm,
633
  ],
634
  )
635
 
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
+ # 颜色
650
  color_options.input(
651
  change_color,
652
  inputs=[color_options, language_options],
653
  outputs=[custom_color],
654
  )
655
 
656
+ # 图片kb
 
 
 
 
 
657
  image_kb_options.input(
658
  change_image_kb,
659
  inputs=[image_kb_options, language_options],
660
  outputs=[custom_image_kb_size],
661
  )
662
 
663
+ # 图片dpi
664
  image_dpi_options.input(
665
  change_image_dpi,
666
  inputs=[image_dpi_options, language_options],
 
679
  custom_color_R,
680
  custom_color_G,
681
  custom_color_B,
682
+ custom_size_height_px,
683
+ custom_size_width_px,
684
+ custom_size_height_mm,
685
+ custom_size_width_mm,
686
  custom_image_kb_size,
687
  language_options,
688
  matting_model_options,