forked from tanishaness/SPROCTOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Image Classification Application: Cheating vs. Not Cheating tanishane…
- Loading branch information
1 parent
52320ed
commit 3129c23
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |