fast-api-omr / application.py
mertbozkurt's picture
Add application file
3b213d5
raw
history blame contribute delete
No virus
7.98 kB
import cv2
import numpy as np
import functions
import json
#fotograf ozellikleri
heightImg = 300*4
widthImg = 210*4
#pathImage = "denemeler/100luk_numarali.jpg"
questions=25
choices=6
a1 = functions.read_answers("answers/test1-1.txt")
a2 = functions.answers2numbers(a1)
a3 = functions.read_answers("answers/test1-2.txt")
a4 = functions.answers2numbers(a3)
a5 = functions.read_answers("answers/test1-3.txt")
a6 = functions.answers2numbers(a5)
a7 = functions.read_answers("answers/test1-4.txt")
a8 = functions.answers2numbers(a7)
def optic1(ans_txt1,ans_txt2,ans_txt3,ans_txt4,pathImage, save_images= True):
#cevap anahtarini dosyadan okuma ve sayiya cevirme
ans_1 = ans_txt1
ans_2 = ans_txt2
ans_3 = ans_txt3
ans_4 = ans_txt4
#perspektif islemleri icin cozunurluk
wrap_h = 18*20
wrap_v = 18*20
#img = pathImage #eger girdi dogrudan np arrayse
#fotonun okunmasi ------------------------------------------------------------------------------------------------
img = cv2.imread(pathImage)
#img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
"""cv2.imshow("Image", img)
cv2.waitKey(0)"""
img = cv2.resize(img, (widthImg, heightImg)) # RESIZE IMAGE
imgBiggestContour = img.copy()
imgFinal = img.copy()
imgContours = img.copy()
imgBlank = np.zeros((heightImg,widthImg, 3), np.uint8)
#donusumler---------------------------------------------------------------------------------
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # CONVERT IMAGE TO GRAY SCALE
imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # ADD GAUSSIAN BLUR
imgCanny = cv2.Canny(imgBlur,10,70) # APPLY CANNY
#CONTOURS-------------------------------------------------------
contours, hierarchy = cv2.findContours(imgCanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS
#dortgen bulma--------------------------------------------------
rectCon = functions.rectContour(contours)
"""rectCon_np = rectCon[0].astype(np.uint8)
cv2.imshow("Image", rectCon_np)"""
biggestContour = functions.getCornerPoints(rectCon[0])
secondContour = functions.getCornerPoints(rectCon[1])
thirdContour = functions.getCornerPoints(rectCon[2])
#fourthContour = functions.getCornerPoints(rectCon[3])
#main
if biggestContour.size != 0 and secondContour.size != 0:
cv2.drawContours(imgBiggestContour, biggestContour,-1,(0,255,0),20)
cv2.drawContours(imgBiggestContour, secondContour,-1,(255,0,0),20) #sondk' kalinlik ortada renk
cv2.drawContours(imgBiggestContour, thirdContour,-1,(0,0,255),20) #sondk' kalinlik ortada renk
#cv2.drawContours(imgBiggestContour, fourthContour,-1,(0,0,20),20) #sondk' kalinlik ortada renk
biggestContour=functions.reorder(biggestContour)
#cevap siklari icin -************************************************************
pts1 = np.float32(biggestContour)
pts2 = np.float32([[0, 0],[wrap_v, 0], [0, wrap_h],[wrap_v, wrap_h]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
imgWarpColored_1 = cv2.warpPerspective(img, matrix, (wrap_v, wrap_h))
imgWarpGray_1 = cv2.cvtColor(imgWarpColored_1,cv2.COLOR_BGR2GRAY)
imgThresh_1 = cv2.threshold(imgWarpGray_1,0,255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#second buyuk icin perspektif
secondContour=functions.reorder(secondContour)
pts1_2 = np.float32(secondContour)
pts2_2 = np.float32([[0, 0],[wrap_v, 0], [0, wrap_h],[wrap_v, wrap_h]])
matrix_2 = cv2.getPerspectiveTransform(pts1_2, pts2_2)
imgWarpColored_2 = cv2.warpPerspective(img, matrix_2, (wrap_v, wrap_h))
imgWarpGray_2 = cv2.cvtColor(imgWarpColored_2,cv2.COLOR_BGR2GRAY)
#imgThresh_2 = cv2.threshold(imgWarpGray_2, 170, 255,cv2.THRESH_BINARY_INV )[1]
imgThresh_2 = cv2.threshold(imgWarpGray_2,0,255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#student id
bubbles = functions.split_num(imgThresh_2, 10, 10)
myPixelVal_2 = functions.pixelVal(10,10,bubbles)
myPixelVal_2 = functions.id_reorder(myPixelVal_2)
student_id = functions.id_answers(10,myPixelVal_2)
#print(student_id)
#soru kisimi
column_3 = functions.splitColumn(imgThresh_1)
boxes_1 = functions.splitBoxes(column_3[0])
boxes_2 = functions.splitBoxes(column_3[1])
boxes_3 = functions.splitBoxes(column_3[2])
boxes_4 = functions.splitBoxes(column_3[3])
#boxes_1
myPixelVal_1 = functions.pixelVal(questions,choices,boxes_1)
for sublist in myPixelVal_1:
if sublist.any(): # Alt liste boş değilse
sublist[0] = 0
myIndex_1 = functions.user_answers(questions,myPixelVal_1)
grading_1, wrong_ans_1, empty_1 = functions.grading(ans_1,questions,myIndex_1)
#boxes_2
myPixelVal_2 = functions.pixelVal(questions,choices,boxes_2)
myIndex_2 = functions.user_answers(questions,myPixelVal_2)
grading_2, wrong_ans_2, empty_2 = functions.grading(ans_2,questions,myIndex_2)
#boxes_3
myPixelVal_3 = functions.pixelVal(questions,choices,boxes_3)
myIndex_3 = functions.user_answers(questions,myPixelVal_3)
grading_3, wrong_ans_3 ,empty_3 = functions.grading(ans_3,questions,myIndex_3)
#boxes_3
myPixelVal_4 = functions.pixelVal(questions,choices,boxes_4)
myIndex_4 = functions.user_answers(questions,myPixelVal_4)
grading_4, wrong_ans_4 ,empty_4 = functions.grading(ans_4,questions,myIndex_4)
resim_listesi = [img,imgGray,imgBlur,imgCanny,imgContours,imgBiggestContour,imgThresh_1,imgThresh_2]
student_idFix = ""
for number in student_id:
student_idFix += str(number)
if save_images:
for i in range(0,len(resim_listesi)):
cv2.imwrite(f"images/{student_idFix}___{i}.jpg",resim_listesi[i])
grading = [grading_1,grading_2,grading_3,grading_4]
wrong_ans = [wrong_ans_1,wrong_ans_2,wrong_ans_3,wrong_ans_4]
empty = [empty_1,empty_2,empty_3,empty_4]
myIndexs = [myIndex_1,myIndex_2,myIndex_3,myIndex_4]
# Convert variables to NumPy arrays if they are not already
grading_1 = np.array(grading_1)
grading_2 = np.array(grading_2)
grading_3 = np.array(grading_3)
grading_4 = np.array(grading_4)
# Convert tolist() only if variables are NumPy arrays
grading = [grading_1.tolist(), grading_2.tolist(), grading_3.tolist(), grading_4.tolist()]
wrong_ans_1 = np.array(wrong_ans_1)
wrong_ans_2 = np.array(wrong_ans_2)
wrong_ans_3 = np.array(wrong_ans_3)
wrong_ans_4 = np.array(wrong_ans_4)
wrong_ans = [wrong_ans_1.tolist(), wrong_ans_2.tolist(), wrong_ans_3.tolist(), wrong_ans_4.tolist()]
empty_1 = np.array(empty_1)
empty_2 = np.array(empty_2)
empty_3 = np.array(empty_3)
empty_4 = np.array(empty_4)
empty = [empty_1.tolist(), empty_2.tolist(), empty_3.tolist(), empty_4.tolist()]
# Similarly, apply the same conversion for other lists like wrong_ans, empty, and myIndexs
# Verileri bir sözlükte topla
data = {"student_id": student_idFix,"grading": grading,"wrong_ans": wrong_ans, "empty": empty}
# JSON formatına dönüştür ve ekrana yazdır
json_data = json.dumps(data, indent=4)
json_data = json.loads(json_data)
print(json_data)
return json_data
"""json_data = optic1(ans_txt1=a2,
ans_txt2=a4,
ans_txt3=a6,
ans_txt4=a8,
pathImage="test/test1.1.PNG",
save_images=True
)"""
"""json_data = json.loads(json_data)
a = json_data["grading"]
print(a)"""