import gradio as gr from PIL import Image import os from utils.face_rec import input_an_image, update_ind2person, image_rec import cv2 import numpy as np ind2person=dict() def video_identity(video): return video def str_intercept(img_path): img_path_ = img_path[::-1] point_index = 0 slash_index = 0 flag_pi = 0 flag_si = 0 for i in range(len(img_path_)): if (img_path_[i] == "." and flag_pi == 0): point_index = i flag_pi = 1 if (img_path_[i] == "/" and flag_si == 0): slash_index = i flag_si = 1 point_index = len(img_path) - 1 - point_index slash_index = len(img_path) - 1 - slash_index return point_index, slash_index def face_entry(img_path, name_text): if img_path == "" or name_text == "" or img_path is None or name_text is None: return None, None, None #point_index, slash_index = str_intercept(img_path) #img_renamePath = f"{img_path[:slash_index+1]}{name_text}{img_path[point_index:]}" #os.rename(img_path, img_renamePath) #img_ = Image.open(img_renamePath) img_ = Image.open(img_path) emb = input_an_image(img_, name_text) update_ind2person(ind2person, emb, name_text) name_text='upload '+name_text+' image done!' return name_text def face_rec_img(image): known_face_encodings=[v['emb'] for k,v in ind2person.items()] image = cv2.cvtColor(np.array(image),cv2.COLOR_RGB2BGR) image=image_rec(image,known_face_encodings=known_face_encodings,_ind2person=ind2person) image = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB)) return image def change_input_image(choice): if choice == "camera": return gr.Image(image_mode="RGB", source="webcam", type="filepath", label="upload face image",visible=True) elif choice == "upload": return gr.Image(image_mode="RGB", source="upload", type="filepath", label="upload face image",visible=True) else: return gr.Image.update(visible=False) def main(): with gr.Blocks(css='style.css') as demo: with gr.Row(): gr.Markdown("Capture Face", elem_id="md1") with gr.Row(): # radio = gr.Radio(["camera","upload"], label="capture face image in your camera or upload face image") with gr.Column(scale=2): with gr.Row(): input_img = gr.Image(image_mode="RGB", source="webcam", type="filepath", label="capture face image") # input_img = gr.Image(image_mode="RGB",interactive=True) # radio.change(fn=change_input_image, inputs=radio, outputs=input_img) with gr.Column(scale=1): with gr.Row(): input_name = gr.Textbox(label="input person name") with gr.Row(): btn = gr.Button(value="upload face image") with gr.Row(): output_name = gr.Textbox(label="echo") with gr.Row(): gr.Markdown("Face Recognition", elem_id="md1") with gr.Row(): with gr.Column(): with gr.Row(): img2rec = gr.Image(image_mode="RGB", source="webcam", type="pil", label="upload face image") with gr.Row(): btn_img_rec = gr.Button(value="upload face image") with gr.Column(): # with gr.Row(): # input_name = gr.Textbox(label="input person name") with gr.Row(): output_rec = gr.Image(image_mode="RGB", source="upload", type="pil", label="rec image") # with gr.Row(): # gr.Markdown("Video Face Recognition") # with gr.Row(): # with gr.Column(): # with gr.Row(): # img2rec = gr.Image(image_mode="RGB", source="webcam", type="pil", label="upload face image") # with gr.Row(): # btn_img_rec = gr.Button(value="upload face image") # with gr.Column(): # # with gr.Row(): # # input_name = gr.Textbox(label="input person name") # with gr.Row(): # output_rec = gr.Image(image_mode="RGB", source="upload", type="pil", label="rec image") btn.click(fn=face_entry, inputs=[input_img,input_name],outputs=[output_name]) btn_img_rec.click(fn=face_rec_img, inputs=[img2rec], outputs=[output_rec]) # video=gr.Video(source='webcam') # demo = gr.Interface(video_identity,video,"video") return demo if __name__=='__main__': demo=main() demo.launch(share=True) # demo.launch()