CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C4 - Convolutional Neural Networks/Week 3/Car detection for Autonomous Driving/yolo_utils.py
Views: 4818
1
import colorsys
2
import imghdr
3
import os
4
import random
5
from keras import backend as K
6
7
import numpy as np
8
from PIL import Image, ImageDraw, ImageFont
9
10
def read_classes(classes_path):
11
with open(classes_path) as f:
12
class_names = f.readlines()
13
class_names = [c.strip() for c in class_names]
14
return class_names
15
16
def read_anchors(anchors_path):
17
with open(anchors_path) as f:
18
anchors = f.readline()
19
anchors = [float(x) for x in anchors.split(',')]
20
anchors = np.array(anchors).reshape(-1, 2)
21
return anchors
22
23
def generate_colors(class_names):
24
hsv_tuples = [(x / len(class_names), 1., 1.) for x in range(len(class_names))]
25
colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
26
colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))
27
random.seed(10101) # Fixed seed for consistent colors across runs.
28
random.shuffle(colors) # Shuffle colors to decorrelate adjacent classes.
29
random.seed(None) # Reset seed to default.
30
return colors
31
32
def scale_boxes(boxes, image_shape):
33
""" Scales the predicted boxes in order to be drawable on the image"""
34
height = image_shape[0]
35
width = image_shape[1]
36
image_dims = K.stack([height, width, height, width])
37
image_dims = K.reshape(image_dims, [1, 4])
38
boxes = boxes * image_dims
39
return boxes
40
41
def preprocess_image(img_path, model_image_size):
42
image_type = imghdr.what(img_path)
43
image = Image.open(img_path)
44
resized_image = image.resize(tuple(reversed(model_image_size)), Image.BICUBIC)
45
image_data = np.array(resized_image, dtype='float32')
46
image_data /= 255.
47
image_data = np.expand_dims(image_data, 0) # Add batch dimension.
48
return image, image_data
49
50
def draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors):
51
52
font = ImageFont.truetype(font='font/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
53
thickness = (image.size[0] + image.size[1]) // 300
54
55
for i, c in reversed(list(enumerate(out_classes))):
56
predicted_class = class_names[c]
57
box = out_boxes[i]
58
score = out_scores[i]
59
60
label = '{} {:.2f}'.format(predicted_class, score)
61
62
draw = ImageDraw.Draw(image)
63
label_size = draw.textsize(label, font)
64
65
top, left, bottom, right = box
66
top = max(0, np.floor(top + 0.5).astype('int32'))
67
left = max(0, np.floor(left + 0.5).astype('int32'))
68
bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
69
right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
70
print(label, (left, top), (right, bottom))
71
72
if top - label_size[1] >= 0:
73
text_origin = np.array([left, top - label_size[1]])
74
else:
75
text_origin = np.array([left, top + 1])
76
77
# My kingdom for a good redistributable image drawing library.
78
for i in range(thickness):
79
draw.rectangle([left + i, top + i, right - i, bottom - i], outline=colors[c])
80
draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=colors[c])
81
draw.text(text_origin, label, fill=(0, 0, 0), font=font)
82
del draw
83