import cv2
import numpy as np
from imutils import contours
from imutils.perspective import four_point_transform
import imutils
import cv2
import matplotlib.pyplot as plt
import numpy as np
from imutils import contours
from imutils.perspective import four_point_transform,order_points
import imutils

cevap_anahtar={0:2,1:1,2:2,3:3,4:1,5:4,6:4,7:3,8:1,9:1,10:0,11:0,12:2,13:1,14:2,15:3,16:4,17:4,18:4,19:3,20:2,21:1,22:0,23:0,24:0,25:4,26:2,27:3,28:4,29:4,30:4,31:3,32:2,33:1,34:0,35:0,36:1,37:2,38:3,39:4} #,
alfabe={0:'A',1:'B',2:'C',3:'Ç',4:'D',5:'E',6:'F',7:'G',8:'Ğ',9:'H',10:'I',11:'İ',12:'J',13:'K',14:'L',15:'M',16:'N',17:'O',18:'Ö',19:'P',20:'Q',21:'R',22:'S',23:'Ş',24:'T',25:'U',26:'Ü',27:'V',28:'W',29:'Y',30:'Z',31:'X'}
def cevap_islemleri(isim,coords):
    a=0

    thresh=cv2.threshold(isim,179,255,cv2.THRESH_BINARY_INV)[1]
    coords=contours.sort_contours(coords,method="top-to-bottom")[0]
    for (s,i) in enumerate(np.arange(0,len(coords),20)):
        cevap=None
        cnt=contours.sort_contours(coords[i:i+30])[0]
        toplam_beyaz=None

        for (j,c) in enumerate(cnt):
            maske=np.zeros(thresh.shape,dtype=np.uint8)

            cv2.drawContours(maske,[c],0,(255,255,255),thickness=-1)
            maske=cv2.bitwise_and(thresh,thresh,mask=maske)

            a+=1
            toplam_beyaz=cv2.countNonZero(maske)
            if cevap is None or toplam_beyaz>cevap[0]:

                cevap=(toplam_beyaz,j,s)



def cevap_contour_bul(isim,isim_gri):
    coord=[]
    thresholded=cv2.adaptiveThreshold(isim_gri,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,9,8)





    contour=cv2.findContours(thresholded,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)




    x_coords=[(0,0)]
    sayac=0
    contour=imutils.grab_contours(contour)
    contour=contours.sort_contours(contour,method="top-to-bottom")[0]
    for c in contour:
        approx=cv2.approxPolyDP(c,0.0001*cv2.arcLength(c,True),True)
        area=cv2.contourArea(approx)

        (x, y, w, h) = cv2.boundingRect(approx)
        ar = w / float(h)

        if  area<1500 and area>250 and ar>=0.9 and ar<=1.1:





            box=cv2.minAreaRect(approx)
            box=cv2.boxPoints(box)
            box=np.array(box,dtype=np.int)
            M=cv2.moments(box)

            x=int(M['m10']/M['m00'])
            y=int(M['m01']/M['m00'])

            res=tekrar_bul(x_coords,x)
            if res is False and abs(x_coords[-1][1]-y)<35:
                coord.append(approx)
                x_coords.append((x,y))
                sayac+=1
                #cv2.drawContours(isim,[box],0,(255,0,0),thickness=3)
                #cv2.drawContours(isim,[approx],0,(0,0,255),thickness=2)
            elif abs(x_coords[-1][1]-y)>=35:
                coord.append(approx)
                x_coords=[(0,0)]
                sayac+=1
                x_coords.append((x,y))
                #cv2.drawContours(isim,[box],0,(255,0,0),thickness=3)
                #cv2.drawContours(isim,[approx],0,(0,0,255),thickness=2)



            else:
                continue
    return coord



def ters_bul(kagit,areas):
    ret=False
    #print(areas[0][0])
    if areas[0][0]!=1 and areas[0][1]+areas[1][1]>2300000:
        kagit=imutils.rotate(kagit,angle=180)
        print("Kağıdı ters koymuşsunuz,çevrildi")
        ret=True
        return ret,kagit
    else:
        return ret,kagit




def kagit_bul(image,gray):
    thr=cv2.threshold(gray,150,255,cv2.THRESH_BINARY)[1]

    contour=cv2.findContours(thr,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    contour=imutils.grab_contours(contour)
    contour=sorted(contour,key=cv2.contourArea,reverse=True)
    for c in contour:
        approx=cv2.approxPolyDP(c,0.02*cv2.arcLength(c,True),True)
        if len(approx)==4:

            #cv2.drawContours(image,[approx],0,(0,255,0),thickness=3)
            break



    warp=four_point_transform(image,approx.reshape(4,2))
    warp_gri=four_point_transform(gray,approx.reshape(4,2))
    return warp,warp_gri
def soru_grup_contour_bul(resim,gri):
    thr2=cv2.threshold(gri,0,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)[1]

    can=cv2.Canny(thr2,50,100)
    can=cv2.dilate(can,None,iterations=3)


    coords=[]
    cont=cv2.findContours(can,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    cont=imutils.grab_contours(cont)
    for c in cont:
        approx=cv2.approxPolyDP(c,0.0001*cv2.arcLength(c,True),True)
        area=cv2.contourArea(approx)

        (x, y, w, h) = cv2.boundingRect(approx)
        ar = w / float(h)

        if cv2.contourArea(c)>30 and ar>=0.9 and ar<=1.1:
            box=cv2.minAreaRect(approx)
            box=cv2.boxPoints(box)
            box=np.array(box,dtype=np.int)
            if cv2.contourArea(box)>150:
                coords.append(approx)
                cv2.drawContours(resim,[box],0,(0,0,255),thickness=3)
    if len(coords)==5:
        return coords
    else:
        return 0
def tekrar_bul(array,koordinat):
    for c in array:
        if koordinat==c[0] or abs(koordinat-c[0])<15:
            return True #Tekrar var
        else:
            pass
    return False
def contour_bul(isim,isim_gri,karmasiklik=0):
    coord=[]
    thr6=cv2.adaptiveThreshold(isim_gri,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,9,8)
   #thr6=cv2.threshold(isim_gri,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
    ar_value=200
    #if karmasiklik==1:
    #    ar_value=800




    cont=cv2.findContours(thr6,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)




    x_coords=[(0,0)]
    sayac=0
    cont=imutils.grab_contours(cont)
    cont=contours.sort_contours(cont,method="top-to-bottom")[0]
    for c in cont:
        approx=cv2.approxPolyDP(c,0.0001*cv2.arcLength(c,True),True)
        area=cv2.contourArea(approx)

        (x, y, w, h) = cv2.boundingRect(approx)
        ar = w / float(h)

        if  area<1300 and area>300 and ar>=0.9 and ar<=1.1:





            box=cv2.minAreaRect(approx)
            box=cv2.boxPoints(box)
            box=np.array(box,dtype=np.int)
            M=cv2.moments(box)

            x=int(M['m10']/M['m00'])
            y=int(M['m01']/M['m00'])
         #   print(x,y)
            res=tekrar_bul(x_coords,x)
            if res is False and abs(x_coords[-1][1]-y)<35:
                coord.append(approx)
                x_coords.append((x,y))
                sayac+=1
                #cv2.drawContours(isim,[box],0,(255,0,0),thickness=3)
                #cv2.drawContours(isim,[approx],0,(0,0,255),thickness=2)
            elif abs(x_coords[-1][1]-y)>=35:
                coord.append(approx)
                x_coords=[(0,0)]
                sayac+=1
                x_coords.append((x,y))
                #cv2.drawContours(isim,[box],0,(255,0,0),thickness=3)
                #cv2.drawContours(isim,[approx],0,(0,0,255),thickness=2)



            else:
                continue
    return coord,thr6

def contour_cizdir(resim,cont,isim="default"):
    for c in cont:
        cv2.drawContours(resim,[c],0,(0,255,0),thickness=4)

    #print(f"Bulunan contour sayısı: {len(cont)}")


def bolge_bul(resim,gri):
    bolgeler={}
    thr2=cv2.adaptiveThreshold(gri,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,9,8)
    areas=[]
    cont=cv2.findContours(thr2,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    cont=imutils.grab_contours(cont)
    temp=[]
    cont=contours.sort_contours(cont,"top-to-bottom")[0]
    a=0
    for c in cont:
        approx=cv2.approxPolyDP(c,0.009*cv2.arcLength(c,True),True)
        if cv2.contourArea(approx)>10050 and len(approx)==4:

            a+=1

            M=cv2.moments(approx)
            x=int(M['m10']/M['m00'])
            y=int(M['m01']/M['m00'])
            #areas.append([a,cv2.contourArea(approx)])
            #cv2.putText(resim,"{}".format(a),(x,y),fontFace=cv2.FONT_HERSHEY_COMPLEX,fontScale=4,color=(255,0,0),thickness=3)
            temp.append(approx.reshape(4,2))
            areas.append([a,cv2.contourArea(approx)])

            #cv2.drawContours(resim,[approx],0,(255,0,0),thickness=3)

    #cv2.imshow("resim_olge",imutils.resize(resim,height=650))
    if len(temp)>=5:
        bolgeler={'isim':temp[0],'ogrno':temp[1],'sinav_turu':temp[2],'soru_grubu':temp[3],'ogretim_onay':temp[4],'cevaplar':temp[5]}
    areas=sorted(areas,key=lambda x:x[1],reverse=True)
    return bolgeler,areas
def cevap_islemleri(cevap,coords,col_no=1):
    iki_cevap=0
    bos=0
    dogru=0
    q_no=0
    yanlıs=0
    if col_no==1:
        pass
    elif col_no==2:
        q_no=30
    elif col_no==3:
        q_no=60
    elif col_no==4:
        q_no=90
    yanit=[]
    #cevap=cv2.cvtColor(cevap,cv2.COLOR_BGR2GRAY)
    thresh=cv2.threshold(cevap,180,255,cv2.THRESH_BINARY_INV)[1]

    coords=contours.sort_contours(coords,method="top-to-bottom")[0]
    for (s,i) in enumerate(np.arange(0,len(coords),5)):
        cevap=None
        cnt=contours.sort_contours(coords[i:i+5])[0]
        toplam_beyaz=None
        say=0

        for (j,c) in enumerate(cnt):
            if len(cevap_anahtar)<=q_no+s:
                return (dogru,yanlıs,bos,iki_cevap)

            maske=np.zeros(thresh.shape,dtype=np.uint8)

            cv2.drawContours(maske,[c],0,(255,255,255),thickness=-1)
            maske=cv2.bitwise_and(thresh,thresh,mask=maske)
            plt.imshow(maske,cmap='gray')
            #plt.show()

            toplam_beyaz=cv2.countNonZero(maske)
            #print(toplam_beyaz,j)
            if cevap is None or toplam_beyaz>cevap[0]:

                cevap=(toplam_beyaz,j,q_no+s)
            if toplam_beyaz>800:
                say+=1
        if say>1: #İKİ ŞIK İŞARETLEME DURUMU
            iki_cevap+=1
            continue
        elif cevap[0]<800:# BOŞ BIRAKMA DURUMU
            bos+=1
            continue
        else:
            if cevap_anahtar[q_no+s]== cevap[1]:
                #print(cevap_anahtar[q_no+s],cevap[1])

                dogru+=1
            else:
                yanlıs+=1

    '''
    NUMBER OF TRUE,FALSE,NOT MARKED AND MARKED MORE THAN 1
    '''
    return(dogru,yanlıs,bos,iki_cevap)



def isim_islemleri(isim,coords,thresh):
    a=0
    yanit=[]
    ad_str=""
    coords=contours.sort_contours(coords,method="left-to-right")[0]
    for (s,i) in enumerate(np.arange(0,len(coords),32)):
        cevap=None
        cnt=contours.sort_contours(coords[i:i+32],method="top-to-bottom")[0]
        toplam_beyaz=None

        for (j,c) in enumerate(cnt):
            maske=np.zeros(thresh.shape,dtype=np.uint8)

            cv2.drawContours(maske,[c],0,(255,255,255),thickness=-1)
            maske=cv2.bitwise_and(thresh,thresh,mask=maske)
            #plt.imshow(maske,cmap='gray')
            #plt.show()
            #a+=1
            toplam_beyaz=cv2.countNonZero(maske)
            #print(toplam_beyaz,j)
            if cevap is None or toplam_beyaz>cevap[0]:

                cevap=(toplam_beyaz,j,s)
           # print("cevap",cevap)
        if cevap[0]>500:
            yanit.append(alfabe[cevap[1]])

        elif cevap[0]<600:
            yanit.append(" ")
    for s in yanit:
        ad_str+=s

    return ad_str
def cevap_kolon(cevap):
    pts1=np.array([(2,50),(300,50),(2,1545),(300,1545)])
    pts2=np.array([(300,50),(600,50),(302,1545),(602,1545)])
    pts3=np.array([(600,50),(900,50),(602,1545),(902,1545)])
    pts4=np.array([(900,50),(1200,50),(902,1545),(1202,1545)])

    col1=four_point_transform(cevap,pts1)
    col2=four_point_transform(cevap,pts2)
    col3=four_point_transform(cevap,pts3)
    col4=four_point_transform(cevap,pts4)
    return col1,col2,col3,col4
def cevap_gri(col1,col2,col3,col4):
    '''
    KOLONLARI GRİ YAPMAK İÇİN,MAİNDE YER KAPLAMASIN

    '''
    col1_gri=cv2.cvtColor(col1,cv2.COLOR_BGR2GRAY)
    col2_gri=cv2.cvtColor(col2,cv2.COLOR_BGR2GRAY)
    col3_gri=cv2.cvtColor(col3,cv2.COLOR_BGR2GRAY)
    col4_gri=cv2.cvtColor(col4,cv2.COLOR_BGR2GRAY)
    return col1_gri,col2_gri,col3_gri,col4_gri
def cevap_contour(col1,col2,col3,col4):
    col1_gri,col2_gri,col3_gri,col4_gri=cevap_gri(col1,col2,col3,col4)
    col1_coord=cevap_contour_bul(col1,col1_gri)
    col2_coord=cevap_contour_bul(col2,col1_gri)
    col3_coord=cevap_contour_bul(col3,col1_gri)
    col4_coord=cevap_contour_bul(col4,col1_gri)
    return col1_coord,col2_coord,col3_coord,col4_coord

def ogrno_islemleri(ogrno,ogrno_gri,coords):

    yanit=""

    thresh=cv2.threshold(ogrno_gri,180,255,cv2.THRESH_BINARY_INV)[1]
    coords=contours.sort_contours(coords,method="left-to-right")[0]
    for (s,i) in enumerate(np.arange(0,len(coords),10)):
        cevap=None
        cnt=contours.sort_contours(coords[i:i+10],method="top-to-bottom")[0]
        toplam_beyaz=None

        for (j,c) in enumerate(cnt):
            maske=np.zeros(thresh.shape,dtype=np.uint8)

            cv2.drawContours(maske,[c],0,(255,255,255),thickness=-1)
            maske=cv2.bitwise_and(thresh,thresh,mask=maske)
            plt.imshow(maske,cmap='gray')
            #plt.show()

            toplam_beyaz=cv2.countNonZero(maske)

            if cevap is None or toplam_beyaz>cevap[0]:

                cevap=(toplam_beyaz,j,s)

        if cevap[0]>500:
            yanit+=str(cevap[1])
    print("Okul Numarası:",yanit)
def sinav_islemleri(sinav,sinav_gri,coords):
    yanit=["QUİZ","ARA","FİNAL","BÜTÜNLEME"]

    thresh=cv2.threshold(sinav_gri,180,255,cv2.THRESH_BINARY_INV)[1]
    coords=contours.sort_contours(coords,method="top-to-bottom")[0]
    for (s,i) in enumerate(np.arange(0,len(coords),10)):
        cevap=None
        cnt=contours.sort_contours(coords[i:i+10],method="left-to-right")[0]
        toplam_beyaz=None

        for (j,c) in enumerate(cnt):
            maske=np.zeros(thresh.shape,dtype=np.uint8)

            cv2.drawContours(maske,[c],0,(255,255,255),thickness=-1)
            maske=cv2.bitwise_and(thresh,thresh,mask=maske)
            plt.imshow(maske,cmap='gray')
            #plt.show()

            toplam_beyaz=cv2.countNonZero(maske)

            if cevap is None or toplam_beyaz>cevap[0]:

                cevap=(toplam_beyaz,j,s)
        return yanit[cevap[1]]
def sorugrup_islemleri(soru,soru_gri,coords):

    yanit=["A","B","C","D","E"]
    sayac=0
    thresh=cv2.threshold(soru_gri,180,255,cv2.THRESH_BINARY_INV)[1]
    coords=contours.sort_contours(coords,method="top-to-bottom")[0]
    for (s,i) in enumerate(np.arange(0,len(coords),10)):
        cevap=None
        cnt=contours.sort_contours(coords[i:i+10],method="left-to-right")[0]
        toplam_beyaz=None

        for (j,c) in enumerate(cnt):
            maske=np.zeros(thresh.shape,dtype=np.uint8)

            cv2.drawContours(maske,[c],0,(255,255,255),thickness=-1)
            maske=cv2.bitwise_and(thresh,thresh,mask=maske)
            plt.imshow(maske,cmap='gray')
            #plt.show()
            sayac+=1
            toplam_beyaz=cv2.countNonZero(maske)

            if cevap is None or toplam_beyaz>cevap[0]:

                cevap=(toplam_beyaz,j,s)
        if sayac==5:
            break
        print(cevap)
    if cevap[0]>500:
        return yanit[cevap[1]]
    #print("tespit edilemedi")
    return "Tespit edilemedi"



####################################################################
def main_starter(bos_kagit,dolu_kagit):
    image=cv2.imread(bos_kagit)
    gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)


    kagit,kagit_gri=kagit_bul(image,gray)

    bolgeler,areas=bolge_bul(kagit,kagit_gri)

    '''
    FIND SCHOOL NUMBER PART
    '''

    ogrno_bos=four_point_transform(kagit,bolgeler['ogrno'])
    ogrno_bos_gri=four_point_transform(kagit_gri,bolgeler['ogrno'])
    ogrno_coord,ogrno_thresh=contour_bul(ogrno_bos,ogrno_bos_gri)
    contour_cizdir(ogrno_bos_gri,ogrno_coord,"ogrenci numarası")
    #v2.imshow("ogrno",imutils.resize(ogrno_bos,height=400))



    '''
    DIVIDE ANSWER PART INTO 4 SLICES AND FIND ONE BY ONE
    '''


    cevap_bos=four_point_transform(kagit,bolgeler['cevaplar'])
    cevap_bos_gri=four_point_transform(kagit_gri,bolgeler['cevaplar'])

    col1,col2,col3,col4=cevap_kolon(cevap_bos)
    col1_gri,col2_gri,col3_gri,col4_gri=cevap_gri(col1,col2,col3,col4)

    col1_coord,col2_coord,col3_coord,col4_coord=cevap_contour(col1,col2,col3,col4)
    #contour_cizdir(col1,col1_coord)
    #cevap_islemleri(col2_gri,coord_cevap)

    '''
    EXAM TYPE FIND PART
    '''

    sinav_bos=four_point_transform(kagit,bolgeler['sinav_turu'])
    sinav_bos_gri=four_point_transform(kagit_gri,bolgeler['sinav_turu'])
    sinav_coord,sinav_thresh=contour_bul(sinav_bos,sinav_bos_gri)

    sinav_islemleri(sinav_bos,sinav_bos_gri,sinav_coord)
    #cv2.imshow("sınav türü",sinav_bos_gri)




    '''
    
    OTHER PARTS THAT ON PAPER 
    '''

    sorugrup_bos=four_point_transform(kagit,bolgeler['soru_grubu'])
    sorugrup_bos_gri=four_point_transform(kagit_gri,bolgeler['soru_grubu'])

    sorugrup_coord,sorugrup_thresh=contour_bul(sorugrup_bos,sorugrup_bos_gri,1)
    coors=soru_grup_contour_bul(sorugrup_bos,sorugrup_bos_gri)
    soru_cont,soru_thr=contour_bul(sorugrup_bos,sorugrup_bos_gri,1)



    ###############################

    ogretim_bos=four_point_transform(kagit,bolgeler['ogretim_onay'])
    ogretim_bos_gri=four_point_transform(kagit_gri,bolgeler['ogretim_onay'])
    ogret_cont,ogret_thr=contour_bul(ogretim_bos,ogretim_bos_gri,1)
















    '''
    
    NAME FIND PART.
    '''

    isim_bos=four_point_transform(kagit,bolgeler['isim'])
    isim_bos_gri=cv2.cvtColor(isim_bos,cv2.COLOR_BGR2GRAY)
    coord_isim, thres=contour_bul(isim_bos, isim_bos_gri)
    #contour_cizdir(isim_bos,coord,"isim_bos")
    #cevap_islemleri(cevap_bos_gri,coord)











    ##############################################

    resim=cv2.imread(dolu_kagit)
    resim_gri=cv2.cvtColor(resim,cv2.COLOR_BGR2GRAY)
    warp2,warp2_gri=kagit_bul(resim,resim_gri)


    bolgeler2,areas2=bolge_bul(warp2,warp2_gri)

    ret,warp2=ters_bul(warp2,areas2)



    '''
    TERS İSE TEKRAR BOLGELERİ BUL
    '''
    if ret==True:
        warp2_gri=cv2.cvtColor(warp2,cv2.COLOR_BGR2GRAY)
        bolgeler2,areas2=bolge_bul(warp2,warp2_gri)


    else:
        pass

    isim_dolu=four_point_transform(warp2,bolgeler2['isim'])
    isim_dolu_gri=cv2.cvtColor(isim_dolu,cv2.COLOR_BGR2GRAY)

    contour_cizdir(isim_dolu,coord_isim,"dolu_kagit_contourlu")
    '''
    OGRETİM ONAY DOLU KAGIT
    '''


    ogretim_dolu=four_point_transform(warp2,bolgeler2['ogretim_onay'])
    ogretim_dolu_gri=cv2.cvtColor(ogretim_dolu,cv2.COLOR_BGR2GRAY)
    ogret_onay=sorugrup_islemleri(ogretim_dolu,ogretim_dolu_gri,ogret_cont)
    print("Öğretim Onayı:",ogret_onay)

    #cv2.drawContours(ogretim_dolu,ogret_cont,-1,(255,0,0),thickness=3)
    #cv2.imshow("ogretc",ogretim_dolu)

    #ogretim_onayı=sorugrup_islemleri(ogretim_dolu,ogretim_dolu_gri,ogretimonay_coord)



    sorugrup_dolu=four_point_transform(warp2,bolgeler2['soru_grubu'])
    sorugrup_dolu_gri=cv2.cvtColor(sorugrup_dolu,cv2.COLOR_BGR2GRAY)
    soru_tur=sorugrup_islemleri(sorugrup_dolu,sorugrup_dolu_gri,soru_cont)
    print("Soru Grubu",soru_tur)






    thresh_dolu=cv2.threshold(isim_dolu_gri,0,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)[1]
    isim_str=isim_islemleri(isim_dolu_gri,coord_isim,thresh_dolu)
    print(isim_str)


    sinav_dolu=four_point_transform(warp2,bolgeler2['sinav_turu'])
    sinav_dolu_gri=cv2.cvtColor(sinav_dolu,cv2.COLOR_BGR2GRAY)
    sinav_turu=sinav_islemleri(sinav_dolu,sinav_dolu_gri,sinav_coord)
    print("Sınav Türü: ",sinav_turu)



    ogrno_dolu=four_point_transform(warp2,bolgeler2['ogrno'])
    ogrno_dolu_gri=cv2.cvtColor(ogrno_dolu,cv2.COLOR_BGR2GRAY)

    ogrno_islemleri(ogrno_dolu,ogrno_dolu_gri,ogrno_coord)

    cevap_dolu=four_point_transform(warp2,bolgeler2['cevaplar'])
    cevap_dolu_gri=cv2.cvtColor(cevap_dolu,cv2.COLOR_BGR2GRAY)
    col1_dolu,col2_dolu,col3_dolu,col4_dolu=cevap_kolon(cevap_dolu)

    col1_gri_dolu,col2_gri_dolu,col3_gri_dolu,col4_gri_dolu=cevap_gri(col1_dolu,col2_dolu,col3_dolu,col4_dolu)

    #contour_cizdir(col1_dolu,col1_coord,"colon1 dolu")
    if len(cevap_anahtar)<=30:

        basarim=cevap_islemleri(col1_gri_dolu,col1_coord,1)

    elif len(cevap_anahtar)<=60:
        basarim1=cevap_islemleri(col1_gri_dolu,col1_coord,1)
        basarim2=cevap_islemleri(col2_gri_dolu,col2_coord,2)
        basarim=(basarim1[0]+basarim2[0],basarim1[1]+basarim2[1],basarim1[2]+basarim2[2],basarim1[3]+basarim2[3])
        #print(basarim)
    elif len(cevap_anahtar)<=90:
        basarim1=cevap_islemleri(col1_gri_dolu,col1_coord,1)
        basarim2=cevap_islemleri(col2_gri_dolu,col2_coord,2)
        basarim3=cevap_islemleri(col3_gri_dolu,col3_coord,3)
        basarim=basarim1+basarim2+basarim3
    elif len(cevap_anahtar)<=120:
        basarim1=cevap_islemleri(col1_gri_dolu,col1_coord,1)
        basarim2=cevap_islemleri(col2_gri_dolu,col2_coord,2)
        basarim3=cevap_islemleri(col3_gri_dolu,col3_coord,3)
        basarim4=cevap_islemleri(col4_gri_dolu,col4_coord,4)
        basarim=basarim1+basarim2+basarim3+basarim4








    print(f"Doğru cevap sayısı:{basarim[0]}\nYanlış cevap sayısı:{basarim[1]}\nBoş sayısı:{basarim[2]}\nİki cevap işaret:{basarim[3]}")

    cv2.waitKey()
    cv2.destroyAllWindows()




if __name__ == '__main__':
    bos_kagit="optic_empty.jpg"
    dolu_kagit="optic_marked.jpg"
    main_starter(bos_kagit,dolu_kagit)
    

[((41, 16, 41, 104), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', ({(41, 38, 41, 46): 'isim_gri', (41, 47, 41, 50): '255', (41, 51, 41, 77): 'cv2.ADAPTIVE_THRESH_MEAN_C', (41, 78, 41, 99): 'cv2.THRESH_BINARY_INV', (41, 100, 41, 101): '9', (41, 102, 41, 103): '8'}, {}), '(isim_gri, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.\n    THRESH_BINARY_INV, 9, 8)', False, 'import cv2\n'), ((47, 12, 47, 77), 'cv2.findContours', 'cv2.findContours', ({(47, 29, 47, 40): 'thresholded', (47, 41, 47, 54): 'cv2.RETR_LIST', (47, 55, 47, 76): 'cv2.CHAIN_APPROX_NONE'}, {}), '(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)', False, 'import cv2\n'), ((54, 12, 54, 42), 'imutils.grab_contours', 'imutils.grab_contours', ({(54, 34, 54, 41): 'contour'}, {}), '(contour)', False, 'import imutils\n'), ((117, 12, 117, 73), 'cv2.findContours', 'cv2.findContours', ({(117, 29, 117, 32): 'thr', (117, 33, 117, 50): 'cv2.RETR_EXTERNAL', (117, 51, 117, 72): 'cv2.CHAIN_APPROX_NONE'}, {}), '(thr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)', False, 'import cv2\n'), ((118, 12, 118, 42), 'imutils.grab_contours', 'imutils.grab_contours', ({(118, 34, 118, 41): 'contour'}, {}), '(contour)', False, 'import imutils\n'), ((135, 8, 135, 30), 'cv2.Canny', 'cv2.Canny', ({(135, 18, 135, 22): 'thr2', (135, 23, 135, 25): '50', (135, 26, 135, 29): '100'}, {}), '(thr2, 50, 100)', False, 'import cv2\n'), ((136, 8, 136, 41), 'cv2.dilate', 'cv2.dilate', (), '', False, 'import cv2\n'), ((140, 9, 140, 70), 'cv2.findContours', 'cv2.findContours', ({(140, 26, 140, 29): 'can', (140, 30, 140, 47): 'cv2.RETR_EXTERNAL', (140, 48, 140, 69): 'cv2.CHAIN_APPROX_NONE'}, {}), '(can, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)', False, 'import cv2\n'), ((141, 9, 141, 36), 'imutils.grab_contours', 'imutils.grab_contours', ({(141, 31, 141, 35): 'cont'}, {}), '(cont)', False, 'import imutils\n'), ((169, 9, 169, 97), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', ({(169, 31, 169, 39): 'isim_gri', (169, 40, 169, 43): '255', (169, 44, 169, 70): 'cv2.ADAPTIVE_THRESH_MEAN_C', (169, 71, 169, 92): 'cv2.THRESH_BINARY_INV', (169, 93, 169, 94): '9', (169, 95, 169, 96): '8'}, {}), '(isim_gri, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.\n    THRESH_BINARY_INV, 9, 8)', False, 'import cv2\n'), ((178, 9, 178, 67), 'cv2.findContours', 'cv2.findContours', ({(178, 26, 178, 30): 'thr6', (178, 31, 178, 44): 'cv2.RETR_LIST', (178, 45, 178, 66): 'cv2.CHAIN_APPROX_NONE'}, {}), '(thr6, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)', False, 'import cv2\n'), ((185, 9, 185, 36), 'imutils.grab_contours', 'imutils.grab_contours', ({(185, 31, 185, 35): 'cont'}, {}), '(cont)', False, 'import imutils\n'), ((238, 9, 238, 92), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', ({(238, 31, 238, 34): 'gri', (238, 35, 238, 38): '255', (238, 39, 238, 65): 'cv2.ADAPTIVE_THRESH_MEAN_C', (238, 66, 238, 87): 'cv2.THRESH_BINARY_INV', (238, 88, 238, 89): '9', (238, 90, 238, 91): '8'}, {}), '(gri, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.\n    THRESH_BINARY_INV, 9, 8)', False, 'import cv2\n'), ((240, 9, 240, 71), 'cv2.findContours', 'cv2.findContours', ({(240, 26, 240, 30): 'thr2', (240, 31, 240, 48): 'cv2.RETR_EXTERNAL', (240, 49, 240, 70): 'cv2.CHAIN_APPROX_NONE'}, {}), '(thr2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)', False, 'import cv2\n'), ((241, 9, 241, 36), 'imutils.grab_contours', 'imutils.grab_contours', ({(241, 31, 241, 35): 'cont'}, {}), '(cont)', False, 'import imutils\n'), ((364, 9, 364, 56), 'numpy.array', 'np.array', ({(364, 18, 364, 55): '[(2, 50), (300, 50), (2, 1545), (300, 1545)]'}, {}), '([(2, 50), (300, 50), (2, 1545), (300, 1545)])', True, 'import numpy as np\n'), ((365, 9, 365, 60), 'numpy.array', 'np.array', ({(365, 18, 365, 59): '[(300, 50), (600, 50), (302, 1545), (602, 1545)]'}, {}), '([(300, 50), (600, 50), (302, 1545), (602, 1545)])', True, 'import numpy as np\n'), ((366, 9, 366, 60), 'numpy.array', 'np.array', ({(366, 18, 366, 59): '[(600, 50), (900, 50), (602, 1545), (902, 1545)]'}, {}), '([(600, 50), (900, 50), (602, 1545), (902, 1545)])', True, 'import numpy as np\n'), ((367, 9, 367, 62), 'numpy.array', 'np.array', ({(367, 18, 367, 61): '[(900, 50), (1200, 50), (902, 1545), (1202, 1545)]'}, {}), '([(900, 50), (1200, 50), (902, 1545), (1202, 1545)])', True, 'import numpy as np\n'), ((369, 9, 369, 41), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(369, 30, 369, 35): 'cevap', (369, 36, 369, 40): 'pts1'}, {}), '(cevap, pts1)', False, 'from imutils.perspective import four_point_transform, order_points\n'), ((370, 9, 370, 41), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(370, 30, 370, 35): 'cevap', (370, 36, 370, 40): 'pts2'}, {}), '(cevap, pts2)', False, 'from imutils.perspective import four_point_transform, order_points\n'), ((371, 9, 371, 41), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(371, 30, 371, 35): 'cevap', (371, 36, 371, 40): 'pts3'}, {}), '(cevap, pts3)', False, 'from imutils.perspective import four_point_transform, order_points\n'), ((372, 9, 372, 41), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(372, 30, 372, 35): 'cevap', (372, 36, 372, 40): 'pts4'}, {}), '(cevap, pts4)', False, 'from imutils.perspective import four_point_transform, order_points\n'), ((379, 13, 379, 50), 'cv2.cvtColor', 'cv2.cvtColor', ({(379, 26, 379, 30): 'col1', (379, 31, 379, 49): 'cv2.COLOR_BGR2GRAY'}, {}), '(col1, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((380, 13, 380, 50), 'cv2.cvtColor', 'cv2.cvtColor', ({(380, 26, 380, 30): 'col2', (380, 31, 380, 49): 'cv2.COLOR_BGR2GRAY'}, {}), '(col2, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((381, 13, 381, 50), 'cv2.cvtColor', 'cv2.cvtColor', ({(381, 26, 381, 30): 'col3', (381, 31, 381, 49): 'cv2.COLOR_BGR2GRAY'}, {}), '(col3, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((382, 13, 382, 50), 'cv2.cvtColor', 'cv2.cvtColor', ({(382, 26, 382, 30): 'col4', (382, 31, 382, 49): 'cv2.COLOR_BGR2GRAY'}, {}), '(col4, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((480, 10, 480, 31), 'cv2.imread', 'cv2.imread', ({(480, 21, 480, 30): 'bos_kagit'}, {}), '(bos_kagit)', False, 'import cv2\n'), ((481, 9, 481, 47), 'cv2.cvtColor', 'cv2.cvtColor', ({(481, 22, 481, 27): 'image', (481, 28, 481, 46): 'cv2.COLOR_BGR2GRAY'}, {}), '(image, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((492, 14, 492, 59), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(492, 35, 492, 40): 'kagit', (492, 41, 492, 58): "bolgeler['ogrno']"}, {}), "(kagit, bolgeler['ogrno'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((493, 18, 493, 67), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(493, 39, 493, 48): 'kagit_gri', (493, 49, 493, 66): "bolgeler['ogrno']"}, {}), "(kagit_gri, bolgeler['ogrno'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((505, 14, 505, 62), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(505, 35, 505, 40): 'kagit', (505, 41, 505, 61): "bolgeler['cevaplar']"}, {}), "(kagit, bolgeler['cevaplar'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((506, 18, 506, 70), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(506, 39, 506, 48): 'kagit_gri', (506, 49, 506, 69): "bolgeler['cevaplar']"}, {}), "(kagit_gri, bolgeler['cevaplar'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((519, 14, 519, 64), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(519, 35, 519, 40): 'kagit', (519, 41, 519, 63): "bolgeler['sinav_turu']"}, {}), "(kagit, bolgeler['sinav_turu'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((520, 18, 520, 72), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(520, 39, 520, 48): 'kagit_gri', (520, 49, 520, 71): "bolgeler['sinav_turu']"}, {}), "(kagit_gri, bolgeler['sinav_turu'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((534, 17, 534, 67), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(534, 38, 534, 43): 'kagit', (534, 44, 534, 66): "bolgeler['soru_grubu']"}, {}), "(kagit, bolgeler['soru_grubu'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((535, 21, 535, 75), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(535, 42, 535, 51): 'kagit_gri', (535, 52, 535, 74): "bolgeler['soru_grubu']"}, {}), "(kagit_gri, bolgeler['soru_grubu'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((545, 16, 545, 68), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(545, 37, 545, 42): 'kagit', (545, 43, 545, 67): "bolgeler['ogretim_onay']"}, {}), "(kagit, bolgeler['ogretim_onay'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((546, 20, 546, 76), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(546, 41, 546, 50): 'kagit_gri', (546, 51, 546, 75): "bolgeler['ogretim_onay']"}, {}), "(kagit_gri, bolgeler['ogretim_onay'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((569, 13, 569, 57), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(569, 34, 569, 39): 'kagit', (569, 40, 569, 56): "bolgeler['isim']"}, {}), "(kagit, bolgeler['isim'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((570, 17, 570, 58), 'cv2.cvtColor', 'cv2.cvtColor', ({(570, 30, 570, 38): 'isim_bos', (570, 39, 570, 57): 'cv2.COLOR_BGR2GRAY'}, {}), '(isim_bos, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((587, 10, 587, 32), 'cv2.imread', 'cv2.imread', ({(587, 21, 587, 31): 'dolu_kagit'}, {}), '(dolu_kagit)', False, 'import cv2\n'), ((588, 14, 588, 52), 'cv2.cvtColor', 'cv2.cvtColor', ({(588, 27, 588, 32): 'resim', (588, 33, 588, 51): 'cv2.COLOR_BGR2GRAY'}, {}), '(resim, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((609, 14, 609, 59), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(609, 35, 609, 40): 'warp2', (609, 41, 609, 58): "bolgeler2['isim']"}, {}), "(warp2, bolgeler2['isim'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((610, 18, 610, 60), 'cv2.cvtColor', 'cv2.cvtColor', ({(610, 31, 610, 40): 'isim_dolu', (610, 41, 610, 59): 'cv2.COLOR_BGR2GRAY'}, {}), '(isim_dolu, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((618, 17, 618, 70), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(618, 38, 618, 43): 'warp2', (618, 44, 618, 69): "bolgeler2['ogretim_onay']"}, {}), "(warp2, bolgeler2['ogretim_onay'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((619, 21, 619, 66), 'cv2.cvtColor', 'cv2.cvtColor', ({(619, 34, 619, 46): 'ogretim_dolu', (619, 47, 619, 65): 'cv2.COLOR_BGR2GRAY'}, {}), '(ogretim_dolu, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((630, 18, 630, 69), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(630, 39, 630, 44): 'warp2', (630, 45, 630, 68): "bolgeler2['soru_grubu']"}, {}), "(warp2, bolgeler2['soru_grubu'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((631, 22, 631, 68), 'cv2.cvtColor', 'cv2.cvtColor', ({(631, 35, 631, 48): 'sorugrup_dolu', (631, 49, 631, 67): 'cv2.COLOR_BGR2GRAY'}, {}), '(sorugrup_dolu, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((645, 15, 645, 66), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(645, 36, 645, 41): 'warp2', (645, 42, 645, 65): "bolgeler2['sinav_turu']"}, {}), "(warp2, bolgeler2['sinav_turu'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((646, 19, 646, 62), 'cv2.cvtColor', 'cv2.cvtColor', ({(646, 32, 646, 42): 'sinav_dolu', (646, 43, 646, 61): 'cv2.COLOR_BGR2GRAY'}, {}), '(sinav_dolu, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((652, 15, 652, 61), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(652, 36, 652, 41): 'warp2', (652, 42, 652, 60): "bolgeler2['ogrno']"}, {}), "(warp2, bolgeler2['ogrno'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((653, 19, 653, 62), 'cv2.cvtColor', 'cv2.cvtColor', ({(653, 32, 653, 42): 'ogrno_dolu', (653, 43, 653, 61): 'cv2.COLOR_BGR2GRAY'}, {}), '(ogrno_dolu, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((657, 15, 657, 64), 'imutils.perspective.four_point_transform', 'four_point_transform', ({(657, 36, 657, 41): 'warp2', (657, 42, 657, 63): "bolgeler2['cevaplar']"}, {}), "(warp2, bolgeler2['cevaplar'])", False, 'from imutils.perspective import four_point_transform, order_points\n'), ((658, 19, 658, 62), 'cv2.cvtColor', 'cv2.cvtColor', ({(658, 32, 658, 42): 'cevap_dolu', (658, 43, 658, 61): 'cv2.COLOR_BGR2GRAY'}, {}), '(cevap_dolu, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((694, 4, 694, 17), 'cv2.waitKey', 'cv2.waitKey', ({}, {}), '()', False, 'import cv2\n'), ((695, 4, 695, 27), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ({}, {}), '()', False, 'import cv2\n'), ((18, 11, 18, 60), 'cv2.threshold', 'cv2.threshold', ({(18, 25, 18, 29): 'isim', (18, 30, 18, 33): '(179)', (18, 34, 18, 37): '(255)', (18, 38, 18, 59): 'cv2.THRESH_BINARY_INV'}, {}), '(isim, 179, 255, cv2.THRESH_BINARY_INV)', False, 'import cv2\n'), ((19, 11, 19, 64), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((55, 12, 55, 66), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((58, 13, 58, 36), 'cv2.contourArea', 'cv2.contourArea', ({(58, 29, 58, 35): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((60, 23, 60, 47), 'cv2.boundingRect', 'cv2.boundingRect', ({(60, 40, 60, 46): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((104, 14, 104, 45), 'imutils.rotate', 'imutils.rotate', (), '', False, 'import imutils\n'), ((115, 8, 115, 53), 'cv2.threshold', 'cv2.threshold', ({(115, 22, 115, 26): 'gray', (115, 27, 115, 30): '(150)', (115, 31, 115, 34): '(255)', (115, 35, 115, 52): 'cv2.THRESH_BINARY'}, {}), '(gray, 150, 255, cv2.THRESH_BINARY)', False, 'import cv2\n'), ((133, 9, 133, 71), 'cv2.threshold', 'cv2.threshold', ({(133, 23, 133, 26): 'gri', (133, 27, 133, 28): '(0)', (133, 29, 133, 32): '(255)', (133, 33, 133, 70): '(cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)'}, {}), '(gri, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)', False, 'import cv2\n'), ((144, 13, 144, 36), 'cv2.contourArea', 'cv2.contourArea', ({(144, 29, 144, 35): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((146, 23, 146, 47), 'cv2.boundingRect', 'cv2.boundingRect', ({(146, 40, 146, 46): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((186, 9, 186, 60), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((189, 13, 189, 36), 'cv2.contourArea', 'cv2.contourArea', ({(189, 29, 189, 35): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((191, 23, 191, 47), 'cv2.boundingRect', 'cv2.boundingRect', ({(191, 40, 191, 46): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((231, 8, 231, 59), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((243, 9, 243, 53), 'imutils.contours.sort_contours', 'contours.sort_contours', ({(243, 32, 243, 36): 'cont', (243, 37, 243, 52): '"""top-to-bottom"""'}, {}), "(cont, 'top-to-bottom')", False, 'from imutils import contours\n'), ((282, 11, 282, 61), 'cv2.threshold', 'cv2.threshold', ({(282, 25, 282, 30): 'cevap', (282, 31, 282, 34): '(180)', (282, 35, 282, 38): '(255)', (282, 39, 282, 60): 'cv2.THRESH_BINARY_INV'}, {}), '(cevap, 180, 255, cv2.THRESH_BINARY_INV)', False, 'import cv2\n'), ((284, 11, 284, 64), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((334, 11, 334, 64), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((396, 11, 396, 65), 'cv2.threshold', 'cv2.threshold', ({(396, 25, 396, 34): 'ogrno_gri', (396, 35, 396, 38): '(180)', (396, 39, 396, 42): '(255)', (396, 43, 396, 64): 'cv2.THRESH_BINARY_INV'}, {}), '(ogrno_gri, 180, 255, cv2.THRESH_BINARY_INV)', False, 'import cv2\n'), ((397, 11, 397, 64), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((423, 11, 423, 65), 'cv2.threshold', 'cv2.threshold', ({(423, 25, 423, 34): 'sinav_gri', (423, 35, 423, 38): '(180)', (423, 39, 423, 42): '(255)', (423, 43, 423, 64): 'cv2.THRESH_BINARY_INV'}, {}), '(sinav_gri, 180, 255, cv2.THRESH_BINARY_INV)', False, 'import cv2\n'), ((424, 11, 424, 64), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((448, 11, 448, 64), 'cv2.threshold', 'cv2.threshold', ({(448, 25, 448, 33): 'soru_gri', (448, 34, 448, 37): '(180)', (448, 38, 448, 41): '(255)', (448, 42, 448, 63): 'cv2.THRESH_BINARY_INV'}, {}), '(soru_gri, 180, 255, cv2.THRESH_BINARY_INV)', False, 'import cv2\n'), ((449, 11, 449, 64), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((602, 18, 602, 56), 'cv2.cvtColor', 'cv2.cvtColor', ({(602, 31, 602, 36): 'warp2', (602, 37, 602, 55): 'cv2.COLOR_BGR2GRAY'}, {}), '(warp2, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((640, 16, 640, 88), 'cv2.threshold', 'cv2.threshold', ({(640, 30, 640, 43): 'isim_dolu_gri', (640, 44, 640, 45): '(0)', (640, 46, 640, 49): '(255)', (640, 50, 640, 87): '(cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)'}, {}), '(isim_dolu_gri, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)', False, 'import cv2\n'), ((22, 12, 22, 50), 'imutils.contours.sort_contours', 'contours.sort_contours', ({(22, 35, 22, 49): 'coords[i:i + 30]'}, {}), '(coords[i:i + 30])', False, 'from imutils import contours\n'), ((26, 18, 26, 55), 'numpy.zeros', 'np.zeros', (), '', True, 'import numpy as np\n'), ((28, 12, 28, 68), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((29, 18, 29, 59), 'cv2.bitwise_and', 'cv2.bitwise_and', (), '', False, 'import cv2\n'), ((32, 25, 32, 48), 'cv2.countNonZero', 'cv2.countNonZero', ({(32, 42, 32, 47): 'maske'}, {}), '(maske)', False, 'import cv2\n'), ((69, 16, 69, 39), 'cv2.minAreaRect', 'cv2.minAreaRect', ({(69, 32, 69, 38): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((70, 16, 70, 34), 'cv2.boxPoints', 'cv2.boxPoints', ({(70, 30, 70, 33): 'box'}, {}), '(box)', False, 'import cv2\n'), ((71, 16, 71, 42), 'numpy.array', 'np.array', (), '', True, 'import numpy as np\n'), ((72, 14, 72, 30), 'cv2.moments', 'cv2.moments', ({(72, 26, 72, 29): 'box'}, {}), '(box)', False, 'import cv2\n'), ((150, 16, 150, 39), 'cv2.minAreaRect', 'cv2.minAreaRect', ({(150, 32, 150, 38): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((151, 16, 151, 34), 'cv2.boxPoints', 'cv2.boxPoints', ({(151, 30, 151, 33): 'box'}, {}), '(box)', False, 'import cv2\n'), ((152, 16, 152, 42), 'numpy.array', 'np.array', (), '', True, 'import numpy as np\n'), ((200, 16, 200, 39), 'cv2.minAreaRect', 'cv2.minAreaRect', ({(200, 32, 200, 38): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((201, 16, 201, 34), 'cv2.boxPoints', 'cv2.boxPoints', ({(201, 30, 201, 33): 'box'}, {}), '(box)', False, 'import cv2\n'), ((202, 16, 202, 42), 'numpy.array', 'np.array', (), '', True, 'import numpy as np\n'), ((203, 14, 203, 30), 'cv2.moments', 'cv2.moments', ({(203, 26, 203, 29): 'box'}, {}), '(box)', False, 'import cv2\n'), ((251, 14, 251, 33), 'cv2.moments', 'cv2.moments', ({(251, 26, 251, 32): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((287, 12, 287, 49), 'imutils.contours.sort_contours', 'contours.sort_contours', ({(287, 35, 287, 48): 'coords[i:i + 5]'}, {}), '(coords[i:i + 5])', False, 'from imutils import contours\n'), ((295, 18, 295, 55), 'numpy.zeros', 'np.zeros', (), '', True, 'import numpy as np\n'), ((297, 12, 297, 68), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((298, 18, 298, 59), 'cv2.bitwise_and', 'cv2.bitwise_and', (), '', False, 'import cv2\n'), ((299, 12, 299, 41), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((302, 25, 302, 48), 'cv2.countNonZero', 'cv2.countNonZero', ({(302, 42, 302, 47): 'maske'}, {}), '(maske)', False, 'import cv2\n'), ((337, 12, 337, 73), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((341, 18, 341, 55), 'numpy.zeros', 'np.zeros', (), '', True, 'import numpy as np\n'), ((343, 12, 343, 68), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((344, 18, 344, 59), 'cv2.bitwise_and', 'cv2.bitwise_and', (), '', False, 'import cv2\n'), ((348, 25, 348, 48), 'cv2.countNonZero', 'cv2.countNonZero', ({(348, 42, 348, 47): 'maske'}, {}), '(maske)', False, 'import cv2\n'), ((400, 12, 400, 73), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((404, 18, 404, 55), 'numpy.zeros', 'np.zeros', (), '', True, 'import numpy as np\n'), ((406, 12, 406, 68), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((407, 18, 407, 59), 'cv2.bitwise_and', 'cv2.bitwise_and', (), '', False, 'import cv2\n'), ((408, 12, 408, 41), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((411, 25, 411, 48), 'cv2.countNonZero', 'cv2.countNonZero', ({(411, 42, 411, 47): 'maske'}, {}), '(maske)', False, 'import cv2\n'), ((427, 12, 427, 73), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((431, 18, 431, 55), 'numpy.zeros', 'np.zeros', (), '', True, 'import numpy as np\n'), ((433, 12, 433, 68), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((434, 18, 434, 59), 'cv2.bitwise_and', 'cv2.bitwise_and', (), '', False, 'import cv2\n'), ((435, 12, 435, 41), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((438, 25, 438, 48), 'cv2.countNonZero', 'cv2.countNonZero', ({(438, 42, 438, 47): 'maske'}, {}), '(maske)', False, 'import cv2\n'), ((452, 12, 452, 73), 'imutils.contours.sort_contours', 'contours.sort_contours', (), '', False, 'from imutils import contours\n'), ((456, 18, 456, 55), 'numpy.zeros', 'np.zeros', (), '', True, 'import numpy as np\n'), ((458, 12, 458, 68), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((459, 18, 459, 59), 'cv2.bitwise_and', 'cv2.bitwise_and', (), '', False, 'import cv2\n'), ((460, 12, 460, 41), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((463, 25, 463, 48), 'cv2.countNonZero', 'cv2.countNonZero', ({(463, 42, 463, 47): 'maske'}, {}), '(maske)', False, 'import cv2\n'), ((57, 41, 57, 62), 'cv2.arcLength', 'cv2.arcLength', ({(57, 55, 57, 56): 'c', (57, 57, 57, 61): 'True'}, {}), '(c, True)', False, 'import cv2\n'), ((121, 39, 121, 60), 'cv2.arcLength', 'cv2.arcLength', ({(121, 53, 121, 54): 'c', (121, 55, 121, 59): 'True'}, {}), '(c, True)', False, 'import cv2\n'), ((143, 41, 143, 62), 'cv2.arcLength', 'cv2.arcLength', ({(143, 55, 143, 56): 'c', (143, 57, 143, 61): 'True'}, {}), '(c, True)', False, 'import cv2\n'), ((149, 11, 149, 29), 'cv2.contourArea', 'cv2.contourArea', ({(149, 27, 149, 28): 'c'}, {}), '(c)', False, 'import cv2\n'), ((153, 15, 153, 35), 'cv2.contourArea', 'cv2.contourArea', ({(153, 31, 153, 34): 'box'}, {}), '(box)', False, 'import cv2\n'), ((155, 16, 155, 69), 'cv2.drawContours', 'cv2.drawContours', (), '', False, 'import cv2\n'), ((188, 41, 188, 62), 'cv2.arcLength', 'cv2.arcLength', ({(188, 55, 188, 56): 'c', (188, 57, 188, 61): 'True'}, {}), '(c, True)', False, 'import cv2\n'), ((246, 40, 246, 61), 'cv2.arcLength', 'cv2.arcLength', ({(246, 54, 246, 55): 'c', (246, 56, 246, 60): 'True'}, {}), '(c, True)', False, 'import cv2\n'), ((247, 11, 247, 34), 'cv2.contourArea', 'cv2.contourArea', ({(247, 27, 247, 33): 'approx'}, {}), '(approx)', False, 'import cv2\n'), ((257, 28, 257, 51), 'cv2.contourArea', 'cv2.contourArea', ({(257, 44, 257, 50): 'approx'}, {}), '(approx)', False, 'import cv2\n')]