<jupyter_start><jupyter_text>RESTART RUNTIME TO USE NEW PACKAGES<jupyter_code>API_KEY = ""  # Please enter your API Key from [https://www.aicrowd.com/participants/me]
!aicrowd login --api-key $API_KEY
!aicrowd dataset download --challenge image-correction
!rm -rf data
!mkdir data

!unzip -q test.zip  -d data/test
!unzip -q train.zip  -d data/train
import shutil

shutil.copy('AIcrowd-AIBlitz7-Solution/challenge5/load.py','load.py')
shutil.copy('AIcrowd-AIBlitz7-Solution/challenge5/matching.py','matching.py')
import cv2, os
import math
from glob import glob
import numpy as np
from trianglesolver import solve, degree
import random
from load import *
from matching import *
def run2(mode, d,out = np.array([])):
    (start, last, label), image, black = preprocess(mode,d)
    y, x = start.shape[:2]
    x_num = int(512/x)
    y_num = int(512/y)
    if out.shape[0] == 0:
        out = np.zeros((x_num,y_num,y,x,3))

        start_right, image, _ = findN2(start,image,2,right)
        start_bottom, image, _ = findN2(start,image,2,bottom)

        last_top, image, _ = findN2(last,image,2,top)
        last_left, image, _ = findN2(last,image,2,left)

        out[0][0] = start
        out[-1][-1] = last
        out[0][1] = start_bottom
        out[1][0] = start_right
        out[-1][-2] = last_top
        out[-2][-1] = last_left

    if image.shape[0] != 0:
        for _ in range(4):
            filled, nCounts = getNei(out)
            nei = np.zeros(nCounts.shape)
            nei[nCounts > 1] = 1

            for i in range(x_num):
                for j in range(y_num):
                    if nei[i][j] == 1:
                        p_images = []
                        scores = []
                        remove_index = []
                        for d in range(4):
                            try:
                                img = getImgNei(out,i,j,d)
                                out_image, image, confidence, removeIndex = findN2(img,image,2,(d+2)%4,remove = False)
                                if d == top or d == bottom:
                                    confidence = confidence
                                p_images.append(out_image)
                                scores.append(confidence)
                                remove_index.append(removeIndex)
                            except (IndexError,ValueError):
                                pass
                        index = scores.index(min(scores))
                        p_image =  p_images[index]
                        out[i][j] =  p_images[index]
                        image = np.delete(image,remove_index[index],axis = 0)
    img = arangeNew(out)
    return img, label

def run3(mode, d,out = np.array([])):
    (start, last, label), image, black = preprocess(mode,d)
    y, x = start.shape[:2]
    x_num = int(512/x)
    y_num = int(512/y)
    if out.shape[0] == 0:
        out = np.zeros((x_num,y_num,y,x,3))

        start_right, image, _ = findN2(start,image,2,right)
        start_bottom, image, _ = findN2(start,image,2,bottom)

        last_top, image, _ = findN2(last,image,2,top)
        last_left, image, _ = findN2(last,image,2,left)

        out[0][0] = start
        out[-1][-1] = last
        out[0][1] = start_bottom
        out[1][0] = start_right
        out[-1][-2] = last_top
        out[-2][-1] = last_left

    if image.shape[0] != 0:
        for _ in range(4):
            filled, nCounts = getNei(out)
            nei = np.zeros(nCounts.shape)
            nei[nCounts > 0] = 1

            for i in range(x_num):
                for j in range(y_num):
                    if nei[i][j] == 1:
                        p_images = []
                        scores = []
                        remove_index = []
                        for d in range(4):
                            try:
                                img = getImgNei(out,i,j,d)
                                out_image, image, confidence, removeIndex = findN2(img,image,2,(d+2)%4,remove = False)
                                if d == top or d == bottom:
                                    confidence = confidence
                                p_images.append(out_image)
                                scores.append(confidence)
                                remove_index.append(removeIndex)
                            except (IndexError,ValueError):
                                pass
                        index = scores.index(min(scores))
                        p_image =  p_images[index]
                        out[i][j] =  p_images[index]
                        image = np.delete(image,remove_index[index],axis = 0)
    img = arangeNew(out)
    return img, label
def run(mode, d,out = np.array([])):
    (start, last, label), imagee, black = preprocess(mode,d)
    y, x = start.shape[:2]
    x_num = int(512/x)
    y_num = int(512/y)
    
    image = imagee.copy()
    start_down, image = findN(start,image,x_num,right,asList = True)
    outs = []
    for starting_image in start_down:
        out, image = findN(starting_image,image,y_num,bottom,last)
        outs.append(out)
    h = np.concatenate((outs),axis = 1)
    
    image = imagee.copy()
    start_down, image = findN(start,image,y_num,bottom,asList = True)
    outs = []
    for starting_image in start_down:
        out, image = findN(starting_image,image,x_num,right,last)
        outs.append(out)
    v = np.concatenate((outs),axis = 0) 
    
    s = run2(mode,d)[0]
    f = run3(mode,d)[0]
    out = np.mean((h,v,s,f),axis = 0).astype('uint8')
    #out = s
    img = fillup(out)
    
    return img, label
if not os.path.exists('submission'):
    os.makedirs('submission/Labels')

for i in range(5000):
    out, label = run('test',i)
    (start, last, label), image, black = preprocess('train',i)
    out = np.mean((out,label),axis = 0)
    out = cv2.cvtColor(out.astype(np.uint16),cv2.COLOR_RGB2BGR)
    cv2.imwrite(f'submission/Labels/{i}.jpg',out,[int(cv2.IMWRITE_JPEG_QUALITY), 50])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12704234744914972, 'fraction_numerical': 0.020006668889629875, 'mean_word_length': 1.6829159212880143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '546133', 'license': 'MIT', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1935, 'n_words': 388, 'path': 'challenge5/findMatches.ipynb', 'repo_name': 'derInformatiker/AIcrowd-AIBlitz7-Solution', 'size': '5998'}