def detect_big_image(self, image: np.ndarray): """检测大图像""" start = timer() assert self.model_image_size[0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[1] % 32 == 0, 'Multiples of 32 required' # reversed: 反向迭代器, 默认输入为hw, 要转化为wh size = segmentation(image, self.model_image_size) H, W, _ = image.shape all_box, all_score, all_classes = [], [], [] print(image.shape) for t in size: img = image[t[1]:t[3], t[0]:t[2]] boxed_image = letterbox_image( img, tuple(reversed(self.model_image_size))) image_data = boxed_image.astype('float32') image_h, image_w, _ = image_data.shape image_data /= 255.0 image_data = np.expand_dims(image_data, 0) out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, # 替换图中的某个tensor的值 self.input_image_shape: [image_w, image_h], # learning_phase, 学习阶段标志是一个布尔张量(0 = test,1 = train) K.learning_phase(): 0 }) out_boxes[..., 0] += t[0] out_boxes[..., 1] += t[1] all_box.append(out_boxes) all_score.append(out_scores) all_classes.append(out_classes) out_boxes = np.concatenate(all_box) out_scores = np.concatenate(all_score) out_classes = np.concatenate(all_classes) out_boxes = wh2xy(out_boxes) keep = nms(out_boxes, out_scores, self.iou) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) out_boxes = np.floor(out_boxes[keep]).astype(np.int) # [N, 5] out_scores = out_scores[keep] # [N,] out_classes = out_classes[keep] # [N,] out_boxes[..., 0:2][out_boxes[..., 0:2] < 0] = 0 out_boxes[..., 2:3][out_boxes[..., 2:3] > (W - 1)] = W - 1 out_boxes[..., 3:4][out_boxes[..., 3:4] > (H - 1)] = H - 1 image = draw_box(image, out_boxes, out_scores, out_classes, self.colors, self.class_names) end = timer() print('time: ', end - start) return image
def detect_image(self, image, score_threshol=0.5, iou_threshold=None): iou_threshold = iou_threshold or self.iou_threshold image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) boxed_image = letterbox_image(image, self.model_image_size) # 输入图像转成 识别大小 boxed_image = boxed_image[np.newaxis, ...].astype(np.float32) out_boxes, out_scores, out_classes = self.eval_img(self.model(boxed_image), image.shape[:2], score_threshol, iou_threshold) # 卷积识别 image = self._draw_rectangle(image, out_boxes, out_scores, out_classes) # 画识别方框 image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) return image
def _main(): # TODO: 定义路径 model_path = "logs/yolov3-model-1.h5" assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.' anchors_path = "model/yolo_anchors.txt" classes_path = "model/voc_classes.txt" test_path = "images/test1.jpg" output_path = "images/test1_out.jpg" anchors = get_anchors(anchors_path) class_names = get_classes(classes_path) num_classes = len(class_names) num_anchors = len(anchors) # 构建模型 image_input = Input(shape=(None, None, 3)) model = yolo_body(image_input, num_anchors // 3, num_classes) model.load_weights(model_path) model.summary() # 给每一个类定义一个颜色 hsv_tuples = [(x / num_classes, 1., 1.) for x in range(num_classes)] colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors)) random.seed(10101) # Fixed seed for consistent colors across runs. random.shuffle(colors) # Shuffle colors to decorrelate adjacent classes. random.seed(None) # Reset seed to default. # TODO: 加载图片 image = Image.open(test_path) print(image.size) # 按照原尺寸缩放图像,空余的地方留灰 boxed_image = letterbox_image(image, (416, 416)) image_data = np.array(boxed_image, dtype='float32') image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. # 推理 y = model.predict(image_data, batch_size=1) boxes_, scores_, classes_ = yolo_eval(y, anchors, num_classes, (image.size[1], image.size[0])) image = cv2.imread(test_path) for i, box in enumerate(boxes_): cv2.rectangle(image, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), colors[classes_[i]]) cv2.putText(image, class_names[classes_[i]], (int(box[0]), int(box[1])), 1, 1, colors[classes_[i]], 1) cv2.imshow('image', image) cv2.imwrite(output_path, image) cv2.waitKey(0)
def detect_image(self, image: np.ndarray): """检测图像""" start = timer() image_h, image_w = image.shape[0:2] assert self.model_image_size[0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[1] % 32 == 0, 'Multiples of 32 required' # reversed: 反向迭代器, 默认输入为hw, 要转化为wh boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size))) image_data = boxed_image.astype('float32') print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. # run run run out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, # 替换图中的某个tensor的值 self.input_image_shape: [image_w, image_h], # learning_phase, 学习阶段标志是一个布尔张量(0 = test,1 = train) K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) out_boxes = wh2xy(out_boxes) keep = nms(out_boxes, out_scores, self.iou) # box中为角度 out_boxes = out_boxes[keep] # [N, 5] out_scores = out_scores[keep] # [N,] out_classes = out_classes[keep] # [N,] print('Found {} boxes for {}'.format(len(out_boxes), 'img')) out_boxes = np.floor(out_boxes).astype(np.int) # draw image = draw_box(image, out_boxes, out_scores, out_classes, self.colors, self.class_names) end = timer() print('time: ', end - start) return image
def create_new_img(self, annotation): line = annotation.split() image_path = line[0] if not path.exists(image_path): raise KeyError("%s does not exist ... " % image_path) image = cv2.imread(image_path) bboxes = np.array([list(map(int, box.split(','))) for box in line[1:]]) image, bboxes = self.random_horizontal_flip(np.copy(image), np.copy(bboxes)) image, bboxes = self.random_crop(np.copy(image), np.copy(bboxes)) image, bboxes = self.random_translate(np.copy(image), np.copy(bboxes)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_paded, gt_boxes = letterbox_image( image, [self.train_input_shape, self.train_input_shape], gt_boxes=bboxes) return image_paded, gt_boxes
def detect_image(self, image): start = timer() if self.model_image_size != (None, None): assert self.model_image_size[ 0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[ 1] % 32 == 0, 'Multiples of 32 required' boxed_image = letterbox_image( image, tuple(reversed(self.model_image_size))) else: new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image = letterbox_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) font = ImageFont.truetype(font='font/FiraMono-Medium.otf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 300 for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) print(label, (left, top), (right, bottom)) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw end = timer() print(end - start) return image
def detect_image(self, image, draw_box=False, convert_dr=False): start = timer() if self.model_image_shape != (None, None): assert self.model_image_shape[ 0] % 32 == 0, 'Multiples of 32 required.' # height assert self.model_image_shape[ 1] % 32 == 0, 'Multiples of 32 required.' # width boxed_image = letterbox_image(image, self.model_image_shape) else: new_image_size = (image.height - (image.height % 32), image.width - (image.width % 32)) boxed_image = letterbox_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') image_data /= 255. # ndarray, 0 to 1 # print('model detector size:', image_data.shape) # Expand batch dim, shape=(1, height, width, RGB). image_data = np.expand_dims(image_data, 0) # Feed test image to the model. with self.sess.as_default(): with self.graph.as_default(): out_boxes, out_scores, out_classes = \ self.sess.run(fetches=[self.boxes, self.scores, self.classes], feed_dict={self.yolo_model.input: image_data, self.original_image_shape: [image.height, image.width], K.learning_phase(): 0}) # print('Found {} boxes from image.'.format(len(out_boxes))) # Draw detect boxes, classes, and scores. font_s = ImageFont.truetype(font='./font/FiraMono-Medium.otf', size=np.floor(1.25e-2 * image.height + 0.5).astype('int32')) font_m = ImageFont.truetype(font='./font/FiraMono-Medium.otf', size=np.floor(2e-2 * image.height + 0.5).astype('int32')) thickness = (image.width + image.height) // 512 boxes_l = [] info_l = [] # c_x c_y x_min x_max w h class for i, c in list(enumerate(out_classes)): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] top, left, bottom, right = box # y_min, x_min, y_max, x_max top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.height, np.floor(bottom + 0.5).astype('int32')) right = min(image.width, np.floor(right + 0.5).astype('int32')) c_x = int((left + right) // 2) c_y = int((top + bottom) // 2) w = right - left h = bottom - top info_l.append('{} {} {} {} {} {} {}'.format( c_x, c_y, left, top, w, h, c)) if draw_box: label = '{} {:.2f}'.format(predicted_class, score) # print(label, (left, top), (right, bottom)) font = font_s if (right - left) < 80 else font_m draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) # test_w, text_h if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. for p in range(thickness): # box draw.rectangle([left + p, top + p, right - p, bottom - p], outline=self.colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) # text background draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw if convert_dr: box_l = [ predicted_class, np.around(score, 6), left, top, right, bottom ] box_l = list(map(str, box_l)) boxes_l.append(' '.join(box_l)) end = timer() detect_time = end - start if not convert_dr: return image, detect_time, info_l assert len(out_boxes) == len(boxes_l), 'Detect boxes miss.' return image, detect_time, '\n'.join(boxes_l)
def detect_image(self, image, file, filename): start = timer() if self.model_image_size != (None, None): assert self.model_image_size[ 0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[ 1] % 32 == 0, 'Multiples of 32 required' boxed_image = letterbox_image( image, tuple(reversed(self.model_image_size))) else: new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image = letterbox_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') # print(image_data.shape) # (416, 416, 3)----全部输出值 image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) # print('Found {} boxes for {}'.format(len(out_boxes), 'img')) print('{} : {} boxes'.format(filename, len(out_boxes))) draw = ImageDraw.Draw(image) draw.text((0, 0), str(len(out_boxes)), fill=(255, 0, 0)) if (len(out_boxes) < 10): draw.text((100, 0), '0', fill=(255, 0, 0)) elif (len(out_boxes) < 25): draw.text((100, 0), '1', fill=(255, 0, 0)) elif (len(out_boxes) < 40): draw.text((100, 0), '2', fill=(255, 0, 0)) else: draw.text((100, 0), '3', fill=(255, 0, 0)) del draw font = ImageFont.truetype(font='font/FiraMono-Medium.otf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 300 # file.write('find '+str(len(out_boxes))+' target(s) \n') passanger_num = 0 for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) passanger_num += 1 print("------------------------->", label, (left, top), (right, bottom), passanger_num) # file.write(predicted_class+' score: '+str(score)+' \nlocation: top: '+str(top)+'、 bottom: '+str(bottom)+'、 left: '+str(left)+'、 right: '+str(right)+'\n') file.write(predicted_class + ' ' + str(score) + ' ' + str(left) + ' ' + str(top) + ' ' + str(right) + ' ' + str(bottom) + ';') if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw end = timer() # print(end - start) # 检测时间没有必要输出 return image