diff --git a/app.py b/app.py new file mode 100644 index 0000000..ad102ec --- /dev/null +++ b/app.py @@ -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) diff --git a/cheat_detector_modelh5.py b/cheat_detector_modelh5.py new file mode 100644 index 0000000..0b40b27 --- /dev/null +++ b/cheat_detector_modelh5.py @@ -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') diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..f4f6a91 --- /dev/null +++ b/static/styles.css @@ -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; +} \ No newline at end of file diff --git a/templates/Imagesprocessing.html b/templates/Imagesprocessing.html new file mode 100644 index 0000000..df208f8 --- /dev/null +++ b/templates/Imagesprocessing.html @@ -0,0 +1,19 @@ + + + + + + Cheating Detection + + +

Upload an Image to Check for Cheating

+
+ + +
+ {% if result %} +

Prediction: {{ result }}

+

Confidence: {{ confidence }}

+ {% endif %} + +