def visualize(self, img, json_data, outfile): """ Visualize detections :param img: image where masks will be drawn :param json_data: json generated by save_json_boxes() :param outfile: output png file """ reverse_map = { val: key for (key, val) in self.__maskrcnn_to_coco.items() } ids = np.array([reverse_map[x['class']] for x in json_data]) scores = np.array([float(x['score']) for x in json_data]) bboxes = [x['bbox'] for x in json_data] masks = [extract_mask(x['mask']) for x in json_data] #Plot data fig = plt.figure() ax = fig.add_subplot(1, 1, 1) out_img = plot_mask(img, masks) plot_bbox(out_img, bboxes, scores, ids, class_names=self.__model.classes, ax=ax) plt.savefig(outfile) plt.close()
def detect(event, context): # get the url data = json.loads(event['body']) if 'url' not in data: response = {"statusCode": 500, "body": "Please specify a url"} return response url = data['url'] # download the image urlSplit = url.split('/') fileName = urlSplit[-1] filePath = wget.download(url, out="/tmp/{0}".format(fileName)) # classify the image x, img = load_test(filePath, short=512) classes, scores, bbox = ssdnet(x) results = [] # for each result, we'll take the each # them if their score is greater than a given threshold for i in range(len(scores[0])): if float(scores[0][i].asnumpy().tolist()[0]) > score_threshold: results.append({ "class": ssdnet.classes[int(classes[0][i].asnumpy().tolist()[0])], "score": float(scores[0][i].asnumpy().tolist()[0]), "bbox": bbox[0][i].asnumpy().tolist() }) # plot the box of the image and then store it in S3 plot_bbox(img, bbox[0], scores[0], classes[0], class_names=ssdnet.classes) tmpOutPath = "/tmp/detect_{0}".format(fileName) plt.savefig(tmpOutPath) s3_key = "images/detect_{0}".format(fileName) s3_bucket_name = "gudongfeng.me" s3.upload_file(tmpOutPath, s3_bucket_name, s3_key) body = { "bounding_boxes": results, "s3_url": getS3Url(s3_bucket_name, s3_key) } response = { "statusCode": 200, "body": json.dumps(body), "headers": { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': True, 'Content-Type': 'application/json' }, } return response
def viz(self, indexes=None): from gluoncv.utils.viz import plot_bbox import matplotlib.pyplot as plt if indexes is None: indexes = range(len(self)) for index in indexes: x = self.at_with_image_path(index) img_path = x[0] img = cv2.imread(img_path)[:, :, ::-1] plot_bbox(img, x[1][:, :4], labels=x[1][:, 4], class_names=self.classes) plt.show()
def plot_example(img, datasets, label, preds, axs, threshold, classes): for i, (dataset, ax) in enumerate(zip(datasets, axs)): rimg = img # sizes = (800, 600) # rimg = cv2.resize(img, dsize=sizes[model], interpolation=cv2.INTER_NEAREST) pred = preds[dataset] ax = viz.plot_bbox(rimg, pred[:, :4], scores=pred[:, 5], labels=pred[:, 4], class_names=classes, ax=ax, thresh=threshold) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if i == 0: bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="b", lw=2) ax.text(20, 60, label, fontdict={ 'weight': 'bold', 'size': 20, 'color': 'black' }, bbox=bbox_props) dataset = val_ds_real
def run(self, img_name, visualize=True, thresh=0.9): ''' User function: Run inference on image and visualize it Args: img_name (str): Relative path to the image file visualize (bool): If True, displays image with predicted bounding boxes and scores thresh (float): Threshold for predicted scores. Scores for objects detected below this score will not be displayed Returns: dict: Contaning IDs, Scores and bounding box locations of predicted objects. ''' x, image = gcv.data.transforms.presets.ssd.load_test( img_name, self.system_dict["img_size"][0]) self.system_dict["local"]["cid"], self.system_dict["local"][ "score"], self.system_dict["local"]["bbox"] = self.system_dict[ "local"]["net"](x.copyto(self.system_dict["local"]["ctx"][0])) tmp = {} tmp["IDs"] = self.system_dict["local"]["cid"] tmp["Scores"] = self.system_dict["local"]["score"] tmp["Boxes"] = self.system_dict["local"]["bbox"] ax = viz.plot_bbox(image, self.system_dict["local"]["bbox"][0], self.system_dict["local"]["score"][0], self.system_dict["local"]["cid"][0], class_names=self.system_dict["classes"], thresh=thresh) if (visualize): plt.show() plt.savefig("output.png") return tmp
def viz_seg(self, indexes=None): from gluoncv.utils.viz import plot_bbox import matplotlib.pyplot as plt if indexes is None: indexes = range(len(self)) for index in indexes: x = self.at_with_image_path(index) img_path = x[0] img = cv2.imread(img_path)[:, :, ::-1] plot_bbox(img, x[1][:, :4], labels=x[1][:, 4], class_names=self.classes) for k in range(len(x[2])): p = plt.Polygon(zip(x[2][k][0][:, 0], x[2][k][0][:, 1]), alpha=.4) plt.gca().add_patch(p) plt.show()
def test_image_bb(file_path): img = mx.image.imread(file_path) print(img) classes = ['mercedes'] # only one foreground class here bb = [615, 1077, 840, 1298] all_boxes = np.array([bb, bb]) print('label:', bb) labels = ["", "", "", ""] # display image and label ax = viz.plot_bbox(img, all_boxes, labels=None, class_names=classes) plt.show()
def test(): #Test classes = ['mercedes'] test_url = 'https://blog.mercedes-benz-passion.com/wp-cb4ef-content/uploads/EQC_SPOT_Mercedes_Antoni_Garage_2019_Spot_EQC_Campaign_EN_DE_11.jpg' download(test_url, 'benz_test.jpg') classes = ['mercedes', 'person', 'car'] net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_custom', classes=classes, pretrained_base=False, ctx=ctx) net.load_parameters('ssd_512_mobilenet1.0_benz100.params') net.hybridize() x, image = gcv.data.transforms.presets.ssd.load_test('benz_test.jpg', 512) cid, score, bbox = net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) plt.show()
def viz_result(img_ORI, det_full_masks, det_bbox, det_id, img_id): CLASSES = [ '1j', '2j', '3j', '4j', '5j', '6j', '7j', '8j', '9j', '10j', '11j', '12j', '13j', '14j', '15j', '16j', '17j' ] train_image = img_ORI.transpose([1, 2, 0]) from matplotlib import pyplot as plt from gluoncv.utils import viz plt.ioff() width, height = train_image.shape[1], train_image.shape[0] train_masks = det_full_masks plt_image = viz.plot_mask(train_image * 255, train_masks) fig = plt.figure(figsize=(5, 10)) ax = fig.add_subplot(1, 1, 1) ax = viz.plot_bbox(plt_image, det_bbox, labels=det_id, class_names=CLASSES, ax=ax) # plt.show() fig.savefig(str(img_id) + '.jpg')
def update_display(self): if self.current_idx < len(self.filelist): x, image = gcv.data.transforms.presets.ssd.load_test(self.filelist[self.current_idx], 512) cid, score, bbox = self.net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names="pot") plt.rcParams['figure.figsize'] = (10,10) plt.savefig("test.png") plt.close() self.img1 = Image.open(self.filelist[self.current_idx]) print(self.filelist[self.current_idx], self.img1) self.render1 = ImageTk.PhotoImage(self.img1) self.image1.configure(image=self.render1) self.image1.image = self.render1 self.image1.update() self.img2 = Image.open("test.png") self.render2 = ImageTk.PhotoImage(self.img2) self.image2.configure(image=self.render2) self.image2.image = self.render2 self.image2.update()
def run(self, img_name, visualize=True, thresh=0.9): x, image = gcv.data.transforms.presets.ssd.load_test( img_name, self.system_dict["img_size"][0]) self.system_dict["local"]["cid"], self.system_dict["local"][ "score"], self.system_dict["local"]["bbox"] = self.system_dict[ "local"]["net"](x.copyto(self.system_dict["local"]["ctx"][0])) tmp = {} tmp["IDs"] = self.system_dict["local"]["cid"] tmp["Scores"] = self.system_dict["local"]["score"] tmp["Boxes"] = self.system_dict["local"]["bbox"] if (visualize): ax = viz.plot_bbox(image, self.system_dict["local"]["bbox"][0], self.system_dict["local"]["score"][0], self.system_dict["local"]["cid"][0], class_names=self.system_dict["classes"], thresh=thresh) plt.show() return tmp
def create_widgets(self): self.save = tk.Button(self) self.save["text"] = "Save" self.save["command"] = self.save_cmd self.save.grid(row=0, column=0) self.next = tk.Button(self) self.next["text"] = "Next" self.next["command"] = self.next_cmd self.next.grid(row=0, column=1) self.prev = tk.Button(self) self.prev["text"] = "Prev" self.prev["command"] = self.prev_cmd self.prev.grid(row=0, column=2) x, image = gcv.data.transforms.presets.ssd.load_test(self.filelist[self.current_idx], 512) cid, score, bbox = self.net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names="pot") plt.rcParams['figure.figsize'] = (10, 10) plt.savefig("test.png") plt.close() self.img1 = Image.open(self.filelist[self.current_idx]) print(self.filelist[self.current_idx], self.img1) self.render1 = ImageTk.PhotoImage(self.img1) self.image1 = Label(self, image=self.render1) self.image1.grid(row=1, column=0) self.img2 = Image.open("test.png") self.render2 = ImageTk.PhotoImage(self.img2) self.image2 = Label(self, image=self.render2) self.image2.grid(row=1, column=2) self.label = Label(self, text="{0:05d}".format(self.current_idx) + "/" + "{0:05d}".format(len(self.filelist))) self.label.grid(row=2, column=0) self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy) self.quit.grid(row=0, column=3)
cids = train_label[:, 4:5] print('image:', train_image.shape) print('bboxes:', bboxes.shape, 'class ids:', cids.shape) # segm is a list of polygons which are arrays of points on the object boundary print('masks', [[poly.shape for poly in polys] for polys in train_segm]) ############################################################################## # Plot the image with boxes and labels: from matplotlib import pyplot as plt from gluoncv.utils import viz fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(1, 1, 1) ax = viz.plot_bbox(train_image, bboxes, labels=cids, class_names=train_dataset.classes, ax=ax) plt.show() ############################################################################## # To actually see the object segmentation, we need to convert polygons to masks import numpy as np from gluoncv.data.transforms import mask as tmask width, height = train_image.shape[1], train_image.shape[0] train_masks = np.stack( [tmask.to_mask(polys, (width, height)) for polys in train_segm]) plt_image = viz.plot_mask(train_image, train_masks) ##############################################################################
train_image, train_label, train_segm = train_dataset[6] bboxes = train_label[:, :4] cids = train_label[:, 4:5] print('image:', train_image.shape) print('bboxes:', bboxes.shape, 'class ids:', cids.shape) # segm is a list of polygons which are arrays of points on the object boundary print('masks', [[poly.shape for poly in polys] for polys in train_segm]) ############################################################################## # Plot the image with boxes and labels: from matplotlib import pyplot as plt from gluoncv.utils import viz fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(1, 1, 1) ax = viz.plot_bbox(train_image, bboxes, labels=cids, class_names=train_dataset.classes, ax=ax) plt.show() ############################################################################## # To actually see the object segmentation, we need to convert polygons to masks import numpy as np from gluoncv.data.transforms import mask as tmask width, height = train_image.shape[1], train_image.shape[0] train_masks = np.stack([tmask.to_mask(polys, (width, height)) for polys in train_segm]) plt_image = viz.plot_mask(train_image, train_masks) ############################################################################## # Now plot the image with boxes, labels and masks fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(1, 1, 1) ax = viz.plot_bbox(plt_image, bboxes, labels=cids, class_names=train_dataset.classes, ax=ax)
classes = [ 'car', 'articulated_truck', 'bus', 'bicycle', 'motorcycle', 'motorized_vehicle', 'pedestrian', 'single_unit_truck', 'work_van', 'pickup_truck', 'non-motorized_vehicle' ] net = gcv.model_zoo.get_model('ssd_512_resnet50_v1_voc', pretrained_base=False) net.reset_class(classes) net.load_parameters( '../models_for_ensemble/epoch_22_ssd_512_resnet50_v1_voc_mio_tcd.params') x, image = gcv.data.transforms.presets.ssd.load_test( '../sample_data/00110281.jpg', 512) cid, score, bbox = net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) plt.show() x, image = gcv.data.transforms.presets.ssd.load_test( '../sample_data/00110288.jpg', 512) cid, score, bbox = net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) plt.show() x, image = gcv.data.transforms.presets.ssd.load_test( '../sample_data/00110306.jpg', 512) cid, score, bbox = net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) plt.show() x, image = gcv.data.transforms.presets.ssd.load_test(
########################################################## # Data transform # -------------- # We can read an image-label pair from the training dataset: train_image, train_label = train_dataset[6] bboxes = train_label[:, :4] cids = train_label[:, 4:5] print('image:', train_image.shape) print('bboxes:', bboxes.shape, 'class ids:', cids.shape) ############################################################################## # Plot the image, together with the bounding box labels: from matplotlib import pyplot as plt from gluoncv.utils import viz ax = viz.plot_bbox(train_image.asnumpy(), bboxes, labels=cids, class_names=train_dataset.classes) plt.show() ############################################################################## # Validation images are quite similar to training because they were # basically split randomly to different sets val_image, val_label = val_dataset[6] bboxes = val_label[:, :4] cids = val_label[:, 4:5] ax = viz.plot_bbox(val_image.asnumpy(), bboxes, labels=cids, class_names=train_dataset.classes) plt.show() ############################################################################## # For Faster-RCNN networks, the only data augmentation is horizontal flip. from gluoncv.data.transforms import presets from gluoncv import utils
def detect(self, image, plot=False): image_tensor, image = gcv.data.transforms.presets.ssd.load_test(image, self.width) labels, scores, bboxes = self.net(image_tensor.as_in_context(self.ctx)) if plot: ax = viz.plot_bbox(image, bboxes[0], scores[0], labels[0], class_names=self.net.classes) plt.show()
# Data transform # -------------- # We can read an image-label pair from the training dataset: train_image, train_label = train_dataset[6] bboxes = train_label[:, :4] cids = train_label[:, 4:5] print('image:', train_image.shape) print('bboxes:', bboxes.shape, 'class ids:', cids.shape) ############################################################################## # Plot the image, together with the bounding box labels: from matplotlib import pyplot as plt from gluoncv.utils import viz ax = viz.plot_bbox(train_image.asnumpy(), bboxes, labels=cids, class_names=train_dataset.classes) plt.show() ############################################################################## # Validation images are quite similar to training because they were # basically split randomly to different sets val_image, val_label = val_dataset[6] bboxes = val_label[:, :4] cids = val_label[:, 4:5] ax = viz.plot_bbox(val_image.asnumpy(), bboxes, labels=cids, class_names=train_dataset.classes) plt.show()
**kwargs) if __name__ == '__main__': classes = ['taxi', 'tax', 'quo', 'general', 'train', 'road', 'plane'] # orientation = ['0','90','180','270'] orientation = ['down', 'left', 'up', 'right'] net = ssd_512_mobilenet1_0_voc(classes=classes) net.load_parameters('ssd_512_mobilenet1.0_pikachu.params') # x, image = gcv.data.transforms.presets.ssd.load_test('./20190722135231_ori.jpg', 512) x, image = gcv.data.transforms.presets.ssd.load_test( './201907121159292.jpg', 512) # x, image = gcv.data.transforms.presets.ssd.load_test('./test.jpg', 512) cid, score, bbox, orien = net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes, thresh=0.3) ax = viz.plot_bbox(image, bbox[0], score[0], orien[0], class_names=orientation, thresh=0.3) plt.show() print(orien[0, :4, 0]) print(cid[0, :4, 0])
""" 使用VIS进行图片显示 """ from matplotlib import pyplot as plt from gluoncv.utils import viz from PIL import Image import numpy as np image = np.array(Image.open("./images/biking.jpg")) print(image.shape) bboxes = np.array([[100, 100, 300, 400]]) cids = np.array([1]) classes = ["person", "cat"] fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(1, 1, 1) ax = viz.plot_bbox(image, bboxes, labels=cids, class_names=classes, ax=ax) plt.show()
# ---------------- # First we will start with a nice Pikachu dataset generated by rendering 3D models on random real-world scenes. # You can refer to :ref:`sphx_glr_build_examples_datasets_detection_custom.py` for tutorial of how to create your own datasets. url = 'https://apache-mxnet.s3-accelerate.amazonaws.com/gluon/dataset/pikachu/train.rec' idx_url = 'https://apache-mxnet.s3-accelerate.amazonaws.com/gluon/dataset/pikachu/train.idx' download(url, path='pikachu_train.rec', overwrite=False) download(idx_url, path='pikachu_train.idx', overwrite=False) ############################################################################################# # We can load dataset using ``RecordFileDetection`` dataset = gcv.data.RecordFileDetection('pikachu_train.rec') classes = ['pikachu'] # only one foreground class here image, label = dataset[0] print('label:', label) # display image and label ax = viz.plot_bbox(image, bboxes=label[:, :4], labels=label[:, 4:5], class_names=classes) plt.show() ############################################################################################# # Pre-trained models # ------------------- # Now we can grab a pre-trained model to finetune from. Here we have so many choices from :ref:`gluoncv-model-zoo-detection` Model Zoo. # Again for demo purpose, we choose a fast SSD network with MobileNet1.0 backbone. net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_voc', pretrained=True) ############################################################################################# # reset network to predict pikachus! net.reset_class(classes) # now the output layers that used to map to VOC classes are now reset to distinguish pikachu (and background).
def test(): x, image = gcv.data.transforms.presets.ssd.load_test('ball.png', 300) cid, score, bbox = net(x) ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names='ball') plt.show()
def page_loop(page_number, beginning_index, predictions, image_folder, pdf, confidence_filter): '''Loop over a single image page of the output pdf.''' row = min(MAX_ROW, int((len(predictions) - beginning_index) / COL + 1)) last_index = min(len(predictions), beginning_index + row * COL) logger.info( "page number: {}, row number: {}, beginning index: {}, last index: {}". format(page_number, row, beginning_index, last_index)) fig, axes = plt.subplots(nrows=row, ncols=COL, figsize=(20, 20), facecolor='white', dpi=100) fig.suptitle('Page {}'.format(page_number), fontsize=24) for i in range(beginning_index, last_index): entry = predictions[i] try: image_path = os.path.join(image_folder, entry['image']) img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB) # Resize image to fit network input # origs = cv2.resize(img, (SHAPE,SHAPE)) origs = img # tensors, origs = yolo_transform.load_test(image_path, short=SHAPE) height, width, _c = origs.shape labels = np.array(entry['prediction']) if (len(entry['prediction']) > 0): cid = labels[:, 0] score = labels[:, 1] bbox = labels[:, range(2, 6)] bbox[:, (0, 2)] *= width bbox[:, (1, 3)] *= height else: cid = [] score = [] bbox = [] img_name = entry['image'].split(".")[0] if row == 1: ax = axes[(i - beginning_index) % COL] else: ax = axes[int((i - beginning_index) / COL), (i - beginning_index) % COL] ax.set_title("{}".format(img_name)) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) axis = viz.plot_bbox(origs, bbox, score, cid, thresh=confidence_filter, class_names=CLASSES, ax=ax) except: logger.error(json.dumps(entry, indent=2)) raise next_page_number = page_number + 1 next_beginning_index = last_index pdf.savefig() plt.close() return next_page_number, next_beginning_index
@property def num_class(self): return len(self.classes) train_dataset = DetectionDataset('train_data') print('class_names:', train_dataset.classes) print('num_images:', len(train_dataset)) sample = train_dataset[0] train_image = sample[0] train_label = sample[1] ax = viz.plot_bbox(train_image.asnumpy(), train_label[:, :4], labels=train_label[:, 4:5], class_names=train_dataset.classes) plt.show() def train_model(train_dataset, epochs=50): ctx = mx.gpu(0) net = gcv.model_zoo.get_model('ssd_512_resnet50_v1_custom', classes=train_dataset.classes, transfer='coco') net.collect_params().reset_ctx(ctx) width, height = 512, 512 # suppose we use 512 as base training size train_transform = gcv.data.transforms.presets.ssd.SSDDefaultTrainTransform( width, height) gcv.utils.random.seed(233)
def imshow(self, index): path, gtboxes = self[index] img = io.imread(path) ax = viz.plot_bbox(img, bboxes=gtboxes, labels = np.zeros(len(gtboxes)), class_names=self.classes) plt.show()
# batch_size, shuffle=False, batchify_fn=batchify_fn, last_batch='keep', num_workers=num_workers) from matplotlib import pyplot as plt from gluoncv.utils import viz for i, batch in enumerate(train_loader): if i > 0: break print('data:', batch[0].shape) print('class targets:', batch[1].shape) print('box targets:', batch[2].shape) data = batch[0][0] img = data.transpose((1, 2, 0)) * nd.array( (0.229, 0.224, 0.225)) + nd.array((0.485, 0.456, 0.406)) # train_image2 = (train_image2 * 255).clip(0, 255) train_image2 = (train_image2 * 255).clip(0, 255) ax = viz.plot_bbox(train_image2.asnumpy(), batch[1][:, :4], labels=batch[2][:, ], class_names=train_dataset.classes) plt.show() # img = data.transpose(1, 2, 0) # img = img.asnumpy() # cv2.imshow("win", img) # cv2.waitKey(0) # train_image, train_label = train_dataset[0] # # train_image2, cids, train_label2 = train_transformed(train_image, train_label) # # train_image2 = train_image2.transpose((1, 2, 0)) * nd.array((0.229, 0.224, 0.225)) + nd.array((0.485, 0.456, 0.406)) # # train_image2 = (train_image2 * 255).clip(0, 255) # cvimg = train_image2.asnumpy() # # # cvimg = cv2.rectangle(cvimg, (train_label2[0][0], train_label2[0][1]), (train_label2[0][2], train_label2[0][3]), (0, 255, 0), 1)
def plot_bbox(img, bbox, score, cid, classes, model): """ plots bounding boxes on the given image """ print("Plotting") ax = viz.plot_bbox(img, bbox, score, cid, thresh=0.5, class_names=classes) plt.suptitle(model) plt.show()
wd = args.wd momentum = args.momentum netName = args.netName gpu_ind = args.gpu_ind path_to_model = args.finetune_model # load dataset from Lst file dataset = gcv.data.LstDetection(LSTpath, root=images_root) print(dataset) image = dataset[0][0] label = dataset[0][1] print('label:', label) # display image and label ax = viz.plot_bbox(image, bboxes=label[:, :4], labels=label[:, 4:5], class_names=classes) plt.savefig('labeled_image.jpg') #initalize model net, input_size = model_zoo.get_model(netName, pretrained=False, classes=classes) if finetune_model == '': net.initialize() net.reset_class(classes) else: net.load_parameters(path_to_model) net.reset_class(classes) print(net)
def test_video(): fourcc = cv2.VideoWriter_fourcc(*"MJPG") video_writer = None classes = ['mercedes', 'person', 'car'] net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_custom', classes=classes, pretrained_base=False, ctx=ctx) net.load_parameters('ssd_512_mobilenet1.0_benz100.params') net.hybridize() #Test video #url ='https://www.youtube.com/watch?v=7iGFH5HbWGM' #'https://github.com/bryanyzhu/tiny-ucf101/raw/master/abseiling_k400.mp4' #video_fname = utils.download(url) vr = VideoReader("test3.mov") duration = len(vr) print('The video contains %d frames' % duration) root_path = "video_rec_tmp/" cap = cv2.VideoCapture("test3.mov") cnt = 0 while (cap.isOpened()): ret, frame = cap.read() imp_path = root_path + str(cnt) + ".jpg" cv2.imwrite(imp_path, frame) cnt += 1 x, image = gcv.data.transforms.presets.ssd.load_test(imp_path, 512) cid, score, bbox = net(x) if (cnt == 500000000): ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) plt.show() if not ret: break #ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) score[0] viz.cv_plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if video_writer is None: height, width, layers = image.shape video_writer = cv2.VideoWriter( "video_rec_tmp/mercedes_rec15min.avi", fourcc, 30, (width, height)) print(video_writer) cv2.imshow('Mercedes Recognition & Track CV', image) print(cnt) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.imwrite(imp_path, image) video_writer.write(image) cap.release() video_writer.release()
def visualize_data(img, gt_box, gt_label, teacher_box, teacher_score, teacher_label, classes): img = inverse_transformation(img) # inverse transformation to get image viz.plot_bbox(img, gt_box, mx.ndarray.ones(gt_box.shape[0]), gt_label, class_names=classes) viz.plot_bbox(img, teacher_box, teacher_score, teacher_label, class_names=classes) plt.show()
# First we will start with a nice Pikachu dataset generated by rendering 3D models on random real-world scenes. # You can refer to :ref:`sphx_glr_build_examples_datasets_detection_custom.py` for tutorial of how to create your own datasets. url = 'https://apache-mxnet.s3-accelerate.amazonaws.com/gluon/dataset/pikachu/train.rec' idx_url = 'https://apache-mxnet.s3-accelerate.amazonaws.com/gluon/dataset/pikachu/train.idx' download(url, path='pikachu_train.rec', overwrite=False) download(idx_url, path='pikachu_train.idx', overwrite=False) ############################################################################################# # We can load dataset using ``RecordFileDetection`` dataset = gcv.data.RecordFileDetection('pikachu_train.rec') classes = ['pikachu'] # only one foreground class here image, label = dataset[0] print('label:', label) # display image and label ax = viz.plot_bbox(image, bboxes=label[:, :4], labels=label[:, 4:5], class_names=classes) plt.show() ############################################################################################# # Pre-trained models # ------------------- # Now we can grab a pre-trained model to finetune from. Here we have so many choices from :ref:`gluoncv-model-zoo-detection` Model Zoo. # Again for demo purpose, we choose a fast SSD network with MobileNet1.0 backbone. net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_voc', pretrained=True) ############################################################################################# # reset network to predict pikachus! net.reset_class(classes) # now the output layers that used to map to VOC classes are now reset to distinguish pikachu (and background).
for data_img, data_label, _ in zip(*batch): ids, scores, bboxes, _ = model(data_img.expand_dims(0)) distil_ids, distil_scores, distil_bboxes, _ = distil_model( data_img.expand_dims(0)) data_label = data_label.expand_dims(0) gt_label = data_label[:, :, 4:5] gt_box = data_label[:, :, :4] # we can change the threshold to display bounding boxes with lower scores # smaller training will result in smaller confidence score img = inverse_transformation( data_img) # inverse transformation to get image viz.plot_bbox(img, gt_box[0], mx.ndarray.ones(gt_box[0].shape[0]), gt_label[0], class_names=dataset.CLASSES) viz.plot_bbox(img, bboxes[0], scores[0], ids[0], class_names=dataset.CLASSES, thresh=0.3) viz.plot_bbox(img, distil_bboxes[0], distil_scores[0], distil_ids[0], class_names=dataset.CLASSES, thresh=0.3) plt.show()