Skip to content

Commit

Permalink
Enhancing Offline Capabilities of S-Proctor for Real-Time Cheating De…
Browse files Browse the repository at this point in the history
…tection (#46)

Fixes #36
  • Loading branch information
sanchitc05 authored Oct 20, 2024
1 parent 55ab0ee commit bf3f606
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Backend/Offline-capabilities/ML-Model-Optimisation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import tensorflow as tf
import tensorflow.lite as tflite

# Load the TensorFlow Lite model
def load_tflite_model(model_path):
interpreter = tflite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()

return interpreter

# Sample cheat detection using the lightweight model
def detect_cheating_tflite(frame, interpreter):
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Preprocess the frame (resize, normalize, etc.)
input_data = preprocess_frame_for_model(frame)

# Set the input tensor
interpreter.set_tensor(input_details[0]['index'], input_data)

# Run inference
interpreter.invoke()

# Get the result
cheat_probability = interpreter.get_tensor(output_details[0]['index'])
return cheat_probability

def preprocess_frame_for_model(frame):
# Example preprocessing step (adjust according to your model)
frame_resized = cv2.resize(frame, (224, 224))
frame_normalized = frame_resized / 255.0
return frame_normalized.reshape(1, 224, 224, 3).astype('float32')
28 changes: 28 additions & 0 deletions Backend/Offline-capabilities/local-storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sqlite3

# Create or connect to a local database
def setup_local_database():
conn = sqlite3.connect('sproctor_offline.db') # Create SQLite DB file
cursor = conn.cursor()

# Create table for storing behavior logs
cursor.execute('''CREATE TABLE IF NOT EXISTS behavior_logs
(id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id TEXT,
timestamp TEXT,
behavior TEXT,
cheat_probability REAL)''')

conn.commit()
conn.close()

# Insert behavior log into the local database
def log_behavior(student_id, behavior, cheat_probability):
conn = sqlite3.connect('sproctor_offline.db')
cursor = conn.cursor()

cursor.execute("INSERT INTO behavior_logs (student_id, timestamp, behavior, cheat_probability) VALUES (?, datetime('now'), ?, ?)",
(student_id, behavior, cheat_probability))

conn.commit()
conn.close()
26 changes: 26 additions & 0 deletions Backend/Offline-capabilities/processing-data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import sqlite3

def sync_data_to_server():
if check_internet_connection():
conn = sqlite3.connect('sproctor_offline.db')
cursor = conn.cursor()

cursor.execute("SELECT * FROM behavior_logs")
logs = cursor.fetchall()

for log in logs:
# Simulate uploading logs to the server
print(f"Syncing log: {log}")
# Code to upload log to the server can be added here

# Clear local logs after successful sync
cursor.execute("DELETE FROM behavior_logs")
conn.commit()
conn.close()
else:
print("No internet connection. Data will be synced later.")

def check_internet_connection():
# Use os.system to ping a public DNS server to check internet connection
return os.system("ping -c 1 google.com") == 0
185 changes: 185 additions & 0 deletions Backend/Offline-capabilities/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# S-Proctor: Offline Exam Proctoring

**S-Proctor** is an AI-based smart proctoring system designed for **offline exams** that ensures fairness, equity, and limited resources during examination by leveraging real-time cheat detection using machine learning and computer vision. The offline proctoring feature enhances its ability to operate even without continuous internet access, making it adaptable to low-infrastructure areas.

---

## **Table of Contents**

- [Overview](#overview)
- [Features](#features)
- [Offline Capabilities](#offline-capabilities)
- [Local Data Storage (SQLite)](#local-data-storage-sqlite)
- [Efficient Video Processing](#efficient-video-processing)
- [Batch Data Syncing](#batch-data-syncing)
- [Lightweight ML Models (TensorFlow Lite)](#lightweight-ml-models-tensorflow-lite)
- [Resource Management](#resource-management)
- [Technologies Used](#technologies-used)
- [Setup Instructions](#setup-instructions)
- [Installation](#installation)
- [How to Run](#how-to-run)
- [How the Offline Feature Works](#how-the-offline-feature-works)
- [Contribution Guidelines](#contribution-guidelines)
- [License](#license)

---

## **Overview**

**S-Proctor** leverages **machine learning (ML)** algorithms to monitor student behavior during offline exams and detect possible cheating activities using just a device camera. The offline proctoring system stores behavior logs locally when the internet is unavailable and synchronizes them when connectivity is restored.

---

## **Features**

1. **AI-Driven Proctoring:** Automatically monitors student behavior using computer vision and ML algorithms.
2. **Real-Time Cheat Detection:** Continuously assesses behavior to detect suspicious activities.
3. **Offline Proctoring:** Works seamlessly without requiring internet access during the exam.
4. **Efficient Syncing:** Stores data locally and syncs it when internet access is restored.

---

## **Offline Capabilities**

The offline proctoring feature enhances the existing capabilities of S-Proctor by ensuring that exams can be proctored without continuous internet connectivity. The solution involves using **local storage** to store video and behavior logs, optimizing the video processing pipeline, and efficiently syncing data once the connection is restored.

### **Local Data Storage (SQLite)**

The offline mode uses **SQLite** to store behavior logs locally. This enables S-Proctor to continue functioning smoothly in areas with limited or no internet connection.

```python
# Initialize a local SQLite database
def setup_local_database():
# Creates or connects to an SQLite database to store logs locally
```

Behavior logs, including **student actions** and **cheat probabilities**, are stored in a table and synced later to the server once the connection is restored.

### **Efficient Video Processing**

Video feeds are processed efficiently using **OpenCV**. To reduce the workload and enable real-time analysis even on low-spec machines, only every nth frame is processed, making the system lighter and faster.

```python
# Process video efficiently by only analyzing every nth frame
def process_video_offline(video_source, frame_interval=30):
# Efficient video processing pipeline
```

This feature ensures that the proctoring system remains responsive, even during offline exams.

### **Batch Data Syncing**

When internet connectivity is restored, the system will sync the stored logs to the server. Until then, logs are kept safe in the local SQLite database.

```python
# Sync data to the server when internet is available
def sync_data_to_server():
# Automatically syncs logs when the connection is restored
```

The system uses the `ping` method to check for internet connectivity and uploads all pending data once the connection is available.

### **Lightweight ML Models (TensorFlow Lite)**

To ensure the offline feature runs smoothly, **TensorFlow Lite** is used for running lightweight machine learning models that detect cheating. These models are smaller, faster, and work efficiently on local machines, reducing dependency on heavy cloud-based models.

```python
# Load a TensorFlow Lite model for efficient local processing
def load_tflite_model(model_path):
# Loading lightweight model
```

The optimized models ensure that cheat detection can happen in real-time, even in offline conditions.

### **Resource Management**

The system dynamically adjusts based on available **CPU and memory resources**. This ensures that if resources are limited, the video processing frame rate or resolution is reduced to maintain performance.

```python
# Dynamically adjust processing based on CPU and memory usage
def adjust_processing_based_on_resources():
# Adjust video processing dynamically
```

---

## **Technologies Used**

- **Python**: The primary language used to implement the offline capabilities.
- **SQLite**: For local storage of data and logs during offline mode.
- **OpenCV**: Used for capturing and processing video frames.
- **MediaPipe**: Employed for facial and gesture recognition.
- **TensorFlow Lite**: Used to run optimized ML models offline.
- **psutil**: For monitoring system resources like CPU and memory usage.

---

## **Setup Instructions**

### **Installation**

1. **Clone the Repository**:
```bash
git clone https://github.com/yourusername/s-proctor.git
cd s-proctor
```

2. **Install Dependencies**:
Inside your **GitHub Codespace** or local environment, install the required packages.
```bash
pip install -r requirements.txt
```

### **How to Run**

1. **Set Up Local Database**:
Run the script to initialize the SQLite database.
```bash
python setup_local_db.py
```

2. **Run the Proctoring System**:
Start the offline proctoring feature by running the following command:
```bash
python proctoring_system.py
```

3. **Sync Data When Internet is Available**:
Sync your offline data logs once you’re connected to the internet.
```bash
python sync_data.py
```

---

## **How the Offline Feature Works**

1. **Start Exam Monitoring**: When an exam starts, S-Proctor monitors student behavior using the device camera.
2. **Capture and Process Video**: Using **OpenCV**, video feeds are captured and analyzed. Instead of processing every frame, the system processes key frames to balance accuracy and efficiency.
3. **Log Behavior Locally**: All behavior data and cheat probabilities are stored locally in an SQLite database.
4. **Sync Data**: Once the exam is over or the internet connection is restored, all logs are synced to the server.

---

## **Contribution Guidelines**

We welcome contributions to enhance **S-Proctor**. To contribute, follow these steps:

1. Fork the repository.
2. Create a new branch for your feature (`git checkout -b feature-branch`).
3. Commit your changes (`git commit -m "Add offline-capabilities"`).
4. Push to the branch (`git push origin feature-branch`).
5. Submit a pull request.

Ensure your code adheres to the following guidelines:
- Write clear, concise, and commented code.
- Include tests for new features.
- Ensure backward compatibility.

For more details, check out our [contribution guidelines](CONTRIBUTING.md).

---

## **License**

This project is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for more details.
16 changes: 16 additions & 0 deletions Backend/Offline-capabilities/resource-management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import psutil

def adjust_processing_based_on_resources():
cpu_usage = psutil.cpu_percent()
memory_usage = psutil.virtual_memory().percent

if cpu_usage > 80 or memory_usage > 75:
print("High resource usage detected, lowering frame rate...")
return 60 # Process fewer frames (lower frame rate)
else:
return 30 # Normal frame rate

# Use this function to adjust the video processing loop
def process_video_dynamically(video_source):
frame_interval = adjust_processing_based_on_resources()
process_video_offline(video_source, frame_interval)
30 changes: 30 additions & 0 deletions Backend/Offline-capabilities/video-processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import cv2

def process_video_offline(video_source, frame_interval=30):
cap = cv2.VideoCapture(video_source)

frame_count = 0

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break

# Process every nth frame
if frame_count % frame_interval == 0:
# Apply cheat detection algorithms here
detect_cheating(frame)

frame_count += 1
cv2.imshow('Exam Monitoring', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

# Sample function to simulate cheat detection
def detect_cheating(frame):
# Perform facial recognition or gesture analysis here
print("Analyzing frame for cheating behavior...")

0 comments on commit bf3f606

Please sign in to comment.