File size: 6,762 Bytes
c352470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473ad58
 
c352470
 
 
473ad58
b3fbbed
473ad58
 
 
b3fbbed
473ad58
 
c352470
 
 
712d61e
e6dc665
c352470
473ad58
c352470
 
 
 
 
473ad58
c352470
 
ab3f8d4
473ad58
 
 
 
 
 
ab3f8d4
473ad58
 
 
 
 
 
c352470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6dc665
c352470
 
 
 
 
 
 
e6dc665
c352470
 
e6dc665
 
eb68b77
 
e6dc665
473ad58
c352470
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import cv2
import numpy as np
import streamlit as st 

def rectContour(contours):
    rectCon = []
    max_area = 0
    for i in contours:
        area = cv2.contourArea(i) #alan hesabi
        #piksel alani 50den byukse gecerlidir
        if area > 30:
            peri = cv2.arcLength(i, True)
            approx = cv2.approxPolyDP(i, 0.02 * peri, True) #kac tane koseye sahip oldugu
            if len(approx) == 4: #4 ise dortgendir
                rectCon.append(i)
    rectCon = sorted(rectCon, key=cv2.contourArea,reverse=True) #alanlari hesaplicak ve siralicak ki ona gore alanlari belirleyelim
    #print(len(rectCon))
    return rectCon

def getCornerPoints(cont):
    peri = cv2.arcLength(cont, True)
    approx = cv2.approxPolyDP(cont, 0.02 * peri, True) #kose degerleri
    return approx


def reorder(myPoints):

    myPoints = myPoints.reshape((4, 2)) #fazla  koseliyi kaldiralim
    #print(myPoints)
    myPointsNew = np.zeros((4, 1, 2), np.int32) 
    add = myPoints.sum(1)
    #print(add)
    #print(np.argmax(add))
    myPointsNew[0] = myPoints[np.argmin(add)]  #[0,0]
    myPointsNew[3] =myPoints[np.argmax(add)]   #[w,h]
    diff = np.diff(myPoints, axis=1)
    myPointsNew[1] =myPoints[np.argmin(diff)]  #[w,0]
    myPointsNew[2] = myPoints[np.argmax(diff)] #[h,0]

    return myPointsNew

#siklari bolmek icin 20 tane soru vertical/ 5 tane isaret alani +1 tane soru sayisi yazan yer
#6 horizatanl bolmek
def splitBoxes(img):
    rows = np.vsplit(img,20) #vertical
    boxes=[]
    for r in rows:
        cols= np.hsplit(r,6) #horizantal
        for box in cols:
            boxes.append(box)
    return boxes

#ogrenci numarasi alani icin ayni fonksiyonu kullandik 
#yuakrdakisini silip sadece bu da kullanilabilir dogru degerler ile
#ogrenci numarasi alani 0-9 arasi sayilardan 10 tane isaretleme yeri iceriyor
#10x10seklinde boleriz
def split_num(img,vertical, horizantal):
    rows = np.vsplit(img,vertical) #vertical
    boxes=[]
    for r in rows:
        cols= np.hsplit(r,horizantal) #horizantal
        for box in cols:
            boxes.append(box)
    return boxes

#yan yana 3 tane birlesik ders alani oldugu icin onlari 3 ayri 
#sekle getiriyor
def splitColumn(img):
    column = np.hsplit(img,3)
    
    return column


#puan hesaplama alani
#soru sayisi dogru cevaplari ve ogrenci cevaplarini aliyor
#bunlari karsilastirip yeni bir listeye 1/0 seklinde kodluyor
#1ler toplanip puan hesaplanmis oluyor
def grading(answers,num_questions,myAnswers):
    grading=[]
    wrong_ans = []
    empty = []
    
    for x in range(0,num_questions):
        if answers[x] == myAnswers[x]:
            grading.append(1)
        
        elif myAnswers[x] == -1:
            grading.append(0)
            wrong_ans.append(x+1)
        
        elif myAnswers[x] == 0:
            empty.append(x+1)    
            
        else:
            grading.append(0)
            wrong_ans.append(x+1)

        
    score = (sum(grading)/num_questions)*100 
    return score ,wrong_ans,empty

#piksel degerlerinde kullanici cevaplarini 
#okuyupu index seklinde listeye kaydediyor
def user_answers(num_questions,myPixelVal):
    myIndex=[]
    
    for x in range (0,num_questions):
        arr = myPixelVal[x]
        t =150
        #empty answers
        if arr[1] < 150.0 and arr[2] < 150.0 and arr[3] < 150.0 and arr[4] < 150.0 and arr[5] < 150.0:
            
            myIndex.append(0)
        
        #2 or more answers
        elif (arr[1]>t and arr[2]>t) or ( arr[1]>t and arr[3]>t) or (arr[1]>t and arr[4]>t) or (arr[1]>t and arr[5]> t) or (arr[2]>t and arr[3]> t) or (arr[2]>t and arr[4]> t) or (arr[2]>t and arr[5]> t) or (arr[3]>t and arr[4]> t) or (arr[3]>t and arr[5]> t) or (arr[4]>t and arr[5]>t) :
            myIndex.append(-1)
            
        else :
            myIndexVal = np.where(arr == np.amax(arr))
            myIndex.append(myIndexVal[0][0])
    print(myIndex)
    return myIndex
    
#student id kismi yukardan asagiya dogru karsilastirma yaparak
#isretli alan tespit edilecegi icin satir ve sutunlari tekrar duzenlemiz gerekti
#[[1,2,3],[4,5,6],[7,8,9]] ----> [[1,4,7],[2,5,8],[3,6,9]]
def id_reorder(myPixelVal):
    duz_liste = []
    for sutun in range(len(myPixelVal[0])):
        for satir in range(len(myPixelVal)):
            duz_liste.append(myPixelVal[satir][sutun])
    yeni_liste = []
    satir = []
    for eleman in duz_liste:
        satir.append(eleman)
        if len(satir) == len(myPixelVal):
            yeni_liste.append(satir)
            satir = []
    return yeni_liste
                
#ogrenci numarasi kisminin piksel degerine gore hangisinin iseretli
#oldugunun tespiti    
def id_answers(vertical_num,myPixelVal):
    myIndex=[]
    for x in range (0,vertical_num):
        arr = myPixelVal[x]
        myIndexVal = np.where(arr == np.amax(arr))
        myIndex.append(myIndexVal[0][0])
    return myIndex

def pixelVal(num_questions,choices,box):
    countR=0 #rows
    countC=0 #column
    myPixelVal = np.zeros((num_questions,choices))
    for image in box:
        totalPixels = cv2.countNonZero(image)
        myPixelVal[countR][countC]= totalPixels
        countC += 1
        if (countC==choices):countC=0;countR +=1
    return myPixelVal

#dosyadan cevap anahtarinin okunmasi
def read_answers(dosya_adi):
    with open(dosya_adi, 'r') as f:
        satirlar = f.readlines()

    okunan_veriler = []
    for satir in satirlar:
        sutunlar = satir.split()
        okunan_veriler.append(sutunlar[1])
        
    return okunan_veriler


#dosyadan okunan cevaplarin numerik hale getirilmesi
def answers2numbers(answers):
    num_answers = []
    for i in answers:
        if i == "a":
            num_answers.append(1)
        elif i == "b":
            num_answers.append(2)
        elif i == "c":
            num_answers.append(3)
        elif i == "d":
            num_answers.append(4)
        elif i == "e":
            num_answers.append(5)
        else:
            print("Oppss Check Txt file")
    return num_answers

   
def image_show(images):
    col1, col2, col3 = st.columns(3)
    with col1:
            st.header("0")
            st.image(images[0],width=200)
    with col2:
            st.header("1")
            st.image(images[1],width=200)
    with col3:
            st.header("2")
            st.image(images[2],width=200)
            
    col4, col5= st.columns(2)
    
    with col4:
            st.header("3")
            st.image(images[4],width=200)
    with col5:
            st.header("4")
            st.image(images[5],width=200)
    col6, col7= st.columns(2)
    with col6:
            st.header("5")
            st.image(images[6],width=150)
    
    with col7:
            st.header("6")
            st.image(images[7],width=150)