def build_rec_process(img_dir, train=False, num_thread=1): rec_dir = os.path.abspath(os.path.join(img_dir, '../rec')) makedirs(rec_dir) prefix = 'train' if train else 'val' print('Building ImageRecord file for ' + prefix + ' ...') to_path = rec_dir # download lst file and im2rec script script_path = os.path.join(rec_dir, 'im2rec.py') script_url = 'https://raw.githubusercontent.com/apache/incubator-mxnet/master/tools/im2rec.py' download(script_url, script_path) lst_path = os.path.join(rec_dir, prefix + '.lst') lst_url = 'http://data.mxnet.io/models/imagenet/resnet/' + prefix + '.lst' download(lst_url, lst_path) # execution import sys cmd = [ sys.executable, script_path, rec_dir, img_dir, '--recursive', '--pass-through', '--pack-label', '--num-thread', str(num_thread), '--resize', '512' ] subprocess.call(cmd) os.remove(script_path) os.remove(lst_path) print('ImageRecord file for ' + prefix + ' has been built!')
def build_rec_process(img_dir, train=False, num_thread=1): rec_dir = os.path.abspath(os.path.join(img_dir, '../rec')) makedirs(rec_dir) prefix = 'train' if train else 'val' print('Building ImageRecord file for ' + prefix + ' ...') to_path = rec_dir # download lst file and im2rec script script_path = os.path.join(rec_dir, 'im2rec.py') script_url = 'https://raw.githubusercontent.com/apache/incubator-mxnet/master/tools/im2rec.py' download(script_url, script_path) lst_path = os.path.join(rec_dir, prefix + '.lst') lst_url = 'http://data.mxnet.io/models/imagenet/resnet/' + prefix + '.lst' download(lst_url, lst_path) # execution import sys cmd = [ sys.executable, script_path, rec_dir, img_dir, '--recursive', '--pass-through', '--pack-label', '--num-thread', str(num_thread) ] subprocess.call(cmd) os.remove(script_path) os.remove(lst_path) print('ImageRecord file for ' + prefix + ' has been built!')
def download_json(path, overwrite=False): url = 'https://data.dgl.ai/dataset/vg.zip' output = 'vg.zip' download(url, path=path) with zipfile.ZipFile(output) as zf: zf.extractall(path=path) json_path = os.path.join(path, 'vg') json_files = os.listdir(json_path) for fl in json_files: shutil.move(os.path.join(json_path, fl), os.path.join(path, fl)) os.rmdir(json_path)
def get_object_detection(img_object, model="yolo3_darknet53_coco", thresh=0.7, is_limited_classes=False, class_list=[]): ''' //TODO ''' net = model_zoo.get_model(model, pretrained=True) # if is_limited_classes: # net.reset_class(class_list, reuse_weights=class_list) try: img_object = utils.download(img_object) except ValueError: pass if "yolo" in model: x, img = data.transforms.presets.yolo.load_test(img_object, short=512) elif "ssd" in model: x, img = data.transforms.presets.ssd.load_test(img_object, short=512) class_IDs, scores, bounding_boxs = net(x) ax = utils.viz.plot_bbox(img, bounding_boxs[0], scores[0], class_IDs[0], class_names=net.classes, thresh=thresh) return ax
def download_aug(path, overwrite=False): _AUG_DOWNLOAD_URLS = [( "http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz", "7129e0a480c2d6afb02b517bb18ac54283bfaa35", )] makedirs(path) for url, checksum in _AUG_DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: tar.extractall(path=path) shutil.move(os.path.join(path, "benchmark_RELEASE"), os.path.join(path, "VOCaug")) filenames = ["VOCaug/dataset/train.txt", "VOCaug/dataset/val.txt"] # generate trainval.txt with open(os.path.join(path, "VOCaug/dataset/trainval.txt"), "w") as outfile: for fname in filenames: fname = os.path.join(path, fname) with open(fname) as infile: for line in infile: outfile.write(line)
def main(argv): try: opts, args = getopt.getopt(argv, "hp:r:", ["prefix=", "root="]) except getopt.GetoptError: print 'create_rec.py -p <prefix> -r <root>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'create_rec.py -p <prefix> -r <root>' sys.exit() elif opt in ("-p", "--prefix"): prefix = arg elif opt in ("-r", "--root"): root = arg if os.path.isfile('im2rec.py'): print 'Using local im2rec.py' else: print 'Downloading im2rec.py' #im2rec = utils.download('https://raw.githubusercontent.com/apache/incubator-mxnet/' + # '6843914f642c8343aaa9a09db803b6af6f5d94a2/tools/im2rec.py', 'im2rec.py_orig') im2rec = utils.download( 'https://raw.githubusercontent.com/apache/incubator-mxnet/master/tools/im2rec.py', 'im2rec.py_new') subprocess.check_output([ sys.executable, 'im2rec.py', prefix + '_train', root, '--pass-through', '--pack-label' ]) subprocess.check_output([ sys.executable, 'im2rec.py', prefix + '_val', root, '--no-shuffle', '--pass-through', '--pack-label' ])
def is_person(daily): from gluoncv import data, utils import pickle import os try: # https://github.com/dmlc/gluon-cv, pretrained faster_rcnn_resnet50_v1b_voc with open('../static/faster_rcnn_resnet50_v1b_voc.pkl', 'rb') as fp: net = pickle.load(fp) except Exception: import urllib.request urllib.request.urlretrieve( 'https://project-lookmorning.s3.ap-northeast-2.amazonaws.com/faster_rcnn_resnet50_v1b_voc.pkl', 'faster_rcnn_resnet50_v1b_voc.pkl') with open('../static/faster_rcnn_resnet50_v1b_voc.pkl', 'rb') as fp: net = pickle.load(fp) im_fname = utils.download(daily['img_path']) x, orig_img = data.transforms.presets.rcnn.load_test(im_fname) box_ids, scores, bboxes = net(x) labels = box_ids[0].asnumpy() scores = scores[0].asnumpy() for i, bbox in enumerate(bboxes[0]): if labels is not None and labels.flat[i] != 14: # box_id 14 is 'person' continue if scores is not None and scores.flat[i] < 0.7: # Let thresh = 0.7 continue return True # delete downloaded file os.remove(im_fname) return False
def predict_route(): if request.method == 'GET': json_response = {} # check if token is in database image_url = request.args.get('image_url', default = None, type = str) percentage_limit = request.args.get('limit', default = 0.4, type = float) print(f'Requesting for {image_url}') print(f'Limit is {percentage_limit}') try: # download image im_fname = utils.download(image_url, path='image.jpg') except Exception as e: print(f'Could not resolve URL: {image_url}') print(e) return Response(status=400) # identify image results = identify(im_fname, percentage_limit=percentage_limit) for i, result in enumerate(results): json_response[str(i)] = result # remove image after request is completed os.system('rm -rf image.jpg') return jsonify(json_response)
def main(): #img1 = cv.imread('banana.jpg') #img1 = cv.resize(img1, (0,0), fx=0.5, fy=0.5) #orb = cv.ORB_create( # nfeatures=5000, edgeThreshold=20, patchSize=20, scaleFactor=1.3, nlevels=20) #kp1, des1 = orb.detectAndCompute(img1, None) frame_count = 0 while True: #try: # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text #image = frame.array #cv.imshow("Frame", image) key = cv.waitKey(1) & 0xFF # clear the stream in preparation for the next frame #rawCapture.truncate(0) # if the `q` key was pressed, break from the loop if key == ord("q"): break # im_fname = utils.download('http://xnftpi.local/html/cam_pic.php?time=' + str(time.time()) + '&pDelay=40000', im_fname = utils.download('http://xnftpi.local/html/cam_pic.php?time=1552819370977&pDelay=40000', path='frame1.jpeg') get_match_image(image) import os os.remove("/home/quelibrio/Deep Learning/SemanticSegmentation/OcadoHackaton/ComputerVision/frame1.jpeg")
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 main(): name = "Market-1501-v15.09.15" url = "http://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/" + name + ".zip" root = osp.expanduser("~/.mxnet/datasets") if not os.path.exists(root): os.mkdir(root) fpath = osp.join(root, name + '.zip') exdir = osp.join(root, name) if os.path.exists(fpath): if not osp.isdir(exdir): extract(fpath, root) make_list(exdir) else: download(url, fpath, False) extract(fpath, root) make_list(exdir)
def download_ade(path, overwrite=False): _AUG_DOWNLOAD_URLS = [ ('http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip', '219e1696abb36c8ba3a3afe7fb2f4b4606a897c7'), ('http://data.csail.mit.edu/places/ADEchallenge/release_test.zip', 'e05747892219d10e9243933371a497e905a4860c'),] download_dir = os.path.join(path, 'downloads') makedirs(download_dir) for url, checksum in _AUG_DOWNLOAD_URLS: filename = download(url, path=download_dir, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename,"r") as zip_ref: zip_ref.extractall(path=path)
def download_ade(path, overwrite=False): _AUG_DOWNLOAD_URLS = [ ('http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip', '219e1696abb36c8ba3a3afe7fb2f4b4606a897c7'), ('http://data.csail.mit.edu/places/ADEchallenge/release_test.zip', 'e05747892219d10e9243933371a497e905a4860c'),] download_dir = os.path.join(path, 'downloads') makedirs(download_dir) for url, checksum in _AUG_DOWNLOAD_URLS: filename = download(url, path=download_dir, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename,"r") as zip_ref: zip_ref.extractall(path=path)
def download_otb(args, overwrite=False): """download otb2015 dataset and Unzip to download_dir""" _DOWNLOAD_URLS = 'http://cvlab.hanyang.ac.kr/tracker_benchmark/seq/' if not os.path.isdir(args.download_dir): makedirs(args.download_dir) for per_otb50 in otb50: url = os.path.join(_DOWNLOAD_URLS, per_otb50 + '.zip') filename = download(url, path=args.download_dir, overwrite=overwrite) with zipfile.ZipFile(filename) as zf: zf.extractall(path=args.download_dir) for per_otb100 in otb100: url = os.path.join(_DOWNLOAD_URLS, per_otb100 + '.zip') filename = download(url, path=args.download_dir, overwrite=overwrite) with zipfile.ZipFile(filename) as zf: zf.extractall(path=args.download_dir) os.rename(os.path.join(args, 'Jogging'), os.path.join(args, 'Jogging-1')) os.rename(os.path.join(args, 'Jogging'), os.path.join(args, 'Jogging-2')) os.rename(os.path.join(args, 'Skating2'), os.path.join(args, 'Skating2-1')) os.rename(os.path.join(args, 'Skating2'), os.path.join(args, 'Skating2-2')) os.rename(os.path.join(args, ' Human4'), os.path.join(args, 'Human4-2'))
def download_VID(args, overwrite=False): """download VID dataset and Unzip to download_dir""" _DOWNLOAD_URLS = [ ('http://bvisionweb1.cs.unc.edu/ilsvrc2015/ILSVRC2015_VID.tar.gz', '077dbdea4dff1853edd81b04fa98e19392287ca3'), ] if not os.path.isdir(args.download_dir): makedirs(args.download_dir) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=args.download_dir, overwrite=overwrite, sha1_hash=checksum) print('dataset is unziping') with tarfile.open(filename) as tar: tar.extractall(path=args.download_dir)
def main(): args = parse_args() name = "Market-1501-v15.09.15" url = "http://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/{name}.zip".format( name=name) root = osp.expanduser(args.download_dir) makedirs(root) fpath = osp.join(root, name + '.zip') exdir = osp.join(root, name) if not osp.exists(fpath) and not osp.isdir(exdir) and args.no_download: raise ValueError( ('{} dataset archive not found, make sure it is present.' ' Or you should not disable "--no-download" to grab it'.format( fpath))) # Download by default if not args.no_download: print('Downloading dataset') download(url, fpath, overwrite=False) print('Dataset downloaded') # Extract dataset if fresh copy downloaded or existing archive is yet to be extracted if not args.no_download or not osp.isdir(exdir): extract(fpath, root) make_list(exdir)
def download_voc(path, overwrite=False): _DOWNLOAD_URLS = [ ('http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar', '34ed68851bce2a36e2a223fa52c661d592c66b3c'), ('http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar', '41a8d6e12baa5ab18ee7f8f8029b9e11805b4ef1'), ('http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar', '4e443f8a2eca6b1dac8a6c57641b67dd40621a49')] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: tar.extractall(path=path)
def download_voc(path, overwrite=False): _DOWNLOAD_URLS = [ ('http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar', '34ed68851bce2a36e2a223fa52c661d592c66b3c'), ('http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar', '41a8d6e12baa5ab18ee7f8f8029b9e11805b4ef1'), ('http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar', '4e443f8a2eca6b1dac8a6c57641b67dd40621a49')] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: tar.extractall(path=path)
def get_pose_estimation(img_object, detector_model="yolo3_mobilenet1.0_coco", pose_model="simple_pose_resnet18_v1b", box_thresh=0.5, keypoint_thresh=0.2): ''' //TODO ''' detector = model_zoo.get_model(detector_model, pretrained=True) pose_net = model_zoo.get_model(pose_model, pretrained=True) # Loading weights for only person class detector.reset_class(["person"], reuse_weights=['person']) try: img_object = utils.download(img_object) except ValueError: pass if "yolo" in detector_model: x, img = data.transforms.presets.yolo.load_test(img_object, short=512) elif "ssd" in detector_model: x, img = data.transforms.presets.ssd.load_test(img_object, short=512) class_IDs, scores, bounding_boxs = detector(x) if "simple_pose" in pose_model: pose_input, upscale_bbox = detector_to_simple_pose( img, class_IDs, scores, bounding_boxs) predicted_heatmap = pose_net(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) elif "alpha_pose" in pose_model: pose_input, upscale_bbox = detector_to_alpha_pose( img, class_IDs, scores, bounding_boxs) predicted_heatmap = pose_net(pose_input) pred_coords, confidence = heatmap_to_coord_alpha_pose( predicted_heatmap, upscale_bbox) ax = utils.viz.plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores, box_thresh=box_thresh, keypoint_thresh=keypoint_thresh) return ax
def download_det(args, overwrite=False): """download DET dataset and Unzip to download_dir""" _DOWNLOAD_URLS = [ ('http://image-net.org/image/ILSVRC2015/ILSVRC2015_DET.tar.gz', 'cbf602d89f2877fa8843392a1ffde03450a18d38'), ] if not os.path.isdir(args.download_dir): makedirs(args.download_dir) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=args.download_dir, overwrite=overwrite, sha1_hash=checksum) print(' dataset has already download completed') with tarfile.open(filename) as tar: tar.extractall(path=args.download_dir) if os.path.isdir(os.path.join(args.download_dir, 'ILSVRC2015')): os.rename(os.path.join(args.download_dir, 'ILSVRC2015'), os.path.join(args.download_dir, 'ILSVRC'))
def download_ucf101(args): target_dir = args.download_dir if not os.path.exists(target_dir): os.makedirs(target_dir) if args.tiny_dataset: video_url = 'https://github.com/bryanyzhu/tiny-ucf101/raw/master/tiny-UCF101.zip' zip_file = download(video_url, path=target_dir) with zipfile.ZipFile(zip_file) as zf: zf.extractall(path=target_dir) else: from gluoncv.utils.filesystem import try_import_rarfile rarfile = try_import_rarfile() video_url = 'https://www.crcv.ucf.edu/data/UCF101/UCF101.rar' rar_file = download(video_url, path=target_dir) with rarfile.RarFile(rar_file) as rf: rf.extractall(path=target_dir) anno_url = 'https://www.crcv.ucf.edu/data/UCF101/UCF101TrainTestSplits-RecognitionTask.zip' zip_file = download(anno_url, path=target_dir) with zipfile.ZipFile(zip_file) as zf: zf.extractall(path=target_dir)
def fire(image_url, save): net = gc.model_zoo.get_model('resnet50_v1d',pretrained=True) im_fname = utils.download(image_url,PARTICLE) img = mx.image.imread(im_fname) transform_image = gc.data.transforms.presets.imagenet.transform_eval(img) pred = net(transform_image) prob = mx.nd.softmax(pred)[0].asnumpy() ind = mx.nd.topk(pred, k=10)[0].astype('int').asnumpy().tolist() respArray = [] for i in range(10): respArray.append({"label":net.classes[ind[i]], "probability":prob[ind[i]]}) if(not save): os.remove(im_fname) return str({PARTICLE:image_url,'flame':(respArray)})
def download_coco(args, overwrite=False): """download COCO dataset and Unzip to download_dir""" _DOWNLOAD_URLS = [ ('http://images.cocodataset.org/zips/train2017.zip', '10ad623668ab00c62c096f0ed636d6aff41faca5'), ('http://images.cocodataset.org/annotations/annotations_trainval2017.zip', '8551ee4bb5860311e79dace7e79cb91e432e78b3'), ('http://images.cocodataset.org/zips/val2017.zip', '4950dc9d00dbe1c933ee0170f5797584351d2a41'), ] if not os.path.isdir(args.download_dir): makedirs(args.download_dir) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=args.download_dir, overwrite=overwrite, sha1_hash=checksum) with zipfile.ZipFile(filename) as zf: zf.extractall(path=args.download_dir)
def download_coco(path, overwrite=False): _DOWNLOAD_URLS = [ ('http://images.cocodataset.org/zips/val2017.zip', '4950dc9d00dbe1c933ee0170f5797584351d2a41'), ('http://images.cocodataset.org/annotations/annotations_trainval2017.zip', '8551ee4bb5860311e79dace7e79cb91e432e78b3') ] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename) as zf: zf.extractall(path=path)
def download_aug(path, overwrite=False): _AUG_DOWNLOAD_URLS = [ ('http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz', '7129e0a480c2d6afb02b517bb18ac54283bfaa35')] makedirs(path) for url, checksum in _AUG_DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: tar.extractall(path=path) shutil.move(os.path.join(path, 'benchmark_RELEASE'), os.path.join(path, 'VOCaug')) filenames = ['VOCaug/dataset/train.txt', 'VOCaug/dataset/val.txt'] # generate trainval.txt with open(os.path.join(path, 'VOCaug/dataset/trainval.txt'), 'w') as outfile: for fname in filenames: fname = os.path.join(path, fname) with open(fname) as infile: for line in infile: outfile.write(line)
def download_coco(path, overwrite=False): _DOWNLOAD_URLS = [ ('http://images.cocodataset.org/zips/train2017.zip', '10ad623668ab00c62c096f0ed636d6aff41faca5'), ('http://images.cocodataset.org/annotations/annotations_trainval2017.zip', '8551ee4bb5860311e79dace7e79cb91e432e78b3'), ('http://images.cocodataset.org/zips/val2017.zip', '4950dc9d00dbe1c933ee0170f5797584351d2a41'), # ('http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip', # '46cdcf715b6b4f67e980b529534e79c2edffe084'), # test2017.zip, for those who want to attend the competition. # ('http://images.cocodataset.org/zips/test2017.zip', # '4e443f8a2eca6b1dac8a6c57641b67dd40621a49'), ] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename) as zf: zf.extractall(path=path)
def download_coco(path, overwrite=False): _DOWNLOAD_URLS = [ ('http://images.cocodataset.org/zips/train2017.zip', '10ad623668ab00c62c096f0ed636d6aff41faca5'), ('http://images.cocodataset.org/annotations/annotations_trainval2017.zip', '8551ee4bb5860311e79dace7e79cb91e432e78b3'), ('http://images.cocodataset.org/zips/val2017.zip', '4950dc9d00dbe1c933ee0170f5797584351d2a41'), # ('http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip', # '46cdcf715b6b4f67e980b529534e79c2edffe084'), # test2017.zip, for those who want to attend the competition. # ('http://images.cocodataset.org/zips/test2017.zip', # '4e443f8a2eca6b1dac8a6c57641b67dd40621a49'), ] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename) as zf: zf.extractall(path=path)
def get_img_segment(img_object, model="mask_rcnn_resnet50_v1b_coco", thresh=0.7, is_limited_classes=False, class_list=[]): net = model_zoo.get_model(model, pretrained=True) # if is_limited_classes: # net.reset_class(class_list, reuse_weights=class_list) try: img_object = utils.download(img_object) except ValueError: pass x, orig_img = data.transforms.presets.rcnn.load_test(img_object) ids, scores, bboxes, masks = [xx[0].asnumpy() for xx in net(x)] # paint segmentation mask on images directly width, height = orig_img.shape[1], orig_img.shape[0] masks, _ = utils.viz.expand_mask(masks, bboxes, (width, height), scores, thresh=thresh) orig_img = utils.viz.plot_mask(orig_img, masks) # identical to Faster RCNN object detection fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(1, 1, 1) ax = utils.viz.plot_bbox(orig_img, bboxes, scores, ids, thresh=thresh, class_names=net.classes, ax=ax) return ax
def read_data(opt): """ Pre-process data -------------------- Next we need a video or video frame if you want to test video frame, you can change opt.video_loader to False and opt.data-dir is your video frame path. meanwhile you need first frame object coordinates in opt.gt-bbox gt_bbox is first frame object coordinates, and it is bbox(center_x,center_y,weight,height) """ video_frames = [] if opt.video_loader: im_video = utils.download(opt.video_path) cap = cv2.VideoCapture(im_video) while (True): ret, img = cap.read() if not ret: break video_frames.append(img) else: for data in sorted(os.listdir(opt.data_dir)): video_frames.append(cv2.imread(os.path.join(opt.data_dir, data))) return video_frames
def download_vg(path, overwrite=False): _DOWNLOAD_URLS = [ ('https://cs.stanford.edu/people/rak248/VG_100K_2/images.zip', 'a055367f675dd5476220e9b93e4ca9957b024b94'), ('https://cs.stanford.edu/people/rak248/VG_100K_2/images2.zip', '2add3aab77623549e92b7f15cda0308f50b64ecf'), ] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract if filename.endswith('zip'): with zipfile.ZipFile(filename) as zf: zf.extractall(path=path) # move all images into folder `VG_100K` vg_100k_path = os.path.join(path, 'VG_100K') vg_100k_2_path = os.path.join(path, 'VG_100K_2') files_2 = os.listdir(vg_100k_2_path) for fl in files_2: shutil.move(os.path.join(vg_100k_2_path, fl), os.path.join(vg_100k_path, fl))
import time from matplotlib import pyplot as plt import numpy as np import mxnet as mx from mxnet import autograd, gluon import gluoncv as gcv from gluoncv.utils import download, viz ############################################################################################# # Pikachu Dataset # ---------------- # 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()
# -*- coding: utf-8 -*- """yolov3.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1eYgi88PNbe-ryMzwy6nUav83CAKFyKNe """ from gluoncv import model_zoo, data, utils utils.download('https://pjreddie.com/media/files/yolov3.weights',path='yolov3.weights') import struct import numpy as np from keras.layers import Conv2D from keras.layers import Input from keras.layers import BatchNormalization from keras.layers import LeakyReLU from keras.layers import ZeroPadding2D from keras.layers import UpSampling2D from keras.layers.merge import add, concatenate from keras.models import Model def _conv_block(inp, convs, skip=True): x = inp count = 0 for conv in convs: if count == (len(convs) - 2) and skip: skip_connection = x count += 1
# But you can feed an arbitrarily sized image. # # You can provide a list of image file names, such as ``[im_fname1, im_fname2, # ...]`` to :py:func:`gluoncv.data.transforms.presets.rcnn.load_test` if you # want to load multiple image together. # # This function returns two results. The first is a NDArray with shape # `(batch_size, RGB_channels, height, width)`. It can be fed into the # model directly. The second one contains the images in numpy format to # easy to be plotted. Since we only loaded a single image, the first dimension # of `x` is 1. # # Please beware that `orig_img` is resized to short edge 600px. im_fname = utils.download('https://github.com/dmlc/web-data/blob/master/' + 'gluoncv/detection/biking.jpg?raw=true', path='biking.jpg') x, orig_img = data.transforms.presets.rcnn.load_test(im_fname) ###################################################################### # Inference and display # --------------------- # # The Faster RCNN model returns predicted class IDs, confidence scores, # bounding boxes coordinates. Their shape are (batch_size, num_bboxes, 1), # (batch_size, num_bboxes, 1) and (batch_size, num_bboxes, 4), respectively. # # We can use :py:func:`gluoncv.utils.viz.plot_bbox` to visualize the # results. We slice the results for the first image and feed them into `plot_bbox`: box_ids, scores, bboxes = net(x)
# But you can feed an arbitrarily sized image. # # You can provide a list of image file names, such as ``[im_fname1, im_fname2, # ...]`` to :py:func:`gluoncv.data.transforms.presets.rcnn.load_test` if you # want to load multiple image together. # # This function returns two results. The first is a NDArray with shape # `(batch_size, RGB_channels, height, width)`. It can be fed into the # model directly. The second one contains the images in numpy format to # easy to be plotted. Since we only loaded a single image, the first dimension # of `x` is 1. # # Please beware that `orig_img` is resized to short edge 600px. im_fname = utils.download('https://github.com/dmlc/web-data/blob/master/' + 'gluoncv/detection/biking.jpg?raw=true', path='biking.jpg') x, orig_img = data.transforms.presets.rcnn.load_test(im_fname) ###################################################################### # Inference and display # --------------------- # # The Faster RCNN model returns predicted class IDs, confidence scores, # bounding boxes coordinates. Their shape are (batch_size, num_bboxes, 1), # (batch_size, num_bboxes, 1) and (batch_size, num_bboxes, 4), respectively. # # We can use :py:func:`gluoncv.utils.viz.plot_bbox` to visualize the # results. We slice the results for the first image and feed them into `plot_bbox`: box_ids, scores, bboxes = net(x)
├── README.txt ├── test ├── train └── val In order to go through this tutorial within a reasonable amount of time, we have prepared a small subset of the ``MINC-2500`` dataset, but you should substitute it with the original dataset for your experiments. We can download and extract it with: """ import zipfile, os from gluoncv.utils import download file_url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/classification/minc-2500-tiny.zip' zip_file = download(file_url, path='./') with zipfile.ZipFile(zip_file, 'r') as zin: zin.extractall(os.path.expanduser('./')) ################################################################################ # Hyperparameters # ---------- # # First, let's import all other necessary libraries. import mxnet as mx import numpy as np import os, time, shutil from mxnet import gluon, image, init, nd from mxnet import autograd as ag
# specify that we resize the short edge of the image to 512 px. You can # feed an arbitrarily sized image. # Once constraint for YOLO is that input height and width can be divided by 32. # # You can provide a list of image file names, such as ``[im_fname1, im_fname2, # ...]`` to :py:func:`gluoncv.data.transforms.presets.yolo.load_test` if you # want to load multiple image together. # # This function returns two results. The first is a NDArray with shape # `(batch_size, RGB_channels, height, width)`. It can be fed into the # model directly. The second one contains the images in numpy format to # easy to be plotted. Since we only loaded a single image, the first dimension # of `x` is 1. im_fname = utils.download('https://raw.githubusercontent.com/zhreshold/' + 'mxnet-ssd/master/data/demo/dog.jpg', path='dog.jpg') x, img = data.transforms.presets.yolo.load_test(im_fname, short=512) print('Shape of pre-processed image:', x.shape) ###################################################################### # Inference and display # --------------------- # # The forward function will return all detected bounding boxes, and the # corresponding predicted class IDs and confidence scores. Their shapes are # `(batch_size, num_bboxes, 1)`, `(batch_size, num_bboxes, 1)`, and # `(batch_size, num_bboxes, 4)`, respectively. # # We can use :py:func:`gluoncv.utils.viz.plot_bbox` to visualize the # results. We slice the results for the first image and feed them into `plot_bbox`:
###################################################################### # Pre-process an image for detector, and make inference # -------------------- # # Next we download an image, and pre-process with preset data transforms. Here we # specify that we resize the short edge of the image to 512 px. But you can # feed an arbitrarily sized image. # # This function returns two results. The first is a NDArray with shape # ``(batch_size, RGB_channels, height, width)``. It can be fed into the # model directly. The second one contains the images in numpy format to # easy to be plotted. Since we only loaded a single image, the first dimension # of `x` is 1. im_fname = utils.download('https://github.com/dmlc/web-data/blob/master/' + 'gluoncv/pose/soccer.png?raw=true', path='soccer.png') x, img = data.transforms.presets.ssd.load_test(im_fname, short=512) print('Shape of pre-processed image:', x.shape) class_IDs, scores, bounding_boxs = detector(x) ###################################################################### # Process tensor from detector to keypoiny network # -------------------- # # Next we process the output from the detector. # # For a Simple Pose network, it expects the input has the size 256x192, # and the human is centered. We crop the bounding boxed area # for each human, and resize it to 256x192, then finally normalize it.
# # There are multiple ways to organize the label format for object detection task. We will briefly introduce the # most widely used: ``bounding box``. # # GluonCV expect all bounding boxes to be encoded as (xmin, ymin, xmax, ymax), aka (left, top, right, bottom) borders of each object of interest. # # First of all, let us plot a real image for example: import os, zipfile from gluoncv import utils import mxnet as mx import numpy as np from matplotlib import pyplot as plt im_fname = utils.download('https://github.com/dmlc/web-data/blob/master/' + 'gluoncv/datasets/dog.jpg?raw=true', path='dog.jpg') img = mx.image.imread(im_fname) ax = utils.viz.plot_image(img) print(img.shape) plt.show() ############################################################################## # Now, let's label the image manually for demo. # # .. hint:: # # In practice, a dedicated GUI labeling tool is more convenient. # # We expect all bounding boxes follow this format: (xmin, ymin, xmax, ymax)
to install ``MXNet`` and ``GluonCV`` if you haven't done so yet. """ import matplotlib.pyplot as plt from mxnet import gluon, nd, image from mxnet.gluon.data.vision import transforms from gluoncv import utils from gluoncv.model_zoo import get_model ################################################################ # # Then, we download and show the example image: url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/classification/plane-draw.jpeg' im_fname = utils.download(url) img = image.imread(im_fname) plt.imshow(img.asnumpy()) plt.show() ################################################################ # In case you don't recognize it, the image is a poorly-drawn airplane :) # # Now we define transformations for the image. transform_fn = transforms.Compose([ transforms.Resize(32), transforms.CenterCrop(32), transforms.ToTensor(),
def download_data(path,file, overwrite=False): _DOWNLOAD_URL = 'https://data.vision.ee.ethz.ch/cvl/DIV2K/' filename = download(_DOWNLOAD_URL + file, path=path, overwrite=overwrite) # extract with zipfile.ZipFile(filename,'r') as zip: zip.extractall(path=path)
def download_data(path,file, overwrite=False): _DOWNLOAD_URL = 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/' filename = download(_DOWNLOAD_URL + file, path=path, overwrite=overwrite) # extract with zipfile.ZipFile(filename,'r') as zip: zip.extractall(path=path)
├── README.txt ├── test ├── train └── val In order to go through this tutorial within a reasonable amount of time, we have prepared a small subset of the ``MINC-2500`` dataset, but you should substitute it with the original dataset for your experiments. We can download and extract it with: """ import zipfile, os from gluoncv.utils import download file_url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/classification/minc-2500-tiny.zip' zip_file = download(file_url, path='./') with zipfile.ZipFile(zip_file, 'r') as zin: zin.extractall(os.path.expanduser('./')) ################################################################################ # Hyperparameters # ---------- # # First, let's import all other necessary libraries. import mxnet as mx import numpy as np import os, time, shutil from mxnet import gluon, image, init, nd from mxnet import autograd as ag
# specify that we resize the short edge of the image to 512 px. You can # feed an arbitrarily sized image. # Once constraint for YOLO is that input height and width can be divided by 32. # # You can provide a list of image file names, such as ``[im_fname1, im_fname2, # ...]`` to :py:func:`gluoncv.data.transforms.presets.yolo.load_test` if you # want to load multiple image together. # # This function returns two results. The first is a NDArray with shape # `(batch_size, RGB_channels, height, width)`. It can be fed into the # model directly. The second one contains the images in numpy format to # easy to be plotted. Since we only loaded a single image, the first dimension # of `x` is 1. im_fname = utils.download('https://raw.githubusercontent.com/zhreshold/' + 'mxnet-ssd/master/data/demo/dog.jpg', path='dog.jpg') x, img = data.transforms.presets.yolo.load_test(im_fname, short=512) print('Shape of pre-processed image:', x.shape) ###################################################################### # Inference and display # --------------------- # # The forward function will return all detected bounding boxes, and the # corresponding predicted class IDs and confidence scores. Their shapes are # `(batch_size, num_bboxes, 1)`, `(batch_size, num_bboxes, 1)`, and # `(batch_size, num_bboxes, 4)`, respectively. # # We can use :py:func:`gluoncv.utils.viz.plot_bbox` to visualize the # results. We slice the results for the first image and feed them into `plot_bbox`:
import time from matplotlib import pyplot as plt import numpy as np import mxnet as mx from mxnet import autograd, gluon import gluoncv as gcv from gluoncv.utils import download, viz ############################################################################################# # Pikachu Dataset # ---------------- # 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