{"id":245,"date":"2025-11-20T15:02:28","date_gmt":"2025-11-20T15:02:28","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=245"},"modified":"2025-11-20T15:02:28","modified_gmt":"2025-11-20T15:02:28","slug":"36-real-world-python-projects-face-recognition-attendance-system","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=245","title":{"rendered":"36 &#8211; Real-World Python Projects &#8211; Face Recognition Attendance System"},"content":{"rendered":"\n<h6 class=\"wp-block-heading\"><\/h6>\n\n\n\n<p>This system automatically:<\/p>\n\n\n\n<p>\u2714 Detects faces<br>\u2714 Recognizes known students\/employees<br>\u2714 Marks attendance into CSV<br>\u2714 Stores date\/time for each entry<br>\u2714 Prevents duplicate attendance for the same day<\/p>\n\n\n\n<p>Perfect for office, classroom, labs, events, coaching centers, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde0 <strong>Tech Stack<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Component<\/th><th>Library<\/th><\/tr><\/thead><tbody><tr><td>Face Detection<\/td><td>OpenCV<\/td><\/tr><tr><td>Face Recognition<\/td><td><code>face_recognition<\/code><\/td><\/tr><tr><td>Data Storage<\/td><td>CSV<\/td><\/tr><tr><td>Camera Capture<\/td><td>OpenCV<\/td><\/tr><tr><td>Encoding Faces<\/td><td>dlib (used internally by face_recognition)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2699\ufe0f <strong>Install Required Libraries<\/strong><\/h1>\n\n\n\n<p>Run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install opencv-python\npip install face_recognition\npip install numpy\npip install pandas\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 <strong>face_recognition requires CMake + dlib<\/strong><br>If error comes, I will give you auto installer for dlib.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcc1 <strong>Folder Structure<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>FaceAttendance\/\n\u2502\n\u251c\u2500\u2500 images\/\n\u2502     \u251c\u2500\u2500 Sameer.jpg\n\u2502     \u251c\u2500\u2500 John.jpg\n\u2502     \u2514\u2500\u2500 Ayesha.jpg\n\u2502\n\u251c\u2500\u2500 attendance.csv\n\u2502\n\u2514\u2500\u2500 face_attendance.py\n<\/code><\/pre>\n\n\n\n<p>You must store <strong>clear front-facing photos<\/strong> in <code>\/images<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 <strong>FULL WORKING CODE (face_attendance.py)<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>import cv2\nimport numpy as np\nimport face_recognition\nimport os\nimport pandas as pd\nfrom datetime import datetime\n\npath = 'images'\nimages = &#91;]\nclassNames = &#91;]\nmyList = os.listdir(path)\n\nprint(\"Loading images...\")\n\nfor cls in myList:\n    curImg = cv2.imread(f'{path}\/{cls}')\n    images.append(curImg)\n    classNames.append(os.path.splitext(cls)&#91;0])\n\ndef findEncodings(images):\n    encodeList = &#91;]\n    for img in images:\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        encode = face_recognition.face_encodings(img)&#91;0]\n        encodeList.append(encode)\n    return encodeList\n\ndef markAttendance(name):\n    df = pd.read_csv('attendance.csv')\n\n    today = datetime.now().strftime('%Y-%m-%d')\n    time_now = datetime.now().strftime('%H:%M:%S')\n\n    if not ((df&#91;'Name'] == name) &amp; (df&#91;'Date'] == today)).any():\n        new_entry = pd.DataFrame(&#91;&#91;name, today, time_now]], \n                                 columns=&#91;'Name', 'Date', 'Time'])\n        df = pd.concat(&#91;df, new_entry], ignore_index=True)\n        df.to_csv('attendance.csv', index=False)\n        print(f\"Attendance marked for {name}\")\n    else:\n        print(f\"{name} already marked today\")\n\nencodeListKnown = findEncodings(images)\nprint(\"Encoding Complete\")\n\ncap = cv2.VideoCapture(0)\n\nwhile True:\n    success, img = cap.read()\n    imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)\n    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)\n\n    facesCurFrame = face_recognition.face_locations(imgS)\n    encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)\n\n    for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):\n        matches = face_recognition.compare_faces(encodeListKnown, encodeFace)\n        faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)\n\n        matchIndex = np.argmin(faceDis)\n\n        if matches&#91;matchIndex]:\n            name = classNames&#91;matchIndex].upper()\n\n            y1, x2, y2, x1 = faceLoc\n            y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4\n\n            cv2.rectangle(img, (x1, y1), (x2, y2), (0,255,0), 2)\n            cv2.rectangle(img, (x1, y2), (x2, y2+35), (0,255,0), cv2.FILLED)\n            cv2.putText(img, name, (x1+6, y2+28), cv2.FONT_HERSHEY_SIMPLEX, \n                        1, (255,255,255), 2)\n\n            markAttendance(name)\n\n    cv2.imshow('Face Attendance', img)\n    if cv2.waitKey(1) == 27:\n        break\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcc4 <strong>Create attendance.csv (Empty File)<\/strong><\/h1>\n\n\n\n<p>Create <code>attendance.csv<\/code> with this header:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name,Date,Time\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfa5 <strong>How It Works<\/strong><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Loads all face images from <code>\/images\/<\/code><\/li>\n\n\n\n<li>Converts them into encodings<\/li>\n\n\n\n<li>Starts webcam<\/li>\n\n\n\n<li>Detects a face<\/li>\n\n\n\n<li>Compares with known encodings<\/li>\n\n\n\n<li>Identifies person<\/li>\n\n\n\n<li>Marks attendance in <code>attendance.csv<\/code><\/li>\n\n\n\n<li>Prevents duplicate attendance for the same day<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This system automatically: \u2714 Detects faces\u2714 Recognizes known students\/employees\u2714 Marks attendance into CSV\u2714 Stores date\/time for each entry\u2714 Prevents duplicate attendance for the same day Perfect for office, classroom, labs, events, coaching centers, etc. \ud83e\udde0 Tech Stack Component Library Face Detection OpenCV Face Recognition face_recognition Data Storage CSV Camera Capture OpenCV Encoding Faces dlib (used [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}