Starship Detection
[Getting Started Notebook] Starship Detection
A Getting Started notebook for Starship Detection Puzzle of BlitzX.
Starter Code for Starship Detection
What we are going to Learn¶
- Basic Image Preprocessing with OpenCV
- Fixing Contours on an image and getting bounding boxes.
- Testing and Submitting the Results to the Challenge.
Note : Create a copy of the notebook and use the copy for submission. Go to File > Save a Copy in Drive to create a new copy
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.
!pip install aicrowd-cli
%load_ext aicrowd.magic
%aicrowd login
# Downloading the Dataset
!rm -rf data
!mkdir data
%aicrowd ds dl -c starship-detection -o data
!unzip data/data.zip -d /content/images >> /dev/null
Importing Libraries¶
# 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 some opencv functions which can help us detecting the starship body!
# Reading a Sample Image
img = Image.open("images/2.jpg")
img
# Converting the image to numpy array
np_img = np.array(img)
np_img.shape
# Converting the Image to RGB to Grayscale ( black/white )
gray = cv2.cvtColor(np_img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)
cv2.Canny
¶
Now cv2.Canny
is Canny Edge Detection which helps us to detect edges in the image. Let's try it our on the starship
canny = cv2.Canny(gray, 10, 15)
plt.imshow(canny)
So as you can see the function detected multiple edges including the starship boly and some noise too, there are many ways to reduce that noise, however you can try changing the parameters in the function and see where that leads.
Countours¶
Contours are lines joining along the bounding of a intensity or color in an image. In the canny image or the original image, we see that the starship has much different color as compared to the sky.
# Finding contours in the image
contours, hierarchy = cv2.findContours(canny,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Sorting the contours in ascending order
contours = sorted(contours, key=cv2.contourArea)
# Getting the bounding boxes of the biggest contours
x,y,w,h = list(cv2.boundingRect(contours[-1]))
x,y,w,h
# Showing the contour
draw_img = img.copy()
draw = ImageDraw.Draw(draw_img)
draw.rectangle([x, y, x+w, y+h], outline ="red")
draw_img
So as you can see, fnding contours did a pretty great job in finfing the starship body. However there are some mistakes with the image in such as the more right side of the starship body in left unchecked.
Submitting Results 📄¶
# Getting all the testing images
test_imgs = natsorted(os.listdir("images"))
print(len(test_imgs))
# Function to generate the bounding boxes
def gen_bounding_boxes(img):
# Converting the image to numpy array
img = np.array(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Getting the edges
canny = cv2.Canny(gray, 100, 150)
# Getting the contours
contours, hierarchy = cv2.findContours(canny,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Sorting the contours
contours = sorted(contours, key=cv2.contourArea)
try:
# Return the boundong boxes of the biggest contour
x,y,w,h = list(cv2.boundingRect(contours[-1]))
# Incase no countous found
except:
x,y,w,h = [1, 1, 1, 1]
return x,y,w,h
bboxes = []
image_ids = []
# Ground through each test image
for img_name in tqdm(test_imgs):
# Reading the test image
img = Image.open(os.path.join("images", img_name))
# Generating the bounding boxes
x,y,w,h = gen_bounding_boxes(img)
# Adding the boundong boxes and image id
bboxes.append([x,y,w,h])
image_ids.append(int(img_name.split(".")[0]))
# Adding the image id and bounding boxes to a dataframe
df = pd.DataFrame({"ImageID":image_ids, "bbox":bboxes})
df = df.sort_values("ImageID").reset_index(drop=True)
df
!rm -rf assets
!mkdir assets
df.to_csv(os.path.join("assets", "submission.csv"), index=False)
Note : Please make sure that there should be filename submission.csv
in assets
folder before submitting it
Uploading the Results¶
!aicrowd notebook submit -c starship-detection -a assets --no-verify
Content
Comments
You must login before you can post a comment.