Loading

AI Blitz X

Template matching with opencv

Template matching

sai_bhargav

Template matching technique using opencv

Starship Detection using opencv template matching


designbanner.jpg

Setting up Environment

Downloading Dataset

So we will first need to download the python library by AIcrowd that will allow us to download the dataset by just inputting the API key.

In [1]:
!pip install aicrowd-cli

%load_ext aicrowd.magic
Requirement already satisfied: aicrowd-cli in /usr/local/lib/python3.7/dist-packages (0.1.9)
Requirement already satisfied: toml<1,>=0.10.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (0.10.2)
Requirement already satisfied: GitPython==3.1.18 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (3.1.18)
Requirement already satisfied: requests-toolbelt<1,>=0.9.1 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (0.9.1)
Requirement already satisfied: tqdm<5,>=4.56.0 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (4.62.0)
Requirement already satisfied: click<8,>=7.1.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (7.1.2)
Requirement already satisfied: rich<11,>=10.0.0 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (10.7.0)
Requirement already satisfied: requests<3,>=2.25.1 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (2.26.0)
Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.7/dist-packages (from GitPython==3.1.18->aicrowd-cli) (4.0.7)
Requirement already satisfied: typing-extensions>=3.7.4.0 in /usr/local/lib/python3.7/dist-packages (from GitPython==3.1.18->aicrowd-cli) (3.7.4.3)
Requirement already satisfied: smmap<5,>=3.0.1 in /usr/local/lib/python3.7/dist-packages (from gitdb<5,>=4.0.1->GitPython==3.1.18->aicrowd-cli) (4.0.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (1.24.3)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2021.5.30)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.0.2)
Requirement already satisfied: colorama<0.5.0,>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (0.4.4)
Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (0.9.1)
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (2.6.1)
In [2]:
%aicrowd login
Please login here: https://api.aicrowd.com/auth/szUjUbj9bDwPHmNU6IK_cb83eI3HdhVdYIC8MKxv2hQ
API Key valid
Saved API Key successfully!
In [3]:
# Downloading the Dataset
!rm -rf data
!mkdir data
%aicrowd ds dl -c starship-detection -o data
In [4]:
!unzip data/data.zip -d /content/images >> /dev/null
replace /content/images/0.jpg? [y]es, [n]o, [A]ll, [N]one, [r]ename: A

Importing Libraries

In [5]:
# Image Reading & Preprocessing
from PIL import Image, ImageDraw
import cv2
import matplotlib.pyplot as plt
import numpy as np

# Misc.
import pandas as pd
from tqdm.notebook import tqdm
import os
from natsort import natsorted

Image Preprocessing

In this section we are going to learn template matching opencv function which can help us detecting the starship body!

In [6]:
from google.colab.patches import cv2_imshow

img = cv2.imread("images/2.jpg",0)
cv2_imshow(img)
In [7]:
cv2_imshow(cv2.rectangle(img.copy(), (245,210), (290,320),(255, 0, 0),2))
In [8]:
template = img[210:320,245:290]

Template matching with threshold value of 95% similarity

In [28]:
#refer this link for other methods availabl
w,h = template.shape[::-1]
res = cv2.matchTemplate(img.copy(),template,eval('cv2.TM_CCOEFF_NORMED'))
#print(res)
loc = np.where( res >= 0.95)
print(len(list(zip(*loc[::-1]))))
for pt in zip(*loc[::-1]):
    result = cv2.rectangle(img.copy(), pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
    cv2_imshow(result)
1

For the instances where is the starship is in horizontal postion we need to take another template for the same.

In [ ]:


Comments

You must login before you can post a comment.

Execute