geniusguy777 Zengyf-CVer commited on
Commit
064a269
·
0 Parent(s):

Duplicate from Zengyf-CVer/FaceRecognition

Browse files

Co-authored-by: ZengYifu <Zengyf-CVer@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio YOLOv5 Det
2
+ # 创建人:曾逸夫
3
+ # 项目地址:https://gitee.com/CV_Lab/gradio_yolov5_det
4
+
5
+ # 图片格式
6
+ *.jpg
7
+ *.jpeg
8
+ *.png
9
+ *.svg
10
+ *.gif
11
+
12
+ # 视频格式
13
+ *.mp4
14
+ *.avi
15
+ .ipynb_checkpoints
16
+ /__pycache__
17
+ */__pycache__
18
+
19
+ # 日志格式
20
+ *.log
21
+ *.data
22
+ *.txt
23
+
24
+ # 生成文件
25
+ *.pdf
26
+ *.xlsx
27
+ *.csv
28
+
29
+ # 参数文件
30
+ *.yaml
31
+ *.json
32
+
33
+ # 压缩文件格式
34
+ *.zip
35
+ *.tar
36
+ *.tar.gz
37
+ *.rar
38
+
39
+ # 字体格式
40
+ *.ttc
41
+ *.ttf
42
+ *.otf
43
+ *.pkl
44
+
45
+ # 模型文件
46
+ *.pt
47
+ *.db
48
+
49
+ /flagged
50
+ /run
51
+ !requirements.txt
52
+ !cls_name/*
53
+ !model_config/*
54
+ !img_examples/*
55
+ !img_examples/known/*
56
+ !img_examples/unknown/*
57
+
58
+ !requirements.txt
59
+ !.pre-commit-config.yaml
60
+
61
+
62
+ test.py
63
+ test*.py
64
+
65
+ model_download.py
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: FaceRecognition
3
+ emoji: 🚀
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.1.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: gpl-3.0
11
+ duplicated_from: Zengyf-CVer/FaceRecognition
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Face Recognition Hub
2
+ # author: Zeng Yifu(曾逸夫)
3
+ # creation time: 2022-07-28
4
+ # email: zyfiy1314@163.com
5
+ # project homepage: https://gitee.com/CV_Lab/face-recognition-hub
6
+
7
+ import os
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ import face_recognition
12
+ import gradio as gr
13
+ from PIL import Image, ImageDraw, ImageFont
14
+
15
+ from util.fonts_opt import is_fonts
16
+
17
+ ROOT_PATH = sys.path[0] # 项目根目录
18
+
19
+ IMG_PATH_Test = "./img_examples/unknown"
20
+
21
+ FONTSIZE = 15
22
+
23
+ OCR_TR_DESCRIPTION = '''# Face Recognition
24
+ <div id="content_align">https://github.com/ageitgey/face_recognition demo</div>'''
25
+
26
+ def str_intercept(img_path):
27
+ img_path_ = img_path[::-1]
28
+ point_index = 0 # 记录反转后第一个点的位置
29
+ slash_index = 0 # 记录反转后第一个斜杠的位置
30
+
31
+ flag_pi = 0
32
+ flag_si = 0
33
+
34
+ for i in range(len(img_path_)):
35
+ if (img_path_[i] == "." and flag_pi == 0):
36
+ point_index = i
37
+ flag_pi = 1
38
+
39
+ if (img_path_[i] == "/" and flag_si == 0):
40
+ slash_index = i
41
+ flag_si = 1
42
+
43
+ point_index = len(img_path) - 1 - point_index
44
+ slash_index = len(img_path) - 1 - slash_index
45
+
46
+ return point_index, slash_index
47
+
48
+
49
+ # 人脸录入
50
+ def face_entry(img_path, name_text):
51
+ if img_path == "" or name_text == "" or img_path is None or name_text is None:
52
+ return None, None, None
53
+
54
+ point_index, slash_index = str_intercept(img_path)
55
+ img_renamePath = f"{img_path[:slash_index+1]}{name_text}{img_path[point_index:]}"
56
+ os.rename(img_path, img_renamePath)
57
+ img_ = Image.open(img_renamePath)
58
+ print(img_renamePath)
59
+
60
+ return img_, img_renamePath, name_text
61
+
62
+
63
+ # 设置示例
64
+ def set_example_image(example: list):
65
+ return gr.Image.update(value=example[0])
66
+
67
+
68
+ def face_recognition_(img_srcPath, img_tagPath, img_personName):
69
+ if img_tagPath == "" or img_tagPath is None:
70
+ return None
71
+
72
+ image_of_person = face_recognition.load_image_file(img_srcPath)
73
+ person_face_encoding = face_recognition.face_encodings(image_of_person)[0]
74
+
75
+ known_face_encodings = [
76
+ person_face_encoding,]
77
+
78
+ known_face_names = [
79
+ img_personName,]
80
+
81
+ test_image = face_recognition.load_image_file(img_tagPath)
82
+
83
+ face_locations = face_recognition.face_locations(test_image)
84
+ face_encodings = face_recognition.face_encodings(test_image, face_locations)
85
+
86
+ pil_image = Image.fromarray(test_image)
87
+ img_pil = ImageDraw.Draw(pil_image)
88
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
89
+ # ymin, xmax, ymax, xmin
90
+ for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
91
+ matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
92
+
93
+ name = "Unknown Person"
94
+
95
+ if True in matches:
96
+ first_matches_index = matches.index(True)
97
+ name = known_face_names[first_matches_index]
98
+
99
+ img_pil.rectangle([left, top, right, bottom], fill=None, outline=(255, 228, 181), width=2) # 边界框
100
+ text_w, text_h = textFont.getsize(name) # 标签尺寸
101
+ # 标签背景
102
+ img_pil.rectangle(
103
+ (left, top, left + text_w, top + text_h),
104
+ fill=(255, 255, 255),
105
+ outline=(255, 255, 255),
106
+ )
107
+
108
+ # 标签
109
+ img_pil.multiline_text(
110
+ (left, top),
111
+ name,
112
+ fill=(0, 0, 0),
113
+ font=textFont,
114
+ align="center",
115
+ )
116
+
117
+ del img_pil
118
+ return pil_image
119
+
120
+
121
+ def main():
122
+ is_fonts(f"{ROOT_PATH}/fonts") # 检查字体文件
123
+
124
+ with gr.Blocks(css='style.css') as demo:
125
+ gr.Markdown(OCR_TR_DESCRIPTION)
126
+
127
+ # -------------- 人脸识别 录入 --------------
128
+ with gr.Row():
129
+ gr.Markdown("### Step 01: Face Entry")
130
+ with gr.Row():
131
+ with gr.Column():
132
+ with gr.Row():
133
+ input_img = gr.Image(image_mode="RGB", source="upload", type="filepath", label="face entry")
134
+ with gr.Row():
135
+ input_name = gr.Textbox(label="Name")
136
+ with gr.Row():
137
+ btn = gr.Button(value="Entry")
138
+
139
+ with gr.Column():
140
+ with gr.Row():
141
+ output_ = gr.Image(image_mode="RGB", source="upload", type="pil", label="entry image")
142
+ input_srcImg = gr.Variable(value="")
143
+ input_srcName = gr.Variable(value="")
144
+ with gr.Row():
145
+ example_list = [["./img_examples/known/ChengLong.jpg", "成龙"],
146
+ ["./img_examples/known/VinDiesel.jpg", "VinDiesel"],
147
+ ["./img_examples/known/JasonStatham.jpg", "JasonStatham"],
148
+ ["./img_examples/known/ZhenZidan.jpg", "甄子丹"]]
149
+ gr.Examples(example_list,
150
+ [input_img, input_name],
151
+ output_,
152
+ set_example_image,
153
+ cache_examples=False)
154
+
155
+
156
+ # -------------- 人脸识别 测试 --------------
157
+ with gr.Row():
158
+ gr.Markdown("### Step 02: Face Test")
159
+ with gr.Row():
160
+ with gr.Column():
161
+ with gr.Row():
162
+ input_img_test = gr.Image(image_mode="RGB", source="upload", type="filepath", label="test image")
163
+ with gr.Row():
164
+ btn_test = gr.Button(value="Test")
165
+ with gr.Row():
166
+ paths = sorted(Path(IMG_PATH_Test).rglob('*.jpg'))
167
+ example_images_test = gr.Dataset(components=[input_img],
168
+ samples=[[path.as_posix()] for path in paths])
169
+
170
+ with gr.Column():
171
+ with gr.Row():
172
+ output_test = gr.Image(image_mode="RGB", source="upload", type="pil", label="identify image")
173
+
174
+ btn.click(fn=face_entry, inputs=[input_img, input_name], outputs=[output_, input_srcImg, input_srcName])
175
+
176
+ btn_test.click(fn=face_recognition_,
177
+ inputs=[input_srcImg, input_img_test, input_srcName],
178
+ outputs=[output_test])
179
+ example_images_test.click(fn=set_example_image, inputs=[
180
+ example_images_test,], outputs=[
181
+ input_img_test,])
182
+
183
+ return demo
184
+
185
+
186
+ if __name__ == "__main__":
187
+ demo = main()
188
+ demo.launch(inbrowser=True)
img_examples/known/ChengLong.jpg ADDED
img_examples/known/JasonStatham.jpg ADDED
img_examples/known/VinDiesel.jpg ADDED
img_examples/known/ZhenZidan.jpg ADDED
img_examples/unknown/test01.jpg ADDED
img_examples/unknown/test02.jpg ADDED
img_examples/unknown/test03.jpg ADDED
img_examples/unknown/test04.jpg ADDED
img_examples/unknown/test05.jpg ADDED
img_examples/unknown/test06.jpg ADDED
img_examples/unknown/test07.jpg ADDED
img_examples/unknown/test08.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ wget
2
+ rich
3
+ face_recognition
style.css ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #content_align {
6
+ text-align: center;
7
+ }
util/fonts_opt.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 字体管理
2
+ # 创建人:曾逸夫
3
+ # 创建时间:2022-05-01
4
+
5
+ import os
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ import wget
10
+ from rich.console import Console
11
+
12
+ ROOT_PATH = sys.path[0] # 项目根目录
13
+
14
+ # 中文、英文、俄语、西班牙语、阿拉伯语、韩语
15
+ fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # 字体列表
16
+ fonts_suffix = ["ttc", "ttf", "otf"] # 字体后缀
17
+
18
+ data_url_dict = {
19
+ "SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
20
+ "TimesNewRoman.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053537/download/TimesNewRoman.ttf",
21
+ "malgun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053538/download/malgun.ttf",}
22
+
23
+ console = Console()
24
+
25
+
26
+ # 创建字体库
27
+ def add_fronts(font_diff):
28
+
29
+ global font_name
30
+
31
+ for k, v in data_url_dict.items():
32
+ if k in font_diff:
33
+ font_name = v.split("/")[-1] # 字体名称
34
+ Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # 创建目录
35
+
36
+ file_path = f"{ROOT_PATH}/fonts/{font_name}" # 字体路径
37
+
38
+ try:
39
+ # 下载字体文件
40
+ wget.download(v, file_path)
41
+ except Exception as e:
42
+ print("路径错误!程序结束!")
43
+ print(e)
44
+ sys.exit()
45
+ else:
46
+ print()
47
+ console.print(f"{font_name} [bold green]字体文件下载完成![/bold green] 已保存至:{file_path}")
48
+
49
+
50
+ # 判断字体文件
51
+ def is_fonts(fonts_dir):
52
+ if os.path.isdir(fonts_dir):
53
+ # 如果字体库存在
54
+ f_list = os.listdir(fonts_dir) # 本地字体库
55
+
56
+ font_diff = list(set(fonts_list).difference(set(f_list)))
57
+
58
+ if font_diff != []:
59
+ # 字体不存在
60
+ console.print("[bold red]字体不存在,正在加载。。。[/bold red]")
61
+ add_fronts(font_diff) # 创建字体库
62
+ else:
63
+ console.print(f"{fonts_list}[bold green]字体已存在![/bold green]")
64
+ else:
65
+ # 字体库不存在,创建字体库
66
+ console.print("[bold red]字体库不存在,正在创建。。。[/bold red]")
67
+ add_fronts(fonts_list) # 创建字体库