def plot_gps_html(gps_list): """ Plots gps coordinates on a map projection InstallBasemap: sudo apt-get install libgeos-dev pip install git+https://github.com/matplotlib/basemap http://matplotlib.org/basemap/users/examples.html pip install gmplot sudo apt-get install netcdf-bin sudo apt-get install libnetcdf-dev pip install netCDF4 Ignore: pip install git+git://github.com/myuser/foo.git@v123 Example: >>> # DISABLE_DOCTEST >>> from wbia.algo.preproc.preproc_occurrence import * # NOQA >>> import wbia >>> ibs = wbia.opendb(defaultdb='testdb1') >>> images = ibs.images() >>> # Setup GPS points to draw >>> print('Setup GPS points') >>> gps_list_ = np.array(images.gps2) >>> unixtime_list_ = np.array(images.unixtime2) >>> has_gps = np.all(np.logical_not(np.isnan(gps_list_)), axis=1) >>> has_unixtime = np.logical_not(np.isnan(unixtime_list_)) >>> isvalid = np.logical_and(has_gps, has_unixtime) >>> gps_list = gps_list_.compress(isvalid, axis=0) >>> unixtime_list = unixtime_list_.compress(isvalid) # NOQA >>> plot_image_gps(gps_list) """ import wbia.plottool as pt import gmplot import matplotlib as mpl import vtool as vt pt.qt4ensure() lat = gps_list.T[0] lon = gps_list.T[1] # Get extent of bbox = vt.bbox_from_verts(gps_list) centerx, centery = vt.bbox_center(bbox) gmap = gmplot.GoogleMapPlotter(centerx, centery, 13) color = mpl.colors.rgb2hex(pt.ORANGE) gmap.scatter(lat, lon, color=color, size=100, marker=False) gmap.draw('mymap.html') ut.startfile('mymap.html')
def makewarp(imgBGR): # hack a projection matrix using dummy homogrpahy imgBGRA = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2BGRA) imgBGRA[:, :, 3] = .87 * 255 # hack alpha imgBGRA = vt.pad_image(imgBGRA, 2, value=[0, 0, 255, 255]) size = np.array(vt.get_size(imgBGRA)) pts1 = np.array([(0, 0), (0, 1), (1, 1), (1, 0)]) * size x_adjust = .15 y_adjust = .5 pts2 = np.array([(x_adjust, 0), (0, 1 - y_adjust), (1, 1 - y_adjust), (1 - x_adjust, 0)]) * size H = cv2.findHomography(pts1, pts2)[0] dsize = np.array(vt.bbox_from_verts(pts2)[2:4]).astype(np.int) warpkw = dict(flags=cv2.INTER_LANCZOS4, borderMode=cv2.BORDER_CONSTANT) imgBGRA_warped = cv2.warpPerspective(imgBGRA, H, tuple(dsize), **warpkw) return imgBGRA_warped
def show_chip(ibs, aid, in_image=False, annote=True, title_suffix='', weight_label=None, weights=None, config2_=None, **kwargs): r"""Driver function to show chips Args: ibs (wbia.IBEISController): aid (int): annotation rowid in_image (bool): displays annotation with the context of its source image annote (bool): enables overlay annoations title_suffix (str): weight_label (None): (default = None) weights (None): (default = None) config2_ (dict): (default = None) Kwargs: enable_chip_title_prefix, nokpts, kpts_subset, kpts, text_color, notitle, draw_lbls, show_aidstr, show_gname, show_name, show_nid, show_exemplar, show_num_gt, show_quality_text, show_viewcode, fnum, title, figtitle, pnum, interpolation, cmap, heatmap, data_colorbar, darken, update, xlabel, redraw_image, ax, alpha, docla, doclf, projection, pts, ell color (3/4-tuple, ndarray, or str): colors for keypoints CommandLine: python -m wbia.viz.viz_chip show_chip --show --ecc python -c "import utool as ut; ut.print_auto_docstr('wbia.viz.viz_chip', 'show_chip')" python -m wbia.viz.viz_chip show_chip --show --db NNP_Master3 --aids 14047 --no-annote python -m wbia.viz.viz_chip show_chip --show --db NNP_Master3 --aids 14047 --no-annote python -m wbia.viz.viz_chip show_chip --show --db PZ_MTEST --aid 1 --bgmethod=cnn python -m wbia.viz.viz_chip show_chip --show --db PZ_MTEST --aid 1 --bgmethod=cnn --scale_max=30 python -m wbia.viz.viz_chip show_chip --show --db PZ_MTEST --aid 1 --ecc --draw_lbls=False --notitle --save=~/slides/lnbnn_query.jpg --dpi=300 Example: >>> # xdoctest: +REQUIRES(module:wbia_cnn) >>> # VIZ_TEST >>> from wbia.viz.viz_chip import * # NOQA >>> import numpy as np >>> import vtool as vt >>> in_image = False >>> ibs, aid_list, kwargs, config2_ = testdata_showchip() >>> aid = aid_list[0] >>> if True: >>> import matplotlib as mpl >>> from wbia.scripts.thesis import TMP_RC >>> mpl.rcParams.update(TMP_RC) >>> if ut.get_argflag('--ecc'): >>> kpts = ibs.get_annot_kpts(aid, config2_=config2_) >>> weights = ibs.get_annot_fgweights([aid], ensure=True, config2_=config2_)[0] >>> kpts = ut.random_sample(kpts[weights > .9], 200, seed=0) >>> ecc = vt.get_kpts_eccentricity(kpts) >>> scale = 1 / vt.get_scales(kpts) >>> #s = ecc if config2_.affine_invariance else scale >>> s = scale >>> colors = pt.scores_to_color(s, cmap_='jet') >>> kwargs['color'] = colors >>> kwargs['kpts'] = kpts >>> kwargs['ell_linewidth'] = 3 >>> kwargs['ell_alpha'] = .7 >>> show_chip(ibs, aid, in_image=in_image, config2_=config2_, **kwargs) >>> pt.show_if_requested() """ if ut.VERBOSE: logger.info('[viz] show_chip(aid=%r)' % (aid, )) # ibs.assert_valid_aids((aid,)) # Get chip # logger.info('in_image = %r' % (in_image,)) chip = vh.get_chips(ibs, aid, in_image=in_image, config2_=config2_) # Create chip title chip_text = vh.get_annot_texts(ibs, [aid], **kwargs)[0] if kwargs.get('enable_chip_title_prefix', True): chip_title_text = chip_text + title_suffix else: chip_title_text = title_suffix chip_title_text = chip_title_text.strip('\n') # Draw chip fig, ax = pt.imshow(chip, **kwargs) # Populate axis user data vh.set_ibsdat(ax, 'viztype', 'chip') vh.set_ibsdat(ax, 'aid', aid) if annote and not kwargs.get('nokpts', False): # Get and draw keypoints if 'color' not in kwargs: if weight_label == 'fg_weights': if weights is None and ibs.has_species_detector( ibs.get_annot_species_texts(aid)): weight_label = 'fg_weights' weights = ibs.get_annot_fgweights([aid], ensure=True, config2_=config2_)[0] if weights is not None: cmap_ = 'hot' # if weight_label == 'dstncvs': # cmap_ = 'rainbow' color = pt.scores_to_color(weights, cmap_=cmap_, reverse_cmap=False) kwargs['color'] = color kwargs['ell_color'] = color kwargs['pts_color'] = color kpts_ = vh.get_kpts( ibs, aid, in_image, config2_=config2_, kpts_subset=kwargs.get('kpts_subset', None), kpts=kwargs.pop('kpts', None), ) pt.viz_keypoints._annotate_kpts(kpts_, **kwargs) if kwargs.get('draw_lbls', True): pt.upperleft_text(chip_text, color=kwargs.get('text_color', None)) use_title = not kwargs.get('notitle', False) if use_title: pt.set_title(chip_title_text) if in_image: gid = ibs.get_annot_gids(aid) aid_list = ibs.get_image_aids(gid) annotekw = viz_image.get_annot_annotations(ibs, aid_list, sel_aids=[aid], draw_lbls=kwargs.get( 'draw_lbls', True)) # Put annotation centers in the axis ph.set_plotdat(ax, 'annotation_bbox_list', annotekw['bbox_list']) ph.set_plotdat(ax, 'aid_list', aid_list) pt.viz_image2.draw_image_overlay(ax, **annotekw) zoom_ = ut.get_argval('--zoom', type_=float, default=None) if zoom_ is not None: import vtool as vt # Zoom into the chip for some image context rotated_verts = ibs.get_annot_rotated_verts(aid) bbox = ibs.get_annot_bboxes(aid) # logger.info(bbox) # logger.info(rotated_verts) rotated_bbox = vt.bbox_from_verts(rotated_verts) imgw, imgh = ibs.get_image_sizes(gid) pad_factor = zoom_ pad_length = min(bbox[2], bbox[3]) * pad_factor minx = max(rotated_bbox[0] - pad_length, 0) miny = max(rotated_bbox[1] - pad_length, 0) maxx = min((rotated_bbox[0] + rotated_bbox[2]) + pad_length, imgw) maxy = min((rotated_bbox[1] + rotated_bbox[3]) + pad_length, imgh) # maxy = imgh - maxy # miny = imgh - miny ax = pt.gca() ax.set_xlim(minx, maxx) ax.set_ylim(miny, maxy) ax.invert_yaxis() else: ph.set_plotdat(ax, 'chipshape', chip.shape) # if 'featweights' in vars() and 'color' in kwargs: if weights is not None and weight_label is not None: # # HACK HACK HACK if len(weights) > 0: cb = pt.colorbar(weights, kwargs['color']) cb.set_label(weight_label) return fig, ax
def fix_annotation_orientation(ibs, min_percentage=0.95): """ Fixes the annotations that are outside the bounds of the image due to a changed image orientation flag in the database CommandLine: python -m ibeis.scripts.fix_annotation_orientation_issue fix_annotation_orientation Example: >>> # ENABLE_DOCTEST >>> import ibeis >>> from ibeis.scripts.fix_annotation_orientation_issue import * # NOQA >>> ibs = ibeis.opendb() >>> unfixable_gid_list = fix_annotation_orientation(ibs) >>> assert len(unfixable_gid_list) == 0 """ from vtool import exif def bbox_overlap(bbox1, bbox2): ax1 = bbox1[0] ay1 = bbox1[1] ax2 = bbox1[0] + bbox1[2] ay2 = bbox1[1] + bbox1[3] bx1 = bbox2[0] by1 = bbox2[1] bx2 = bbox2[0] + bbox2[2] by2 = bbox2[1] + bbox2[3] overlap_x = max(0, min(ax2, bx2) - max(ax1, bx1)) overlap_y = max(0, min(ay2, by2) - max(ay1, by1)) return overlap_x * overlap_y orient_dict = exif.ORIENTATION_DICT_INVERSE good_orient_list = [ exif.ORIENTATION_UNDEFINED, exif.ORIENTATION_000, ] good_orient_key_list = [ orient_dict.get(good_orient) for good_orient in good_orient_list ] assert None not in good_orient_key_list gid_list = ibs.get_valid_gids() orient_list = ibs.get_image_orientation(gid_list) flag_list = [orient not in good_orient_key_list for orient in orient_list] # Filter based on based gids unfixable_gid_list = [] gid_list = ut.filter_items(gid_list, flag_list) if len(gid_list) > 0: args = (len(gid_list), ) print('Found %d images with non-standard orientations' % args) aids_list = ibs.get_image_aids(gid_list, is_staged=None, __check_staged__=False) size_list = ibs.get_image_sizes(gid_list) invalid_gid_list = [] zipped = zip(gid_list, orient_list, aids_list, size_list) for gid, orient, aid_list, (w, h) in zipped: image = ibs.get_images(gid) h_, w_ = image.shape[:2] if h != h_ or w != w_: ibs._set_image_sizes([gid], [w_], [h_]) orient_str = exif.ORIENTATION_DICT[orient] image_bbox = (0, 0, w, h) verts_list = ibs.get_annot_rotated_verts(aid_list) invalid = False for aid, vert_list in zip(aid_list, verts_list): annot_bbox = vt.bbox_from_verts(vert_list) overlap = bbox_overlap(image_bbox, annot_bbox) area = annot_bbox[2] * annot_bbox[3] percentage = overlap / area if percentage < min_percentage: args = (gid, orient_str, aid, overlap, area, percentage) print( '\tInvalid GID %r, Orient %r, AID %r: Overlap %0.2f, Area %0.2f (%0.2f %%)' % args) invalid = True # break if invalid: invalid_gid_list.append(gid) invalid_gid_list = list(set(invalid_gid_list)) if len(invalid_gid_list) > 0: args = ( len(invalid_gid_list), len(gid_list), invalid_gid_list, ) print('Found %d / %d images with invalid annotations = %r' % args) orient_list = ibs.get_image_orientation(invalid_gid_list) aids_list = ibs.get_image_aids(invalid_gid_list, is_staged=None, __check_staged__=False) size_list = ibs.get_image_sizes(invalid_gid_list) zipped = zip(invalid_gid_list, orient_list, aids_list, size_list) for invalid_gid, orient, aid_list, (w, h) in zipped: orient_str = exif.ORIENTATION_DICT[orient] image_bbox = (0, 0, w, h) args = ( invalid_gid, len(aid_list), ) print('Fixing GID %r with %d annotations' % args) theta = np.pi / 2.0 tx = 0.0 ty = 0.0 if orient == orient_dict.get(exif.ORIENTATION_090): theta *= 1.0 tx = w elif orient == orient_dict.get(exif.ORIENTATION_180): theta *= 2.0 tx = w ty = h elif orient == orient_dict.get(exif.ORIENTATION_270): theta *= -1.0 ty = h else: raise ValueError('Unrecognized invalid orientation') H = np.array([[np.cos(theta), -np.sin(theta), tx], [np.sin(theta), np.cos(theta), ty], [0.0, 0.0, 1.0]]) # print(H) verts_list = ibs.get_annot_rotated_verts(aid_list) for aid, vert_list in zip(aid_list, verts_list): vert_list = np.array(vert_list) # print(vert_list) vert_list = vert_list.T transformed_vert_list = vt.transform_points_with_homography( H, vert_list) transformed_vert_list = transformed_vert_list.T # print(transformed_vert_list) ibs.set_annot_verts([aid], [transformed_vert_list], update_visual_uuids=False) current_theta = ibs.get_annot_thetas(aid) new_theta = current_theta + theta ibs.set_annot_thetas(aid, new_theta, update_visual_uuids=False) fixed_vert_list = ibs.get_annot_rotated_verts(aid) fixed_annot_bbox = vt.bbox_from_verts(fixed_vert_list) fixed_overlap = bbox_overlap(image_bbox, fixed_annot_bbox) fixed_area = fixed_annot_bbox[2] * fixed_annot_bbox[3] fixed_percentage = fixed_overlap / fixed_area args = (invalid_gid, orient_str, aid, fixed_overlap, fixed_area, fixed_percentage) print( '\tFixing GID %r, Orient %r, AID %r: Overlap %0.2f, Area %0.2f (%0.2f %%)' % args) if fixed_percentage < min_percentage: print('\tWARNING: FIXING DID NOT CORRECT AID %r' % (aid, )) unfixable_gid_list.append(gid) print('Un-fixable gid_list = %r' % (unfixable_gid_list, )) return unfixable_gid_list
def show_chip(ibs, aid, in_image=False, annote=True, title_suffix='', weight_label=None, weights=None, config2_=None, **kwargs): r""" Driver function to show chips Args: ibs (ibeis.IBEISController): aid (int): annotation rowid in_image (bool): displays annotation with the context of its source image annote (bool): enables overlay annoations title_suffix (str): weight_label (None): (default = None) weights (None): (default = None) config2_ (dict): (default = None) Kwargs: enable_chip_title_prefix, nokpts, kpts_subset, kpts, text_color, notitle, draw_lbls, show_aidstr, show_gname, show_name, show_nid, show_exemplar, show_num_gt, show_quality_text, show_yawtext, fnum, title, figtitle, pnum, interpolation, cmap, heatmap, data_colorbar, darken, update, xlabel, redraw_image, ax, alpha, docla, doclf, projection, use_gridspec, pts, ell color (3/4-tuple, ndarray, or str): colors for keypoints CommandLine: python -m ibeis.viz.viz_chip --test-show_chip --show --ecc python -c "import utool as ut; ut.print_auto_docstr('ibeis.viz.viz_chip', 'show_chip')" python -m ibeis.viz.viz_chip --test-show_chip --show --db NNP_Master3 --aids 14047 --no-annote python -m ibeis.viz.viz_chip --test-show_chip --show --db NNP_Master3 --aids 14047 --no-annote python -m ibeis.viz.viz_chip --test-show_chip --show --db PZ_MTEST --aid 1 --bgmethod=cnn python -m ibeis.viz.viz_chip --test-show_chip --show --db PZ_MTEST --aid 1 --bgmethod=cnn --scale_max=30 Example: >>> # VIZ_TEST >>> from ibeis.viz.viz_chip import * # NOQA >>> import numpy as np >>> import vtool as vt >>> in_image = False >>> ibs, aid_list, kwargs, config2_ = testdata_showchip() >>> aid = aid_list[0] >>> if ut.get_argflag('--ecc'): >>> kpts = ibs.get_annot_kpts(aid, config2_=config2_) >>> weights = ibs.get_annot_fgweights([aid], ensure=True, config2_=config2_)[0] >>> kpts = ut.random_sample(kpts[weights > .9], 200, seed=0) >>> ecc = vt.get_kpts_eccentricity(kpts) >>> scale = 1 / vt.get_scales(kpts) >>> s = ecc if config2_.affine_invariance else scale >>> colors = pt.scores_to_color(s, cmap_='jet') >>> kwargs['color'] = colors >>> kwargs['kpts'] = kpts >>> show_chip(ibs, aid, in_image=in_image, config2_=config2_, **kwargs) >>> pt.show_if_requested() """ if ut.VERBOSE: print('[viz] show_chip(aid=%r)' % (aid,)) #ibs.assert_valid_aids((aid,)) # Get chip #print('in_image = %r' % (in_image,)) chip = vh.get_chips(ibs, aid, in_image=in_image, config2_=config2_) # Create chip title chip_text = vh.get_annot_texts(ibs, [aid], **kwargs)[0] if kwargs.get('enable_chip_title_prefix', True): chip_title_text = chip_text + title_suffix else: chip_title_text = title_suffix chip_title_text = chip_title_text.strip('\n') # Draw chip fig, ax = pt.imshow(chip, **kwargs) # Populate axis user data vh.set_ibsdat(ax, 'viztype', 'chip') vh.set_ibsdat(ax, 'aid', aid) if annote and not kwargs.get('nokpts', False): # Get and draw keypoints if 'color' not in kwargs: if weight_label == 'fg_weights': if weights is None and ibs.has_species_detector(ibs.get_annot_species_texts(aid)): weight_label = 'fg_weights' weights = ibs.get_annot_fgweights([aid], ensure=True, config2_=config2_)[0] if weights is not None: cmap_ = 'hot' #if weight_label == 'dstncvs': # cmap_ = 'rainbow' color = pt.scores_to_color(weights, cmap_=cmap_, reverse_cmap=False) kwargs['color'] = color kwargs['ell_color'] = color kwargs['pts_color'] = color kpts_ = vh.get_kpts(ibs, aid, in_image, config2_=config2_, kpts_subset=kwargs.get('kpts_subset', None), kpts=kwargs.get('kpts', None)) try: del kwargs['kpts'] except KeyError: pass pt.viz_keypoints._annotate_kpts(kpts_, **kwargs) if not ut.get_argflag('--noaidlabel'): pt.upperleft_text(chip_text, color=kwargs.get('text_color', None)) use_title = not kwargs.get('notitle', False) if use_title: pt.set_title(chip_title_text) if in_image: gid = ibs.get_annot_gids(aid) aid_list = ibs.get_image_aids(gid) annotekw = viz_image.get_annot_annotations( ibs, aid_list, sel_aids=[aid], draw_lbls=kwargs.get('draw_lbls', True)) # Put annotation centers in the axis ph.set_plotdat(ax, 'annotation_bbox_list', annotekw['bbox_list']) ph.set_plotdat(ax, 'aid_list', aid_list) pt.viz_image2.draw_image_overlay(ax, **annotekw) zoom_ = ut.get_argval('--zoom', type_=float, default=None) if zoom_ is not None: import vtool as vt # Zoom into the chip for some image context rotated_verts = ibs.get_annot_rotated_verts(aid) bbox = ibs.get_annot_bboxes(aid) #print(bbox) #print(rotated_verts) rotated_bbox = vt.bbox_from_verts(rotated_verts) imgw, imgh = ibs.get_image_sizes(gid) pad_factor = zoom_ pad_length = min(bbox[2], bbox[3]) * pad_factor minx = max(rotated_bbox[0] - pad_length, 0) miny = max(rotated_bbox[1] - pad_length, 0) maxx = min((rotated_bbox[0] + rotated_bbox[2]) + pad_length, imgw) maxy = min((rotated_bbox[1] + rotated_bbox[3]) + pad_length, imgh) #maxy = imgh - maxy #miny = imgh - miny ax = pt.gca() ax.set_xlim(minx, maxx) ax.set_ylim(miny, maxy) ax.invert_yaxis() else: ph.set_plotdat(ax, 'chipshape', chip.shape) #if 'featweights' in vars() and 'color' in kwargs: if weights is not None and weight_label is not None: ## HACK HACK HACK if len(weights) > 0: cb = pt.colorbar(weights, kwargs['color']) cb.set_label(weight_label) return fig, ax