forked from Manthan47/Scribbler--Air-Canvas-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.py
94 lines (76 loc) · 3.17 KB
/
quiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cv2
import csv
from cvzone.HandTrackingModule import HandDetector
import cvzone
import time
def quiz_main():
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = HandDetector(detectionCon=0.8)
class MCQ():
def __init__(self, data):
self.question = data[0]
self.choice1 = data[1]
self.choice2 = data[2]
self.choice3 = data[3]
self.choice4 = data[4]
self.answer = int(data[5])
self.userAns = None
def update(self, cursor, bboxs):
for x, bbox in enumerate(bboxs):
x1, y1, x2, y2 = bbox
if x1 < cursor[0] < x2 and y1 < cursor[1] < y2:
self.userAns = x + 1
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), cv2.FILLED)
# Import csv file data
pathCSV = r"mcqs.csv"
with open(pathCSV, newline='\n') as f:
reader = csv.reader(f)
dataAll = list(reader)[1:]
# Create Object for each MCQ
mcqList = []
for q in dataAll:
mcqList.append(MCQ(q))
print("Total MCQ Objects Created:", len(mcqList))
qNo = 0
qTotal = len(dataAll)
while True:
success, img = cap.read()
img = cv2.flip(img, 1)
hands, img = detector.findHands(img, flipType=False)
if qNo < qTotal:
mcq = mcqList[qNo]
img, bbox = cvzone.putTextRect(img, mcq.question, [100, 100], 2, 2, offset=50, border=5)
img, bbox1 = cvzone.putTextRect(img, mcq.choice1, [100, 250], 2, 2, offset=50, border=5)
img, bbox2 = cvzone.putTextRect(img, mcq.choice2, [400, 250], 2, 2, offset=50, border=5)
img, bbox3 = cvzone.putTextRect(img, mcq.choice3, [100, 400], 2, 2, offset=50, border=5)
img, bbox4 = cvzone.putTextRect(img, mcq.choice4, [400, 400], 2, 2, offset=50, border=5)
if hands:
lmList = hands[0]['lmList']
cursor = lmList[8]
length, info, img= detector.findDistance(lmList[8], lmList[12], img)
print(length)
if length < 35:
mcq.update(cursor, [bbox1, bbox2, bbox3, bbox4])
if mcq.userAns is not None:
time.sleep(0.5)
qNo += 1
else:
score = 0
for mcq in mcqList:
if mcq.answer == mcq.userAns:
score += 1
score = round((score / qTotal) * 100, 2)
img, _ = cvzone.putTextRect(img, "Quiz Completed", [250, 300], 2, 2, offset=50, border=5)
img, _ = cvzone.putTextRect(img, f'Your Score: {score}%', [700, 300], 2, 2, offset=50, border=5)
# Draw Progress Bar
barValue = 150 + (950 // qTotal) * qNo
cv2.rectangle(img, (150, 600), (barValue, 650), (0, 255, 0), cv2.FILLED)
cv2.rectangle(img, (150, 600), (1100, 650), (255, 0, 255), 5)
img, _ = cvzone.putTextRect(img, f'{round((qNo / qTotal) * 100)}%', [1130, 635], 2, 2, offset=16)
cv2.imshow("Img", img)
cv2.waitKey(1)
key = cv2.waitKey(1)
if key == ord('q'):
break