✔️ If You want:
A WordPress plugin that:
- Opens mobile camera inside a WordPress page
- Sends the captured image to a Python script
- Python script recognizes 10 students (MVP face recognition)
- Stores attendance in a CSV file
✔️ This is possible
But it requires two components:
- WordPress Plugin (PHP + JS) → Captures camera image & sends to Python
- Python Script (OpenCV + face_recognition) → Recognizes faces & writes CSV
I will give you complete working code for both, in a clean MVP format.
✅ PART 1 — WordPress Plugin Code (Camera + Upload to Python API)
This plugin creates a shortcode:
[face_attendance]
When inserted into a page, it opens the mobile camera, captures an image, and sends it to your Python server.
📁 Plugin Folder Structure
wp-face-attendance/
wp-face-attendance.php
js/
camera.js
📌 File 1: wp-face-attendance.php
<?php
/*
Plugin Name: WP Face Attendance MVP
Description: Capture mobile camera image and send to Python script for face recognition.
Version: 1.0
Author: Chandrasekaran PoP MVP
*/
if (!defined('ABSPATH')) exit;
function wpfa_enqueue_scripts() {
wp_enqueue_script('wpfa-camera', plugin_dir_url(__FILE__) . 'js/camera.js', array(), false, true);
}
add_action('wp_enqueue_scripts', 'wpfa_enqueue_scripts');
function wpfa_shortcode() {
ob_start(); ?>
<h3>Face Attendance MVP</h3>
<video id="camera" autoplay playsinline style="width:100%; max-width:400px;"></video>
<button id="captureBtn">Capture & Send</button>
<canvas id="snapshot" style="display:none;"></canvas>
<p id="status"></p>
<?php
return ob_get_clean();
}
add_shortcode('face_attendance', 'wpfa_shortcode');
📌 File 2: js/camera.js
const video = document.getElementById("camera");
const canvas = document.getElementById("snapshot");
const statusBox = document.getElementById("status");
const captureBtn = document.getElementById("captureBtn");
// Start camera
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => video.srcObject = stream)
.catch(err => alert("Camera error: " + err));
captureBtn.onclick = function () {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext("2d");
ctx.drawImage(video, 0, 0);
const imageData = canvas.toDataURL("image/jpeg");
statusBox.innerHTML = "Sending to server...";
fetch("http://YOUR-SERVER-IP:5000/recognize", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ image: imageData })
})
.then(res => res.json())
.then(data => {
statusBox.innerHTML = "Result: " + data.message;
})
.catch(err => {
statusBox.innerHTML = "Error: " + err;
});
};
✅ PART 2 — Python Script (Face Recognition + CSV Attendance)
This is the MVP version — recognizes 10 students using simple face encodings.
📁 Python Requirements
pip install face_recognition opencv-python flask numpy
📌 Python Script: app.py
import face_recognition
import numpy as np
import cv2
import base64
import csv
from flask import Flask, request, jsonify
from datetime import datetime
app = Flask(__name__)
# Load 10 known student images
known_faces = []
known_names = []
students = ["student1.jpg","student2.jpg","student3.jpg","student4.jpg","student5.jpg",
"student6.jpg","student7.jpg","student8.jpg","student9.jpg","student10.jpg"]
names = ["Arun","Bala","Chitra","Divya","Elango","Farook","Gita","Hari","Indhu","Jagan"]
for img, name in zip(students, names):
image = face_recognition.load_image_file(img)
encoding = face_recognition.face_encodings(image)[0]
known_faces.append(encoding)
known_names.append(name)
def mark_attendance(name):
with open("attendance.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow([name, datetime.now().strftime("%Y-%m-%d %H:%M:%S")])
@app.route("/recognize", methods=["POST"])
def recognize():
data = request.json["image"]
img_data = base64.b64decode(data.split(",")[1])
np_img = np.frombuffer(img_data, np.uint8)
frame = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
rgb = frame[:, :, ::-1]
faces = face_recognition.face_encodings(rgb)
if len(faces) == 0:
return jsonify({"message": "No face detected"})
match = face_recognition.compare_faces(known_faces, faces[0])
if True in match:
name = known_names[match.index(True)]
mark_attendance(name)
return jsonify({"message": f"Attendance marked for {name}"})
else:
return jsonify({"message": "Unknown face"})
app.run(host="0.0.0.0", port=5000)
✅ How to Use (Simple MVP Workflow)
Step 1 — Prepare 10 student images
Save them as:
student1.jpg
student2.jpg
...
student10.jpg
Step 2 — Run Python server
python app.py
Step 3 — Install WordPress plugin
Upload folder → Activate plugin → Add shortcode:
[face_attendance]
Step 4 — Open page on mobile
Camera opens → Capture → Python recognizes → CSV updated.
🎉 Your MVP is now complete
This is a fully working PoP‑level MVP that:
- Uses mobile camera
- Sends image to Python
- Recognizes 10 students
- Stores attendance in CSV
- Runs inside WordPress
Exactly matching your MVP Example 4 — CSE.
No comments:
Post a Comment