def annotate_image(self, path_to_image, num_gpus=1, min_image_dimension=800, max_image_dimension=1024, steps_per_epoch=100, validation_steps=70): labels_to_names = self._load_labels() model = models.load_model(self._path_to_model, backbone_name='resnet50') # load image image = read_image_bgr(path_to_image) # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() outputs = model.predict_on_batch(np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) boxes = outputs[-4][0] scores = outputs[-3][0] labels = outputs[-2][0] masks = outputs[-1][0] # correct for image scale boxes /= scale # visualize detections for box, score, label, mask in zip(boxes, scores, labels, masks): if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) mask = mask[:, :, label] draw_mask(draw, b, mask, color=label_color(label)) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) plt.figure(figsize=(15, 15)) plt.axis('off') plt.imshow(draw) plt.show() """config = ImageMonkeyConfig(len(labels), num_gpus, min_image_dimension, max_image_dimension, steps_per_epoch, validation_steps)
def detect_marine_objects(image_path, latitude, longtitude): objects_points_detected_so_far = [] backup(image_path, latitude, longtitude) print("backed up image") image = Image.open(image_path).convert('RGB') image_array = im_to_im_array(image) preprocessed_image = preprocess_image(image_array) model = load_debris_model() boxes, scores, labels = model.predict_on_batch(np.expand_dims(preprocessed_image, axis=0)) # image.thumbnail((480, 480), Image.ANTIALIAS) result = {} new_images = {} debris_count = {} result['original'] = encode_image(image.copy()) all_obj_image = image.copy() for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < 0.15: continue color = tuple(label_color(label)) b = box.astype(int) points = {'x1':b[0], 'y1':b[1], 'x2': b[2], 'y2':b[3]} if len(objects_points_detected_so_far)>0: max_overlap = max([get_iou(points,v) for v in objects_points_detected_so_far]) if max_overlap>0.2: continue cls = label if cls not in new_images.keys(): new_images[cls] = image.copy() debris_count[cls]=1 else: debris_count[cls]+=1 draw_bounding_box_on_image(new_images[cls], box,color=color) draw_bounding_box_on_image(all_obj_image, box,color=color) objects_points_detected_so_far.append(points) result['all'] = encode_image(all_obj_image) result['summary'] = {} result['color'] = {} for cls, new_image in new_images.items(): category = label_lookup[cls] result[category] = encode_image(new_image) result['summary'][category] = debris_count[cls] result['color'][category] = tuple(label_color(cls)) result['summary']['all'] = sum(debris_count.values()) # import pdb;pdb.set_trace() # <td><hr style="height:1px;border-top:1px solid {{'#%02x%02x%02x' % result['color']['Buoys'] }}" /></td> #also calculate total number of debris, and counts by type of debris return result
def retinamask(self): backbone = models.backbone('resnet50') batch_size = 1 train_generator, validation_generator = Retinamask.create_generators( batch_size, self.annotations, self.classes) freeze_backbone = 'store_true' weights = self.Input_weights_path print('Creating model, this may take a second...') model, training_model, prediction_model = Retinamask.create_models( backbone_retinanet=backbone.maskrcnn, num_classes=train_generator.num_classes(), weights=weights, freeze_backbone=freeze_backbone) #print(model.summary()) training_model.fit_generator(generator=train_generator, steps_per_epoch=1000, epochs=self.epoch, verbose=1, max_queue_size=1) training_model.save(self.trained_weights_path + 'retinamask.h5') #Testing model_path = self.trained_weights_path + 'retinamask.h5' model = models.load_model(model_path, backbone_name='resnet50') labels_to_names = {0: 'ship'} # load image image = read_image_bgr(test_image_path) # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() outputs = model.predict_on_batch(np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) boxes = outputs[-4][0] scores = outputs[-3][0] labels = outputs[-2][0] masks = outputs[-1][0] # correct for image scale boxes /= scale # visualize detections for box, score, label, mask in zip(boxes, scores, labels, masks): if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) mask = mask[:, :, label] draw_mask(draw, b, mask, color=label_color(label)) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) plt.imsave(self.output_image_path + 'output.jpg', draw)
def visualize_detections(draw, boxes, reco_scores, class_ids): start = time.time() for box, score, label_id in zip(boxes, reco_scores, class_ids): # print(label) # scores are sorted so we can break if score < THRESH_SCORE: break color = label_color(label_id) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(label_to_names[label_id], score) draw_text(draw, caption, pos=(b[0], b[1] - 10)) processing_time = time.time() - start print("processing time to visualize: ", processing_time) # show the output image_rgb plt.figure(figsize=(10, 10)) plt.axis('off') plt.imshow(draw) plt.show(block=False)
def perd_from_model(model, image, th=0.5, box_only=False): # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) # correct for image scale boxes /= scale if box_only: return scores, boxes # visualize detections for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) return draw
def predict(model, class_names, image_file, predict_file): image = read_image_bgr(image_file) draw = image.copy() image = preprocess_image(image) image, scale = resize_image(image) start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) print("File {} Processing Time {:.3f}".format(image_file, time.time() - start)) boxes /= scale for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(class_names[label], score) draw_caption(draw, b, caption) cv2.imwrite(predict_file, draw)
async def detect_image(image_path): # load image image = read_image_bgr(image_path) # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) # correct for image scale boxes /= scale # visualize detections for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) marked_image = cv2.cvtColor(draw, cv2.COLOR_RGB2BGR) marked_image_path = os.path.join( os.path.split(image_path)[0], 'marked-' + os.path.split(image_path)[1]) marked_image_name = os.path.split(marked_image_path)[1] cv2.imwrite(marked_image_path, marked_image) return marked_image_name
def label(model, image_path, save_path, csv_path, label_name=True): # copy to draw on image = read_image_bgr(image_path) draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0)) # correct for image scale boxes /= scale # visualize detections with open(csv_path, 'w') as csv_file: csv_file.write('x1,y1,x2,y2') for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break color = label_color(label) b = box.astype(int) csv_file.write('{},{},{},{}'.format(*b)) draw_box(draw, b, color=color) caption = "{:.3f}".format(score) draw_caption(draw, b, caption) cv.imwrite(save_path, cv.cvtColor(draw, cv.COLOR_RGB2BGR))
def show_image_debug(draw, boxes, scores, labels, masks, classes): from keras_retinanet.utils.visualization import draw_box, draw_caption from keras_maskrcnn.utils.visualization import draw_mask from keras_retinanet.utils.colors import label_color # visualize detections limit_conf = 0.2 for box, score, label, mask in zip(boxes, scores, labels, masks): # scores are sorted so we can break if score < limit_conf: break color = label_color(label) color_mask = (255, 0, 0) b = box.astype(int) draw_box(draw, b, color=color) mask = mask[:, :] draw_mask(draw, b, mask, color=color_mask) caption = "{} {:.3f}".format(classes[label], score) print(caption) draw_caption(draw, b, caption) draw = cv2.cvtColor(draw, cv2.COLOR_RGB2BGR) show_image(draw)
def TestSinglePic(model, image, imgname): # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) image = preprocess_image(image) image, scale = resize_image(image, min_side=800, max_side=800) boxes, scores, labels = model.predict(np.expand_dims(image, axis=0)) boxes /= scale # visualize detections flag = False ret = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): if not flag: top_label = label # scores are sorted so we can break # if flag and score < 0.05 and top_label != label: # if top_label != label: # continue if score < 0.05: continue flag = True color = label_color(label) ret.append((box.astype(int), score, label)) return ret
def predict_video(model, video_path): # load image img_name_list = [] bboxes_list = [] class_list = [] score_list = [] cap = cv2.VideoCapture(video_path) # 保存视频成mp4 fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') sz = (int(cap.get(3)), int(cap.get(4))) fps = int(cap.get(5)) video_out = cv2.VideoWriter() video_out.open('result/output.mp4', fourcc, fps, sz) while (cap.isOpened()): ret, image_BGR = cap.read() draw = image_BGR image = cv2.cvtColor(image_BGR, cv2.COLOR_BGR2RGB) # 不需要 image = np.asarray(image)[:, :, ::-1].copy() # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # 不需要 # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() # print(image.shape) # print(scale) # boxes: 预测的box,scores:预测的概率,labels预测的labels boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) # correct for image scale boxes /= scale i = 0 for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break,概率小于0.5的被舍弃 if score < 0.5: break color = label_color(label) b = box.astype(int) bboxes_list.append(b) class_list.append(labels[0][i]) score_list.append(score) i += 1 draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw = draw_caption(draw, b, caption) video_out.write(draw) cv2.imshow('RetinaNet-test', draw) k = cv2.waitKey(20) #q键退出 if (k & 0xff == ord('q')): break cap.release() video_out.release() cv2.destroyAllWindows()
def process_frame(frame): height, width, channels = frame.shape frame_area = width * height draw = frame.copy() frame = preprocess_image(frame) frame, scale = resize_image(frame) boxes, scores, labels, = MODEL.predict_on_batch( np.expand_dims(frame, axis=0)) boxes /= scale box_json_array = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): b = box.astype(int) box_area = np.abs(b[2] - b[0]) * np.abs(b[3] - b[1]) if score < CONFIDENCE_THRESHOLD or box_area > ( frame_area * BOX_AREA_RATIO_TO_IGNORE) or label > MAX_CLASS_ID: break color = label_color(label) draw_box(draw, b, color=color) caption = '{} {:.3f}'.format(LABELS_TO_NAMES[label], score) draw_caption(draw, b, caption) box_json_array.append({ 'label': caption, 'topLeft': [int(b[0]), int(b[1])], 'bottomRight': [int(b[2]), int(b[3])] }) return draw, box_json_array
def process_frame(frame): height, width, channels = frame.shape frame_area = width * height draw = frame.copy() frame = preprocess_image(frame) frame, scale = resize_image(frame) boxes, scores, labels, = MODEL.predict_on_batch(np.expand_dims(frame, axis=0)) boxes /= scale box_json_array = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): b = box.astype(int) box_area = np.abs(b[2] - b[0]) * np.abs(b[3] - b[1]) if score < CONFIDENCE_THRESHOLD or box_area > (frame_area * BOX_AREA_RATIO_TO_IGNORE) or label > MAX_CLASS_ID: break color = label_color(label) draw_box(draw, b, color=color) caption = '{} {:.3f}'.format(LABELS_TO_NAMES[label], score) draw_caption(draw, b, caption) box_json_array.append({ 'label': caption, 'topLeft': [int(b[0]), int(b[1])], 'bottomRight': [int(b[2]), int(b[3])] }) return draw, box_json_array
def detect(image): draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) image = preprocess_image(image) image, scale = resize_image(image) boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) boxes /= scale for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break # if score < 0.5: if score < 0.4: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) plt.figure(figsize=(15, 15)) plt.axis('off') plt.imshow(draw) plt.show()
def read_images(images_dir,model,lbls,output): images = os.listdir(images_dir) for im in images: base = im im = os.path.join(images_dir,im) image, draw, scale =operate_image(im) # process image start = time.time() boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0)) # correct for image scale boxes /= scale print("processing time: ", time.time() - start) # visualize detections for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break label = label -1 color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(lbls[label], score) draw_caption(draw, b, caption) out = os.path.join(output,base) write_image(out,draw)
def predict(image): # copy to draw on draw = image.copy() # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) # correct for image scale boxes /= scale # visualize detections for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break print("Item ", labels_to_names[label], score) color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) cv2.imshow('Prediction', draw)
def run(): args = parse_args() print(args) if not test_args(args): return keras.backend.tensorflow_backend.set_session(get_session()) label_map = get_labels(args.label) model = models.load_model(args.model, backbone_name=args.backbone) for img_file in args.image: img = read_image_bgr(img_file) canvas = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2RGB) img, scale = resize_image(preprocess_image(img)) start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(img, axis=0)) print("Processing time: ", time.time() - start) boxes /= scale for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < args.threshold: # Labels are sorted break color = label_color(label) b = box.astype(int) draw_box(canvas, b, color=color) caption = "{} {:.3f}".format(label_map[label], score) draw_caption(canvas, b, caption) canvas = cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR) out_file = args.out_prefix + os.path.split(img_file)[1] out_full = os.path.join(args.out_dir, out_file) cv2.imwrite(out_full, canvas) print("Done with writing to file {:s}.".format(out_full))
def visualize_detections(draw, boxes, scores, labels): # load label to names mapping for visualization purposes labels_to_names = { 0: 'arrabida', 1: 'camara', 2: 'clerigos', 3: 'musica', 4: 'serralves' } # visualize detections for box, score, label in zip(boxes[0], scores[0], labels[0]): if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) plt.figure(figsize=(15, 15)) plt.axis('off') plt.imshow(draw) plt.show()
def detect(graph, model, image_paths): label_to_name = {0: 'open', 1: 'closed'} saved = [] for path in image_paths: img = read_image_bgr(path) draw = img.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) img = preprocess_image(img) img, scale = resize_image(img) with graph.as_default(): bs, ss, ls = model.predict_on_batch(np.expand_dims(img, axis=0)) bs /= scale for b, s, l in zip(bs[0], ss[0], ls[0]): if s < 0.5: break color = label_color(l + 5) b = b.astype(int) # pred = gen_prediction(l, _b, s) # result.append(pred) caption = '{} {:.3f}'.format(label_to_name[l], s) draw_box(draw, b, color) draw_caption(draw, b, caption) dest = get_dest_with_fname(path) # thr = Thread(target=save_det, args=(draw, dest)) # thr.start() cv2.imwrite(dest, cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)) saved.append(os.path.basename(dest)) return saved
def plot_pred(boxes, scores, labels, draw): labels_to_names = { 0: 'car', 1: 'articulated_truck', 2: 'bus', 3: 'bicycle', 4: 'motorcycle', 5: 'motorized_vehicle', 6: 'pedestrian', 7: 'single_unit_truck', 8: 'work_van', 9: 'pickup_truck', 10: 'non-motorized_vehicle' } # visualize detections for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) plt.figure(figsize=(15, 15)) plt.axis('off') plt.suptitle("Retina") plt.imshow(draw)
def get_detections(generator, model, out_dir, score_threshold=0.5): all_results = [None for _ in range(generator.num_images)] start = time() print("Processing %i images." % generator.num_images) for i, path, image, draw, scale in generator.load_images(): outputs = model.predict_on_batch(np.expand_dims(image, axis=0)) boxes = outputs[-4][0] scores = outputs[-3][0] labels = outputs[-2][0] masks = outputs[-1][0] boxes /= scale for box, score, label, mask in zip(boxes, scores, labels, masks): if score < score_threshold: break b = box.astype(int) mask = mask[:, :, label] draw_mask(draw, b, mask, color=label_color(label)) out_name = os.path.join(out_dir, path + '.png') cv2.imwrite(out_name, draw) rles = _masks_to_rles(masks, threshold=score_threshold) all_results[i] = (path, boxes, rles) end = time() print("Processed images in %is" % (end - start)) return all_results
def plot_predictions(img: np.ndarray, scaled_boxes: Iterable[Tuple[int, int, int, int]], scores: Iterable[float], labels: Iterable[int], label_dict: Dict, out_figsize: Tuple[int, int], score_cutoff: float = 0.) -> pyplot: draw = img.copy() # TODO check if it is always necessary to convert the color mode ... # is it asuming cv2 or PIL input format?? draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # TODO handle instances when scores has a single element for box, score, label in zip(scaled_boxes, scores, labels): # scores are sorted so we can break if score < score_cutoff: continue color = label_color(int(score // 0.1)) b = np.array(box).astype(int) draw_box(draw, b, color=color) # caption = "{} {:.3f}".format(label_dict[label], score) # draw_caption(draw, b, caption) pyplot.figure(figsize=out_figsize) pyplot.axis('off') pyplot.imshow(draw) return (pyplot)
def drawcube(self, tp, anchors, thresh=0.3): stack = [] cube = self.P.getCube(tp) bar = progressbar.ProgressBar() for z in bar(range(1,cube.shape[2]-2)): # print("Annotating Z:", z) boxes, scores, labels = anchors[z]['boxes'], anchors[z]['scores'], anchors[z]['labels'] draw = cube[:,:,z-1:z+2] draw[:,:,0] = draw[:,:,1] draw[:,:,2] = draw[:,:,1] draw = draw/draw.max() draw = draw*255 draw = draw.astype(np.uint8) draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # boxes /= scale for box, score, label in zip(boxes, scores, labels): # scores are sorted so we can break if score < thresh: break color = label_color(8) b = box.astype(int) draw_box(draw, b, color=color) stack.append(draw) return np.array(stack)
def plot(file_path): image = read_image_bgr(file_path) # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image, 960, 1280) label, score, box = res(file_path) label, score, box = after_processing(label, score, box) for box, score, label in zip(box, score, label): color = label_color(label) b = box draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) print(labels_to_names[label], score) draw_caption(draw, b, caption) plt.figure(figsize=(20, 20)) plt.axis('off') plt.imshow(draw) plt.show()
def process(self, image): draw = image.copy() h, w, _ = draw.shape # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) image = preprocess_image(image) image, scale = resize_image(image) start = time.time() boxes, scores, labels = self.model.predict_on_batch( np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) boxes /= scale # visualize detections centers = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5 or self.labels_to_names[label] not in ['person']: break x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3]) w, h = (x2 - x1), (y2 - y1) centers.append([x1, y1, w, h]) color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(self.labels_to_names[label], score) draw_caption(draw, b, caption) return draw, centers
def main(args=None): args = parse_args(args) load_model(args) cap = cv2.VideoCapture(args.capture) while (cap.isOpened()): ret, frame = cap.read() draw = frame.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) boxes, scores, labels = run_detection_image(model, frame) for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption) # Display the resulting frame resized_image = cv2.resize(frame, (1280, 820)) cv2.imshow('Video', resized_image) if cv2.waitKey(1) & 0xFF == ord('q'): break
def listen(): while True: path = sys.stdin.readline() path = path.split('\n')[0] if path: if path == "stap": break #make a guess path = str(path) image = read_image_bgr(imgloc + "/" + path) image = preprocess_image(image) image, scale = resize_image(image) boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0)) boxes /= scale msg = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break color = label_color(label) b = box.astype(int) msg.append({"topleft":{"x":b[0],"y":b[1]},"bottomright":{"x":b[2],"y":b[3]},"label":labels_to_names[label],"confidence":score}) print("#" + path + "#" + str(msg) + "#") sys.stdout.flush()
def pred_video(video_path): cap = cv2.VideoCapture(video_path) width = cap.get(3) height = cap.get(4) fps = cap.get(5) print((width, height)) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') writer = cv2.VideoWriter() writer.open('./images/output.mp4', fourcc, int(fps), (int(width), int(height))) while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() if ret == True: # 图像copy draw = frame.copy() # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # 输入网络准备 image = preprocess_image(frame) image, scale = resize_image(image) # 图像 inference start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) # 映射到原图 boxes /= scale # 可视化检测结果 for box, score, label in zip(boxes[0], scores[0], labels[0]): # 是排序额score if score < 0.5: break color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(labels_to_names[label], score) draw_caption(draw, b, caption, color=color) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(draw, "Model:RetinaNet GPU:NVIDIA Tesla V100 32GB by XJ", (40, 50), font, 0.6, (0, 0, 255), 2) cv2.imshow('result.jpg', draw) writer.write(draw) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break writer.release() cv2.destroyAllWindows()
def TestSinglePic(model, image, savepath, imgname): # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) image = preprocess_image(image) image, scale = resize_image(image) boxes, scores, labels = model.predict(np.expand_dims(image, axis=0)) boxes /= scale # visualize detections flag = False boxes, scores, labels = boxes_filter(boxes[0], scores[0], labels[0]) # boxes, scores, labels = boxes[0], scores[0], labels[0] for box, score, label in zip(boxes, scores, labels): # scores are sorted so we can break if flag and score < 0.3: continue flag = True label = label.astype(int) color = label_color(label) b = box.astype(int) draw_box(draw, b, color=color) caption = "{} {:.3f}".format(label + 1, score) draw_caption(draw, b, caption) plt.figure(figsize=(15, 15)) plt.axis('off') plt.imshow(draw) plt.savefig(os.path.join(savepath, imgname), bbox_inches='tight') plt.clf()
def draw_bounding_boxes(image, objects): """ Draw the bounding boxes around the objects in the image. :param image: the input image (BGR) :param objects: the list of the objects :return: the image with the bounding boxes around the objects """ # Get thickness of border lines, font and font size x, y = image.shape[0], image.shape[1] thickness = get_thickness(x, y) # Copy to draw on image_draw = image.copy() image_draw = cv2.cvtColor(image_draw, cv2.COLOR_BGR2RGB) # For every object draw the bounding box and the caption for obj in objects: box = obj.box score = obj.score label = obj.label color = label_color(label) draw_box(image_draw, box, color=color, thickness=thickness) caption = "%.1f%%" % (score * 100) # For detection various types of objects can be use following caption # caption = "%s: %.1f%%" % (self.labels_to_names[label], score * 100) draw_caption(image_draw, box, caption, int(thickness*0.75), int(thickness*0.75)) return image_draw
def draw_mask_only(image, box, mask, label=None, color=None, binarize_threshold=0.5): """ Draws a mask in a given box and makes everything else black. Args image : Three dimensional image to draw on. box : Vector of at least 4 values (x1, y1, x2, y2) representing a box in the image. mask : A 2D float mask which will be reshaped to the size of the box, binarized and drawn over the image. color : Color to draw the mask with. If the box has 5 values, the last value is assumed to be the label and used to construct a default color. binarize_threshold : Threshold used for binarizing the mask. """ from keras_retinanet.utils.colors import label_color # import miscellaneous modules import cv2 import numpy as np if label is not None: color = label_color(label) if color is None: color = (255, 255, 255) # resize to fit the box mask = mask.astype(np.float32) mask = cv2.resize(mask, (box[2] - box[0], box[3] - box[1])) # binarize the mask mask = (mask > binarize_threshold).astype(np.uint8) # draw the mask in the image mask_image = np.zeros((image.shape[0], image.shape[1]), np.uint8) mask_image[box[1]:box[3], box[0]:box[2]] = mask mask = mask_image # compute a nice border around the mask border = mask - cv2.erode(mask, np.ones((5, 5), np.uint8), iterations=1) # apply color to the mask and border mask = (np.stack([mask] * 3, axis=2) * color).astype(np.uint8) border = (np.stack([border] * 3, axis=2) * (255, 255, 255)).astype(np.uint8) # this is how you look into the mask # for i in mask: # for j in i: # b = False # for k in i: # for l in k: # if l != 0: # b = True # if b: # break # if b: # break # if b: # print (j) # draw the mask indices = np.where(mask != color) image[indices[0], indices[1], :] = 0 * image[indices[0], indices[1], :] return mask
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) image_paths = list() with open(args.csv_path, 'w', newline='') as csvfile: csv_writer = csv.writer(csvfile, delimiter=',') tree = ET.parse(args.gt_path) # get root element root = tree.getroot() annotation_items = root.findall('./annotation') for annotation_item in annotation_items: filename_item = annotation_item.find('./filename') text = filename_item.text if 'FigureSeparationTraining2016' in args.gt_path: image_path = os.path.join('/datasets/ImageCLEF/2016/training/FigureSeparationTraining2016/', text+'.jpg') elif 'FigureSeparationTest2016GT' in args.gt_path: image_path = os.path.join('/datasets/ImageCLEF/2016/test/FigureSeparationTest2016/', text+'.jpg') else: raise Exception('Error {0}'.format(args.gt_path)) image_paths.append(image_path+"\n") image = read_image_bgr(image_path) draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) csv_path_individual = os.path.join('csv/', text + '.csv') jpg_path_individual = os.path.join('preview/', text + '.jpg') with open(csv_path_individual, 'w', newline='') as csvfile_individual: csv_writer_individual = csv.writer(csvfile_individual, delimiter=',') object_items = annotation_item.findall('./object') for idx, object_item in enumerate(object_items): point_items = object_item.findall('./point') x1 = point_items[0].get('x') y1 = point_items[0].get('y') x2 = point_items[3].get('x') y2 = point_items[3].get('y') if int(x1) >= int(x2) or int(y1) >= int(y2): continue csv_writer.writerow([image_path, x1, y1, x2, y2, 'panel']) csv_writer_individual.writerow([image_path, x1, y1, x2, y2, 'panel']) color = label_color(idx) box = [int(x1), int(y1), int(x2), int(y2)] draw_box(draw, box, color) cv2.imwrite(jpg_path_individual, draw) with open(args.list_path, "w") as text_file: text_file.writelines(image_paths)