[pre-commit.ci lite] apply automatic fixes
This commit is contained in:
parent
da69690477
commit
5965757137
1 changed files with 47 additions and 30 deletions
|
|
@ -1,30 +1,42 @@
|
||||||
from flask import Flask, render_template, request, redirect, url_for, session
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from my_blockchain import Blockchain
|
|
||||||
import cv2
|
import cv2
|
||||||
|
from my_blockchain import Blockchain
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
|
from flask import redirect
|
||||||
|
from flask import render_template
|
||||||
|
from flask import request
|
||||||
|
from flask import session
|
||||||
|
from flask import url_for
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = "e-voting-secure-key"
|
app.secret_key = "e-voting-secure-key"
|
||||||
voting_chain = Blockchain()
|
voting_chain = Blockchain()
|
||||||
|
|
||||||
# Load face detection model
|
# Load face detection model
|
||||||
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
face_cascade = cv2.CascadeClassifier(
|
||||||
|
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Database Connection
|
# Database Connection
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
conn = sqlite3.connect('voters.db')
|
conn = sqlite3.connect("voters.db")
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
# Home Route
|
# Home Route
|
||||||
@app.route('/')
|
@app.route("/")
|
||||||
def home():
|
def home():
|
||||||
return render_template('index.html')
|
return render_template("index.html")
|
||||||
|
|
||||||
|
|
||||||
# Voter Authentication (Live Face Detection)
|
# Voter Authentication (Live Face Detection)
|
||||||
@app.route('/login', methods=['POST'])
|
@app.route("/login", methods=["POST"])
|
||||||
def login():
|
def login():
|
||||||
voter_name = request.form['name']
|
voter_name = request.form["name"]
|
||||||
|
|
||||||
# Check voter in database
|
# Check voter in database
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
|
|
@ -44,54 +56,59 @@ def login():
|
||||||
break
|
break
|
||||||
|
|
||||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||||
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
|
faces = face_cascade.detectMultiScale(
|
||||||
|
gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100)
|
||||||
|
)
|
||||||
|
|
||||||
if len(faces) > 0:
|
if len(faces) > 0:
|
||||||
verified = True
|
verified = True
|
||||||
break
|
break
|
||||||
|
|
||||||
cv2.imshow("Face Verification - Press 'Q' to Exit", frame)
|
cv2.imshow("Face Verification - Press 'Q' to Exit", frame)
|
||||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||||
break
|
break
|
||||||
|
|
||||||
cap.release()
|
cap.release()
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
if verified:
|
if verified:
|
||||||
session['voter_name'] = voter_name
|
session["voter_name"] = voter_name
|
||||||
return redirect(url_for('vote'))
|
return redirect(url_for("vote"))
|
||||||
else:
|
else:
|
||||||
return "❌ Face Verification Failed! Try Again."
|
return "❌ Face Verification Failed! Try Again."
|
||||||
|
|
||||||
return "❌ Voter Not Found! Please Register First."
|
return "❌ Voter Not Found! Please Register First."
|
||||||
|
|
||||||
# Voting Page
|
|
||||||
@app.route('/vote', methods=['GET', 'POST'])
|
|
||||||
def vote():
|
|
||||||
if 'voter_name' not in session:
|
|
||||||
return redirect(url_for('home'))
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
# Voting Page
|
||||||
vote_choice = request.form['vote']
|
@app.route("/vote", methods=["GET", "POST"])
|
||||||
voter_name = session['voter_name']
|
def vote():
|
||||||
|
if "voter_name" not in session:
|
||||||
|
return redirect(url_for("home"))
|
||||||
|
|
||||||
|
if request.method == "POST":
|
||||||
|
vote_choice = request.form["vote"]
|
||||||
|
voter_name = session["voter_name"]
|
||||||
|
|
||||||
voting_chain.add_vote(voter_name, vote_choice)
|
voting_chain.add_vote(voter_name, vote_choice)
|
||||||
session.pop('voter_name', None)
|
session.pop("voter_name", None)
|
||||||
|
|
||||||
return redirect(url_for('results'))
|
return redirect(url_for("results"))
|
||||||
|
|
||||||
|
return render_template("vote.html")
|
||||||
|
|
||||||
return render_template('vote.html')
|
|
||||||
|
|
||||||
# Results Page
|
# Results Page
|
||||||
@app.route('/results')
|
@app.route("/results")
|
||||||
def results():
|
def results():
|
||||||
vote_counts = {}
|
vote_counts = {}
|
||||||
for block in voting_chain.chain[1:]:
|
for block in voting_chain.chain[1:]:
|
||||||
vote = block['vote']
|
vote = block["vote"]
|
||||||
vote_counts[vote] = vote_counts.get(vote, 0) + 1
|
vote_counts[vote] = vote_counts.get(vote, 0) + 1
|
||||||
|
|
||||||
return render_template('results.html', votes=vote_counts)
|
return render_template("results.html", votes=vote_counts)
|
||||||
|
|
||||||
|
|
||||||
# Run Flask App
|
# Run Flask App
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue