Spaces:
Runtime error
Runtime error
Commit ·
6b532f5
1
Parent(s): e3b5911
app update
Browse files- .gitignore +0 -1
- app.py +177 -0
.gitignore
CHANGED
|
@@ -59,7 +59,6 @@
|
|
| 59 |
!.pre-commit-config.yaml
|
| 60 |
|
| 61 |
|
| 62 |
-
app.py
|
| 63 |
test.py
|
| 64 |
test*.py
|
| 65 |
|
|
|
|
| 59 |
!.pre-commit-config.yaml
|
| 60 |
|
| 61 |
|
|
|
|
| 62 |
test.py
|
| 63 |
test*.py
|
| 64 |
|
app.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import face_recognition
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
+
|
| 9 |
+
from util.fonts_opt import is_fonts
|
| 10 |
+
|
| 11 |
+
ROOT_PATH = sys.path[0] # 项目根目录
|
| 12 |
+
|
| 13 |
+
IMG_PATH_Test = "./img_examples/unknown"
|
| 14 |
+
|
| 15 |
+
FONTSIZE = 15
|
| 16 |
+
|
| 17 |
+
OCR_TR_DESCRIPTION = '''# Face Recognition
|
| 18 |
+
<div id="content_align">https://github.com/ageitgey/face_recognition demo</div>'''
|
| 19 |
+
|
| 20 |
+
def str_intercept(img_path):
|
| 21 |
+
img_path_ = img_path[::-1]
|
| 22 |
+
point_index = 0 # 记录反转后第一个点的位置
|
| 23 |
+
slash_index = 0 # 记录反转后第一个斜杠的位置
|
| 24 |
+
|
| 25 |
+
flag_pi = 0
|
| 26 |
+
flag_si = 0
|
| 27 |
+
|
| 28 |
+
for i in range(len(img_path_)):
|
| 29 |
+
if (img_path_[i] == "." and flag_pi == 0):
|
| 30 |
+
point_index = i
|
| 31 |
+
flag_pi = 1
|
| 32 |
+
|
| 33 |
+
if (img_path_[i] == "/" and flag_si == 0):
|
| 34 |
+
slash_index = i
|
| 35 |
+
flag_si = 1
|
| 36 |
+
|
| 37 |
+
point_index = len(img_path) - 1 - point_index
|
| 38 |
+
slash_index = len(img_path) - 1 - slash_index
|
| 39 |
+
|
| 40 |
+
return point_index, slash_index
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# 人脸录入
|
| 44 |
+
def face_entry(img_path, name_text):
|
| 45 |
+
|
| 46 |
+
point_index, slash_index = str_intercept(img_path)
|
| 47 |
+
img_renamePath = f"{img_path[:slash_index+1]}{name_text}{img_path[point_index:]}"
|
| 48 |
+
os.rename(img_path, img_renamePath)
|
| 49 |
+
img_ = Image.open(img_renamePath)
|
| 50 |
+
print(img_renamePath)
|
| 51 |
+
|
| 52 |
+
return img_, img_renamePath, name_text
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# 设置示例
|
| 56 |
+
def set_example_image(example: list):
|
| 57 |
+
return gr.Image.update(value=example[0])
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def face_recognition_(img_srcPath, img_tagPath, img_personName):
|
| 61 |
+
image_of_person = face_recognition.load_image_file(img_srcPath)
|
| 62 |
+
person_face_encoding = face_recognition.face_encodings(image_of_person)[0]
|
| 63 |
+
|
| 64 |
+
known_face_encodings = [
|
| 65 |
+
person_face_encoding,]
|
| 66 |
+
|
| 67 |
+
known_face_names = [
|
| 68 |
+
img_personName,]
|
| 69 |
+
|
| 70 |
+
test_image = face_recognition.load_image_file(img_tagPath)
|
| 71 |
+
|
| 72 |
+
face_locations = face_recognition.face_locations(test_image)
|
| 73 |
+
face_encodings = face_recognition.face_encodings(test_image, face_locations)
|
| 74 |
+
|
| 75 |
+
pil_image = Image.fromarray(test_image)
|
| 76 |
+
img_pil = ImageDraw.Draw(pil_image)
|
| 77 |
+
textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
|
| 78 |
+
# ymin, xmax, ymax, xmin
|
| 79 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
| 80 |
+
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
|
| 81 |
+
|
| 82 |
+
name = "Unknown Person"
|
| 83 |
+
|
| 84 |
+
if True in matches:
|
| 85 |
+
first_matches_index = matches.index(True)
|
| 86 |
+
name = known_face_names[first_matches_index]
|
| 87 |
+
|
| 88 |
+
img_pil.rectangle([left, top, right, bottom], fill=None, outline=(255, 228, 181), width=2) # 边界框
|
| 89 |
+
text_w, text_h = textFont.getsize(name) # 标签尺寸
|
| 90 |
+
# 标签背景
|
| 91 |
+
img_pil.rectangle(
|
| 92 |
+
(left, top, left + text_w, top + text_h),
|
| 93 |
+
fill=(255, 255, 255),
|
| 94 |
+
outline=(255, 255, 255),
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# 标签
|
| 98 |
+
img_pil.multiline_text(
|
| 99 |
+
(left, top),
|
| 100 |
+
name,
|
| 101 |
+
fill=(0, 0, 0),
|
| 102 |
+
font=textFont,
|
| 103 |
+
align="center",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
del img_pil
|
| 107 |
+
return pil_image
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def main():
|
| 111 |
+
is_fonts(f"{ROOT_PATH}/fonts") # 检查字体文件
|
| 112 |
+
|
| 113 |
+
with gr.Blocks(css='style.css') as demo:
|
| 114 |
+
gr.Markdown(OCR_TR_DESCRIPTION)
|
| 115 |
+
|
| 116 |
+
# -------------- 人脸识别 录入 --------------
|
| 117 |
+
with gr.Row():
|
| 118 |
+
gr.Markdown("### Step 01: Face Entry")
|
| 119 |
+
with gr.Row():
|
| 120 |
+
with gr.Column():
|
| 121 |
+
with gr.Row():
|
| 122 |
+
input_img = gr.Image(image_mode="RGB", source="upload", type="filepath", label="face entry")
|
| 123 |
+
with gr.Row():
|
| 124 |
+
input_name = gr.Textbox(label="Name")
|
| 125 |
+
with gr.Row():
|
| 126 |
+
btn = gr.Button(value="Entry")
|
| 127 |
+
|
| 128 |
+
with gr.Column():
|
| 129 |
+
with gr.Row():
|
| 130 |
+
output_ = gr.Image(image_mode="RGB", source="upload", type="pil", label="entry image")
|
| 131 |
+
input_srcImg = gr.Variable(value="")
|
| 132 |
+
input_srcName = gr.Variable(value="")
|
| 133 |
+
with gr.Row():
|
| 134 |
+
example_list = [["./img_examples/known/ChengLong.jpg", "成龙"],
|
| 135 |
+
["./img_examples/known/VinDiesel.jpg", "VinDiesel"],
|
| 136 |
+
["./img_examples/known/JasonStatham.jpg", "JasonStatham"],
|
| 137 |
+
["./img_examples/known/ZhenZidan.jpg", "甄子丹"]]
|
| 138 |
+
gr.Examples(example_list,
|
| 139 |
+
[input_img, input_name],
|
| 140 |
+
output_,
|
| 141 |
+
set_example_image,
|
| 142 |
+
cache_examples=False)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# -------------- 人脸识别 测试 --------------
|
| 146 |
+
with gr.Row():
|
| 147 |
+
gr.Markdown("### Step 02: Face Test")
|
| 148 |
+
with gr.Row():
|
| 149 |
+
with gr.Column():
|
| 150 |
+
with gr.Row():
|
| 151 |
+
input_img_test = gr.Image(image_mode="RGB", source="upload", type="filepath", label="test image")
|
| 152 |
+
with gr.Row():
|
| 153 |
+
btn_test = gr.Button(value="Test")
|
| 154 |
+
with gr.Row():
|
| 155 |
+
paths = sorted(Path(IMG_PATH_Test).rglob('*.jpg'))
|
| 156 |
+
example_images_test = gr.Dataset(components=[input_img],
|
| 157 |
+
samples=[[path.as_posix()] for path in paths])
|
| 158 |
+
|
| 159 |
+
with gr.Column():
|
| 160 |
+
with gr.Row():
|
| 161 |
+
output_test = gr.Image(image_mode="RGB", source="upload", type="pil", label="identify image")
|
| 162 |
+
|
| 163 |
+
btn.click(fn=face_entry, inputs=[input_img, input_name], outputs=[output_, input_srcImg, input_srcName])
|
| 164 |
+
|
| 165 |
+
btn_test.click(fn=face_recognition_,
|
| 166 |
+
inputs=[input_srcImg, input_img_test, input_srcName],
|
| 167 |
+
outputs=[output_test])
|
| 168 |
+
example_images_test.click(fn=set_example_image, inputs=[
|
| 169 |
+
example_images_test,], outputs=[
|
| 170 |
+
input_img_test,])
|
| 171 |
+
|
| 172 |
+
return demo
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
demo = main()
|
| 177 |
+
demo.launch(inbrowser=True)
|