# -*- coding: utf-8 -*-
"""question1_1.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1KzjLtsxoqoCrQk1_u3LRrtzFdTF6FhX2
"""

#Nishtha Singhal 2017302

from google.colab import drive
drive.mount('/content/drive')

import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import pickle

images_dir = "/content/drive/My Drive/MCA - Assignment 1/images/"
queries_dir = "/content/drive/My Drive/MCA - Assignment 1/train/query/"
gt_dir = "/content/drive/My Drive/MCA - Assignment 1/train/ground_truth/"
pkl_dir = "/content/drive/My Drive/MCA - Assignment 1/Nishtha's Code/data/1/"

count = 1
for filename in os.listdir(images_dir):
  print("processing " + str(count))
  count += 1
  image = cv2.imread(images_dir + filename)
  
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  width = int(image.shape[1] * 0.1)
  height = int(image.shape[0] * 0.1)
  image = cv2.resize(image, (width,height))
  image = image // 64 * 64 
  image = cv2.blur(image,(3,3))
  image = np.array(image)
  # plt.imshow(image)
  # plt.show()
  lh = np.zeros((10, width, height, 64))
  lv = np.zeros((10, width, height, 64))
  h = np.zeros(64)

  ic = [[] for i in range(64)]
  # print(lh.shape)
  # print(lv.shape)
  
  for x in range(width):
    for y in range(height):
      pc = image[y][x][0]//64 * 16 + image[y][x][1]//64 * 4 + image[y][x][2]//64
      h[pc] = 1
      lh[0][x][y][pc] = 1
      lv[0][x][y][pc] = 1
      ic[pc].append((x,y))
  
  for x in range(width):
    for y in range(height):
      for z in range(1, 10):
        pc = image[y][x][0]//64 * 16 + image[y][x][1]//64 * 4 + image[y][x][2]//64
        lh[z][x][y][pc] = lh[z - 1][x][y][pc] 
        lv[z][x][y][pc] = lv[z - 1][x][y][pc] 
        if (z + x < width):
          lh[z][x][y][pc] += lh[0][z + x][y][pc]
        if (z + y < height):
          lv[z][x][y][pc] += lh[0][x][z + y][pc]
  cac = np.zeros((4, 64))
   
  for c in range(64):
    for x,y in ic[c]:
      cac[0][c] += ((lh[0][x - 1][y - 1][c] if (x - 1 >= 0) else 0) + lh[0][x][y - 1][c] if (y - 1 >= 0) else 0) + ((lh[0][x - 1][y + 1][c] if (x - 1 >= 0) else 0) + lh[0][x][y + 1][c] if (y + 1 < height) else 0) + (lv[0][x - 1][y][c] + lv[0][x - 1][y][c] if (x - 1 >= 0) else 0) + (lv[0][x + 1][y][c] + lv[0][x + 1][y][c] if (x + 1 < width) else 0)
      # print('dist0 calculated', cac[0][c], x,y,c)
  
  for z in [3,5,7]:
     #check if d=1-10
    for c in range(64):
      for x,y in ic[c]:
        cac[z//2][c] += ((lh[z - 1][x - z][y - z][c] if (x - z >= 0)  else 0) + lh[z - 1][x][y - z][c] if (y - z >= 0) else 0) + (( lh[z - 1][x - z][y + z][c] if (x - z >= 0) else 0) + lh[z - 1][x][y + z][c] if (y + z < height) else 0) +(( lv[z - 2][x - z][y - z + 1][c] if (y - z + 1 >= 0) else 0) + lv[z - 2][x - z][y][c] if (x - z >= 0) else 0) +((lv[z - 2][x + z][y - z + 1][c] if (y - z + 1 >= 0)  else 0) + lv[z - 2][x + z][y][c]  if (x + z < width) else 0)
        
        # print('dist calculated', cac[z//2][c], z, x,y,c)

  # cacsum = np.sum(cac, axis = 1)
  for z in [1,3,5,7]:
    for c in range(64):
      if h[c] > 0 :
        cac[z//2][c] /= (h[c]*z*8)
      # cac[z][c] /= cacsum[z]
      
      # if(cac[z][c] ==  0.00000000e+00 ): cac[z][c]=0
    # print(cac[z])
  
  with open(pkl_dir+filename+".pkl", "wb") as f:
    pickle.dump(cac, f)
    f.close()

import time
queries = {}
t=[]

for filename in os.listdir(queries_dir):
  # print(filename)
  query = open(queries_dir + filename).read()
  queries[('_').join(filename.split('_')[:-1])] = ('_').join(query.split(' ')[0].split('_')[1:])
count = 1
current = time.time()
for q,i in enumerate(queries): 

  print("processing ", count)
  count+=1
  with open(pkl_dir+queries[i]+".jpg.pkl", "rb") as f:
    caci = pickle.load(f)
    caci = np.array(caci)
    f.close()
  differences = {}
  for filename in os.listdir(images_dir):
    with open(pkl_dir+filename.split('.')[0]+".jpg.pkl", "rb") as f:
      cacother= np.array(pickle.load(f))
      f.close()
    differences[filename.split('.')[0]] = np.sum(np.abs(caci - cacother))
  ranked = {k:v for k, v in sorted(differences.items(), key = lambda item:item[1])[:15]} 
  # t.append(time.time() - current)
  with open(pkl_dir+"ranks/" + queries[i] +".pkl", "wb") as f:
    pickle.dump(ranked, f)
    f.close()
t = time.time()-current
# print("Average retrieval time: ", sum(t)/len(t))
      # print("processing " + str(count))
    # count += 1
  # print(i)

#   print(('_').join(filename.split('_')[:-1]),('_').join(query.split(' ')[0].split('_')[1:]))
# print(queries)

# for filename in os.listdir(queries_dir):
#   # print(filename)
#   query = open(queries_dir + filename).read()
#   queries[('_').join(filename.split('_')[:-1])] = ('_').join(query.split(' ')[0].split('_')[1:])

gt_good = {}
gt_ok = {}
gt_junk = {}

p = []
r = []
f1 = []
good_r = []
ok_r = []
junk_r = []

for filename in os.listdir(gt_dir):
  gt = open(gt_dir + filename).read()
  tempname = filename.split('_')
  gt_type = tempname[-1].split('.')[0]
  # print(tempname, gt_type)
  tempname = ('_').join(tempname[:-1])

  if(gt_type == 'good'):
    gt_good[tempname] = gt.split('\n')
    for i in gt_good[tempname]:
      if(i==''):
        gt_good[tempname].remove(i)
  elif(gt_type == 'ok'):
    gt_ok[tempname] = gt.split('\n')
    for i in gt_ok[tempname]:
      if(i==''):
        gt_ok[tempname].remove(i)
  else:
    gt_junk[tempname] = gt.split('\n')
    for i in gt_junk[tempname]:
      if(i==''):
        gt_junk[tempname].remove(i)

for tempname in queries.keys():  
  with open(pkl_dir+"ranks/" +queries[tempname]+".pkl",  "rb") as f:
    ranked = list(pickle.load(f))[1:]
    # ranked =[1:]
    # caci = np.array(caci)
    f.close()
  good_count = 0
  ok_count = 0
  junk_count = 0
  for rd in ranked:
    if rd in gt_good[tempname]:
      good_count += 1
    elif rd in gt_ok[tempname]:
      ok_count += 1
    elif rd in gt_junk[tempname]:
      junk_count += 1

  p.append( (good_count + junk_count+ ok_count) / len(ranked))
  r.append((good_count + junk_count+ ok_count) / (len(gt_good[tempname]) + len(gt_ok[tempname]) + len(gt_junk[tempname])))
  if (p[-1] + r[-1] > 0 ):
    f1.append((2 * p[-1] * r[-1])/ (p[-1] + r[-1]))
  else:
    f1.append(0)

  good_r.append(good_count / len(gt_good[tempname]))
  ok_r.append(ok_count / len(gt_ok[tempname]))
  junk_r.append(junk_count / len(gt_junk[tempname]))



print("Maximum Precision: ", max(p))
print("Minimum Precision: ", min(p))
print("Average Precision: ", sum(p)/len(p))

print("Maximum Recall: ", max(r))
print("Minimum Recall: ", min(r))
print("Average Recall: ", sum(r)/len(r))

print("Maximum F1 : ", max(f1))
print("Minimum F1 : ", min(f1))
print("Average F1 : ", sum(f1)/len(f1))


print("Average retrieval time: ", t[-1]/len(t))


print("Average percentage of good queries: ", sum(good_r)/len(good_r)*100)
print("Average percentage of Junk queries: ", sum(junk_r)/len(junk_r)*100)
print("Average percentage of ok queries: ", sum(ok_r)/len(ok_r)*100)


cfca525baf0448687d1c49f4e0c79be786835289
<mask token>

<mask token>
drive.mount('/content/drive')
<mask token>
for filename in os.listdir(images_dir):
    print('processing ' + str(count))
    count += 1
    image = cv2.imread(images_dir + filename)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    width = int(image.shape[1] * 0.1)
    height = int(image.shape[0] * 0.1)
    image = cv2.resize(image, (width, height))
    image = image // 64 * 64
    image = cv2.blur(image, (3, 3))
    image = np.array(image)
    lh = np.zeros((10, width, height, 64))
    lv = np.zeros((10, width, height, 64))
    h = np.zeros(64)
    ic = [[] for i in range(64)]
    for x in range(width):
        for y in range(height):
            pc = image[y][x][0] // 64 * 16 + image[y][x][1] // 64 * 4 + image[y
                ][x][2] // 64
            h[pc] = 1
            lh[0][x][y][pc] = 1
            lv[0][x][y][pc] = 1
            ic[pc].append((x, y))
    for x in range(width):
        for y in range(height):
            for z in range(1, 10):
                pc = image[y][x][0] // 64 * 16 + image[y][x][1
                    ] // 64 * 4 + image[y][x][2] // 64
                lh[z][x][y][pc] = lh[z - 1][x][y][pc]
                lv[z][x][y][pc] = lv[z - 1][x][y][pc]
                if z + x < width:
                    lh[z][x][y][pc] += lh[0][z + x][y][pc]
                if z + y < height:
                    lv[z][x][y][pc] += lh[0][x][z + y][pc]
    cac = np.zeros((4, 64))
    for c in range(64):
        for x, y in ic[c]:
            cac[0][c] += ((lh[0][x - 1][y - 1][c] if x - 1 >= 0 else 0) +
                lh[0][x][y - 1][c] if y - 1 >= 0 else 0) + ((lh[0][x - 1][y +
                1][c] if x - 1 >= 0 else 0) + lh[0][x][y + 1][c] if y + 1 <
                height else 0) + (lv[0][x - 1][y][c] + lv[0][x - 1][y][c] if
                x - 1 >= 0 else 0) + (lv[0][x + 1][y][c] + lv[0][x + 1][y][
                c] if x + 1 < width else 0)
    for z in [3, 5, 7]:
        for c in range(64):
            for x, y in ic[c]:
                cac[z // 2][c] += ((lh[z - 1][x - z][y - z][c] if x - z >= 
                    0 else 0) + lh[z - 1][x][y - z][c] if y - z >= 0 else 0
                    ) + ((lh[z - 1][x - z][y + z][c] if x - z >= 0 else 0) +
                    lh[z - 1][x][y + z][c] if y + z < height else 0) + ((lv
                    [z - 2][x - z][y - z + 1][c] if y - z + 1 >= 0 else 0) +
                    lv[z - 2][x - z][y][c] if x - z >= 0 else 0) + ((lv[z -
                    2][x + z][y - z + 1][c] if y - z + 1 >= 0 else 0) + lv[
                    z - 2][x + z][y][c] if x + z < width else 0)
    for z in [1, 3, 5, 7]:
        for c in range(64):
            if h[c] > 0:
                cac[z // 2][c] /= h[c] * z * 8
    with open(pkl_dir + filename + '.pkl', 'wb') as f:
        pickle.dump(cac, f)
        f.close()
<mask token>
for filename in os.listdir(queries_dir):
    query = open(queries_dir + filename).read()
    queries['_'.join(filename.split('_')[:-1])] = '_'.join(query.split(' ')
        [0].split('_')[1:])
<mask token>
for q, i in enumerate(queries):
    print('processing ', count)
    count += 1
    with open(pkl_dir + queries[i] + '.jpg.pkl', 'rb') as f:
        caci = pickle.load(f)
        caci = np.array(caci)
        f.close()
    differences = {}
    for filename in os.listdir(images_dir):
        with open(pkl_dir + filename.split('.')[0] + '.jpg.pkl', 'rb') as f:
            cacother = np.array(pickle.load(f))
            f.close()
        differences[filename.split('.')[0]] = np.sum(np.abs(caci - cacother))
    ranked = {k: v for k, v in sorted(differences.items(), key=lambda item:
        item[1])[:15]}
    with open(pkl_dir + 'ranks/' + queries[i] + '.pkl', 'wb') as f:
        pickle.dump(ranked, f)
        f.close()
<mask token>
for filename in os.listdir(gt_dir):
    gt = open(gt_dir + filename).read()
    tempname = filename.split('_')
    gt_type = tempname[-1].split('.')[0]
    tempname = '_'.join(tempname[:-1])
    if gt_type == 'good':
        gt_good[tempname] = gt.split('\n')
        for i in gt_good[tempname]:
            if i == '':
                gt_good[tempname].remove(i)
    elif gt_type == 'ok':
        gt_ok[tempname] = gt.split('\n')
        for i in gt_ok[tempname]:
            if i == '':
                gt_ok[tempname].remove(i)
    else:
        gt_junk[tempname] = gt.split('\n')
        for i in gt_junk[tempname]:
            if i == '':
                gt_junk[tempname].remove(i)
for tempname in queries.keys():
    with open(pkl_dir + 'ranks/' + queries[tempname] + '.pkl', 'rb') as f:
        ranked = list(pickle.load(f))[1:]
        f.close()
    good_count = 0
    ok_count = 0
    junk_count = 0
    for rd in ranked:
        if rd in gt_good[tempname]:
            good_count += 1
        elif rd in gt_ok[tempname]:
            ok_count += 1
        elif rd in gt_junk[tempname]:
            junk_count += 1
    p.append((good_count + junk_count + ok_count) / len(ranked))
    r.append((good_count + junk_count + ok_count) / (len(gt_good[tempname]) +
        len(gt_ok[tempname]) + len(gt_junk[tempname])))
    if p[-1] + r[-1] > 0:
        f1.append(2 * p[-1] * r[-1] / (p[-1] + r[-1]))
    else:
        f1.append(0)
    good_r.append(good_count / len(gt_good[tempname]))
    ok_r.append(ok_count / len(gt_ok[tempname]))
    junk_r.append(junk_count / len(gt_junk[tempname]))
print('Maximum Precision: ', max(p))
print('Minimum Precision: ', min(p))
print('Average Precision: ', sum(p) / len(p))
print('Maximum Recall: ', max(r))
print('Minimum Recall: ', min(r))
print('Average Recall: ', sum(r) / len(r))
print('Maximum F1 : ', max(f1))
print('Minimum F1 : ', min(f1))
print('Average F1 : ', sum(f1) / len(f1))
print('Average retrieval time: ', t[-1] / len(t))
print('Average percentage of good queries: ', sum(good_r) / len(good_r) * 100)
print('Average percentage of Junk queries: ', sum(junk_r) / len(junk_r) * 100)
print('Average percentage of ok queries: ', sum(ok_r) / len(ok_r) * 100)

<mask token>
drive.mount('/content/drive')
<mask token>
images_dir = '/content/drive/My Drive/MCA - Assignment 1/images/'
queries_dir = '/content/drive/My Drive/MCA - Assignment 1/train/query/'
gt_dir = '/content/drive/My Drive/MCA - Assignment 1/train/ground_truth/'
pkl_dir = "/content/drive/My Drive/MCA - Assignment 1/Nishtha's Code/data/1/"
count = 1
for filename in os.listdir(images_dir):
    print('processing ' + str(count))
    count += 1
    image = cv2.imread(images_dir + filename)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    width = int(image.shape[1] * 0.1)
    height = int(image.shape[0] * 0.1)
    image = cv2.resize(image, (width, height))
    image = image // 64 * 64
    image = cv2.blur(image, (3, 3))
    image = np.array(image)
    lh = np.zeros((10, width, height, 64))
    lv = np.zeros((10, width, height, 64))
    h = np.zeros(64)
    ic = [[] for i in range(64)]
    for x in range(width):
        for y in range(height):
            pc = image[y][x][0] // 64 * 16 + image[y][x][1] // 64 * 4 + image[y
                ][x][2] // 64
            h[pc] = 1
            lh[0][x][y][pc] = 1
            lv[0][x][y][pc] = 1
            ic[pc].append((x, y))
    for x in range(width):
        for y in range(height):
            for z in range(1, 10):
                pc = image[y][x][0] // 64 * 16 + image[y][x][1
                    ] // 64 * 4 + image[y][x][2] // 64
                lh[z][x][y][pc] = lh[z - 1][x][y][pc]
                lv[z][x][y][pc] = lv[z - 1][x][y][pc]
                if z + x < width:
                    lh[z][x][y][pc] += lh[0][z + x][y][pc]
                if z + y < height:
                    lv[z][x][y][pc] += lh[0][x][z + y][pc]
    cac = np.zeros((4, 64))
    for c in range(64):
        for x, y in ic[c]:
            cac[0][c] += ((lh[0][x - 1][y - 1][c] if x - 1 >= 0 else 0) +
                lh[0][x][y - 1][c] if y - 1 >= 0 else 0) + ((lh[0][x - 1][y +
                1][c] if x - 1 >= 0 else 0) + lh[0][x][y + 1][c] if y + 1 <
                height else 0) + (lv[0][x - 1][y][c] + lv[0][x - 1][y][c] if
                x - 1 >= 0 else 0) + (lv[0][x + 1][y][c] + lv[0][x + 1][y][
                c] if x + 1 < width else 0)
    for z in [3, 5, 7]:
        for c in range(64):
            for x, y in ic[c]:
                cac[z // 2][c] += ((lh[z - 1][x - z][y - z][c] if x - z >= 
                    0 else 0) + lh[z - 1][x][y - z][c] if y - z >= 0 else 0
                    ) + ((lh[z - 1][x - z][y + z][c] if x - z >= 0 else 0) +
                    lh[z - 1][x][y + z][c] if y + z < height else 0) + ((lv
                    [z - 2][x - z][y - z + 1][c] if y - z + 1 >= 0 else 0) +
                    lv[z - 2][x - z][y][c] if x - z >= 0 else 0) + ((lv[z -
                    2][x + z][y - z + 1][c] if y - z + 1 >= 0 else 0) + lv[
                    z - 2][x + z][y][c] if x + z < width else 0)
    for z in [1, 3, 5, 7]:
        for c in range(64):
            if h[c] > 0:
                cac[z // 2][c] /= h[c] * z * 8
    with open(pkl_dir + filename + '.pkl', 'wb') as f:
        pickle.dump(cac, f)
        f.close()
<mask token>
queries = {}
t = []
for filename in os.listdir(queries_dir):
    query = open(queries_dir + filename).read()
    queries['_'.join(filename.split('_')[:-1])] = '_'.join(query.split(' ')
        [0].split('_')[1:])
count = 1
current = time.time()
for q, i in enumerate(queries):
    print('processing ', count)
    count += 1
    with open(pkl_dir + queries[i] + '.jpg.pkl', 'rb') as f:
        caci = pickle.load(f)
        caci = np.array(caci)
        f.close()
    differences = {}
    for filename in os.listdir(images_dir):
        with open(pkl_dir + filename.split('.')[0] + '.jpg.pkl', 'rb') as f:
            cacother = np.array(pickle.load(f))
            f.close()
        differences[filename.split('.')[0]] = np.sum(np.abs(caci - cacother))
    ranked = {k: v for k, v in sorted(differences.items(), key=lambda item:
        item[1])[:15]}
    with open(pkl_dir + 'ranks/' + queries[i] + '.pkl', 'wb') as f:
        pickle.dump(ranked, f)
        f.close()
t = time.time() - current
gt_good = {}
gt_ok = {}
gt_junk = {}
p = []
r = []
f1 = []
good_r = []
ok_r = []
junk_r = []
for filename in os.listdir(gt_dir):
    gt = open(gt_dir + filename).read()
    tempname = filename.split('_')
    gt_type = tempname[-1].split('.')[0]
    tempname = '_'.join(tempname[:-1])
    if gt_type == 'good':
        gt_good[tempname] = gt.split('\n')
        for i in gt_good[tempname]:
            if i == '':
                gt_good[tempname].remove(i)
    elif gt_type == 'ok':
        gt_ok[tempname] = gt.split('\n')
        for i in gt_ok[tempname]:
            if i == '':
                gt_ok[tempname].remove(i)
    else:
        gt_junk[tempname] = gt.split('\n')
        for i in gt_junk[tempname]:
            if i == '':
                gt_junk[tempname].remove(i)
for tempname in queries.keys():
    with open(pkl_dir + 'ranks/' + queries[tempname] + '.pkl', 'rb') as f:
        ranked = list(pickle.load(f))[1:]
        f.close()
    good_count = 0
    ok_count = 0
    junk_count = 0
    for rd in ranked:
        if rd in gt_good[tempname]:
            good_count += 1
        elif rd in gt_ok[tempname]:
            ok_count += 1
        elif rd in gt_junk[tempname]:
            junk_count += 1
    p.append((good_count + junk_count + ok_count) / len(ranked))
    r.append((good_count + junk_count + ok_count) / (len(gt_good[tempname]) +
        len(gt_ok[tempname]) + len(gt_junk[tempname])))
    if p[-1] + r[-1] > 0:
        f1.append(2 * p[-1] * r[-1] / (p[-1] + r[-1]))
    else:
        f1.append(0)
    good_r.append(good_count / len(gt_good[tempname]))
    ok_r.append(ok_count / len(gt_ok[tempname]))
    junk_r.append(junk_count / len(gt_junk[tempname]))
print('Maximum Precision: ', max(p))
print('Minimum Precision: ', min(p))
print('Average Precision: ', sum(p) / len(p))
print('Maximum Recall: ', max(r))
print('Minimum Recall: ', min(r))
print('Average Recall: ', sum(r) / len(r))
print('Maximum F1 : ', max(f1))
print('Minimum F1 : ', min(f1))
print('Average F1 : ', sum(f1) / len(f1))
print('Average retrieval time: ', t[-1] / len(t))
print('Average percentage of good queries: ', sum(good_r) / len(good_r) * 100)
print('Average percentage of Junk queries: ', sum(junk_r) / len(junk_r) * 100)
print('Average percentage of ok queries: ', sum(ok_r) / len(ok_r) * 100)

<mask token>
from google.colab import drive
drive.mount('/content/drive')
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import pickle
images_dir = '/content/drive/My Drive/MCA - Assignment 1/images/'
queries_dir = '/content/drive/My Drive/MCA - Assignment 1/train/query/'
gt_dir = '/content/drive/My Drive/MCA - Assignment 1/train/ground_truth/'
pkl_dir = "/content/drive/My Drive/MCA - Assignment 1/Nishtha's Code/data/1/"
count = 1
for filename in os.listdir(images_dir):
    print('processing ' + str(count))
    count += 1
    image = cv2.imread(images_dir + filename)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    width = int(image.shape[1] * 0.1)
    height = int(image.shape[0] * 0.1)
    image = cv2.resize(image, (width, height))
    image = image // 64 * 64
    image = cv2.blur(image, (3, 3))
    image = np.array(image)
    lh = np.zeros((10, width, height, 64))
    lv = np.zeros((10, width, height, 64))
    h = np.zeros(64)
    ic = [[] for i in range(64)]
    for x in range(width):
        for y in range(height):
            pc = image[y][x][0] // 64 * 16 + image[y][x][1] // 64 * 4 + image[y
                ][x][2] // 64
            h[pc] = 1
            lh[0][x][y][pc] = 1
            lv[0][x][y][pc] = 1
            ic[pc].append((x, y))
    for x in range(width):
        for y in range(height):
            for z in range(1, 10):
                pc = image[y][x][0] // 64 * 16 + image[y][x][1
                    ] // 64 * 4 + image[y][x][2] // 64
                lh[z][x][y][pc] = lh[z - 1][x][y][pc]
                lv[z][x][y][pc] = lv[z - 1][x][y][pc]
                if z + x < width:
                    lh[z][x][y][pc] += lh[0][z + x][y][pc]
                if z + y < height:
                    lv[z][x][y][pc] += lh[0][x][z + y][pc]
    cac = np.zeros((4, 64))
    for c in range(64):
        for x, y in ic[c]:
            cac[0][c] += ((lh[0][x - 1][y - 1][c] if x - 1 >= 0 else 0) +
                lh[0][x][y - 1][c] if y - 1 >= 0 else 0) + ((lh[0][x - 1][y +
                1][c] if x - 1 >= 0 else 0) + lh[0][x][y + 1][c] if y + 1 <
                height else 0) + (lv[0][x - 1][y][c] + lv[0][x - 1][y][c] if
                x - 1 >= 0 else 0) + (lv[0][x + 1][y][c] + lv[0][x + 1][y][
                c] if x + 1 < width else 0)
    for z in [3, 5, 7]:
        for c in range(64):
            for x, y in ic[c]:
                cac[z // 2][c] += ((lh[z - 1][x - z][y - z][c] if x - z >= 
                    0 else 0) + lh[z - 1][x][y - z][c] if y - z >= 0 else 0
                    ) + ((lh[z - 1][x - z][y + z][c] if x - z >= 0 else 0) +
                    lh[z - 1][x][y + z][c] if y + z < height else 0) + ((lv
                    [z - 2][x - z][y - z + 1][c] if y - z + 1 >= 0 else 0) +
                    lv[z - 2][x - z][y][c] if x - z >= 0 else 0) + ((lv[z -
                    2][x + z][y - z + 1][c] if y - z + 1 >= 0 else 0) + lv[
                    z - 2][x + z][y][c] if x + z < width else 0)
    for z in [1, 3, 5, 7]:
        for c in range(64):
            if h[c] > 0:
                cac[z // 2][c] /= h[c] * z * 8
    with open(pkl_dir + filename + '.pkl', 'wb') as f:
        pickle.dump(cac, f)
        f.close()
import time
queries = {}
t = []
for filename in os.listdir(queries_dir):
    query = open(queries_dir + filename).read()
    queries['_'.join(filename.split('_')[:-1])] = '_'.join(query.split(' ')
        [0].split('_')[1:])
count = 1
current = time.time()
for q, i in enumerate(queries):
    print('processing ', count)
    count += 1
    with open(pkl_dir + queries[i] + '.jpg.pkl', 'rb') as f:
        caci = pickle.load(f)
        caci = np.array(caci)
        f.close()
    differences = {}
    for filename in os.listdir(images_dir):
        with open(pkl_dir + filename.split('.')[0] + '.jpg.pkl', 'rb') as f:
            cacother = np.array(pickle.load(f))
            f.close()
        differences[filename.split('.')[0]] = np.sum(np.abs(caci - cacother))
    ranked = {k: v for k, v in sorted(differences.items(), key=lambda item:
        item[1])[:15]}
    with open(pkl_dir + 'ranks/' + queries[i] + '.pkl', 'wb') as f:
        pickle.dump(ranked, f)
        f.close()
t = time.time() - current
gt_good = {}
gt_ok = {}
gt_junk = {}
p = []
r = []
f1 = []
good_r = []
ok_r = []
junk_r = []
for filename in os.listdir(gt_dir):
    gt = open(gt_dir + filename).read()
    tempname = filename.split('_')
    gt_type = tempname[-1].split('.')[0]
    tempname = '_'.join(tempname[:-1])
    if gt_type == 'good':
        gt_good[tempname] = gt.split('\n')
        for i in gt_good[tempname]:
            if i == '':
                gt_good[tempname].remove(i)
    elif gt_type == 'ok':
        gt_ok[tempname] = gt.split('\n')
        for i in gt_ok[tempname]:
            if i == '':
                gt_ok[tempname].remove(i)
    else:
        gt_junk[tempname] = gt.split('\n')
        for i in gt_junk[tempname]:
            if i == '':
                gt_junk[tempname].remove(i)
for tempname in queries.keys():
    with open(pkl_dir + 'ranks/' + queries[tempname] + '.pkl', 'rb') as f:
        ranked = list(pickle.load(f))[1:]
        f.close()
    good_count = 0
    ok_count = 0
    junk_count = 0
    for rd in ranked:
        if rd in gt_good[tempname]:
            good_count += 1
        elif rd in gt_ok[tempname]:
            ok_count += 1
        elif rd in gt_junk[tempname]:
            junk_count += 1
    p.append((good_count + junk_count + ok_count) / len(ranked))
    r.append((good_count + junk_count + ok_count) / (len(gt_good[tempname]) +
        len(gt_ok[tempname]) + len(gt_junk[tempname])))
    if p[-1] + r[-1] > 0:
        f1.append(2 * p[-1] * r[-1] / (p[-1] + r[-1]))
    else:
        f1.append(0)
    good_r.append(good_count / len(gt_good[tempname]))
    ok_r.append(ok_count / len(gt_ok[tempname]))
    junk_r.append(junk_count / len(gt_junk[tempname]))
print('Maximum Precision: ', max(p))
print('Minimum Precision: ', min(p))
print('Average Precision: ', sum(p) / len(p))
print('Maximum Recall: ', max(r))
print('Minimum Recall: ', min(r))
print('Average Recall: ', sum(r) / len(r))
print('Maximum F1 : ', max(f1))
print('Minimum F1 : ', min(f1))
print('Average F1 : ', sum(f1) / len(f1))
print('Average retrieval time: ', t[-1] / len(t))
print('Average percentage of good queries: ', sum(good_r) / len(good_r) * 100)
print('Average percentage of Junk queries: ', sum(junk_r) / len(junk_r) * 100)
print('Average percentage of ok queries: ', sum(ok_r) / len(ok_r) * 100)

# -*- coding: utf-8 -*-
"""question1_1.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1KzjLtsxoqoCrQk1_u3LRrtzFdTF6FhX2
"""

#Nishtha Singhal 2017302

from google.colab import drive
drive.mount('/content/drive')

import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import pickle

images_dir = "/content/drive/My Drive/MCA - Assignment 1/images/"
queries_dir = "/content/drive/My Drive/MCA - Assignment 1/train/query/"
gt_dir = "/content/drive/My Drive/MCA - Assignment 1/train/ground_truth/"
pkl_dir = "/content/drive/My Drive/MCA - Assignment 1/Nishtha's Code/data/1/"

count = 1
for filename in os.listdir(images_dir):
  print("processing " + str(count))
  count += 1
  image = cv2.imread(images_dir + filename)
  
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  width = int(image.shape[1] * 0.1)
  height = int(image.shape[0] * 0.1)
  image = cv2.resize(image, (width,height))
  image = image // 64 * 64 
  image = cv2.blur(image,(3,3))
  image = np.array(image)
  # plt.imshow(image)
  # plt.show()
  lh = np.zeros((10, width, height, 64))
  lv = np.zeros((10, width, height, 64))
  h = np.zeros(64)

  ic = [[] for i in range(64)]
  # print(lh.shape)
  # print(lv.shape)
  
  for x in range(width):
    for y in range(height):
      pc = image[y][x][0]//64 * 16 + image[y][x][1]//64 * 4 + image[y][x][2]//64
      h[pc] = 1
      lh[0][x][y][pc] = 1
      lv[0][x][y][pc] = 1
      ic[pc].append((x,y))
  
  for x in range(width):
    for y in range(height):
      for z in range(1, 10):
        pc = image[y][x][0]//64 * 16 + image[y][x][1]//64 * 4 + image[y][x][2]//64
        lh[z][x][y][pc] = lh[z - 1][x][y][pc] 
        lv[z][x][y][pc] = lv[z - 1][x][y][pc] 
        if (z + x < width):
          lh[z][x][y][pc] += lh[0][z + x][y][pc]
        if (z + y < height):
          lv[z][x][y][pc] += lh[0][x][z + y][pc]
  cac = np.zeros((4, 64))
   
  for c in range(64):
    for x,y in ic[c]:
      cac[0][c] += ((lh[0][x - 1][y - 1][c] if (x - 1 >= 0) else 0) + lh[0][x][y - 1][c] if (y - 1 >= 0) else 0) + ((lh[0][x - 1][y + 1][c] if (x - 1 >= 0) else 0) + lh[0][x][y + 1][c] if (y + 1 < height) else 0) + (lv[0][x - 1][y][c] + lv[0][x - 1][y][c] if (x - 1 >= 0) else 0) + (lv[0][x + 1][y][c] + lv[0][x + 1][y][c] if (x + 1 < width) else 0)
      # print('dist0 calculated', cac[0][c], x,y,c)
  
  for z in [3,5,7]:
     #check if d=1-10
    for c in range(64):
      for x,y in ic[c]:
        cac[z//2][c] += ((lh[z - 1][x - z][y - z][c] if (x - z >= 0)  else 0) + lh[z - 1][x][y - z][c] if (y - z >= 0) else 0) + (( lh[z - 1][x - z][y + z][c] if (x - z >= 0) else 0) + lh[z - 1][x][y + z][c] if (y + z < height) else 0) +(( lv[z - 2][x - z][y - z + 1][c] if (y - z + 1 >= 0) else 0) + lv[z - 2][x - z][y][c] if (x - z >= 0) else 0) +((lv[z - 2][x + z][y - z + 1][c] if (y - z + 1 >= 0)  else 0) + lv[z - 2][x + z][y][c]  if (x + z < width) else 0)
        
        # print('dist calculated', cac[z//2][c], z, x,y,c)

  # cacsum = np.sum(cac, axis = 1)
  for z in [1,3,5,7]:
    for c in range(64):
      if h[c] > 0 :
        cac[z//2][c] /= (h[c]*z*8)
      # cac[z][c] /= cacsum[z]
      
      # if(cac[z][c] ==  0.00000000e+00 ): cac[z][c]=0
    # print(cac[z])
  
  with open(pkl_dir+filename+".pkl", "wb") as f:
    pickle.dump(cac, f)
    f.close()

import time
queries = {}
t=[]

for filename in os.listdir(queries_dir):
  # print(filename)
  query = open(queries_dir + filename).read()
  queries[('_').join(filename.split('_')[:-1])] = ('_').join(query.split(' ')[0].split('_')[1:])
count = 1
current = time.time()
for q,i in enumerate(queries): 

  print("processing ", count)
  count+=1
  with open(pkl_dir+queries[i]+".jpg.pkl", "rb") as f:
    caci = pickle.load(f)
    caci = np.array(caci)
    f.close()
  differences = {}
  for filename in os.listdir(images_dir):
    with open(pkl_dir+filename.split('.')[0]+".jpg.pkl", "rb") as f:
      cacother= np.array(pickle.load(f))
      f.close()
    differences[filename.split('.')[0]] = np.sum(np.abs(caci - cacother))
  ranked = {k:v for k, v in sorted(differences.items(), key = lambda item:item[1])[:15]} 
  # t.append(time.time() - current)
  with open(pkl_dir+"ranks/" + queries[i] +".pkl", "wb") as f:
    pickle.dump(ranked, f)
    f.close()
t = time.time()-current
# print("Average retrieval time: ", sum(t)/len(t))
      # print("processing " + str(count))
    # count += 1
  # print(i)

#   print(('_').join(filename.split('_')[:-1]),('_').join(query.split(' ')[0].split('_')[1:]))
# print(queries)

# for filename in os.listdir(queries_dir):
#   # print(filename)
#   query = open(queries_dir + filename).read()
#   queries[('_').join(filename.split('_')[:-1])] = ('_').join(query.split(' ')[0].split('_')[1:])

gt_good = {}
gt_ok = {}
gt_junk = {}

p = []
r = []
f1 = []
good_r = []
ok_r = []
junk_r = []

for filename in os.listdir(gt_dir):
  gt = open(gt_dir + filename).read()
  tempname = filename.split('_')
  gt_type = tempname[-1].split('.')[0]
  # print(tempname, gt_type)
  tempname = ('_').join(tempname[:-1])

  if(gt_type == 'good'):
    gt_good[tempname] = gt.split('\n')
    for i in gt_good[tempname]:
      if(i==''):
        gt_good[tempname].remove(i)
  elif(gt_type == 'ok'):
    gt_ok[tempname] = gt.split('\n')
    for i in gt_ok[tempname]:
      if(i==''):
        gt_ok[tempname].remove(i)
  else:
    gt_junk[tempname] = gt.split('\n')
    for i in gt_junk[tempname]:
      if(i==''):
        gt_junk[tempname].remove(i)

for tempname in queries.keys():  
  with open(pkl_dir+"ranks/" +queries[tempname]+".pkl",  "rb") as f:
    ranked = list(pickle.load(f))[1:]
    # ranked =[1:]
    # caci = np.array(caci)
    f.close()
  good_count = 0
  ok_count = 0
  junk_count = 0
  for rd in ranked:
    if rd in gt_good[tempname]:
      good_count += 1
    elif rd in gt_ok[tempname]:
      ok_count += 1
    elif rd in gt_junk[tempname]:
      junk_count += 1

  p.append( (good_count + junk_count+ ok_count) / len(ranked))
  r.append((good_count + junk_count+ ok_count) / (len(gt_good[tempname]) + len(gt_ok[tempname]) + len(gt_junk[tempname])))
  if (p[-1] + r[-1] > 0 ):
    f1.append((2 * p[-1] * r[-1])/ (p[-1] + r[-1]))
  else:
    f1.append(0)

  good_r.append(good_count / len(gt_good[tempname]))
  ok_r.append(ok_count / len(gt_ok[tempname]))
  junk_r.append(junk_count / len(gt_junk[tempname]))



print("Maximum Precision: ", max(p))
print("Minimum Precision: ", min(p))
print("Average Precision: ", sum(p)/len(p))

print("Maximum Recall: ", max(r))
print("Minimum Recall: ", min(r))
print("Average Recall: ", sum(r)/len(r))

print("Maximum F1 : ", max(f1))
print("Minimum F1 : ", min(f1))
print("Average F1 : ", sum(f1)/len(f1))


print("Average retrieval time: ", t[-1]/len(t))


print("Average percentage of good queries: ", sum(good_r)/len(good_r)*100)
print("Average percentage of Junk queries: ", sum(junk_r)/len(junk_r)*100)
print("Average percentage of ok queries: ", sum(ok_r)/len(ok_r)*100)

