{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os, json, random, cv2\nimport numpy as np, pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf, re, math\nfrom tqdm import tqdm\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:53.76457Z","iopub.execute_input":"2021-09-21T11:27:53.764928Z","iopub.status.idle":"2021-09-21T11:27:53.769515Z","shell.execute_reply.started":"2021-09-21T11:27:53.764898Z","shell.execute_reply":"2021-09-21T11:27:53.768568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/landmark-recognition-2021/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:53.851972Z","iopub.execute_input":"2021-09-21T11:27:53.852254Z","iopub.status.idle":"2021-09-21T11:27:54.744305Z","shell.execute_reply.started":"2021-09-21T11:27:53.852227Z","shell.execute_reply":"2021-09-21T11:27:54.743212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob \n\ntrain_file_list=glob.glob('../input/landmark-recognition-2021/train')\ntrain_file_list\nmy_train_list=[]\npath='../input/landmark-recognition-2021/train'\nfor file in glob.glob(path):\n    print(file)\n    a=cv2.imread(file)\n    my_train_list.append(a)\nmy_train_list   \n","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:54.745988Z","iopub.execute_input":"2021-09-21T11:27:54.746362Z","iopub.status.idle":"2021-09-21T11:27:54.757167Z","shell.execute_reply.started":"2021-09-21T11:27:54.746325Z","shell.execute_reply":"2021-09-21T11:27:54.755961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_grid5x5(image_array, landmarks):\n    fig = plt.figure(figsize=(15., 15.))\n    grid = ImageGrid(fig, 111,\n                     nrows_ncols=(5, 5),\n                     axes_pad=1)\n    \n    for idx, (ax, im) in enumerate(zip(grid, image_array)):\n        ax.imshow(im)\n        ax.set_title(landmarks[idx])\n        ax.set_xlabel(f'{im.shape}')\n        \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:54.759756Z","iopub.execute_input":"2021-09-21T11:27:54.760186Z","iopub.status.idle":"2021-09-21T11:27:54.765919Z","shell.execute_reply.started":"2021-09-21T11:27:54.76015Z","shell.execute_reply":"2021-09-21T11:27:54.764985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_img_path(img_id):\n    return \"/\".join([char for char in img_id[:3]]) + \"/\" + img_id + \".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:54.767795Z","iopub.execute_input":"2021-09-21T11:27:54.768417Z","iopub.status.idle":"2021-09-21T11:27:54.775226Z","shell.execute_reply.started":"2021-09-21T11:27:54.768382Z","shell.execute_reply":"2021-09-21T11:27:54.774328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_numpy(img_id, base=\"/train\"):\n    img_path = make_img_path(img_id)\n    img = Image.open(base + \"/\" + img_path)\n    return np.asarray(img)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:54.776457Z","iopub.execute_input":"2021-09-21T11:27:54.776818Z","iopub.status.idle":"2021-09-21T11:27:54.785347Z","shell.execute_reply.started":"2021-09-21T11:27:54.776784Z","shell.execute_reply":"2021-09-21T11:27:54.784247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mpl_toolkits.axes_grid1 import ImageGrid\nfrom PIL import Image\nimport seaborn as sns\nBASE_PATH = \"../input/landmark-recognition-2021\"\nimg_array_train = [get_img_numpy(img, BASE_PATH + \"/train\") for img in train['id'][500:525]]\n\nimage_grid5x5(img_array_train, [landmark for landmark in train['landmark_id'][500:525]])","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:54.786712Z","iopub.execute_input":"2021-09-21T11:27:54.787087Z","iopub.status.idle":"2021-09-21T11:27:58.470351Z","shell.execute_reply.started":"2021-09-21T11:27:54.78705Z","shell.execute_reply":"2021-09-21T11:27:58.469385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data=train, x=\"landmark_id\", bins=100)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:58.471822Z","iopub.execute_input":"2021-09-21T11:27:58.472143Z","iopub.status.idle":"2021-09-21T11:27:58.960734Z","shell.execute_reply.started":"2021-09-21T11:27:58.472109Z","shell.execute_reply":"2021-09-21T11:27:58.959747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\nplt.title('Training set: number of images per class(line plot)')\nlandmarks_fold = pd.DataFrame(train['landmark_id'].value_counts())\nlandmarks_fold.reset_index(inplace=True)\nlandmarks_fold.columns = ['landmark_id','count']\nax = landmarks_fold['count'].plot(logy=True, grid=True)\nlocs, labels = plt.xticks()\nplt.setp(labels, rotation=30)\nax.set(xlabel=\"Landmarks\", ylabel=\"Number of images\")","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:58.963154Z","iopub.execute_input":"2021-09-21T11:27:58.963682Z","iopub.status.idle":"2021-09-21T11:27:59.480265Z","shell.execute_reply.started":"2021-09-21T11:27:58.963639Z","shell.execute_reply":"2021-09-21T11:27:59.47929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmarks_fold.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:59.481967Z","iopub.execute_input":"2021-09-21T11:27:59.482321Z","iopub.status.idle":"2021-09-21T11:27:59.493371Z","shell.execute_reply.started":"2021-09-21T11:27:59.482283Z","shell.execute_reply":"2021-09-21T11:27:59.492127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**here i am tring to get only the landmark image which has landmark_id 20883 **","metadata":{}},{"cell_type":"code","source":"train = train[train.landmark_id==138982]\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:59.494815Z","iopub.execute_input":"2021-09-21T11:27:59.49516Z","iopub.status.idle":"2021-09-21T11:27:59.510385Z","shell.execute_reply.started":"2021-09-21T11:27:59.495121Z","shell.execute_reply":"2021-09-21T11:27:59.509646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"def get_train_file_path(image_id):\n    return \"../input/landmark-recognition-2021/train/{}/{}/{}/{}.jpg\".format(\n        image_id[0], image_id[1], image_id[2], image_id)\ntrain['file_path'] = train['id'].apply(get_train_file_path)\ntrain.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:27:59.51153Z","iopub.execute_input":"2021-09-21T11:27:59.511868Z","iopub.status.idle":"2021-09-21T11:27:59.528655Z","shell.execute_reply.started":"2021-09-21T11:27:59.511833Z","shell.execute_reply":"2021-09-21T11:27:59.527736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"there are two image in train data set which belong to landmark_id , i get the file path and i am going to plot using the file path ","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(30,20))\nx=1\nfor i in train.file_path[:20409]:\n    image = cv2.imread(i)\n    \n    fig.add_subplot(4, 6, x)\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    x+=1","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:30:26.2819Z","iopub.execute_input":"2021-09-21T11:30:26.282215Z","iopub.status.idle":"2021-09-21T11:30:30.469023Z","shell.execute_reply.started":"2021-09-21T11:30:26.282186Z","shell.execute_reply":"2021-09-21T11:30:30.46742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\\I am now going to extract featears from the image ","metadata":{}},{"cell_type":"code","source":"import skimage \nfrom skimage.metrics import structural_similarity\nimg = cv2.imread(\"../input/landmark-recognition-2021/train/7/2/7/727628e42171abcc.jpg\",cv2.IMREAD_GRAYSCALE)\ncap = cv2.imread('../input/landmark-recognition-2021/train/d/0/2/d020b53cc1a28aa6.jpg',cv2.IMREAD_GRAYSCALE)\n\ndef orb_sim(img1 ,img2):\n    orb=cv2.ORB_create()\n    kp_a, desc_a = orb.detectAndCompute(img1, None)\n    kp_b, desc_b = orb.detectAndCompute(img2, None)\n    \n    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)\n    matches = bf.match(desc_a, desc_b)\n    similar_regions = [i for i in matches if i.distance < 50]  \n    if len(matches) == 0:\n         return 0\n    return len(similar_regions) / len(matches)\n\ndef structural_sim(img1, img2):\n    sim, diff = structural_similarity(img1, img2, full=True)\n    return sim\n\norb_similarity = orb_sim(img,cap)\nprint(\"Similarity using ORB above image is: \", orb_similarity)\nfrom skimage.transform import resize\nimg_gen = resize(cap, (img.shape[0], img.shape[1]), anti_aliasing=True, preserve_range=True)\nssim = structural_sim(img, img_gen) #1.0 means identical. Lower = not similar\nprint(\"Similarity using SSIM for above image is: \", ssim)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:30:38.073083Z","iopub.execute_input":"2021-09-21T11:30:38.073432Z","iopub.status.idle":"2021-09-21T11:30:38.918891Z","shell.execute_reply.started":"2021-09-21T11:30:38.073398Z","shell.execute_reply":"2021-09-21T11:30:38.917925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}