Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,23 +8,101 @@ import pandas as pd
|
|
8 |
import plotly.express as px
|
9 |
import json
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def create_gradio_interface():
|
14 |
face_system = FaceRecognitionSystem()
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
return
|
19 |
|
20 |
def upload_face(image, name):
|
21 |
if image is None or name.strip() == "":
|
22 |
return "Lütfen hem resim hem de isim giriniz."
|
23 |
-
|
24 |
os.makedirs(face_system.images_folder, exist_ok=True)
|
25 |
file_path = os.path.join(face_system.images_folder, f"{name.strip()}.jpg")
|
26 |
-
cv2.imwrite(file_path,
|
27 |
-
|
28 |
face_system.load_face_database()
|
29 |
return f"{name} başarıyla kaydedildi!"
|
30 |
|
@@ -32,13 +110,10 @@ def create_gradio_interface():
|
|
32 |
df = face_system.get_attendance_stats()
|
33 |
if df.empty:
|
34 |
return "Henüz katılım kaydı bulunmamaktadır."
|
35 |
-
|
36 |
fig = px.bar(df, x='name', y='total_entries',
|
37 |
title='Kişi Bazlı Toplam Katılım',
|
38 |
labels={'name': 'İsim', 'total_entries': 'Toplam Katılım'})
|
39 |
-
|
40 |
table_html = df.to_html(classes='table table-striped', index=False)
|
41 |
-
|
42 |
return f"""
|
43 |
<div style='margin-bottom: 20px;'>
|
44 |
{fig.to_html()}
|
@@ -49,30 +124,31 @@ def create_gradio_interface():
|
|
49 |
"""
|
50 |
|
51 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
52 |
-
gr.Markdown("# 🎥 Yüz Tanıma ve Katılım Takip Sistemi--ERAY
|
53 |
|
54 |
with gr.Tabs():
|
55 |
-
with gr.Tab("
|
56 |
-
gr.Markdown("##
|
57 |
-
|
58 |
-
output_image = gr.Image()
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
with gr.Tab("Yeni Kişi Kaydı"):
|
67 |
gr.Markdown("## 👤 Yeni Kişi Ekle")
|
68 |
with gr.Row():
|
69 |
-
|
70 |
name_input = gr.Textbox(label="Kişi Adı")
|
71 |
upload_button = gr.Button("Kaydet")
|
72 |
upload_result = gr.Textbox(label="Sonuç")
|
73 |
upload_button.click(
|
74 |
upload_face,
|
75 |
-
inputs=[
|
76 |
outputs=[upload_result]
|
77 |
)
|
78 |
|
|
|
8 |
import plotly.express as px
|
9 |
import json
|
10 |
|
11 |
+
class FaceRecognitionSystem:
|
12 |
+
def __init__(self, images_folder='known_faces'):
|
13 |
+
self.images_folder = images_folder
|
14 |
+
self.known_face_encodings = []
|
15 |
+
self.known_face_names = []
|
16 |
+
self.attendance_file = 'attendance.json'
|
17 |
+
self.load_face_database()
|
18 |
+
|
19 |
+
def load_face_database(self):
|
20 |
+
self.known_face_encodings = []
|
21 |
+
self.known_face_names = []
|
22 |
+
os.makedirs(self.images_folder, exist_ok=True)
|
23 |
+
for filename in os.listdir(self.images_folder):
|
24 |
+
if filename.endswith((".jpg", ".png", ".jpeg")):
|
25 |
+
image_path = os.path.join(self.images_folder, filename)
|
26 |
+
try:
|
27 |
+
image = face_recognition.load_image_file(image_path)
|
28 |
+
face_locations = face_recognition.face_locations(image)
|
29 |
+
if face_locations:
|
30 |
+
face_encoding = face_recognition.face_encodings(image, face_locations)[0]
|
31 |
+
self.known_face_encodings.append(face_encoding)
|
32 |
+
self.known_face_names.append(filename.split('.')[0])
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Hata: {filename} dosyası yüklenirken hata oluştu - {str(e)}")
|
35 |
+
|
36 |
+
def record_attendance(self, name):
|
37 |
+
current_time = datetime.now()
|
38 |
+
attendance_data = self.load_attendance_data()
|
39 |
+
current_date = current_time.strftime("%Y-%m-%d")
|
40 |
+
current_time_str = current_time.strftime("%H:%M:%S")
|
41 |
+
if current_date not in attendance_data:
|
42 |
+
attendance_data[current_date] = {}
|
43 |
+
if name not in attendance_data[current_date]:
|
44 |
+
attendance_data[current_date][name] = []
|
45 |
+
attendance_data[current_date][name].append(current_time_str)
|
46 |
+
self.save_attendance_data(attendance_data)
|
47 |
+
return True
|
48 |
+
|
49 |
+
def load_attendance_data(self):
|
50 |
+
if os.path.exists(self.attendance_file):
|
51 |
+
with open(self.attendance_file, 'r') as f:
|
52 |
+
return json.load(f)
|
53 |
+
return {}
|
54 |
+
|
55 |
+
def save_attendance_data(self, data):
|
56 |
+
with open(self.attendance_file, 'w') as f:
|
57 |
+
json.dump(data, f, indent=4)
|
58 |
+
|
59 |
+
def process_image(self, image):
|
60 |
+
if image is None:
|
61 |
+
return None, []
|
62 |
+
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
63 |
+
face_locations = face_recognition.face_locations(rgb_image)
|
64 |
+
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
|
65 |
+
detected_names = []
|
66 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
67 |
+
matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding, tolerance=0.6)
|
68 |
+
name = "Bilinmeyen"
|
69 |
+
if True in matches:
|
70 |
+
first_match_index = matches.index(True)
|
71 |
+
name = self.known_face_names[first_match_index]
|
72 |
+
self.record_attendance(name)
|
73 |
+
detected_names.append(name)
|
74 |
+
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
|
75 |
+
cv2.rectangle(image, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED)
|
76 |
+
cv2.putText(image, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1)
|
77 |
+
return image, detected_names
|
78 |
+
|
79 |
+
def get_attendance_stats(self):
|
80 |
+
attendance_data = self.load_attendance_data()
|
81 |
+
stats = []
|
82 |
+
for date, entries in attendance_data.items():
|
83 |
+
for name, times in entries.items():
|
84 |
+
stats.append({
|
85 |
+
'date': date,
|
86 |
+
'name': name,
|
87 |
+
'total_entries': len(times),
|
88 |
+
'first_entry': min(times),
|
89 |
+
'last_entry': max(times)
|
90 |
+
})
|
91 |
+
return pd.DataFrame(stats)
|
92 |
|
93 |
def create_gradio_interface():
|
94 |
face_system = FaceRecognitionSystem()
|
95 |
|
96 |
+
def process_uploaded_image(image):
|
97 |
+
processed_image, detected_names = face_system.process_image(image)
|
98 |
+
return processed_image, ", ".join(detected_names) if detected_names else "Kimse tespit edilmedi."
|
99 |
|
100 |
def upload_face(image, name):
|
101 |
if image is None or name.strip() == "":
|
102 |
return "Lütfen hem resim hem de isim giriniz."
|
|
|
103 |
os.makedirs(face_system.images_folder, exist_ok=True)
|
104 |
file_path = os.path.join(face_system.images_folder, f"{name.strip()}.jpg")
|
105 |
+
cv2.imwrite(file_path, image)
|
|
|
106 |
face_system.load_face_database()
|
107 |
return f"{name} başarıyla kaydedildi!"
|
108 |
|
|
|
110 |
df = face_system.get_attendance_stats()
|
111 |
if df.empty:
|
112 |
return "Henüz katılım kaydı bulunmamaktadır."
|
|
|
113 |
fig = px.bar(df, x='name', y='total_entries',
|
114 |
title='Kişi Bazlı Toplam Katılım',
|
115 |
labels={'name': 'İsim', 'total_entries': 'Toplam Katılım'})
|
|
|
116 |
table_html = df.to_html(classes='table table-striped', index=False)
|
|
|
117 |
return f"""
|
118 |
<div style='margin-bottom: 20px;'>
|
119 |
{fig.to_html()}
|
|
|
124 |
"""
|
125 |
|
126 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
127 |
+
gr.Markdown("# 🎥 Yüz Tanıma ve Katılım Takip Sistemi--ERAY COŞKUN")
|
128 |
|
129 |
with gr.Tabs():
|
130 |
+
with gr.Tab("Yüz Tanıma"):
|
131 |
+
gr.Markdown("## 📷 Resim Yükle ve Tanı")
|
132 |
+
image_input = gr.Image(type="numpy", label="Resim Yükle")
|
133 |
+
output_image = gr.Image(label="İşlenmiş Resim")
|
134 |
+
output_text = gr.Textbox(label="Tespit Edilen Kişiler")
|
135 |
+
process_button = gr.Button("Resmi İşle")
|
136 |
+
process_button.click(
|
137 |
+
process_uploaded_image,
|
138 |
+
inputs=[image_input],
|
139 |
+
outputs=[output_image, output_text]
|
140 |
+
)
|
141 |
|
142 |
with gr.Tab("Yeni Kişi Kaydı"):
|
143 |
gr.Markdown("## 👤 Yeni Kişi Ekle")
|
144 |
with gr.Row():
|
145 |
+
new_image_input = gr.Image(type="numpy", label="Kişi Fotoğrafı")
|
146 |
name_input = gr.Textbox(label="Kişi Adı")
|
147 |
upload_button = gr.Button("Kaydet")
|
148 |
upload_result = gr.Textbox(label="Sonuç")
|
149 |
upload_button.click(
|
150 |
upload_face,
|
151 |
+
inputs=[new_image_input, name_input],
|
152 |
outputs=[upload_result]
|
153 |
)
|
154 |
|