Skip to content

Commit

Permalink
Image Classification Application: Cheating vs. Not Cheating tanishane…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ananya-vastare committed Nov 9, 2024
1 parent 52320ed commit 3129c23
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
65 changes: 65 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from flask import Flask, request, render_template, jsonify
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import os
from werkzeug.utils import secure_filename

app = Flask(__name__)

# Load the pre-trained model
model = tf.keras.models.load_model("cheat_detector_model.h5")

# Define the path to save uploaded images
UPLOAD_FOLDER = "static/uploads"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER

# Ensure the upload folder exists
os.makedirs(UPLOAD_FOLDER, exist_ok=True)


# Preprocess the image for prediction
def prepare_image(image_path):
img = load_img(
image_path, target_size=(150, 150)
) # Resize as per model input shape
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0 # Normalize if required by the model
return img_array


@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
if "file" not in request.files:
return "No file part"
file = request.files["file"]
if file.filename == "":
return "No selected file"
if file:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(filepath)

# Preprocess the image and make prediction
img_array = prepare_image(filepath)
prediction = model.predict(img_array)[0][0]

# Classify as "Cheating" or "Not Cheating"
if prediction > 0.5: # Adjust threshold as needed
result = "Cheating"
else:
result = "Not Cheating"

# Remove the saved image after prediction
os.remove(filepath)

# Return result as JSON
return jsonify({"result": result, "confidence": float(prediction)})

return render_template("index.html")


if __name__ == "__main__":
app.run(debug=True)
29 changes: 29 additions & 0 deletions cheat_detector_modelh5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Define a simple CNN model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(128, activation='relu'),
Dense(1, activation='sigmoid') # Sigmoid for binary classification
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Training data generators
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'data/train', # Training data directory
target_size=(150, 150),
batch_size=32,
class_mode='binary'
)

# Fit the model
model.fit(train_generator, epochs=10)
model.save('cheat_detector_model.h5')
19 changes: 19 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
}

h2 {
margin-top: 20px;
}

#upload-form {
margin-top: 30px;
}

#result {
margin-top: 20px;
font-weight: bold;
}
19 changes: 19 additions & 0 deletions templates/Imagesprocessing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cheating Detection</title>
</head>
<body>
<h2>Upload an Image to Check for Cheating</h2>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="image/*" required>
<button type="submit">Upload and Detect</button>
</form>
{% if result %}
<h3>Prediction: {{ result }}</h3>
<p>Confidence: {{ confidence }}</p>
{% endif %}
</body>
</html>

0 comments on commit 3129c23

Please sign in to comment.