# -- Code Cell --
import pandas as pd
train = pd.read_csv("train.csv")
test = pd.read_csv('test.csv')

# -- Code Cell --
import matplotlib.pyplot as plt
import cv2 as cv
img = cv.imread(test['id'][0])
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img,cmap='gray')

# -- Code Cell --
labels = []
for i in range(len(test)):
    img = cv.imread(test['id'][i])
    img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    img = img[0:64, 0:64]
    rows = img.shape[0]
    circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, rows / 8, param1=100, param2=30, minRadius=1, maxRadius=30)
    try: 
        len(circles)
        labels.append(0)
    except:
        labels.append(1)

# -- Code Cell --
labels

# -- Code Cell --
task2 = []
for i in range(len(test)):
    img = cv.imread(test['id'][i])
    img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    temp_values = []
    for i1 in range(4):
        for i2 in range(4):
            crop = img[64*i1:64*(i1+1), 64*i2:64*(i2+1)]
            rows = crop.shape[0]
            circles = cv.HoughCircles(crop, cv.HOUGH_GRADIENT, 1, rows / 8, param1=100, param2=30, minRadius=1, maxRadius=30)
            try: 
                len(circles)
                temp_values.append(0)
            except:
                temp_values.append(1)
    task2.append(temp_values)

# -- Code Cell --
task2[0]

# -- Code Cell --
answer = []
for i in range(len(task2)):
    bits = task2[i]
    num = int("".join(map(str, bits)), 2)
    if bits[0] == 1:
        num -= 1 << 16
    answer.append(num)

# -- Code Cell --
answer

# -- Code Cell --
rows =[]
for idx, row in test.iterrows():
    rows.append({'subtaskID':1, "ID":row['id'],'answer':labels[idx]})
    rows.append({'subtaskID':2, "ID":row['id'],'answer':answer[idx]})
df = pd.DataFrame(rows)
df.to_csv("subs.csv",index=False)

# -- Code Cell --
