wayandadang commited on
Commit
21ebafe
1 Parent(s): 1043400

first commit

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import face_recognition
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from PIL import Image
6
+
7
+ def hitung_kemiripan_wajah(foto_1, foto_2):
8
+ # Menggunakan library face_recognition untuk memuat gambar dan mengenali wajah
9
+ gambar_1 = face_recognition.load_image_file(foto_1)
10
+ gambar_2 = face_recognition.load_image_file(foto_2)
11
+
12
+ # Mengambil lokasi wajah pada gambar
13
+ wajah_1 = face_recognition.face_locations(gambar_1)
14
+ wajah_2 = face_recognition.face_locations(gambar_2)
15
+
16
+ # Menghasilkan encoding (vektor fitur) untuk setiap wajah
17
+ encoding_1 = face_recognition.face_encodings(gambar_1, wajah_1)
18
+ encoding_2 = face_recognition.face_encodings(gambar_2, wajah_2)
19
+
20
+ if len(encoding_1) == 0 or len(encoding_2) == 0:
21
+ # Jika tidak ada wajah yang terdeteksi pada salah satu gambar
22
+ return 0.0
23
+
24
+ # Mengkonversi daftar vektor fitur menjadi array NumPy
25
+ encoding_1 = np.array(encoding_1)
26
+ encoding_2 = np.array(encoding_2)
27
+
28
+ # Menghitung kemiripan menggunakan metode euclidean distance
29
+ kemiripan = np.linalg.norm(encoding_1 - encoding_2, axis=1)
30
+
31
+ # Mengembalikan nilai kemiripan antara 0 dan 1 (semakin dekat ke 0, semakin mirip)
32
+ return 1 - kemiripan[0]
33
+
34
+ # Streamlit app
35
+ st.title("Pengukur Kemiripan Dua Wajah")
36
+
37
+ # Upload foto 1
38
+ uploaded_foto_1 = st.sidebar.file_uploader("Unggah Foto 1", type=["jpg", "jpeg", "png"])
39
+ if uploaded_foto_1:
40
+ st.sidebar.image(uploaded_foto_1, caption='Upload Gambar 1', use_column_width=True)
41
+
42
+ # Upload foto 2
43
+ uploaded_foto_2 = st.sidebar.file_uploader("Unggah Foto 2", type=["jpg", "jpeg", "png"])
44
+ if uploaded_foto_2:
45
+ st.sidebar.image(uploaded_foto_2, caption='Upload Gambar 2', use_column_width=True)
46
+
47
+ if uploaded_foto_1 and uploaded_foto_2:
48
+ if st.button("Ukur Kemiripan"):
49
+ # Simpan file yang diunggah ke dalam file sementara
50
+ with open("temp1.jpg", "wb") as f:
51
+ f.write(uploaded_foto_1.getbuffer())
52
+ with open("temp2.jpg", "wb") as f:
53
+ f.write(uploaded_foto_2.getbuffer())
54
+
55
+ # Hitung kemiripan wajah
56
+ kemiripan = hitung_kemiripan_wajah("temp1.jpg", "temp2.jpg")
57
+ st.write(f"Kemiripan antara dua wajah adalah: {kemiripan * 100:.2f} %")
58
+
59
+ # Memuat dan menampilkan gambar
60
+ gambar_1 = Image.open("temp1.jpg")
61
+ gambar_2 = Image.open("temp2.jpg")
62
+
63
+ # Menampilkan gambar dan kemiripan wajah
64
+ col1, col2 = st.columns(2)
65
+ with col1:
66
+ st.image(gambar_1, caption='Wajah 1')
67
+ with col2:
68
+ st.image(gambar_2, caption='Wajah 2')
69
+
70
+ # Menampilkan kemiripan di judul plot
71
+ fig, ax = plt.subplots(1, 2, figsize=(10, 5))
72
+ ax[0].imshow(gambar_1)
73
+ ax[0].axis('off')
74
+ ax[0].set_title('Wajah 1')
75
+
76
+ ax[1].imshow(gambar_2)
77
+ ax[1].axis('off')
78
+ ax[1].set_title('Wajah 2')
79
+
80
+ plt.suptitle(f'Kemiripan Wajah: {kemiripan * 100:.2f} %')
81
+ st.pyplot(fig)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ face_recognition
2
+ dlib
3
+ numpy
4
+ matplotlib
5
+ pillow
6
+ streamlit