[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,38 +1,50 @@
|
|||
from flask import Flask, render_template, request, redirect, url_for, session
|
||||
import sqlite3
|
||||
from my_blockchain import Blockchain
|
||||
|
||||
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.secret_key = "e-voting-secure-key"
|
||||
voting_chain = Blockchain()
|
||||
|
||||
# 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
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect('voters.db')
|
||||
conn = sqlite3.connect("voters.db")
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
# Home Route
|
||||
@app.route('/')
|
||||
@app.route("/")
|
||||
def home():
|
||||
return render_template('index.html')
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
# Voter Authentication (Live Face Detection)
|
||||
@app.route('/login', methods=['POST'])
|
||||
@app.route("/login", methods=["POST"])
|
||||
def login():
|
||||
voter_name = request.form['name']
|
||||
|
||||
voter_name = request.form["name"]
|
||||
|
||||
# Check voter in database
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM voters WHERE name=?", (voter_name,))
|
||||
voter = cursor.fetchone()
|
||||
conn.close()
|
||||
|
||||
|
||||
if voter:
|
||||
# Start Face Verification
|
||||
cap = cv2.VideoCapture(0)
|
||||
|
|
@ -44,54 +56,59 @@ def login():
|
|||
break
|
||||
|
||||
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:
|
||||
verified = True
|
||||
break
|
||||
|
||||
cv2.imshow("Face Verification - Press 'Q' to Exit", frame)
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
if verified:
|
||||
session['voter_name'] = voter_name
|
||||
return redirect(url_for('vote'))
|
||||
session["voter_name"] = voter_name
|
||||
return redirect(url_for("vote"))
|
||||
else:
|
||||
return "❌ Face Verification Failed! Try Again."
|
||||
|
||||
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':
|
||||
vote_choice = request.form['vote']
|
||||
voter_name = session['voter_name']
|
||||
# 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":
|
||||
vote_choice = request.form["vote"]
|
||||
voter_name = session["voter_name"]
|
||||
|
||||
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
|
||||
@app.route('/results')
|
||||
@app.route("/results")
|
||||
def results():
|
||||
vote_counts = {}
|
||||
for block in voting_chain.chain[1:]:
|
||||
vote = block['vote']
|
||||
vote = block["vote"]
|
||||
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
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue