Skip to content

Commit

Permalink
Added user accounts and database
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetkkn07 committed Jan 4, 2021
1 parent 74d1cb4 commit c33af0d
Show file tree
Hide file tree
Showing 20 changed files with 735 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/*
# Created by https://www.toptal.com/developers/gitignore/api/flask,python,windows,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=flask,python,windows,macos

Expand Down
Empty file added database.db
Empty file.
213 changes: 213 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
from os import curdir
import sqlite3 as db
import hashlib

from numpy.core.records import record

SALT = "PasswordSalt"
DB_NAME = "database.db"


def hash(password):
saltedPassword = password + SALT
hashedPassword = hashlib.md5(saltedPassword.encode()).hexdigest()
return hashedPassword


def connectToDb():
try:
con = db.connect(DB_NAME)
cursor = con.cursor()
except:
pass
else:
return con, cursor

# USERS


def createUsersTable(arg):
con, cursor = arg
try:
cursor.execute(
"CREATE TABLE IF NOT EXISTS users(name TEXT, surname TEXT, email TEXT NOT NULL PRIMARY KEY, password TEXT)")
con.commit()
except:
pass
else:
return con, cursor


def initializeUsersTable():
return createUsersTable(connectToDb())


def register(name, surname, email, password):
con, cursor = initializeUsersTable()
try:
cursor.execute("INSERT INTO users VALUES(?,?,?,?)",
(name, surname, email, hash(password)))
except:
return False
else:
con.commit()
con.close()
return True


def getUserByEmail(email):
con, cursor = initializeUsersTable()
try:
cursor.execute("SELECT * FROM users WHERE email = ?", (email,))
user = cursor.fetchone()
except:
return None
else:
con.close()
if not user:
return None
else:
return user


def deleteUserByEmail(email):
con, cursor = initializeUsersTable()
cursor.execute("DELETE FROM users WHERE email = ?", (email,))
con.commit()


def login(email, password):
con, cursor = initializeUsersTable()
try:
cursor.execute(
"SELECT email, password FROM users WHERE email = ?", (email,))
user = cursor.fetchone()
except:
return False
else:
if user[0] == email and user[1] == hash(password):
return True
return False


# OPERATIONS


def createOperationsTable(arg):
con, cursor = arg
try:
cursor.execute(
"CREATE TABLE IF NOT EXISTS operations(id TEXT NOT NULL PRIMARY KEY, email TEXT, answerKey TEXT)")
con.commit()
except:
pass
else:
return con, cursor


def initializeOperationsTable():
return createOperationsTable(connectToDb())


def getOperationsByEmail(email):
con, cursor = initializeOperationsTable()
try:
cursor.execute("SELECT * FROM operations WHERE email = ?", (email,))
operations = cursor.fetchall()
except:
return None
else:
con.close()
if not operations:
return None
else:
operations.reverse()
return operations


def addOperation(id, email, answerKey):
con, cursor = initializeOperationsTable()
id = id.split('uploads/')[1]
try:
cursor.execute("INSERT INTO operations VALUES(?,?,?)",
(id, email, answerKey))
except:
return False
else:
con.commit()
con.close()
return True


def getOperationById(id):
con, cursor = initializeOperationsTable()
try:
cursor.execute("SELECT * FROM operations WHERE id = ?", (id,))
record = cursor.fetchone()
except:
return None
else:
con.close()
if not record:
return None
else:
return record


def deleteOperation(id):
con, cursor = initializeOperationsTable()
try:
cursor.execute("DELETE FROM operations WHERE id = ?", (id))
except:
return False
else:
con.commit()
con.close()
return True

# RECORDS


def createRecordsTable(arg):
con, cursor = arg
try:
cursor.execute(
"CREATE TABLE IF NOT EXISTS records(id TEXT, nameImage TEXT, correct INT, wrong INT, empty INT, score REAL, answers TEXT, image TEXT)")
con.commit()
except:
pass
else:
return con, cursor


def initializeRecordsTable():
return createRecordsTable(connectToDb())


def getRecordsById(id):
con, cursor = initializeRecordsTable()
try:
cursor.execute("SELECT * FROM records WHERE id = ?", (id,))
records = cursor.fetchall()
except:
return None
else:
con.close()
if not records:
return None
else:
return records


def addRecord(id, nameImage, correct, wrong, empty, score, answer, image):
con, cursor = initializeRecordsTable()
id = id.split('uploads/')[1]
try:
cursor.execute("INSERT INTO records VALUES(?,?,?,?,?,?,?,?)",
(id, nameImage, correct, wrong, empty, score, answer, image))
except:
return False
else:
con.commit()
con.close()
return True
13 changes: 6 additions & 7 deletions omr.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def getAnswers(img):
thresh = cv2.threshold(
blurred, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
edged = cv2.Canny(thresh, 75, 200)

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
Expand Down Expand Up @@ -110,7 +109,6 @@ def getAnswers(img):
center, radius = cv2.minEnclosingCircle(qCnt)
if radius < (meanRadius / 2) or radius > (meanRadius * 2):
questionCnts.remove(qCnt)
# powered by sametuluerr

for (qNum, i) in enumerate(np.arange(0, len(questionCnts), 5)):
bubbledCount = 0
Expand Down Expand Up @@ -156,6 +154,7 @@ def getScores(img, ANSWER_KEY, UPLOAD_FOLDER):
list: score, image of name field, image of bubbled correct and wrong answers, correct, wrong, empty
"""
image = cv2.imread(img)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(
Expand Down Expand Up @@ -200,16 +199,14 @@ def getScores(img, ANSWER_KEY, UPLOAD_FOLDER):
nameField = cv2.medianBlur(nameField, 5)
nameField = cv2.threshold(
nameField, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
nameImg = img.split(".jpg")[0]+"_name.jpg"
cv2.imwrite(nameImg, nameField)
nameImg = img.split(".jpg")[0] + "_name.jpg"

# Sonuçları göstermek için
paper = image
topOfBubbles = nameRect.reshape(1, 8)[0][5]
paperTop = paper[:topOfBubbles, :]
paper = paper[topOfBubbles:, :]
thresh = thresh[topOfBubbles:, :]

cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
Expand Down Expand Up @@ -242,7 +239,6 @@ def getScores(img, ANSWER_KEY, UPLOAD_FOLDER):
center, radius = cv2.minEnclosingCircle(qCnt)
if radius < (meanRadius / 2) or radius > (meanRadius * 2):
questionCnts.remove(qCnt)
# powered by sametuluerr

correct = 0
wrong = 0
Expand Down Expand Up @@ -282,6 +278,7 @@ def getScores(img, ANSWER_KEY, UPLOAD_FOLDER):
color = redColor
if answer <= 4:
cv2.drawContours(paper, [cnts[answer]], -1, color, 3)
cv2.imwrite(os.getcwd()+"/"+str(qNum)+str(i)+"image.jpg", paper)

constant = 100.0 / len(ANSWER_KEY)
score = correct * constant
Expand All @@ -290,7 +287,9 @@ def getScores(img, ANSWER_KEY, UPLOAD_FOLDER):
cv2.putText(paper, scoreText, scoreLoc, font, 0.9, redColor, 2)

cv2.imwrite(img, paper)
img = "/static" + \
img = "static" + \
img.split('static')[1]
nameImg = "static" + \
nameImg.split('static')[1]

return ["{: .2f}".format(score), nameImg, img, correct, wrong, empty]
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
flask
imutils
numpy
numpy==1.19.3
opencv-python
scipy
flask_fontawesome
flask_fontawesome
wtforms
Loading

0 comments on commit c33af0d

Please sign in to comment.