import os import cv2 import face_recognition import numpy as np from datetime import datetime import gradio as gr import pandas as pd import plotly.express as px import json class FaceRecognitionSystem: def __init__(self, images_folder='known_faces'): self.images_folder = images_folder self.known_face_encodings = [] self.known_face_names = [] self.last_recognition_time = {} self.recognition_cooldown = 60 self.attendance_file = 'attendance.json' self.load_face_database() def load_face_database(self): """Kayıtlı yüzleri yükler.""" self.known_face_encodings = [] self.known_face_names = [] os.makedirs(self.images_folder, exist_ok=True) for filename in os.listdir(self.images_folder): if filename.endswith((".jpg", ".png", ".jpeg")): image_path = os.path.join(self.images_folder, filename) try: image = face_recognition.load_image_file(image_path) face_locations = face_recognition.face_locations(image) if face_locations: face_encoding = face_recognition.face_encodings(image, face_locations)[0] self.known_face_encodings.append(face_encoding) self.known_face_names.append(filename.split('.')[0]) except Exception as e: print(f"Hata: {filename} dosyası yüklenirken hata oluştu - {str(e)}") def record_attendance(self, name): """Kişinin katılımını kaydeder.""" current_time = datetime.now() if name in self.last_recognition_time: time_diff = (current_time - self.last_recognition_time[name]).total_seconds() if time_diff < self.recognition_cooldown: return False attendance_data = self.load_attendance_data() current_date = current_time.strftime("%Y-%m-%d") current_time_str = current_time.strftime("%H:%M:%S") if current_date not in attendance_data: attendance_data[current_date] = {} if name not in attendance_data[current_date]: attendance_data[current_date][name] = [] attendance_data[current_date][name].append(current_time_str) self.save_attendance_data(attendance_data) self.last_recognition_time[name] = current_time return True def load_attendance_data(self): """Katılım verilerini yükler.""" if os.path.exists(self.attendance_file): with open(self.attendance_file, 'r') as f: return json.load(f) return {} def save_attendance_data(self, data): """Katılım verilerini kaydeder.""" with open(self.attendance_file, 'w') as f: json.dump(data, f, indent=4) def process_frame(self, frame): """Bir kareyi işler ve tanınan yüzleri işaretler.""" if frame is None: return None, [] rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) small_frame = cv2.resize(rgb_frame, (0, 0), fx=0.25, fy=0.25) face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings(small_frame, face_locations) detected_names = [] for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding, tolerance=0.6) name = "Bilinmeyen" if True in matches: first_match_index = matches.index(True) name = self.known_face_names[first_match_index] if self.record_attendance(name): detected_names.append(name) top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED) cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1) return frame, detected_names def get_attendance_stats(self): """Katılım istatistiklerini getirir.""" attendance_data = self.load_attendance_data() stats = [] for date, entries in attendance_data.items(): for name, times in entries.items(): stats.append({ 'date': date, 'name': name, 'total_entries': len(times), 'first_entry': min(times), 'last_entry': max(times) }) return pd.DataFrame(stats) def create_gradio_interface(): face_system = FaceRecognitionSystem() def process_webcam(frame): processed_frame, detected_names = face_system.process_frame(frame) return processed_frame def upload_face(image, name): if image is None or name.strip() == "": return "Lütfen hem resim hem de isim giriniz." os.makedirs(face_system.images_folder, exist_ok=True) file_path = os.path.join(face_system.images_folder, f"{name.strip()}.jpg") cv2.imwrite(file_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) face_system.load_face_database() return f"{name} başarıyla kaydedildi!" def get_attendance_report(): df = face_system.get_attendance_stats() if df.empty: return "Henüz katılım kaydı bulunmamaktadır." fig = px.bar(df, x='name', y='total_entries', title='Kişi Bazlı Toplam Katılım', labels={'name': 'İsim', 'total_entries': 'Toplam Katılım'}) table_html = df.to_html(classes='table table-striped', index=False) return f"""
{fig.to_html()}
{table_html}
""" with gr.Blocks(theme=gr.themes.Soft()) as interface: gr.Markdown("# 🎥 Yüz Tanıma ve Katılım Takip Sistemi") with gr.Tabs(): with gr.Tab("Canlı Tanıma"): gr.Markdown("## 📹 Kamera Görüntüsü") webcam = gr.Image(source="webcam", streaming=True) output_image = gr.Image() webcam.stream(process_webcam, inputs=[webcam], outputs=[output_image]) with gr.Tab("Yeni Kişi Kaydı"): gr.Markdown("## 👤 Yeni Kişi Ekle") with gr.Row(): image_input = gr.Image(type="numpy", label="Kişi Fotoğrafı") name_input = gr.Textbox(label="Kişi Adı") upload_button = gr.Button("Kaydet") upload_result = gr.Textbox(label="Sonuç") upload_button.click( upload_face, inputs=[image_input, name_input], outputs=[upload_result] ) with gr.Tab("Katılım Raporu"): gr.Markdown("## 📊 Katılım İstatistikleri") refresh_button = gr.Button("Raporu Yenile") report_html = gr.HTML() refresh_button.click( get_attendance_report, outputs=[report_html] ) return interface if __name__ == "__main__": interface = create_gradio_interface() interface.launch()