|
| 1 | +import os |
| 2 | +import colorsys |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from keras import backend as K |
| 6 | +from keras.models import load_model |
| 7 | +from keras.layers import Input |
| 8 | + |
| 9 | +from yolo4_model.model import yolo_eval, yolo4_body |
| 10 | +from yolo4_model.utils import letterbox_image |
| 11 | + |
| 12 | +class Yolo4(object): |
| 13 | + def get_class(self): |
| 14 | + classes_path = os.path.expanduser(self.classes_path) |
| 15 | + with open(classes_path) as f: |
| 16 | + class_names = f.readlines() |
| 17 | + class_names = [c.strip() for c in class_names] |
| 18 | + return class_names |
| 19 | + |
| 20 | + def get_anchors(self): |
| 21 | + anchors_path = os.path.expanduser(self.anchors_path) |
| 22 | + with open(anchors_path) as f: |
| 23 | + anchors = f.readline() |
| 24 | + anchors = [float(x) for x in anchors.split(',')] |
| 25 | + return np.array(anchors).reshape(-1, 2) |
| 26 | + |
| 27 | + def load_yolo(self): |
| 28 | + model_path = os.path.expanduser(self.model_path) |
| 29 | + assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.' |
| 30 | + |
| 31 | + self.class_names = self.get_class() |
| 32 | + self.anchors = self.get_anchors() |
| 33 | + |
| 34 | + num_anchors = len(self.anchors) |
| 35 | + num_classes = len(self.class_names) |
| 36 | + |
| 37 | + # Generate colors for drawing bounding boxes. |
| 38 | + hsv_tuples = [(x / len(self.class_names), 1., 1.) |
| 39 | + for x in range(len(self.class_names))] |
| 40 | + self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) |
| 41 | + self.colors = list( |
| 42 | + map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), |
| 43 | + self.colors)) |
| 44 | + |
| 45 | + self.sess = K.get_session() |
| 46 | + |
| 47 | + # Load model, or construct model and load weights. |
| 48 | + self.yolo4_model = yolo4_body(Input(shape=(608, 608, 3)), num_anchors//3, num_classes) |
| 49 | + self.yolo4_model.load_weights(model_path) |
| 50 | + |
| 51 | + print('{} model, anchors, and classes loaded.'.format(model_path)) |
| 52 | + |
| 53 | + if self.gpu_num>=2: |
| 54 | + self.yolo4_model = multi_gpu_model(self.yolo4_model, gpus=self.gpu_num) |
| 55 | + |
| 56 | + self.input_image_shape = K.placeholder(shape=(2, )) |
| 57 | + self.boxes, self.scores, self.classes = yolo_eval(self.yolo4_model.output, self.anchors, |
| 58 | + len(self.class_names), self.input_image_shape, |
| 59 | + score_threshold=self.score) |
| 60 | + |
| 61 | + def __init__(self, score, iou, anchors_path, classes_path, model_path, gpu_num=1): |
| 62 | + self.score = score |
| 63 | + self.iou = iou |
| 64 | + self.anchors_path = anchors_path |
| 65 | + self.classes_path = classes_path |
| 66 | + self.model_path = model_path |
| 67 | + self.gpu_num = gpu_num |
| 68 | + self.load_yolo() |
| 69 | + |
| 70 | + def close_session(self): |
| 71 | + self.sess.close() |
| 72 | + |
| 73 | + def detect_image(self, image, model_image_size=(608, 608)): |
| 74 | + start = timer() |
| 75 | + |
| 76 | + boxed_image = letterbox_image(image, tuple(reversed(model_image_size))) |
| 77 | + image_data = np.array(boxed_image, dtype='float32') |
| 78 | + |
| 79 | + |
| 80 | + image_data /= 255. |
| 81 | + image_data = np.expand_dims(image_data, 0) # Add batch dimension. |
| 82 | + |
| 83 | + out_boxes, out_scores, out_classes = self.sess.run( |
| 84 | + [self.boxes, self.scores, self.classes], |
| 85 | + feed_dict={ |
| 86 | + self.yolo4_model.input: image_data, |
| 87 | + self.input_image_shape: [image.size[1], image.size[0]], |
| 88 | + K.learning_phase(): 0 |
| 89 | + }) |
| 90 | + |
| 91 | + print('Found {} boxes for {}'.format(len(out_boxes), 'img')) |
| 92 | + |
| 93 | + #font = ImageFont.truetype(font='font/FiraMono-Medium.otf', |
| 94 | + # size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) |
| 95 | + #thickness = (image.size[0] + image.size[1]) // 300 |
| 96 | + cars = [] |
| 97 | + for i, c in reversed(list(enumerate(out_classes))): |
| 98 | + predicted_class = self.class_names[c] |
| 99 | + |
| 100 | + if "car" in predicted_class: |
| 101 | + car = { |
| 102 | + "box": out_boxes[i], |
| 103 | + "score": out_scores[i] |
| 104 | + } |
| 105 | + cars.append(car) |
| 106 | + return cars |
| 107 | + ''' |
| 108 | + label = '{} {:.2f}'.format(predicted_class, score) |
| 109 | + draw = ImageDraw.Draw(image) |
| 110 | + label_size = draw.textsize(label, font) |
| 111 | + |
| 112 | + top, left, bottom, right = box |
| 113 | + top = max(0, np.floor(top + 0.5).astype('int32')) |
| 114 | + left = max(0, np.floor(left + 0.5).astype('int32')) |
| 115 | + bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) |
| 116 | + right = min(image.size[0], np.floor(right + 0.5).astype('int32')) |
| 117 | + print(label, (left, top), (right, bottom)) |
| 118 | + |
| 119 | + if top - label_size[1] >= 0: |
| 120 | + text_origin = np.array([left, top - label_size[1]]) |
| 121 | + else: |
| 122 | + text_origin = np.array([left, top + 1]) |
| 123 | + |
| 124 | + # My kingdom for a good redistributable image drawing library. |
| 125 | + for i in range(thickness): |
| 126 | + draw.rectangle( |
| 127 | + [left + i, top + i, right - i, bottom - i], |
| 128 | + outline=self.colors[c]) |
| 129 | + draw.rectangle( |
| 130 | + [tuple(text_origin), tuple(text_origin + label_size)], |
| 131 | + fill=self.colors[c]) |
| 132 | + draw.text(text_origin, label, fill=(0, 0, 0), font=font) |
| 133 | + del draw |
| 134 | + ''' |
0 commit comments