Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C4 - Convolutional Neural Networks/Week 3/Car detection for Autonomous Driving/yolo_utils.py
Views: 4818
import colorsys1import imghdr2import os3import random4from keras import backend as K56import numpy as np7from PIL import Image, ImageDraw, ImageFont89def read_classes(classes_path):10with open(classes_path) as f:11class_names = f.readlines()12class_names = [c.strip() for c in class_names]13return class_names1415def read_anchors(anchors_path):16with open(anchors_path) as f:17anchors = f.readline()18anchors = [float(x) for x in anchors.split(',')]19anchors = np.array(anchors).reshape(-1, 2)20return anchors2122def generate_colors(class_names):23hsv_tuples = [(x / len(class_names), 1., 1.) for x in range(len(class_names))]24colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))25colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))26random.seed(10101) # Fixed seed for consistent colors across runs.27random.shuffle(colors) # Shuffle colors to decorrelate adjacent classes.28random.seed(None) # Reset seed to default.29return colors3031def scale_boxes(boxes, image_shape):32""" Scales the predicted boxes in order to be drawable on the image"""33height = image_shape[0]34width = image_shape[1]35image_dims = K.stack([height, width, height, width])36image_dims = K.reshape(image_dims, [1, 4])37boxes = boxes * image_dims38return boxes3940def preprocess_image(img_path, model_image_size):41image_type = imghdr.what(img_path)42image = Image.open(img_path)43resized_image = image.resize(tuple(reversed(model_image_size)), Image.BICUBIC)44image_data = np.array(resized_image, dtype='float32')45image_data /= 255.46image_data = np.expand_dims(image_data, 0) # Add batch dimension.47return image, image_data4849def draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors):5051font = ImageFont.truetype(font='font/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))52thickness = (image.size[0] + image.size[1]) // 3005354for i, c in reversed(list(enumerate(out_classes))):55predicted_class = class_names[c]56box = out_boxes[i]57score = out_scores[i]5859label = '{} {:.2f}'.format(predicted_class, score)6061draw = ImageDraw.Draw(image)62label_size = draw.textsize(label, font)6364top, left, bottom, right = box65top = max(0, np.floor(top + 0.5).astype('int32'))66left = max(0, np.floor(left + 0.5).astype('int32'))67bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))68right = min(image.size[0], np.floor(right + 0.5).astype('int32'))69print(label, (left, top), (right, bottom))7071if top - label_size[1] >= 0:72text_origin = np.array([left, top - label_size[1]])73else:74text_origin = np.array([left, top + 1])7576# My kingdom for a good redistributable image drawing library.77for i in range(thickness):78draw.rectangle([left + i, top + i, right - i, bottom - i], outline=colors[c])79draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=colors[c])80draw.text(text_origin, label, fill=(0, 0, 0), font=font)81del draw8283