|
import streamlit as st |
|
import face_recognition |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from PIL import Image |
|
|
|
def hitung_kemiripan_wajah(foto_1, foto_2): |
|
|
|
gambar_1 = face_recognition.load_image_file(foto_1) |
|
gambar_2 = face_recognition.load_image_file(foto_2) |
|
|
|
|
|
wajah_1 = face_recognition.face_locations(gambar_1) |
|
wajah_2 = face_recognition.face_locations(gambar_2) |
|
|
|
|
|
encoding_1 = face_recognition.face_encodings(gambar_1, wajah_1) |
|
encoding_2 = face_recognition.face_encodings(gambar_2, wajah_2) |
|
|
|
if len(encoding_1) == 0 or len(encoding_2) == 0: |
|
|
|
return 0.0 |
|
|
|
|
|
encoding_1 = np.array(encoding_1) |
|
encoding_2 = np.array(encoding_2) |
|
|
|
|
|
kemiripan = np.linalg.norm(encoding_1 - encoding_2, axis=1) |
|
|
|
|
|
return 1 - kemiripan[0] |
|
|
|
|
|
st.title("Pengukur Kemiripan Dua Wajah") |
|
|
|
|
|
uploaded_foto_1 = st.sidebar.file_uploader("Unggah Foto 1", type=["jpg", "jpeg", "png"]) |
|
if uploaded_foto_1: |
|
st.sidebar.image(uploaded_foto_1, caption='Upload Gambar 1', use_column_width=True) |
|
|
|
|
|
uploaded_foto_2 = st.sidebar.file_uploader("Unggah Foto 2", type=["jpg", "jpeg", "png"]) |
|
if uploaded_foto_2: |
|
st.sidebar.image(uploaded_foto_2, caption='Upload Gambar 2', use_column_width=True) |
|
|
|
if uploaded_foto_1 and uploaded_foto_2: |
|
if st.button("Ukur Kemiripan"): |
|
|
|
with open("temp1.jpg", "wb") as f: |
|
f.write(uploaded_foto_1.getbuffer()) |
|
with open("temp2.jpg", "wb") as f: |
|
f.write(uploaded_foto_2.getbuffer()) |
|
|
|
|
|
kemiripan = hitung_kemiripan_wajah("temp1.jpg", "temp2.jpg") |
|
st.write(f"Kemiripan antara dua wajah adalah: {kemiripan * 100:.2f} %") |
|
|
|
|
|
gambar_1 = Image.open("temp1.jpg") |
|
gambar_2 = Image.open("temp2.jpg") |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
with col1: |
|
st.image(gambar_1, caption='Wajah 1') |
|
with col2: |
|
st.image(gambar_2, caption='Wajah 2') |
|
|
|
|
|
fig, ax = plt.subplots(1, 2, figsize=(10, 5)) |
|
ax[0].imshow(gambar_1) |
|
ax[0].axis('off') |
|
ax[0].set_title('Wajah 1') |
|
|
|
ax[1].imshow(gambar_2) |
|
ax[1].axis('off') |
|
ax[1].set_title('Wajah 2') |
|
|
|
plt.suptitle(f'Kemiripan Wajah: {kemiripan * 100:.2f} %') |
|
st.pyplot(fig) |
|
|