Spaces:
Build error
Build error
Upload 3 files
Browse files- requirements.txt +5 -0
- run.sh +1 -0
- test.py +190 -0
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.19.0
|
| 2 |
+
Pillow==9.3.0
|
| 3 |
+
deepface==0.0.75
|
| 4 |
+
opencv-python-headless==4.6.0.66
|
| 5 |
+
pandas==1.5.3
|
run.sh
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
streamlit run test.py
|
test.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from deepface import DeepFace
|
| 4 |
+
import tempfile
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import cv2 as cv
|
| 7 |
+
import threading
|
| 8 |
+
from time import sleep
|
| 9 |
+
|
| 10 |
+
st.title('Image Upload and Verification App')
|
| 11 |
+
|
| 12 |
+
st.write('Please upload two images for facial verification.')
|
| 13 |
+
|
| 14 |
+
# Upload two images
|
| 15 |
+
uploaded_file1 = st.file_uploader("Choose the first image...", type=["jpg", "png", "jpeg"], key="1")
|
| 16 |
+
uploaded_file2 = st.file_uploader("Choose the second image...", type=["jpg", "png", "jpeg"], key="2")
|
| 17 |
+
|
| 18 |
+
# Define the global variables
|
| 19 |
+
df = None
|
| 20 |
+
analyze_img1 = None
|
| 21 |
+
analyze_img2 = None
|
| 22 |
+
|
| 23 |
+
def verify(img1_path, img2_path):
|
| 24 |
+
global df
|
| 25 |
+
model_name = 'VGG-Face' # You can change this to other models like "Facenet", "OpenFace", "DeepFace", etc.
|
| 26 |
+
result = DeepFace.verify(img1_path=img1_path, img2_path=img2_path, model_name=model_name)
|
| 27 |
+
result["img1_facial_areas"] = result["facial_areas"]["img1"]
|
| 28 |
+
result["img2_facial_areas"] = result["facial_areas"]["img2"]
|
| 29 |
+
del result["facial_areas"]
|
| 30 |
+
df = pd.DataFrame([result])
|
| 31 |
+
|
| 32 |
+
def analyze_image1(img1_path):
|
| 33 |
+
global analyze_img1
|
| 34 |
+
analyze_img1 = DeepFace.analyze(img_path=img1_path)[0]
|
| 35 |
+
|
| 36 |
+
def analyze_image2(img2_path):
|
| 37 |
+
global analyze_img2
|
| 38 |
+
analyze_img2 = DeepFace.analyze(img_path=img2_path)[0]
|
| 39 |
+
|
| 40 |
+
def generate_analysis_sentence(analysis):
|
| 41 |
+
age = analysis['age']
|
| 42 |
+
gender = [i for i in analysis['gender'].keys()][-1]
|
| 43 |
+
dominant_emotion = analysis['dominant_emotion']
|
| 44 |
+
dominant_race = analysis['dominant_race']
|
| 45 |
+
|
| 46 |
+
# Highlight specific words in blue
|
| 47 |
+
age_html = f"<span style='color:blue'>{age}</span>"
|
| 48 |
+
gender_html = f"<span style='color:blue'>{gender}</span>"
|
| 49 |
+
dominant_emotion_html = f"<span style='color:blue'>{dominant_emotion}</span>"
|
| 50 |
+
dominant_race_html = f"<span style='color:blue'>{dominant_race}</span>"
|
| 51 |
+
|
| 52 |
+
return f"""The person in the image appears to be {age_html} years old, identified as '{gender_html}'.
|
| 53 |
+
The dominant emotion detected is {dominant_emotion_html}.
|
| 54 |
+
Ethnicity prediction indicates {dominant_race_html}."""
|
| 55 |
+
|
| 56 |
+
def display_image_with_analysis(image, analysis):
|
| 57 |
+
# Display the image
|
| 58 |
+
st.image(image, caption='Image', use_column_width=True)
|
| 59 |
+
|
| 60 |
+
# Display the analysis results
|
| 61 |
+
st.write("Analysis:")
|
| 62 |
+
st.markdown(generate_analysis_sentence(analysis), unsafe_allow_html=True)
|
| 63 |
+
|
| 64 |
+
def drow_rectangle():
|
| 65 |
+
# Load images with OpenCV
|
| 66 |
+
img1 = cv.imread(img1_path)
|
| 67 |
+
img2 = cv.imread(img2_path)
|
| 68 |
+
|
| 69 |
+
# Get facial areas and draw rectangles
|
| 70 |
+
face_area1 = df.iloc[0]["img1_facial_areas"]
|
| 71 |
+
p1_1 = (face_area1["x"], face_area1["y"])
|
| 72 |
+
p2_1 = (face_area1["x"] + face_area1["w"], face_area1["y"] + face_area1["h"])
|
| 73 |
+
rect_img1 = cv.rectangle(img1.copy(), p1_1, p2_1, (0, 255, 0), 2)
|
| 74 |
+
|
| 75 |
+
face_area2 = df.iloc[0]["img2_facial_areas"]
|
| 76 |
+
p1_2 = (face_area2["x"], face_area2["y"])
|
| 77 |
+
p2_2 = (face_area2["x"] + face_area2["w"], face_area2["y"] + face_area2["h"])
|
| 78 |
+
rect_img2 = cv.rectangle(img2.copy(), p1_2, p2_2, (0, 255, 0), 2)
|
| 79 |
+
|
| 80 |
+
# Resize images with a better interpolation method
|
| 81 |
+
rect_img1 = cv.cvtColor(rect_img1, cv.COLOR_BGR2RGB)
|
| 82 |
+
rect_img1 = cv.resize(rect_img1, (200, 250), interpolation=cv.INTER_AREA)
|
| 83 |
+
|
| 84 |
+
rect_img2 = cv.cvtColor(rect_img2, cv.COLOR_BGR2RGB)
|
| 85 |
+
rect_img2 = cv.resize(rect_img2, (200, 250), interpolation=cv.INTER_AREA)
|
| 86 |
+
|
| 87 |
+
#st.dataframe(df)
|
| 88 |
+
|
| 89 |
+
# Display the results
|
| 90 |
+
if df["verified"].iloc[0]:
|
| 91 |
+
message = "The faces in the images match!"
|
| 92 |
+
else:
|
| 93 |
+
message = "The faces in the images do not match!"
|
| 94 |
+
|
| 95 |
+
st.title(message)
|
| 96 |
+
|
| 97 |
+
col1, col2 = st.columns(2)
|
| 98 |
+
col1.image(rect_img1, caption='Verified Image 1', use_column_width=True)
|
| 99 |
+
col2.image(rect_img2, caption='Verified Image 2', use_column_width=True)
|
| 100 |
+
|
| 101 |
+
def get_analyze():
|
| 102 |
+
# Display the analysis results
|
| 103 |
+
st.write("Analysis for Image 1:")
|
| 104 |
+
try:
|
| 105 |
+
st.markdown(generate_analysis_sentence(analyze_img1), unsafe_allow_html=True)
|
| 106 |
+
except:
|
| 107 |
+
st.warning("can't detect image 1")
|
| 108 |
+
|
| 109 |
+
st.write("Analysis for Image 2:")
|
| 110 |
+
try:
|
| 111 |
+
st.markdown(generate_analysis_sentence(analyze_img2), unsafe_allow_html=True)
|
| 112 |
+
except:
|
| 113 |
+
st.warning("can't detect image 2")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
col1, col2 = st.columns(2)
|
| 117 |
+
with col1:
|
| 118 |
+
st.text("Check if the faces in the images match!")
|
| 119 |
+
check = st.button("Check")
|
| 120 |
+
with col2:
|
| 121 |
+
st.text("Analyze the faces in each image!")
|
| 122 |
+
analyze = st.button("Analyze")
|
| 123 |
+
|
| 124 |
+
if uploaded_file1 is not None and uploaded_file2 is not None:
|
| 125 |
+
# Open the images with PIL
|
| 126 |
+
image1 = Image.open(uploaded_file1)
|
| 127 |
+
image2 = Image.open(uploaded_file2)
|
| 128 |
+
|
| 129 |
+
st.write("Here are your images:")
|
| 130 |
+
|
| 131 |
+
# Convert images to RGB if they are in RGBA mode
|
| 132 |
+
if image1.mode == 'RGBA':
|
| 133 |
+
image1 = image1.convert('RGB')
|
| 134 |
+
if image2.mode == 'RGBA':
|
| 135 |
+
image2 = image2.convert('RGB')
|
| 136 |
+
|
| 137 |
+
# Save the uploaded images to a temporary directory
|
| 138 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file1:
|
| 139 |
+
image1.save(tmp_file1.name)
|
| 140 |
+
img1_path = tmp_file1.name
|
| 141 |
+
|
| 142 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file2:
|
| 143 |
+
image2.save(tmp_file2.name)
|
| 144 |
+
img2_path = tmp_file2.name
|
| 145 |
+
|
| 146 |
+
t1 = threading.Thread(target=verify, args=(img1_path, img2_path))
|
| 147 |
+
t2 = threading.Thread(target=analyze_image1, args=(img1_path,))
|
| 148 |
+
t3 = threading.Thread(target=analyze_image2, args=(img2_path,))
|
| 149 |
+
t1.start()
|
| 150 |
+
t2.start()
|
| 151 |
+
t3.start()
|
| 152 |
+
t1.join()
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
if check and not t1.is_alive():
|
| 156 |
+
n = 0
|
| 157 |
+
while True:
|
| 158 |
+
try:
|
| 159 |
+
drow_rectangle()
|
| 160 |
+
sleep(2)
|
| 161 |
+
break
|
| 162 |
+
except:
|
| 163 |
+
n = n + 1
|
| 164 |
+
print(f"Try : {n}")
|
| 165 |
+
if n == 4:
|
| 166 |
+
st.warning("Please make sure there are people's faces in each of the two photos or try again")
|
| 167 |
+
break
|
| 168 |
+
|
| 169 |
+
t2.join()
|
| 170 |
+
t3.join()
|
| 171 |
+
if analyze:
|
| 172 |
+
n = 0
|
| 173 |
+
|
| 174 |
+
while t2.is_alive() or t3.is_alive():
|
| 175 |
+
sleep(2)
|
| 176 |
+
while True:
|
| 177 |
+
try:
|
| 178 |
+
get_analyze()
|
| 179 |
+
sleep(2)
|
| 180 |
+
break
|
| 181 |
+
except:
|
| 182 |
+
n = n + 1
|
| 183 |
+
print(f"Try : {n}")
|
| 184 |
+
if n == 4:
|
| 185 |
+
st.warning("Please make sure there are people's faces in each of the two photos or try again")
|
| 186 |
+
break
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
else:
|
| 190 |
+
st.write("Please upload both images to proceed.")
|