CupidAI / app.py
suinY00N's picture
Rename app_1:1.py to app.py
476140b verified
import gradio as gr
from deepface import DeepFace
def face_similarity(img1, img2):
result = DeepFace.verify(img1, img2)
# distance๋ฅผ ์ด์šฉํ•ด ์œ ์‚ฌ๋„๋ฅผ 1์—์„œ ๋นผ์„œ ๋†’์„์ˆ˜๋ก ์œ ์‚ฌํ•˜๊ฒŒ ๋ณ€ํ™˜
similarity = (1 - result["distance"])*100
# ์œ ์‚ฌ๋„์— ๋”ฐ๋ฅธ ๋ฉ”์‹œ์ง€ ์กฐ๊ฑด ๋ถ„๊ธฐ
if similarity > 40:
message = "๋‘ ๋ถ„์€ ์ฒœ์ƒ์—ฐ๋ถ„!"
elif 30 < similarity <= 40:
message = "๋‘ ๋ถ„์€ ๋‚ด๊บผ์ธ๋“ฏ ๋‚ด๊บผ์•„๋‹Œ ๋‚ด๊บผ๊ฐ™์€...?"
else:
message = "์˜คํžˆ๋ ค ์ข‹์•„! ์ •๋ฐ˜๋Œ€์˜ ์‚ฌ๋žŒ์ด ๋Œ๋ฆด์ง€๋„ ๋ชฐ๋ผ์š”"
return f"์œ ์‚ฌ๋„: {similarity:.2f}\n{message}"
iface = gr.Interface(
fn=face_similarity,
inputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
outputs="text",
title="์–ผ๊ตด ์œ ์‚ฌ๋„ ์ธก์ •",
description="๋‘ ์–ผ๊ตด ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค."
)
iface.launch()
# import gradio as gr
# from deepface import DeepFace
# def face_similarity(img1, img2):
# result = DeepFace.verify(img1, img2)
# similarity = result["distance"] # ๋‚ฎ์„์ˆ˜๋ก ๋” ์œ ์‚ฌ
# verified = result["verified"]
# # ์œ ์‚ฌ๋„์— ๋”ฐ๋ฅธ ๋ฉ”์‹œ์ง€ ์กฐ๊ฑด ๋ถ„๊ธฐ
# if similarity >= 0.5:
# message = "๋‘ ๋ถ„์€ ์ฒœ์ƒ์—ฐ๋ถ„!"
# elif 0.3 <= similarity < 0.5:
# message = "๋‘ ๋ถ„์€ ๋‚ด๊บผ์ธ๋“ฏ ๋‚ด๊บผ์•„๋‹Œ ๋‚ด๊บผ๊ฐ™์€...?"
# else:
# message = "์˜คํžˆ๋ ค ์ข‹์•„! ์ •๋ฐ˜๋Œ€์˜ ์‚ฌ๋žŒ์ด ๋Œ๋ฆด์ง€๋„ ๋ชฐ๋ผ์š”"
# return f"์œ ์‚ฌ๋„: {similarity}, ๋งค์นญ ๊ฒฐ๊ณผ: {verified}\n{message}"
# iface = gr.Interface(fn=face_similarity,
# inputs=[gr.inputs.Image(shape=(224, 224)), gr.inputs.Image(shape=(224, 224))],
# outputs="text",
# title="์–ผ๊ตด ์œ ์‚ฌ๋„ ์ธก์ •",
# description="๋‘ ์–ผ๊ตด ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค.")
# iface.launch()
# import gradio as gr
# from deepface import DeepFace
# def face_similarity(img1, img2):
# result = DeepFace.verify(img1, img2)
# similarity = result["distance"] # ๋‚ฎ์„์ˆ˜๋ก ๋” ์œ ์‚ฌ
# verified = result["verified"]
# return f"์œ ์‚ฌ๋„: {similarity}, ๋งค์นญ ๊ฒฐ๊ณผ: {verified}"
# iface = gr.Interface(fn=face_similarity,
# inputs=[gr.inputs.Image(shape=(224, 224)), gr.inputs.Image(shape=(224, 224))],
# outputs="text",
# title="์–ผ๊ตด ์œ ์‚ฌ๋„ ์ธก์ •",
# description="๋‘ ์–ผ๊ตด ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค.")
# iface.launch()
# import gradio as gr
# from deepface import DeepFace
# import pandas as pd
# from PIL import Image
# import numpy as np
# # ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
# results = []
# def save_results(img1_path, img2_path, similarity):
# # ๊ฒฐ๊ณผ๋ฅผ ๊ธ€๋กœ๋ฒŒ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
# results.append([img1_path, img2_path, similarity])
# # ๊ฒฐ๊ณผ๋ฅผ pandas DataFrame์œผ๋กœ ๋ณ€ํ™˜
# df = pd.DataFrame(results, columns=['Image 1', 'Image 2', 'Similarity'])
# # DataFrame์„ CSV ํŒŒ์ผ๋กœ ์ €์žฅ
# df.to_csv('similarity_results.csv', index=False)
# def face_similarity(img1, img2):
# result = DeepFace.verify(img1, img2)
# similarity = result["distance"] # ๋‚ฎ์„์ˆ˜๋ก ๋” ์œ ์‚ฌ
# verified = result["verified"]
# # ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ €์žฅ
# img1_path = f"img1_{len(results)}.jpg"
# img2_path = f"img2_{len(results)}.jpg"
# # Convert img1 and img2 to PIL.Image objects and save them
# img1_pil = Image.fromarray(img1.astype(np.uint8))
# img2_pil = Image.fromarray(img2.astype(np.uint8))
# img1_pil.save(img1_path)
# img2_pil.save(img2_path)
# # ๊ฒฐ๊ณผ ์ €์žฅ
# save_results(img1_path, img2_path, similarity)
# return f"์œ ์‚ฌ๋„: {similarity}, ์ธ์ฆ ๊ฒฐ๊ณผ: {verified}"
# iface = gr.Interface(fn=face_similarity,
# inputs=[gr.inputs.Image(shape=(224, 224)), gr.inputs.Image(shape=(224, 224))],
# outputs="text",
# title="์–ผ๊ตด ์œ ์‚ฌ๋„ ์ธก์ •",
# description="๋‘ ์–ผ๊ตด ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค.")
# iface.launch()