Spaces:
Sleeping
Sleeping
| import cv2 | |
| import numpy as np | |
| def rectContour(contours): | |
| rectCon = [] | |
| 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): | |
| # Bölme işlemi yapmadan önce boyutları eşit olacak şekilde ayarlayın | |
| img = img[5:] | |
| if img.shape[0] % 25 != 0: | |
| img = img[:-(img.shape[0] % 25), :] | |
| rows = np.vsplit(img,25) #vertical | |
| boxes=[] | |
| for r in rows: | |
| cols= np.hsplit(r,6) #horizantal | |
| for box in cols: | |
| boxes.append(box) | |
| """cv2.imshow(" s",box) | |
| cv2.waitKey(0)""" | |
| 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,4) | |
| 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 =70.0 | |
| #empty answers | |
| if arr[1] < t and arr[2] < t and arr[3] < t and arr[4] < t and arr[5] < t: | |
| 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 | |