8.
Car detection using YOLO
Procedure
1. Open google colabarotary.
2. Import necessary packages like numpy, pandas and CV2.
3. Load the car images dataset from kaggle.(testing, training and training_bounding_boxes).
4. Provide bounding boxes for car image accordingly.
5. Implement data generator to train and test the car images.
6. Display images with grid.
7. Now car object is detected.
.
Program Coding
Data Manipulation
import numpy as np
import pandas as pd
# Visualization/Image Processing
import cv2
import matplotlib.pyplot as plt
# Machine Learning
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Input, BatchNormalization, Flatten, MaxPool2D,
Dense
# Other
from pathlib import Path
train_path = Path("car_detection/data/training_images")
test_path = Path("car_detection/data/testing_images")
train = pd.read_csv("train_solution_bounding_boxes (1).csv")
train[['xmin', 'ymin', 'xmax', 'ymax']] = train[['xmin', 'ymin', 'xmax', 'ymax']].astype(int)
train.drop_duplicates(subset='image', inplace=True, ignore_index=True)
file = pd.read_csv('train_solution_bounding_boxes (1).csv')
file['label'] = 'car'
print(file)
def display_image(img, bbox_coords=[], pred_coords=[], norm=False):
# if the image has been normalized, scale it up
if norm:
img *= 255.
img = img.astype(np.uint8)
# Draw the bounding boxes
if len(bbox_coords) == 4:
xmin, ymin, xmax, ymax = bbox_coords
img = cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0), 3)
if len(pred_coords) == 4:
xmin, ymin, xmax, ymax = pred_coords
img = cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 3)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
def display_image_from_file(name, bbox_coords=[], path=train_path):
img = cv2.imread(str(path/name))
display_image(img, bbox_coords=bbox_coords)
def display_from_dataframe(row, path=train_path):
display_image_from_file(row['image'], bbox_coords=(row.xmin, row.ymin, row.xmax,
row.ymax), path=path)
def display_grid(df=train, n_items=3):
plt.figure(figsize=(20, 10))
# get 3 random entries and plot them in a 1x3 grid
rand_indices = [np.random.randint(0, df.shape[0]) for _ in range(n_items)]
for pos, index in enumerate(rand_indices):
plt.subplot(1, n_items, pos + 1)
display_from_dataframe(df.loc[index, :])
display_image_from_file("vid_4_10520.jpg")
display_grid()
def data_generator(df=train, batch_size=16, path=train_path):
while True:
images = np.zeros((batch_size, 380, 676, 3))
bounding_box_coords = np.zeros((batch_size, 4))
for i in range(batch_size):
rand_index = np.random.randint(0, train.shape[0])
row = df.loc[rand_index, :]
images[i] = cv2.imread(str(train_path/row.image)) / 255.
bounding_box_coords[i] = np.array([row.xmin, row.ymin, row.xmax, row.ymax])
yield {'image': images}, {'coords': bounding_box_coords}
example, label = next(data_generator(batch_size=1))
img = example['image'][0]
bbox_coords = label['coords'][0]
display_image(img, bbox_coords=bbox_coords, norm=True)
OUTPUT