def plot_symbol(self, bbox, alignment, column_i, seq_i): from matplotlib.patches import Rectangle trace = alignment.trace if trace[column_i, seq_i] != -1: symbol = alignment.sequences[seq_i][trace[column_i, seq_i]] else: symbol = "-" color = self.get_color(alignment, column_i, seq_i) box = Rectangle(bbox.p0, bbox.width, bbox.height) self.axes.add_patch(box) text = self.axes.text(bbox.x0 + bbox.width / 2, bbox.y0 + bbox.height / 2, symbol, color="black", ha="center", va="center", size=self._font_size, **self._font_param) text.set_clip_on(True) if self._color_symbols: box.set_color("None") text.set_color(color) else: box.set_color(color)
def plot_quad_system_geometry(stateSolMat, timeStep): ''' stateSol is the state vector of the payload, for each time step X=x xDot y yDot theta thetaDot ax is the returned figure axis ''' stateSol = stateSolMat[timeStep] # Figure #1 fig = plt.figure(figsize=(13, 8)) ax = fig.gca() # ax.axis todo: set limits to axes so they are bigger then expected geometry ax.scatter(stateSol[0], stateSol[2], s=20, c='b') #stateSol[:,0] print stateSol[0], stateSol[2] # # LumpedMass payload # lumpedPayload = Circle((stateSol[0,0], stateSol[0,0]), 13) # Circle.set_color(lumpedPayload, '0.75') # Circle.set_alpha(lumpedPayload, 0.1) # ax.add_patch(lumpedPayload) # print stateSol[0,0] # rectangular payload payloadW = 1.2 payloadH = 0.1 rectPayload = Rectangle((stateSol[0], stateSol[2]), payloadW, payloadH) Rectangle.set_color(rectPayload, '0.75') Rectangle.set_alpha(rectPayload, 0.71) #1-opac 0-transparent ax.add_patch(rectPayload) return fig
def __fill_fibonacci_retracement_rectangle_dic__(self): index_left = self.xy[0, 0] index_right = self.xy[self.xy.shape[0] - 1, 0] value_left = self.xy[0, 1] value_right = self.xy[self.xy.shape[0] - 1, 1] value_range = value_right - value_left width = index_right - index_left for k in range( 0, len(fibonacci_helper.retracement_array_for_plotting) - 1): ret_01 = fibonacci_helper.retracement_array_for_plotting[k] ret_02 = fibonacci_helper.retracement_array_for_plotting[k + 1] value_01 = round(value_left + ret_01 * value_range, 2) value_02 = round(value_left + ret_02 * value_range, 2) height = value_02 - value_01 rectangle = Rectangle(np.array([index_left, value_01]), width=width, height=height) rectangle.set_linewidth(1) rectangle.set_linestyle('dashed') rectangle.set_color(self.color_list[k]) rectangle.set_edgecolor('k') rectangle.set_alpha(0.1) rectangle.set_visible(False) self.__fib_retracement_rectangle_dic[ret_02] = rectangle self.__fill_retracement_text_annotations__(index_left, ret_02, value_02) self.__fill_retracement_spikes_text_annotations__(ret_02, k + 1)
def add_conv_patch(self, conv1, conv2, patch_size=5, stride=1, size=3, ratio=None, direction=1): if ratio is None: ratio = [0.7, 0.5] start_loc = self.layers[conv1].get_xy() + np.array( (self.layers[conv1].get_width() * ratio[0], self.layers[conv1].get_height() * ratio[1])) end_loc = self.layers[conv2].get_xy() + np.array( (self.layers[conv2].get_width() * ratio[0], self.layers[conv2].get_height() * ratio[1])) # start_loc = top_left_list[ind_bgn] \ # + (num_show_list[ind_bgn] - 1) * np.array(loc_diff_list[ind_bgn]) \ # + np.array([start_ratio[0] * (size_list[ind_bgn][1] - patch_size[1]), # - start_ratio[1] * (size_list[ind_bgn][0] - patch_size[0])] # ) # end_loc = top_left_list[ind_bgn + 1] \ # + (num_show_list[ind_bgn + 1] - 1) * np.array( # loc_diff_list[ind_bgn + 1]) \ # + np.array([end_ratio[0] * size_list[ind_bgn + 1][1], # - end_ratio[1] * size_list[ind_bgn + 1][0]]) patch = Rectangle(start_loc, patch_size, -patch_size) patch.set_color(Dark * np.ones(3)) patch.set_edgecolor(Black * np.ones(3)) self.ax.add_patch(patch) line1 = Line2D([start_loc[0], end_loc[0]], [start_loc[1], end_loc[1]]) line1.set_color(Darker * np.ones(3)) self.ax.add_line(line1) line2 = Line2D([start_loc[0] + patch_size, end_loc[0]], [start_loc[1], end_loc[1]]) line2.set_color(Darker * np.ones(3)) self.ax.add_line(line2) line3 = Line2D([start_loc[0], end_loc[0]], [start_loc[1] - patch_size, end_loc[1]]) line3.set_color(Darker * np.ones(3)) self.ax.add_line(line3) line4 = Line2D([start_loc[0] + patch_size, end_loc[0]], [start_loc[1] - patch_size, end_loc[1]]) line4.set_color(Darker * np.ones(3)) self.ax.add_line(line4)
class Well: """ A class for individual wells in the matplotlib plot """ def __init__(self, center, shape, size_param): self.center = center self.size_param = size_param if shape == 'circle': self.marker = Circle(self.center, radius=size_param, color='gray', zorder=0) elif shape == 'square': self.marker = Rectangle(self.center, width=size_param, height=size_param, color = 'gray', zorder=0) self.barcode = '' def markEmpty(self): self.marker.set_color('gray') self.marker.zorder=0 def markFilled(self): self.marker.set_color('red') self.marker.zorder=1 def markTarget(self): self.marker.set_color('yellow') self.marker.zorder=2 def markRescanned(self): self.marker.set_color('blue') self.marker.zorder=2
def add_conv(self, dim, draw_size, depth=1, size=64, in_channels=1, out_channels=32, num=3): if self.previous_depth == depth: self.xy += np.array( (self.layers[-1].get_width() + self.layer_width, 0), dtype=float) elif self.previous_depth is not None and self.previous_depth < depth: self.xy += np.array( (self.layers[-1].get_width() - draw_size, -(self.layers[-1].get_height() + self.layer_width)), dtype=float) elif self.previous_depth is not None and self.previous_depth > depth: self.xy = np.array( (self.layers[-1].get_x() + 6, self.depth_xy[depth][1]), dtype=float) elif self.previous_depth is None: #Start self.xy -= np.array((self.layer_width, 0), dtype=float) self.depth_xy[depth] = self.xy.copy() self.depths[len(self.layers)] = depth conv_type = "Inputs" if len(self.layers) == 0 else "Channels" name = "{}\n{}@{}".format(conv_type, in_channels, "x".join(map(str, [size] * dim))) self._label(self.xy + np.array([0, draw_size]), name) for ind in xrange(num): #print ind * self.loc_diff patch = Rectangle(self.xy + ind * self.loc_diff, draw_size, draw_size) color = Medium if ind % 2 else Light patch.set_color(color * np.ones(3)) patch.set_edgecolor(Black * np.ones(3)) self.ax.add_patch(patch) if ind == num - 1: self.layers.append(patch) self.previous_depth = depth return len(self.layers) - 1
def create_board_figure(pcb, bom_row, layer=pcbnew.F_Cu, invert_axis=False): qty, value, footpr, highlight_refs = bom_row plt.figure(figsize=(5.8, 8.2)) ax = plt.subplot("111", aspect="equal") color_pad1 = "lightgray" color_pad2 = "#AA0000" color_pad3 = "#CC4444" color_bbox1 = "None" color_bbox2 = "#E9AFAF" # get board edges (assuming rectangular, axis aligned pcb) edge_coords = [] for d in pcb.GetDrawings(): if (d.GetLayer() == pcbnew.Edge_Cuts): edge_coords.append(d.GetStart()) edge_coords.append(d.GetEnd()) edge_coords = np.asarray(edge_coords) * 1e-6 board_xmin, board_ymin = edge_coords.min(axis=0) board_xmax, board_ymax = edge_coords.max(axis=0) # draw board edges rct = Rectangle((board_xmin, board_ymin), board_xmax - board_xmin, board_ymax - board_ymin, angle=0) rct.set_color("None") rct.set_edgecolor("black") rct.set_linewidth(3) ax.add_patch(rct) # add title ax.text(board_xmin + .5 * (board_xmax - board_xmin), board_ymin - 0.5, "%dx %s, %s" % (qty, value, footpr), wrap=True, horizontalalignment='center', verticalalignment='bottom')\ # add ref list textsize = 12 refdes_text = ", ".join(highlight_refs) if len(refdes_text) > 200: # limit the size to prevent truncation textsize = 10 if len(refdes_text) > 500: # limit the size to prevent truncation textsize = 8 if len(refdes_text) > 1100: textsize = 6 ax.text(board_xmin + .5 * (board_xmax - board_xmin), board_ymax + 0.5, ", ".join(highlight_refs), wrap=True, horizontalalignment='center', verticalalignment='top', fontsize=textsize) # draw parts for m in pcb.GetModules(): if m.GetLayer() != layer: continue ref, center = m.GetReference(), np.asarray(m.GetCenter()) * 1e-6 highlight = ref in highlight_refs # bounding box mrect = m.GetFootprintRect() mrect_pos = np.asarray(mrect.GetPosition()) * 1e-6 mrect_size = np.asarray(mrect.GetSize()) * 1e-6 rct = Rectangle(mrect_pos, mrect_size[0], mrect_size[1]) rct.set_color(color_bbox2 if highlight else color_bbox1) rct.set_alpha(0.7) rct.set_zorder(-1) if highlight: rct.set_linewidth(.1) rct.set_edgecolor(color_pad2) ax.add_patch(rct) # center marker if highlight: plt.plot(center[0], center[1], ".", markersize=mrect_size.min() / 4, color=color_pad2) # plot pads for p in m.Pads(): pos = np.asarray(p.GetPosition()) * 1e-6 #additional scaling pads result in strange effects on pads made #from multiple pads - so I removed the * 0.9 size = np.asarray(p.GetSize()) * 1e-6 is_pin1 = p.GetPadName() == "1" or p.GetPadName() == "A1" shape = p.GetShape() offset = p.GetOffset() # TODO: check offset # pad rect angle = p.GetOrientation() * 0.1 cos, sin = np.cos(np.pi / 180. * angle), np.sin(np.pi / 180. * angle) dpos = np.dot([[cos, -sin], [sin, cos]], -.5 * size) if shape == pcbnew.PAD_SHAPE_RECT: rct = Rectangle(pos + dpos, size[0], size[1], angle=angle) elif shape == pcbnew.PAD_SHAPE_ROUNDRECT: # subtract 2x corner radius from pad size, as FancyBboxPatch draws a rounded rectangle around the specified rectangle pad = p.GetRoundRectCornerRadius() * 1e-6 # the bottom-left corner of the FancyBboxPatch is the inside rectangle so need to compensate with the corner radius corneroffset = np.asarray([pad, pad]) #draw rounded patch rct = FancyBboxPatch(pos + dpos + corneroffset, size[0] - 2 * pad, size[1] - 2 * pad, boxstyle=matplotlib.patches.BoxStyle( "Round", pad=pad)) #and rotate it xy = pos + dpos tfm = matplotlib.transforms.Affine2D().rotate_deg_around( xy[0], xy[1], angle) + ax.transData rct.set_transform(tfm) elif shape == pcbnew.PAD_SHAPE_OVAL: rct = Ellipse(pos, size[0], size[1], angle=angle) elif shape == pcbnew.PAD_SHAPE_CIRCLE: rct = Ellipse(pos, size[0], size[0], angle=angle) elif shape == pcbnew.PAD_SHAPE_TRAPEZOID: #draw trapezoid from scratch sx = size[0] sy = size[1] delta = p.GetDelta()[1] * 1e-6 xy = np.array([[(sx + delta) / 2, sy / 2], [(sx - delta) / 2, -sy / 2], [(-sx + delta) / 2, -sy / 2], [(-sx - delta) / 2, sy / 2]]) xy = xy + pos rct = Polygon(xy) #and rotate it xy = pos # TODO DEBUG: based on corrections made in ROUNDRECT code above, the angle should NOT be negative(might be bug). No use case so ignored for now tfm = matplotlib.transforms.Affine2D().rotate_deg_around( xy[0], xy[1], -angle) + ax.transData rct.set_transform(tfm) else: print("Unsupported pad shape: {0} ".format(shape)) continue rct.set_linewidth(0) rct.set_color(color_pad2 if highlight else color_pad1) rct.set_zorder(1) # highlight pin1 if highlight and is_pin1: rct.set_color(color_pad3) rct.set_linewidth(.1) rct.set_edgecolor(color_pad2) ax.add_patch(rct) plt.xlim(board_xmin, board_xmax) plt.ylim(board_ymax, board_ymin) if (invert_axis): plt.gca().invert_xaxis() plt.axis('off')
class Annotate(object): def __init__(self, image,name, imgid): self.img = image self.imgname = name self.imgid = imgid self.i = 1 self.col = 'b' # deafult color for true positive label self.ax = plt.gca() # Initialize the Reactangle patch object with properties self.rect = Rectangle((0,0), 1, 1, alpha = 1,ls = 'solid',fill = False, clip_on = True,color = self.col) # Initialize two diagonally opposite co-ordinates of reactangle as None self.xc = None self.yc = None self.x0 = None self.y0 = None self.x1 = None self.y1 = None self.height = None self.width = None self.mx0 = None self.my0 = None self.mx1 = None self.my1 = None self.sizeModifier = 2 self.w = 30.0 self.h = 40.0 self.qkey = None #self.centers # The list that will store value of those two co-ordinates of # all the patches for storing into the file later self.xy = [] self.ax.add_patch(self.rect) # Initialize mpl connect object connect = self.ax.figure.canvas.mpl_connect # Create objects that will handle user initiated events # We are using three events # First event is button press event (on left key click)- # on which on_click function is called connect('button_press_event', self.on_click) connect('close_event', self.handle_close) # Second event to draw, in case a mistake in labelling is made, # deleting the patch requires redrawing the original canvas self.draw_cid = connect('draw_event', self.grab_background) # Third event - key press event # To change color of the patches when you want to switch between # true postive and false postive labels connect('key_press_event',self.colorChange) def objCreation(self): # The new reactangle object to use after blit function (clearing # the canvas and removing rectangle objects) self.rect = Rectangle((0,0), 1, 1, alpha = 1,ls = 'solid',fill = False, clip_on = True) self.xc = None # x co-ordinate of patch center self.yc = None # y co-ordinate of patch center self.x0 = None # top left x co-ordinate of patch center self.y0 = None # top left y co-ordinate of patch center self.x1 = None # lower right y co-ordinate of patch center self.y1 = None # lower right y co-ordinate of patch center self.sizeModifier = 2 # The amount by which width/height will increase/decrease self.w = 30.0 # Initial width self.h = 40.0 # Initial height # Aspect Ratio of 3/4 # Add the patch on the axes object of figure self.ax.add_patch(self.rect) def deletePrevious(self): ''' Deletes the latest patch that was drawn ''' # Clear the screen by calling blit function self.blit() # Remove the last patch co-ordinates from the list self.xy = self.xy[:-1] # Redraw all the rects except the previous ones for coords in self.xy: self.rect.set_width(coords[2] - coords[0]) self.rect.set_height(coords[3] - coords[1]) self.rect.set_xy((coords[0], coords[1])) self.rect.set_color(coords[4]) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.bbox) def resize(self,det): ''' Resizing at the same center, maintaing the same aspect ratio and using key only (without dragging) ''' # Resizing without dragging requires deleting previous patch # Saving the center, width, height of the patch before deleting it # As it will be used for reconstructing with increased/decreased size last_obj = self.xy[-1] # print last_obj xc = last_obj[-2] yc = last_obj[-1] col = last_obj[-3] w = last_obj[2] - last_obj[0] h = last_obj[3] - last_obj[1] self.deletePrevious() self.xc = xc self.yc = yc self.col = col self.w = w*det print self.w self.h = h*det self.drawRect() def handle_close(self,event): ''' if you ended up closing the plot using the plot's X button instead of 'q' key ''' if self.qkey != 'q': self.close_plot() def close_plot(self): ''' saving numpy patches and co-ordinates of the patches ''' print 'close' header = open('header.txt','a') ##print self.xy #self.xy = filter(lambda x: 0 not in np.shape(x) , self.xy) blue_patches = filter(lambda x: x[4]=='b',self.xy) for i, blue_patch_list in enumerate(blue_patches): topx = blue_patch_list[0] topy = blue_patch_list[1] botx = blue_patch_list[2] boty = blue_patch_list[3] patch_path = self.imgname[:-4] + '_blue_' + str(i) + '.npy' #Saving to database conn = psycopg2.connect("dbname='dot_pub_cams'") cursor = conn.cursor() cursor.execute("""UPDATE images SET labeled=TRUE WHERE id=%s""" % (self.imgid)) cursor.execute("""INSERT INTO labels (image, topx, topy, botx, boty, label, patch_path, type ) VALUES (%s, %s, %s, %s, %s, %s, '%s', '%s') """ % (self.imgid, topx, topy, botx, boty, 1, patch_path, "BLUE")) # Closing db connections conn.commit() cursor.close() conn.close() patch_array = self.img[topy:boty,topx:botx] if 0 not in np.shape(patch_array): np.save(patch_path, patch_array) header.write("%s" % self.imgname+',') for item in blue_patch_list[:5]: header.write("%s" % item+',') header.write('\n') red_patches = filter(lambda x: x[4]=='r',self.xy) for i, red_patch_list in enumerate(red_patches): topx = red_patch_list[0] topy = red_patch_list[1] botx = red_patch_list[2] boty = red_patch_list[3] patch_path = self.imgname[:-4] + '_red_' + str(i) + '.npy' #Saving to database conn = psycopg2.connect("dbname='dot_pub_cams'") cursor = conn.cursor() cursor.execute("""UPDATE images SET labeled=TRUE WHERE id=%s""" % (self.imgid)) cursor.execute("""INSERT INTO labels (image, topx, topy, botx, boty, label, patch_path, type ) VALUES (%s, %s, %s, %s, %s, %s, '%s', '%s') """ % (self.imgid, topx, topy, botx, boty, 1, patch_path, "RED")) # Closing db connections conn.commit() cursor.close() conn.close() patch_array = self.img[topy:boty,topx:botx] if 0 not in np.shape(patch_array): np.save(patch_path, patch_array) header.write("%s" % self.imgname+',') for item in red_patch_list[:5]: header.write("%s" % item+',') header.write('\n') # xy = self.xy[0] # patch = img[xy[1]:xy[3],xy[0]:xy[2]] # imgplot = plt.imshow(patch) # plt.show() plt.close() def colorChange(self,event): ''' To change color to take false positves into consideration - the default is color blue for true postive ''' print('press', event.key) sys.stdout.flush() if event.key == 'r': # red color # When 'r' key is pressed, the color of the next patch will be red self.col = 'r' elif event.key == 'b': # blue color # When 'b' key is pressed, the color of the next patch will be blue self.col = 'b' # Optional setting for drawing patched using spacebar # elif event.key == ' ': # self.on_click(event) elif event.key == 'd': # delete # When 'd' key is pressed, the latest patch drawn is deleted self.deletePrevious() elif event.key == 'c': # clear # When 'c' key is pressed, all the patches are cleared, only orignal background is present self.blit() self.xy = [] # Flush out the list as we don't want to consider any patch co-ordinates elif event.key == 'tab': # use tab to increase the aspect ratio of the patch self.resize(1.2) elif event.key == 'control': # use control key to decrease the aspect ratio of the patch self.resize(0.95) elif event.key == '2': # use control key to decrease the aspect ratio of the patch self.resize(0.85) elif event.key == '3': # use control key to decrease the aspect ratio of the patch self.resize(0.50) elif event.key == 'q': # quit plot, show up the next # save necessary labels and close the plot self.qkey = 'q' self.close_plot() elif event.key == '0': sys.exit()
class Annotate(object): def __init__(self, image, name, imgid): self.zone = { 'imgid': None, 'location_x': None, 'location_y': None, 'zone': '0000', 'agg_zone': '0000' } self.zone['imgid'] = imgid self.img = image self.imgname = name self.imgid = imgid self.i = 1 self.col = 'b' # deafult color for true positive label self.ax = plt.gca() # Initialize the Reactangle patch object with properties self.rect = Rectangle((0, 0), 1, 1, alpha=1, ls='solid', fill=False, clip_on=True, color=self.col) # Initialize two diagonally opposite co-ordinates of reactangle as None self.xc = None self.yc = None self.x0 = None self.y0 = None self.x1 = None self.y1 = None self.xmin = None self.xmax = None self.ymin = None self.ymax = None self.sizeModifier = 2 self.w = 30.0 self.h = 40.0 self.qkey = None #self.centers # The list that will store value of those two co-ordinates of # all the patches for storing into the file later self.xy = [] self.ax.add_patch(self.rect) # Initialize mpl connect object connect = self.ax.figure.canvas.mpl_connect # Create objects that will handle user initiated events # We are using three events # First event is button press event (on left key click)- # on which on_click function is called connect('button_press_event', self.on_click) connect('close_event', self.handle_close) # Second event to draw, in case a mistake in labelling is made, # deleting the patch requires redrawing the original canvas self.draw_cid = connect('draw_event', self.grab_background) # Third event - key press event # To change color of the patches when you want to switch between # true postive and false postive labels connect('key_press_event', self.colorChange) def objCreation(self): # The new reactangle object to use after blit function (clearing # the canvas and removing rectangle objects) self.rect = Rectangle((0, 0), 1, 1, alpha=1, ls='solid', fill=False, clip_on=True) self.xc = None # x co-ordinate of patch center self.yc = None # y co-ordinate of patch center self.x0 = None # top left x co-ordinate of patch center self.y0 = None # top left y co-ordinate of patch center self.x1 = None # lower right x co-ordinate of patch center self.y1 = None # lower right y co-ordinate of patch center self.sizeModifier = 2 # The amount by which width/height will increase/decrease self.w = 30.0 # Initial width self.h = 40.0 # Initial height # Aspect Ratio of 3/4 # Add the patch on the axes object of figure self.ax.add_patch(self.rect) def deletePrevious(self): ''' Deletes the latest patch that was drawn ''' # Clear the screen by calling blit function self.blit() # Remove the last patch co-ordinates from the list self.xy = self.xy[:-1] # Redraw all the rects except the previous ones for coords in self.xy: self.rect.set_width(coords[2] - coords[0]) self.rect.set_height(coords[3] - coords[1]) self.rect.set_xy((coords[0], coords[1])) self.rect.set_color(coords[4]) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.bbox) def resize(self, det): ''' Resizing at the same center, maintaing the same aspect ratio and using key only (without dragging) ''' # Resizing without dragging requires deleting previous patch # Saving the center, width, height of the patch before deleting it # As it will be used for reconstructing with increased/decreased size last_obj = self.xy[-1] # print last_obj xc = last_obj[-2] yc = last_obj[-1] col = last_obj[-3] w = last_obj[2] - last_obj[0] h = last_obj[3] - last_obj[1] self.deletePrevious() self.xc = xc self.yc = yc self.col = col self.w = w * det print self.w self.h = h * det self.drawRect() def colorChange(self, event): ''' To change color to take false positves into consideration - the default is color blue for true postive ''' print('press', event.key) sys.stdout.flush() if event.key == 'r': # red color # When 'r' key is pressed, the color of the next patch will be red self.col = 'r' elif event.key == 'b': # blue color # When 'b' key is pressed, the color of the next patch will be blue self.col = 'b' elif event.key == 'd': # delete # When 'd' key is pressed, the latest patch drawn is deleted self.deletePrevious() elif event.key == 'c': # clear # When 'c' key is pressed, all the patches are cleared, only orignal background is present self.blit() self.xy = [] # Flush out the list as we don't want to consider any patch co-ordinates elif event.key == 'tab': # use tab to increase the aspect ratio of the patch self.resize(1.2) elif event.key == 'control': # use control key to decrease the aspect ratio of the patch self.resize(0.95) elif event.key == '2': # use control key to decrease the aspect ratio of the patch self.resize(0.85) elif event.key == '3': # use control key to decrease the aspect ratio of the patch self.resize(0.50) elif event.key == 'q': # quit plot, show up the next # save necessary labels and close the plot self.qkey = 'q' self.close_plot() elif event.key == '0': sys.exit() def handle_close(self, event): ''' if you ended up closing the plot using the plot's X button instead of 'q' key ''' if self.qkey != 'q': self.close_plot() def close_plot(self): ''' saving numpy patches and co-ordinates of the patches ''' if self.zone['location_x'] == None: self.zone['location_x'], self.zone['location_y'] = [None], [None] df = pd.DataFrame.from_dict(self.zone) with open('pic_1_500_location.csv', 'a') as f: df.to_csv(f, header=False) print 'close' def on_click(self, event): #print 'click1' self.xc = event.xdata self.yc = event.ydata # Chosing Aspect Ratio of 3/4 self.w = 30.0 self.h = 40.0 self.xy.append([self.xc, self.yc]) self.zone['location_x'] = [self.xc] self.zone['location_y'] = [self.yc] print self.zone['location_x'][0], self.zone['location_y'][0] img_center_x = (self.xmax + self.xmin) / 2.0 img_center_y = (self.ymax + self.ymin) / 2.0 binZone = int(self.zone['agg_zone'], 2) if self.zone['location_x'][0] > img_center_x: if self.zone['location_y'][0] > img_center_y: self.zone['zone'] = '0001' self.zone['agg_zone'] = "{0:b}".format(binZone | 1) elif self.zone['location_y'][0] <= img_center_y: self.zone['zone'] = '1000' self.zone['agg_zone'] = "{0:b}".format(binZone | 8) elif self.zone['location_x'][0] <= img_center_x: if self.zone['location_y'][0] > img_center_y: self.zone['zone'] = '0010' self.zone['agg_zone'] = "{0:b}".format(binZone | 2) elif self.zone['location_y'][0] <= img_center_y: self.zone['zone'] = '0100' self.zone['agg_zone'] = "{0:b}".format(binZone | 4) self.zone['agg_zone'] = self.zone['agg_zone'].zfill(4) print self.zone['zone'] #print self.zone['agg_zone'] df = pd.DataFrame.from_dict(self.zone) with open('pic_1_500_location.csv', 'a') as f: df.to_csv(f, header=False) def drawRect(self): # Set the two diagonally opposite co-ordinates of the patch by width and height self.x0 = self.xc - self.w / 2 self.y0 = self.yc - self.h / 2 self.x1 = self.xc + self.w / 2 self.y1 = self.yc + self.h / 2 # set the stated width self.rect.set_width(self.w) # set the stated height self.rect.set_height(self.h) # set the top left corner self.rect.set_xy((self.x0, self.y0)) # Set the color of the reactangle - can be blue/red depending on postive/negative label respectively self.rect.set_color(self.col) self.ax.draw_artist(self.rect) # Blit is used to successively retain and display patches on the screen # Else Successively drawing one patch will remove the last drawn patch self.ax.figure.canvas.blit(self.ax.bbox) # The following three functions taken from # http://stackoverflow.com/questions/29277080/efficient-matplotlib-redrawing def safe_draw(self): """Temporarily disconnect the draw_event callback to avoid recursion""" canvas = self.ax.figure.canvas canvas.mpl_disconnect(self.draw_cid) canvas.draw() self.draw_cid = canvas.mpl_connect('draw_event', self.grab_background) def grab_background(self, event=None): """ When the figure is resized, hide the rect, draw everything, and update the background. """ self.rect.set_visible(False) self.safe_draw() # With most backends (e.g. TkAgg), we could grab (and refresh, in # self.blit) self.ax.bbox instead of self.fig.bbox, but Qt4Agg, and # some others, requires us to update the _full_ canvas, instead. #self.background = self.ax.figure.canvas.copy_from_bbox(self.ax.figure.bbox) self.rect.set_visible(True) # self.blit() def blit(self): """ Efficiently update the figure, without needing to redraw the "background" artists. """ self.objCreation() self.ax.figure.canvas.restore_region(self.background) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.figure.bbox)
def generate(self): from matplotlib.patches import Rectangle from matplotlib.text import Text fig_size_x = self._box_size[0] * self._symbols_per_line if self._show_labels: fig_size_x += self._label_size if self._show_numbers: fig_size_x += self._number_size fig_size_x += 2 * self._margin seq_num = self._alignment.trace.shape[1] seq_len = self._alignment.trace.shape[0] line_count = seq_len // self._symbols_per_line # Only extend line count by 1 if there is a remainder # (remaining symbols) if seq_len % self._symbols_per_line != 0: line_count += 1 fig_size_y = line_count * self._box_size[1] * seq_num fig_size_y += (line_count - 1) * self._spacing fig_size_y += 2 * self._margin fig = self.create_figure(size=(fig_size_x, fig_size_y)) ### Draw labels ### if self._labels is not None: y = fig_size_y - self._margin y -= self._box_size[1] / 2 for i in range(line_count): for j in range(seq_num): label = self._labels[j] text = Text(self._margin, y, label, color="black", ha="left", va="center", size=self._label_font_size, figure=fig, fontproperties=self._label_font) fig.texts.append(text) y -= self._box_size[1] y -= self._spacing ### Draw numbers ### if self._show_numbers: y = fig_size_y - self._margin y -= self._box_size[1] / 2 for i in range(line_count): for j in range(seq_num): if i == line_count - 1: # Last line -> get number of last column in trace trace_pos = len(self._alignment.trace) - 1 else: trace_pos = (i + 1) * self._symbols_per_line - 1 seq_index = self._get_last_real_index( self._alignment, trace_pos, j) number = self._number_func[j](seq_index) text = Text(fig_size_x - self._margin, y, str(number), color="black", ha="right", va="center", size=self._number_font_size, figure=fig, fontproperties=self._number_font) fig.texts.append(text) y -= self._box_size[1] y -= self._spacing ### Draw symbols in boxes ### x_start = self._label_size if self._labels is not None else 0 x_start += self._margin y_start = fig_size_y - self._box_size[1] y_start -= self._margin x = x_start line_pos = 0 for i in range(seq_len): y = y_start for j in range(seq_num): if self._alignment.trace[i, j] != -1: symbol = self._alignment.sequences[j] \ [self._alignment.trace[i,j]] else: symbol = "-" color = self.get_color(self._alignment, i, j) box = Rectangle((x, y), self._box_size[0] - 1, self._box_size[1] - 1) text = Text(x + self._box_size[0] / 2, y + self._box_size[1] / 2, symbol, color="black", ha="center", va="center", size=self._symbol_font_size, figure=fig, fontproperties=self._symbol_font) if self._color_symbols: box.set_color("None") text.set_color(color) else: box.set_color(color) fig.patches.append(box) fig.texts.append(text) y -= self._box_size[1] line_pos += 1 if line_pos >= self._symbols_per_line: line_pos = 0 x = x_start y_start -= seq_num * self._box_size[1] + self._spacing else: x += self._box_size[0] return fig
class Annotate(object): def __init__(self, image,name): self.img = image self.imgname = name self.i = 1 self.col = 'b' # deafult color for true positive label self.ax = plt.gca() # Initialize the Reactangle patch object with properties self.rect = Rectangle((0,0), 1, 1, alpha = 1,ls = 'solid',fill = False, clip_on = True,color = self.col) # Initialize two diagonally opposite co-ordinates of reactangle as None self.xc = None self.yc = None self.x0 = None self.y0 = None self.x1 = None self.y1 = None self.sizeModifier = 2 self.w = 30.0 self.h = 40.0 self.qkey = None self.objList = [] #self.centers # The list that will store value of those two co-ordinates of # all the patches for storing into the file later self.xy = [] self.ax.add_patch(self.rect) # Initialize mpl connect object connect = self.ax.figure.canvas.mpl_connect # Create objects that will handle user initiated events # We are using three events # First event is button press event (on left key click)- # on which on_click function is called self.ax.figure.canvas.mpl_connect('button_press_event', self.on_click) self.ax.figure.canvas.mpl_connect('close_event', self.handle_close) # Second event to draw, in case a mistake in labelling is made, # deleting the patch requires redrawing the original canvas self.draw_cid = connect('draw_event', self.grab_background) # Third event - key press event # To change color of the patches when you want to switch between # true postive and false postive labels self.ax.figure.canvas.mpl_connect('key_press_event',self.colorChange) def objCreation(self): # The new reactangle object to use after blit function (clearing # the canvas and removing rectangle objects) self.rect = Rectangle((0,0), 1, 1, alpha = 1,ls = 'solid',fill = False, clip_on = True) self.xc = None # x co-ordinate of patch center self.yc = None # y co-ordinate of patch center self.x0 = None # top left x co-ordinate of patch center self.y0 = None # top left y co-ordinate of patch center self.x1 = None # lower right y co-ordinate of patch center self.y1 = None # lower right y co-ordinate of patch center self.sizeModifier = 2 # The amount by which width/height will increase/decrease self.w = 30.0 # Initial width self.h = 40.0 # Initial height # Aspect Ratio of 3/4 # Add the patch on the axes object of figure self.ax.add_patch(self.rect) def deletePrevious(self): ''' Deletes the latest patch that was drawn ''' # Clear the screen by calling blit function self.blit() # Remove the last patch co-ordinates from the list self.xy = self.xy[:-1] # Redraw all the rects except the previous ones for coords in self.xy: self.rect.set_width(coords[2] - coords[0]) self.rect.set_height(coords[3] - coords[1]) self.rect.set_xy((coords[0], coords[1])) self.rect.set_color(coords[4]) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.bbox) def resize(self,det): ''' Resizing at the same center, maintaing the same aspect ratio and using key only (without dragging) ''' # Resizing without dragging requires deleting previous patch # Saving the center, width, height of the patch before deleting it # As it will be used for reconstructing with increased/decreased size last_obj = self.xy[-1] # print last_obj xc = last_obj[-2] yc = last_obj[-1] col = last_obj[-3] w = last_obj[2] - last_obj[0] h = last_obj[3] - last_obj[1] self.deletePrevious() self.xc = xc self.yc = yc self.col = col self.w = w*det print self.w self.h = h*det self.drawRect() def handle_close(self,event): ''' if you ended up closing the plot using the plot's X button instead of 'q' key ''' if self.qkey != 'q': self.close_plot() def close_plot(self): ''' saving numpy patches and co-ordinates of the patches ''' b = 0 r = 0 print os.getcwd() print 'close' header = open('header.txt','a') ##print self.xy #self.xy = filter(lambda x: 0 not in np.shape(x) , self.xy) blue_patches = filter(lambda x: x[4]=='b',self.xy) for blue_patch_list in blue_patches: xy = blue_patch_list name = str('NumpyPatches\\')+str(self.imgname)+'_blue'+str(b)+'.npy' patch_array = self.img[xy[1]:xy[3],xy[0]:xy[2]] if 0 not in np.shape(patch_array): header.write("%s" % self.imgname+',') print os.getcwd() np.save(name, patch_array) b = b+1 for item in xy[:5]: header.write("%s" % item+',') header.write('\n') red_patches = filter(lambda x: x[4]=='r',self.xy) for red_patch_list in red_patches: xy = red_patch_list name = self.imgname+'_red'+str(r)+'.npy' patch_array = self.img[xy[1]:xy[3],xy[0]:xy[2]] if 0 not in np.shape(patch_array): header.write("%s" % self.imgname+',') np.save(name, patch_array) r = r+1 for item in xy[:5]: header.write("%s" % item+',') header.write('\n') # xy = self.xy[0] # patch = img[xy[1]:xy[3],xy[0]:xy[2]] # imgplot = plt.imshow(patch) # plt.show() plt.close() def colorChange(self,event): ''' To change color to take false positves into consideration - the default is color blue for true postive ''' print('press', event.key) sys.stdout.flush() if event.key == 'r': # red color # When 'r' key is pressed, the color of the next patch will be red self.col = 'r' elif event.key == 'b': # blue color # When 'b' key is pressed, the color of the next patch will be blue self.col = 'b' # Optional setting for drawing patched using spacebar # elif event.key == ' ': # self.on_click(event) elif event.key == 'd': # delete # When 'd' key is pressed, the latest patch drawn is deleted #self.deletePrevious() nr = self.objList[-1] nr.set_visible(False) elif event.key == 'c': # clear # When 'c' key is pressed, all the patches are cleared, only orignal background is present self.blit() self.xy = [] # Flush out the list as we don't want to consider any patch co-ordinates elif event.key == 'tab': # use tab to increase the aspect ratio of the patch self.resize(1.2) elif event.key == 'control': # use control key to decrease the aspect ratio of the patch self.resize(0.95) elif event.key == '2': # use control key to decrease the aspect ratio of the patch self.resize(0.85) elif event.key == '3': # use control key to decrease the aspect ratio of the patch self.resize(0.50) elif event.key == 'q': # quit plot, show up the next # save necessary labels and close the plot self.qkey = 'q' self.close_plot() elif event.key == '0': sys.exit() def on_click(self, event): ''' Using one click on the center of the human, make a patch of fixed aspect ratio ''' # The first click to mark center point of the rectangle and save the coordinates print 'click1' self.xc = event.xdata self.yc = event.ydata # Chosing Aspect Ratio of 3/4 self.w = 30.0 self.h = 40.0 self.drawRect() def drawRect(self): # Set the two diagonally opposite co-ordinates of the patch by width and height self.x0 = self.xc-self.w/2 self.y0 = self.yc-self.h/2 self.x1 = self.xc+self.w/2 self.y1 = self.yc+self.h/2 # set the stated width self.rect.set_width(self.w) # set the stated height self.rect.set_height(self.h) # set the top left corner self.rect.set_xy((self.x0, self.y0 )) # append to the list of patch co-ordinates self.xy.append([self.x0,self.y0,self.x1,self.y1,self.col,self.xc,self.yc]) #print self.xy self.objList.append(self.rect) # Set the color of the reactangle - can be blue/red depending on postive/negative label respectively self.rect.set_color(self.col) self.ax.draw_artist(self.rect) self.ax.figure.canvas.update() # Blit is used to successively retain and display patches on the screen # Else Successively drawing one patch will remove the last drawn patch #self.ax.figure.canvas.blit(self.ax.bbox) # The following three functions taken from # http://stackoverflow.com/questions/29277080/efficient-matplotlib-redrawing def safe_draw(self): """Temporarily disconnect the draw_event callback to avoid recursion""" canvas = self.ax.figure.canvas canvas.mpl_disconnect(self.draw_cid) canvas.draw() self.draw_cid = canvas.mpl_connect('draw_event', self.grab_background) def grab_background(self, event=None): """ When the figure is resized, hide the rect, draw everything, and update the background. """ self.rect.set_visible(False) self.safe_draw() # With most backends (e.g. TkAgg), we could grab (and refresh, in # self.blit) self.ax.bbox instead of self.fig.bbox, but Qt4Agg, and # some others, requires us to update the _full_ canvas, instead. self.background = self.ax.figure.canvas.copy_from_bbox(self.ax.figure.bbox) self.rect.set_visible(True) self.blit() def blit(self): """ Efficiently update the figure, without needing to redraw the "background" artists. """ self.objCreation() self.ax.figure.canvas.restore_region(self.background) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.figure.bbox)
def getRectangle(x, y, width, height, color, alpha=1): rect = Rectangle((x, y), width, height) rect.set_color(color) rect.set_alpha(alpha) return rect
class Annotate(object): def __init__(self, image,name, imgid): self.img = image self.imgname = name self.imgid = imgid self.i = 1 self.col = 'b' # deafult color for true positive label self.ax = plt.gca() # Initialize the Reactangle patch object with properties self.rect = Rectangle((0,0), 1, 1, alpha = 1,ls = 'solid',fill = False, clip_on = True,color = self.col) # Initialize two diagonally opposite co-ordinates of reactangle as None self.xc = None self.yc = None self.x0 = None self.y0 = None self.x1 = None self.y1 = None self.height = None self.width = None self.mx0 = None self.my0 = None self.mx1 = None self.my1 = None self.sizeModifier = 2 self.w = 30.0 self.h = 40.0 self.qkey = None #self.centers # The list that will store value of those two co-ordinates of # all the patches for storing into the file later self.xy = [] self.ax.add_patch(self.rect) # Initialize mpl connect object connect = self.ax.figure.canvas.mpl_connect # Create objects that will handle user initiated events # We are using three events # First event is button press event (on left key click)- # on which on_click function is called connect('button_press_event', self.on_click) connect('close_event', self.handle_close) # Second event to draw, in case a mistake in labelling is made, # deleting the patch requires redrawing the original canvas self.draw_cid = connect('draw_event', self.grab_background) # Third event - key press event # To change color of the patches when you want to switch between # true postive and false postive labels connect('key_press_event',self.colorChange) def objCreation(self): # The new reactangle object to use after blit function (clearing # the canvas and removing rectangle objects) self.rect = Rectangle((0,0), 1, 1, alpha = 1,ls = 'solid',fill = False, clip_on = True) self.xc = None # x co-ordinate of patch center self.yc = None # y co-ordinate of patch center self.x0 = None # top left x co-ordinate of patch center self.y0 = None # top left y co-ordinate of patch center self.x1 = None # lower right y co-ordinate of patch center self.y1 = None # lower right y co-ordinate of patch center self.sizeModifier = 2 # The amount by which width/height will increase/decrease self.w = 30.0 # Initial width self.h = 40.0 # Initial height # Aspect Ratio of 3/4 # Add the patch on the axes object of figure self.ax.add_patch(self.rect) def deletePrevious(self): ''' Deletes the latest patch that was drawn ''' # Clear the screen by calling blit function self.blit() # Remove the last patch co-ordinates from the list self.xy = self.xy[:-1] # Redraw all the rects except the previous ones for coords in self.xy: self.rect.set_width(coords[2] - coords[0]) self.rect.set_height(coords[3] - coords[1]) self.rect.set_xy((coords[0], coords[1])) self.rect.set_color(coords[4]) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.bbox) def resize(self,det): ''' Resizing at the same center, maintaing the same aspect ratio and using key only (without dragging) ''' # Resizing without dragging requires deleting previous patch # Saving the center, width, height of the patch before deleting it # As it will be used for reconstructing with increased/decreased size last_obj = self.xy[-1] # print last_obj xc = last_obj[-2] yc = last_obj[-1] col = last_obj[-3] w = last_obj[2] - last_obj[0] h = last_obj[3] - last_obj[1] self.deletePrevious() self.xc = xc self.yc = yc self.col = col self.w = w*det print self.w self.h = h*det self.drawRect() def handle_close(self,event): ''' if you ended up closing the plot using the plot's X button instead of 'q' key ''' if self.qkey != 'q': self.close_plot() def skipCrowd(self): '''Function to skip crowded scene, label them as crowd in the db''' conn = psycopg2.connect("dbname='dot_pub_cams'") cursor = conn.cursor() cursor.execute("""UPDATE images SET labeled=TRUE, set_type='crowd' WHERE id=%s""" % (self.imgid)) # Closing db connections conn.commit() cursor.close() conn.close() plt.close() def close_plot(self): ''' saving numpy patches and co-ordinates of the patches ''' print 'close' header = open('log.txt','a') header.write("Image id:%s" % (self.imgid)) #Blue Bounding Boxes blue_patches = filter(lambda x: x[4]=='b',self.xy) ##print self.xy #Saving to database conn = psycopg2.connect("dbname='dot_pub_cams'") cursor = conn.cursor() blueCount = 0 for blue_patch_list in blue_patches: if len(blue_patch_list) <4: continue topx = blue_patch_list[0] topy = blue_patch_list[1] botx = blue_patch_list[2] boty = blue_patch_list[3] patch_array = self.img[topy:boty,topx:botx] if 0 not in np.shape(patch_array): patch_path = self.imgname[:-4] + '_pos_' + str(blueCount) + '.npy' blueCount+=1 cursor.execute("""INSERT INTO labels (image, topx, topy, botx, boty, label, patch_path, type ) VALUES (%s, %s, %s, %s, %s, %s, '%s', '%s') """ % (self.imgid, topx, topy, botx, boty, 1, patch_path, "pos")) np.save(patch_path, patch_array) os.chmod(patch_path, 0777) header.write("%s" % self.imgname+',') for item in blue_patch_list[:5]: header.write("%s" % item+',') header.write('\n') cursor.execute("""UPDATE images SET labeled=TRUE, ped_count=%s WHERE id=%s""" % (blueCount, self.imgid)) red_patches = filter(lambda x: x[4]=='r',self.xy) for i, red_patch_list in enumerate(red_patches): if len(red_patch_list) <4: continue topx = red_patch_list[0] topy = red_patch_list[1] botx = red_patch_list[2] boty = red_patch_list[3] patch_array = self.img[topy:boty,topx:botx] if 0 in np.shape(patch_array): i-=1 if 0 not in np.shape(patch_array): patch_path = self.imgname[:-4] + '_neg_' + str(i) + '.npy' cursor.execute("""INSERT INTO labels (image, topx, topy, botx, boty, label, patch_path, type ) VALUES (%s, %s, %s, %s, %s, %s, '%s', '%s') """ % (self.imgid, topx, topy, botx, boty, 1, patch_path, "neg")) np.save(patch_path, patch_array) os.chmod(patch_path, 0777) header.write("%s" % self.imgname+',') for item in red_patch_list[:5]: header.write("%s" % item+',') header.write('\n') # Closing db connections conn.commit() cursor.close() conn.close() plt.close() def colorChange(self,event): ''' To change color to take false positves into consideration - the default is color blue for true postive ''' print('press', event.key) sys.stdout.flush() if event.key == 'r': # red color # When 'r' key is pressed, the color of the next patch will be red self.col = 'r' elif event.key == 'b': # blue color # When 'b' key is pressed, the color of the next patch will be blue self.col = 'b' # Optional setting for drawing patched using spacebar # elif event.key == ' ': # self.on_click(event) elif event.key == 'e': # escape # When 'e' key is pressed, escape the image label it as crowd self.skipCrowd() elif event.key == 't': # skip # When 'e' key is pressed, escape the image label it as crowd plt.close() elif event.key == 'd': # delete # When 'd' key is pressed, the latest patch drawn is deleted self.deletePrevious() elif event.key == 'c': # clear # When 'c' key is pressed, all the patches are cleared, only orignal background is present self.blit() self.xy = [] # Flush out the list as we don't want to consider any patch co-ordinates elif event.key == 'tab': # use tab to increase the aspect ratio of the patch self.resize(1.2) elif event.key == 'control': # use control key to decrease the aspect ratio of the patch self.resize(0.95) elif event.key == '2': # use control key to decrease the aspect ratio of the patch self.resize(0.85) elif event.key == '3': # use control key to decrease the aspect ratio of the patch self.resize(0.50) elif event.key == 'q': # quit plot, show up the next # save necessary labels and close the plot self.qkey = 'q' self.close_plot() elif event.key == '0': sys.exit() def on_click(self, event): ''' Using two diagonally opposite clicks to draw a reactangle ''' self.i = self.i + 1 if self.i%2 == 0: # The first click to mark one point of the rectangle and save the coordinates print 'click1' self.mx0 = event.xdata self.my0 = event.ydata if self.i%2 == 1: # on second click - the rectangle should show up print 'click2' self.mx1 = event.xdata self.my1 = event.ydata self.drawRect() def drawRect(self): self.mx0, self.y0 = (self.mx0, self.my0) if (self.my1>self.my0) else (self.mx1,self.my1) self.mx1, self.y1 = (self.mx0, self.my0) if (self.my1<self.my0) else (self.mx1,self.my1) # Set the two diagonally opposite co-ordinates of the patch by width and height self.height = self.y1 - self.y0 self.width = 3.0/4.0 * self.height self.x0 = self.mx0 - self.width/2 self.x1 = self.mx0 + self.width/2 print self.x0, self.x1 self.xy.append([self.x0,self.y0,self.x1,self.y1,self.col]) print self.xy # Set the width and height of the rectangle patch as these two alone can characterize the patch self.rect.set_width(self.width) self.rect.set_height(self.height) self.rect.set_xy((self.x0, self.y0)) # Set the color of the reactangle - can be blue/red depending on postive/negative label respectively self.rect.set_color(self.col) self.ax.draw_artist(self.rect) # Blit is used to successively retain and display patches on the screen # Else Successively drawing one patch will remove the last drawn patch self.ax.figure.canvas.blit(self.ax.bbox) # The following three functions taken from # http://stackoverflow.com/questions/29277080/efficient-matplotlib-redrawing def safe_draw(self): """Temporarily disconnect the draw_event callback to avoid recursion""" canvas = self.ax.figure.canvas canvas.mpl_disconnect(self.draw_cid) canvas.draw() self.draw_cid = canvas.mpl_connect('draw_event', self.grab_background) def grab_background(self, event=None): """ When the figure is resized, hide the rect, draw everything, and update the background. """ self.rect.set_visible(False) self.safe_draw() # With most backends (e.g. TkAgg), we could grab (and refresh, in # self.blit) self.ax.bbox instead of self.fig.bbox, but Qt4Agg, and # some others, requires us to update the _full_ canvas, instead. self.background = self.ax.figure.canvas.copy_from_bbox(self.ax.figure.bbox) self.rect.set_visible(True) # self.blit() def blit(self): """ Efficiently update the figure, without needing to redraw the "background" artists. """ self.objCreation() self.ax.figure.canvas.restore_region(self.background) self.ax.draw_artist(self.rect) self.ax.figure.canvas.blit(self.ax.figure.bbox)
def mplu_realsquare(ax, x, y, x_side, **kwargs): _color = kwargs.get('color', 'black') y_side = convert_x_len_in_equivalent_y_len(ax, x_side) rect = Rectangle((x, y), x_side, y_side) rect.set_color(_color) ax.add_artist(rect)
def create_board_figure(pcb, bom_row, layer=pcbnew.F_Cu): qty, value, footpr, highlight_refs = bom_row plt.figure(figsize=(5.8, 8.2)) ax = plt.subplot("111", aspect="equal") color_pad1 = "lightgray" color_pad2 = "#AA0000" color_bbox1 = "None" color_bbox2 = "#E9AFAF" # get board edges (assuming rectangular, axis aligned pcb) edge_coords = [] for d in pcb.GetDrawings(): if (d.GetLayer() == pcbnew.Edge_Cuts): edge_coords.append(d.GetStart()) edge_coords.append(d.GetEnd()) edge_coords = np.asarray(edge_coords) * 1e-6 board_xmin, board_ymin = edge_coords.min(axis=0) board_xmax, board_ymax = edge_coords.max(axis=0) # draw board edges rct = Rectangle((board_xmin, board_ymin), board_xmax - board_xmin, board_ymax - board_ymin, angle=0) rct.set_color("None") rct.set_edgecolor("black") rct.set_linewidth(3) ax.add_patch(rct) # add title ax.text(board_xmin + .5 * (board_xmax - board_xmin), board_ymin - 0.5, "%dx %s, %s" % (qty, value, footpr), wrap=True, horizontalalignment='center', verticalalignment='bottom')\ # add ref list ax.text(board_xmin + .5 * (board_xmax - board_xmin), board_ymax + 0.5, ", ".join(highlight_refs), wrap=True, horizontalalignment='center', verticalalignment='top') # draw parts for m in pcb.GetModules(): if m.GetLayer() != layer: continue ref, center = m.GetReference(), np.asarray(m.GetCenter()) * 1e-6 highlight = ref in highlight_refs # bounding box mrect = m.GetFootprintRect() mrect_pos = np.asarray(mrect.GetPosition()) * 1e-6 mrect_size = np.asarray(mrect.GetSize()) * 1e-6 rct = Rectangle(mrect_pos, mrect_size[0], mrect_size[1]) rct.set_color(color_bbox2 if highlight else color_bbox1) rct.set_zorder(-1) if highlight: rct.set_linewidth(.1) rct.set_edgecolor(color_pad2) ax.add_patch(rct) # center marker if highlight: plt.plot(center[0], center[1], ".", markersize=mrect_size.min(), color=color_pad2) # plot pads for p in m.Pads(): pos = np.asarray(p.GetPosition()) * 1e-6 size = np.asarray(p.GetSize()) * 1e-6 * .9 shape = p.GetShape() offset = p.GetOffset() # TODO: check offset # pad rect angle = p.GetOrientation() * 0.1 cos, sin = np.cos(np.pi / 180. * angle), np.sin(np.pi / 180. * angle) dpos = np.dot([[cos, -sin], [sin, cos]], -.5 * size) if shape == 1: rct = Rectangle(pos + dpos, size[0], size[1], angle=angle) elif shape == 2: rct = Rectangle(pos + dpos, size[0], size[1], angle=angle) elif shape == 0: rct = Ellipse(pos, size[0], size[1], angle=angle) else: print("Unsupported pad shape") continue rct.set_color(color_pad2 if highlight else color_pad1) rct.set_zorder(1) ax.add_patch(rct) plt.xlim(board_xmin, board_xmax) plt.ylim(board_ymax, board_ymin) plt.axis('off')
import numpy as np from numpy.random import rand if 0: # simple picking, lines, rectangles and text fig, (ax1, ax2) = plt.subplots(2, 1) ax1.set_title('click on points, rectangles or text', picker=True) ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red')) line, = ax1.plot(rand(100), 'o', picker=5) # 5 points tolerance # pick the rectangle bars = ax2.bar(range(10), rand(10), picker=False) for label in ax2.get_xticklabels(): # make the xtick labels pickable label.set_picker(True) rect = Rectangle((0,0),5,5,edgecolor='red') rect.set_color('red') rect.set_picker(True) ax2.add_patch(rect) def onpick1(event): if isinstance(event.artist, Line2D): thisline = event.artist xdata = thisline.get_xdata() ydata = thisline.get_ydata() ind = event.ind print('onpick1 line:', zip(np.take(xdata, ind), np.take(ydata, ind))) elif isinstance(event.artist, Rectangle): print(event.artist.get_xy()) print(event.artist.get_width(),event.artist.get_height()) patch = event.artist print('onpick1 patch:', patch.get_path())
class Well: """ A class for individual wells in the matplotlib plot """ def __init__(self, center, shape, size_param, status=WStatus.empty): self.center = center self.shape = shape self.size_param = size_param self.status = status if shape == 'circle': self.marker = Circle(self.center, radius=size_param, color=self.status.color(), zorder=0) elif shape == 'square': self.marker = Rectangle((self.center[0] - self.size_param / 2, self.center[1] - self.size_param / 2), width=size_param, height=size_param, color=self.status.color(), zorder=0) def markEmpty(self): self.status = WStatus.empty self.marker.set_color(self.status.color()) self.marker.zorder = 1 def markDiscarded(self): self.status = WStatus.discarded self.marker.set_color(self.status.color()) self.marker.zorder = 3 def markFilled(self): self.status = WStatus.filled self.marker.set_color(self.status.color()) self.marker.zorder = 2 def markTarget(self): self.status = WStatus.target self.marker.set_color(self.status.color()) self.marker.zorder = 3 def markControl(self): self.status = WStatus.control self.marker.set_color(self.status.color()) self.marker.zorder = 3 def markUnused(self): self.status = WStatus.unused self.marker.set_color(self.status.color()) self.marker.zorder = 0 def markRescan(self): self.status = WStatus.rescan self.marker.set_color(self.status.color()) self.marker.zorder = 5 def setMarker(self, shape, size): self.shape = shape self.size_param = size if shape == 'circle': self.marker = Circle(self.center, radius=self.size_param, color=self.status.color(), zorder=0) elif shape == 'square': self.marker = Rectangle((self.center[0] - self.size_param / 2, self.center[1] - self.size_param / 2), width=self.size_param, height=self.size_param, color=self.status.color(), zorder=0)
# Figure fig = plt.figure(figsize=(13, 8)) ax = fig.gca() ax.scatter(stateSol[0], stateSol[2], s=20, c='b') #stateSol[:,0] # LumpedMass payload lumpedPayload = Circle((stateSol[0, 0], stateSol[0, 0]), 13) Circle.set_color(lumpedPayload, '0.75') Circle.set_alpha(lumpedPayload, 0.1) ax.add_patch(lumpedPayload) # art3d.pathpatch_2d_to_3d(plane, z=0, zdir="z") # rectangular payload rectPayload = Rectangle((stateSol[0, 0], stateSol[0, 0]), 13, 20) Rectangle.set_color(rectPayload, '0.75') Rectangle.set_alpha(rectPayload, 0.1) ax.add_patch(rectPayload) # art3d.pathpatch_2d_to_3d(plane, z=0, zdir="z") def axAddStarts(ax): # Some stars (real stars should *NOT* move so quickly!) ax.set_axis_bgcolor('#060A49') for k in range(50): fact = 10**8 fact = 1 rangeX = 15 rangeY = 25 X = randint(-rangeX * fact, rangeX * fact) Y = randint(-rangeY * fact * 2, rangeY * fact * 2)
detections[frame]['people'].append(im[int(y):int(y + height), int(x):int(x + width)]) detections[frame]['team'].append(team) if team == 'Fighting Maroon': # all_ptsx.append(label['center_x']*1920) # all_ptsy.append((label['center_y']+label['height']/2)*1080) ax.text(x, y - 10, confidence, color='red', fontsize=15) # ax.plot(label['center_x']*1920, (label['center_y']+label['height']/2)*1080, 'o', color='magenta') ax.text(x - 70, y + height + 35, team, color='red', fontsize=15) rect.set_color('red') rect.set_linewidth(4) ax.add_patch(rect) # ax2.plot(all_ptsx, all_ptsy, '.', color='magenta') # else: # ax.text(x, y, confidence, color='red', fontsize=7) # ax.plot(label['center_x']*1920, (label['center_y']+label['height']/2)*1080, 'ro') # ax.text(x, y+height+50, team, color='red', fontsize=7) # rect.set_color('r') # ax.add_patch(rect) if oppteam == 'Opponent': # all_ptsx.append(label['center_x']*1920) # all_ptsy.append((label['center_y']+label['height']/2)*1080) ax.text(x, y - 10, confidence, color='cyan', fontsize=15)
def create_board_figure(pcb, bom_row, layer=pcbnew.F_Cu): msg = "" unsupported_pads = [] qty, value, footpr, highlight_refs = bom_row # global ax plt.figure(figsize=(5.8, 8.2)) ax = plt.subplot("111", aspect="equal") color_pad1 = "lightgray" color_pad2 = "#AA0000" color_pad3 = "#CC4444" color_bbox1 = "None" color_bbox2 = "#E9AFAF" # get board edges (assuming rectangular, axis aligned pcb) edge_coords = [] for d in pcb.GetDrawings(): if (d.GetLayer() == pcbnew.Edge_Cuts): edge_coords.append(d.GetStart()) edge_coords.append(d.GetEnd()) edge_coords = np.asarray(edge_coords) * 1e-6 board_xmin, board_ymin = edge_coords.min(axis=0) board_xmax, board_ymax = edge_coords.max(axis=0) # draw board edges rct = Rectangle((board_xmin, board_ymin), board_xmax - board_xmin, board_ymax - board_ymin, angle=0) rct.set_color("None") rct.set_edgecolor("black") rct.set_linewidth(3) ax.add_patch(rct) # add title ax.text(board_xmin + .5 * (board_xmax - board_xmin), board_ymin - 0.5, "%dx %s, %s" % (qty, value, footpr), horizontalalignment='center', verticalalignment='bottom') # add ref list ax.text((board_xmax + board_xmin) * 0.5, board_ymax + 0.5, textwrap.fill(", ".join(highlight_refs), 60), horizontalalignment='center', verticalalignment='top') # draw parts for m in pcb.GetModules(): if m.GetLayer() != layer: continue ref, center = m.GetReference(), np.asarray(m.GetCenter()) * 1e-6 highlight = ref in highlight_refs # bounding box mrect = m.GetFootprintRect() mrect_pos = np.asarray(mrect.GetPosition()) * 1e-6 mrect_size = np.asarray(mrect.GetSize()) * 1e-6 rct = Rectangle(mrect_pos, mrect_size[0], mrect_size[1]) rct.set_color(color_bbox2 if highlight else color_bbox1) rct.set_zorder(-1) if highlight: rct.set_linewidth(.1) rct.set_edgecolor(color_pad2) ax.add_patch(rct) # center marker if highlight: plt.plot(center[0], center[1], ".", markersize=mrect_size.min(), color=color_pad2) # plot pads for p in m.Pads(): pos = np.asarray(p.GetPosition()) * 1e-6 size = np.asarray(p.GetSize()) * 1e-6 * .9 is_pin1 = p.GetPadName() == "1" or p.GetPadName() == "A1" shape = p.GetShape() offset = p.GetOffset() # TODO: check offset # pad rect angle = p.GetOrientation() * 0.1 cos, sin = np.cos(np.pi / 180. * angle), np.sin(np.pi / 180. * angle) dpos = np.dot([[cos, -sin], [sin, cos]], -.5 * size) if shape == 1: rct = Rectangle(pos + dpos, size[0], size[1], angle=angle) elif shape == 2: rct = Rectangle(pos + dpos, size[0], size[1], angle=angle) elif shape == 0: rct = Ellipse(pos, size[0], size[1], angle=angle) else: #todo: check if already printed this error #already_found_unsupported_shape = ref+p.GetPadName() in unsupported_pads #if !already_found_unsupported_shape: # unsupported_pads.append(ref+p.GetPadName()) print("Unsupported pad shape", shape) msg += "Unsupported pad shape " + str( shape) + " for " + ref + " at " + str( p.GetPosition().x) + ", " + str( p.GetPosition().y) + "\n" continue rct.set_linewidth(0) rct.set_color(color_pad2 if highlight else color_pad1) rct.set_zorder(1) # highlight pin1 if highlight and is_pin1: rct.set_color(color_pad3) rct.set_linewidth(.1) rct.set_edgecolor(color_pad2) ax.add_patch(rct) #checks for F.Cu layer - if not, plot is mirrored if m.GetLayer() == pcbnew.F_Cu: plt.xlim(board_xmin, board_xmax) plt.ylim(board_ymax, board_ymin) else: plt.xlim(board_xmax, board_xmin) plt.ylim(board_ymax, board_ymin) plt.axis('off') return msg
class MplWidget(QtWidgets.QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.scroll = QtWidgets.QScrollArea(self) self.scroll.setParent(None) #self.fig =Figure(tight_layout=True) self.fig = Figure() left = 0.0 bottom = 0.0 width = 1 height = 1 self.fig.add_axes([left, bottom, width, height]) self.canvas = FigureCanvas(self.fig) self.fig.set_facecolor([0.23, 0.23, 0.23, 0.5]) self.canvas.axes = self.canvas.figure.gca() #self.canvas.figure.tight_layout(pad=0) self.vertical_layout = QVBoxLayout() self.vertical_layout.addWidget(self.canvas) self.mpl_toolbar = my_toolbar(self.canvas, self) self.mpl_toolbar.setParentClass(self) self.mpl_toolbar.setMinimumWidth(100) self.mpl_toolbar.setFixedHeight(26) self.mpl_toolbar.setStyleSheet( "QToolBar { opacity: 1;border: 0px; background-color: rgb(133, 196, 65); border-bottom: 1px solid #19232D;padding: 2px; font-weight: bold;spacing: 2px; } " ) self.mpl_toolbar.setObjectName("myToolBar") #self.canvas.mpl_connect("resize_event", self.resize) self.vertical_layout.addWidget(self.mpl_toolbar) self.setLayout(self.vertical_layout) self.layout().setContentsMargins(0, 0, 0, 0) self.layout().setSpacing(0) self.rect = Rectangle((0, 0), 1, 1) self.updateSecondImage = None self.patchesTotal = 0 self.typeOfAnnotation = "autoDetcted" self.frameAtString = "Frame 0" self.currentSelectedOption = None self.AllBoxListDictionary = { "eraseBox": [], "oneWormLive": [], "multiWormLive": [], "oneWormDead": [], "multiWormDead": [], "miscBoxes": [], "autoDetcted": [] } self.eraseBoxXYValues = self.AllBoxListDictionary["eraseBox"] self.addBoxXYValues = self.AllBoxListDictionary["miscBoxes"] self.oneWormLiveBoxXYValues = self.AllBoxListDictionary["oneWormLive"] self.multiWormLiveBoxXYValues = self.AllBoxListDictionary[ "multiWormLive"] self.oneWormDeadBoxXYValues = self.AllBoxListDictionary["oneWormDead"] self.multiWormDeadBoxXYValues = self.AllBoxListDictionary[ "multiWormDead"] self.autoDetectedBoxXYValues = self.AllBoxListDictionary["autoDetcted"] self.tempList = [] def resetAllBoxListDictionary(self): self.AllBoxListDictionary = { "eraseBox": [], "oneWormLive": [], "multiWormLive": [], "oneWormDead": [], "multiWormDead": [], "miscBoxes": [], "autoDetcted": [] } def updateAllBoxListDictionary(self): self.AllBoxListDictionary["eraseBox"] = self.eraseBoxXYValues self.AllBoxListDictionary["miscBoxes"] = self.addBoxXYValues self.AllBoxListDictionary["oneWormLive"] = self.oneWormLiveBoxXYValues self.AllBoxListDictionary[ "multiWormLive"] = self.multiWormLiveBoxXYValues self.AllBoxListDictionary["oneWormDead"] = self.oneWormDeadBoxXYValues self.AllBoxListDictionary[ "multiWormDead"] = self.multiWormDeadBoxXYValues self.AllBoxListDictionary["autoDetcted"] = self.autoDetectedBoxXYValues def updateAllListFromAllBoxListDictionary(self): self.eraseBoxXYValues = self.AllBoxListDictionary["eraseBox"] self.addBoxXYValues = self.AllBoxListDictionary["miscBoxes"] self.oneWormLiveBoxXYValues = self.AllBoxListDictionary["oneWormLive"] self.multiWormLiveBoxXYValues = self.AllBoxListDictionary[ "multiWormLive"] self.oneWormDeadBoxXYValues = self.AllBoxListDictionary["oneWormDead"] self.multiWormDeadBoxXYValues = self.AllBoxListDictionary[ "multiWormDead"] self.autoDetectedBoxXYValues = self.AllBoxListDictionary["autoDetcted"] def setFrameAtString(self, text): self.frameAtString = text def getFrameAtString(self): return self.frameAtString def getCurrentSelectedOption(self): return self.currentSelectedOption def setCurrentSelectedOption(self, option): self.currentSelectedOption = option def setDarkTheme(self): self.mpl_toolbar.setStyleSheet( "QToolBar#myToolBar{ border: 0px; background-color: rgb(133, 0,s 65); border-bottom: 1px solid #19232D;padding: 2px; font-weight: bold;spacing: 2px; } " ) self.fig.set_facecolor([0.23, 0.23, 0.23, 0.5]) #self.fig.set_facecolor('grey') self.canvas.draw() def setGreenTheme(self): self.mpl_toolbar.setStyleSheet( "QToolBar { border: 0px; background-color: rgb(133, 196, 65); border-bottom: 1px solid #19232D;padding: 2px; font-weight: bold;spacing: 2px; } " ) self.fig.set_facecolor('grey') self.canvas.draw() def setTypeOfAnnotation(self, text): self.typeOfAnnotation = text def restrictCanvasMinimumSize(self, size): self.canvas.setMinimumSize(size) def unmountWidgetAndClear(self): self.vertical_layout.removeWidget(self.canvas) self.vertical_layout.removeWidget(self.scroll) self.scroll.setParent(None) self.canvas.setParent(None) sip.delete(self.scroll) del self.canvas self.scroll = None self.canvas = None self.canvas = FigureCanvas(Figure()) self.canvas.axes = self.canvas.figure.gca() #self.canvas.figure.tight_layout() self.scroll = QtWidgets.QScrollArea(self) self.scroll.setWidgetResizable(True) def connectClickListnerToCurrentImageForCrop(self, givenController, updateSecondImage=None, listOfControllers=None, keyForController=None): self.cid1 = self.canvas.mpl_connect("button_press_event", self.on_press_for_crop) self.cid2 = self.canvas.mpl_connect("motion_notify_event", self.onmove_for_crop) self.cid3 = self.canvas.mpl_connect("button_release_event", self.on_release_for_crop) self.givenControllerObject = givenController self.updateSecondImage = updateSecondImage self.pressevent = None self.listOfControllers = listOfControllers self.keyForController = keyForController def on_press_for_crop(self, event): if (self.mpl_toolbar.mode): return try: self.rect.remove() except: pass self.addedPatch = None self.x0 = event.xdata self.y0 = event.ydata self.rect = Rectangle((self.x0, self.y0), 1, 1) self.rect._alpha = 0.5 self.rect._linewidth = 2 self.rect.set_color("C2") self.rect.set self.pressevent = 1 self.addedPatch = self.canvas.axes.add_patch(self.rect) def on_release_for_crop(self, event): if (self.mpl_toolbar.mode): return self.pressevent = None minMaxVertices = [ int(np.ceil(min(self.x0, self.x1))), int(np.ceil(min(self.y0, self.y1))), int(np.round(max(self.x0, self.x1))), int(np.round(max(self.y0, self.y1))), ] self.givenControllerObject.updateManualCropCoordinates(minMaxVertices) image = self.givenControllerObject.showManualCropImage() self.canvas.axes.clear() self.canvas.axes.axis("off") self.canvas.axes.imshow(image) self.canvas.draw() if self.updateSecondImage is not None: self.updateSecondImage.canvas.axes.clear() self.updateSecondImage.canvas.axes.axis("off") self.updateSecondImage.canvas.axes.imshow( self.givenControllerObject.getCroppedImage(0)) self.updateSecondImage.canvas.draw() self.listOfControllers[ self.keyForController] = self.givenControllerObject def onmove_for_crop(self, event): if self.pressevent is None: return self.x1 = event.xdata self.y1 = event.ydata self.rect.set_width(self.x1 - self.x0) self.rect.set_height(self.y1 - self.y0) self.rect.set_xy((self.x0, self.y0)) self.canvas.draw() def disconnectClickListnerFromCurrentImageForCrop(self): try: self.canvas.mpl_disconnect(self.cid1) self.canvas.mpl_disconnect(self.cid2) self.canvas.mpl_disconnect(self.cid3) self.updateSecondImage = None except: pass def getCurrentScrollParam(self): self.currentVerticalSliderValue = self.scroll.verticalScrollBar( ).value() self.currentHorizontalSliderValue = self.scroll.horizontalScrollBar( ).value() def resetCurrentScrollParam(self): self.scroll.verticalScrollBar().setValue( self.currentVerticalSliderValue) self.scroll.horizontalScrollBar().setValue( self.currentHorizontalSliderValue) def resize(self, event): # on resize reposition the navigation toolbar to (0,0) of the axes. x, y = self.fig.axes[0].transAxes.transform((0, 0)) figw, figh = self.fig.get_size_inches() ynew = figh * self.fig.dpi - y - self.mpl_toolbar.frameGeometry( ).height() self.mpl_toolbar.move(x, ynew) def connectClickListnerToCurrentImageForAnnotate(self, givenController, updateSecondImage=None): self.cid4 = self.canvas.mpl_connect("button_press_event", self.on_press_for_annotate) self.cid7 = self.canvas.mpl_connect('pick_event', self.onpick) #self.cid7 = self.canvas.mpl_connect('button_press_event', self.right_click_press_for_annotate) self.givenControllerObject = givenController self.updateSecondImage = updateSecondImage self.pressevent = None def autoAnnotateOnOverlay(self, autoDetectedObjects): for index, row in autoDetectedObjects.iterrows(): print(row.bbox3) #if self.pressevent is None: # return #self.x1 = event.xdata #self.y1 = event.ydata self.rect.set_width(row.bbox3 - row.bbox1) self.rect.set_height(row.bbox2 - row.bbox0) self.rect.set_xy((row.bbox1, row.bbox0)) self.canvas.draw() self.rect = Rectangle((row.bbox1, row.bbox0), 1, 1, picker=True) self.rect._alpha = 1 self.rect._edgecolor = (0, 1, 0, 1) self.rect._facecolor = (0, 0, 0, 0) self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) self.patchesTotal = self.patchesTotal + 1 if [row.bbox1, row.bbox0, row.bbox3, row.bbox2] not in self.autoDetectedBoxXYValues: self.autoDetectedBoxXYValues.append( [row.bbox1, row.bbox0, row.bbox3, row.bbox2]) # Update latest values self.updateAllBoxListDictionary() #print(self.typeOfAnnotation) '''if self.typeOfAnnotation == "eraseBox": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation not in ["eraseBox", "oneWormLive", "multiWormLive", "oneWormDead", "multiWormDead"]: if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "oneWormLive": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "multiWormLive": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "oneWormDead": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "multiWormDead": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1])''' #self.canvas.draw() #return(self.canvas) def on_press_for_annotate(self, event): # try: # self.rect.remove() # except: # pass if (self.mpl_toolbar.mode): return if event.button == 1: self.cid5 = self.canvas.mpl_connect("motion_notify_event", self.onmove_for_annotate) self.cid6 = self.canvas.mpl_connect("button_release_event", self.on_release_for_annotate) self.x0 = event.xdata self.y0 = event.ydata self.rect = Rectangle((self.x0, self.y0), 1, 1, picker=True) self.rect._alpha = 1 if self.typeOfAnnotation not in [ "eraseBox", "oneWormLive", "multiWormLive", "oneWormDead", "multiWormDead" ]: self.rect._edgecolor = (0, 1, 0, 1) self.rect._facecolor = (0, 0, 0, 0) elif self.typeOfAnnotation == "autoDetcted": self.rect._edgecolor = (0, 1, 0, 1) self.rect._facecolor = (0, 0, 0, 0) elif self.typeOfAnnotation == "eraseBox": self.rect._edgecolor = (0, 0, 0, 1) self.rect._facecolor = (0, 0, 0, 0) elif self.typeOfAnnotation == "oneWormLive": self.rect._edgecolor = (0, 0, 1, 1) self.rect._facecolor = (0, 0, 0, 0) elif self.typeOfAnnotation == "multiWormLive": self.rect._edgecolor = (1, 1, 0, 1) self.rect._facecolor = (0, 0, 0, 0) elif self.typeOfAnnotation == "oneWormDead": self.rect._edgecolor = (1, 0, 0, 1) self.rect._facecolor = (0, 0, 0, 0) elif self.typeOfAnnotation == "multiWormDead": self.rect._edgecolor = (1, 1, 1, 1) self.rect._facecolor = (0, 0, 0, 0) self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) self.patchesTotal = self.patchesTotal + 1 def on_release_for_annotate(self, event): if (self.mpl_toolbar.mode): return if event.button == 1: self.canvas.mpl_disconnect(self.cid5) if (self.rect.get_height() == 1) and (self.rect.get_width() == 1): self.rect.remove() self.pressevent = None self.canvas.mpl_disconnect(self.cid6) if self.typeOfAnnotation == "eraseBox": #print(self.typeOfAnnotation) self.eraseBoxXYValues.append(self.tempList[-1]) self.tempList = [] if self.typeOfAnnotation not in [ "eraseBox", "oneWormLive", "multiWormLive", "oneWormDead", "multiWormDead" ]: #print(self.typeOfAnnotation) self.addBoxXYValues.append(self.tempList[-1]) self.tempList = [] if self.typeOfAnnotation == "oneWormLive": self.oneWormLiveBoxXYValues.append(self.tempList[-1]) self.tempList = [] if self.typeOfAnnotation == "multiWormLive": self.multiWormLiveBoxXYValues.append(self.tempList[-1]) self.tempList = [] if self.typeOfAnnotation == "oneWormDead": self.oneWormDeadBoxXYValues.append(self.tempList[-1]) self.tempList = [] if self.typeOfAnnotation == "multiWormDead": self.multiWormDeadBoxXYValues.append(self.tempList[-1]) self.tempList = [] # updateAllBoxListDictionary(self) self.updateAllBoxListDictionary() # self.givenControllerObject.updateManualCropCoordinates(minMaxVertices) # image = self.givenControllerObject.showManualCropImage() # self.canvas.axes.clear() # self.canvas.axes.axis("off") # self.canvas.axes.imshow(image) # self.canvas.draw() # if self.updateSecondImage is not None: # self.updateSecondImage.canvas.axes.clear() # self.updateSecondImage.canvas.axes.axis("off") # self.updateSecondImage.canvas.axes.imshow(self.givenControllerObject.getCroppedImage(0)) # self.updateSecondImage.canvas.draw() def onmove_for_annotate(self, event): if self.pressevent is None: return self.x1 = event.xdata self.y1 = event.ydata self.rect.set_width(self.x1 - self.x0) self.rect.set_height(self.y1 - self.y0) self.rect.set_xy((self.x0, self.y0)) #print(self.typeOfAnnotation) if self.typeOfAnnotation == "eraseBox": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation not in [ "eraseBox", "oneWormLive", "multiWormLive", "oneWormDead", "multiWormDead" ]: if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "oneWormLive": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "multiWormLive": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "oneWormDead": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) if self.typeOfAnnotation == "multiWormDead": if [self.x0, self.y0, self.x1, self.y1] not in self.tempList: self.tempList.append([self.x0, self.y0, self.x1, self.y1]) self.canvas.draw() def getEraseBoxXYValues(self): return (self.eraseBoxXYValues) def getAutoDetctedBoxXYValues(self): return (self.autoDetectedBoxXYValues) def getAddBoxXYValues(self): return (self.addBoxXYValues) def getOneWormLiveBoxXYValues(self): return (self.oneWormLiveBoxXYValues) def getMultiWormLiveBoxXYValues(self): return (self.multiWormLiveBoxXYValues) def getOneWormDeadBoxXYValues(self): return (self.oneWormDeadBoxXYValues) def getMultiWormDeadBoxXYValues(self): return (self.multiWormDeadBoxXYValues) def resetEraseBoxXYValues(self): self.eraseBoxXYValues = [] def resetAutoDetctedBoxXYValues(self): self.autoDetectedBoxXYValues = [] def resetAddBoxXYValues(self): self.addBoxXYValues = [] def resetOneWormLiveBoxXYValues(self): self.oneWormLiveBoxXYValues = [] def resetMultiWormLiveBoxXYValues(self): self.multiWormLiveBoxXYValues = [] def resetOneWormDeadBoxXYValues(self): self.oneWormDeadBoxXYValues = [] def resetMultiWormDeadBoxXYValues(self): self.multiWormDeadBoxXYValues = [] def disconnectClickListnerFromCurrentImageForAnnotate(self): try: self.canvas.mpl_disconnect(self.cid4) self.canvas.mpl_disconnect(self.cid7) self.updateSecondImage = None except: pass def onpick(self, event): #if event.button == 3: #"3" is the right button # print "you click the right button" # print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%( # event.button, event.x, event.y, event.xdata, event.ydata) #Get the coordinates of the mouse click #I create the action if (self.mpl_toolbar.mode): return if event.mouseevent.button == 3: self.objectPicked = event.artist noteAction_1 = QtWidgets.QAction('Delete Box', self) noteAction_2 = QtWidgets.QAction('Classify', self) #noteAction_5 = QtWidgets.QAction('Add Once',self) #noteAction_2 = QtWidgets.QAction('Add Through',self) #noteAction_3 = QtWidgets.QAction('Mask Here',self) #noteAction_4 = QtWidgets.QAction('Mask Through',self) #noteAction_6 = QtWidgets.QAction('Live here',self) #noteAction_7 = QtWidgets.QAction('Live all',self) #noteAction_8 = QtWidgets.QAction('Dead here',self) #noteAction_9 = QtWidgets.QAction('Dead all',self) #I create the context menu self.popMenu = QtWidgets.QMenu(self) self.popMenu.addAction(noteAction_1) self.popMenu.addAction(noteAction_2) # self.popMenu.addAction(noteAction_2) # self.popMenu.addAction(noteAction_3) # self.popMenu.addAction(noteAction_4) # self.popMenu.addAction(noteAction_5) # self.popMenu.addAction(noteAction_6) # self.popMenu.addAction(noteAction_7) # self.popMenu.addAction(noteAction_8) # self.popMenu.addAction(noteAction_9) cursor = QtGui.QCursor() #self.connect(self.figure_canvas, SIGNAL("clicked()"), self.context_menu) #self.popMenu.exec_(self.mapToGlobal(event.globalPos())) noteAction_1.triggered.connect(lambda: self.removeThisArea(1)) noteAction_2.triggered.connect( lambda: self.classifyAsCurrentSelection(1)) # noteAction_2.triggered.connect(lambda :self.removeThisArea(2)) # noteAction_3.triggered.connect(lambda :self.removeThisArea(3)) # noteAction_4.triggered.connect(lambda :self.removeThisArea(4)) # noteAction_5.triggered.connect(lambda :self.removeThisArea(5)) # noteAction_6.triggered.connect(lambda :self.removeThisArea(5)) # noteAction_7.triggered.connect(lambda :self.removeThisArea(2)) # noteAction_8.triggered.connect(lambda :self.removeThisArea(3)) # noteAction_9.triggered.connect(lambda :self.removeThisArea(4)) self.popMenu.popup(cursor.pos()) else: return def right_click_press_for_annotate(self, event): if (self.mpl_toolbar.mode): return if event.button == 3: #"3" is the right button # print "you click the right button" # print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%( # event.button, event.x, event.y, event.xdata, event.ydata) #Get the coordinates of the mouse click #I create the action noteAction_1 = QtWidgets.QAction('Remove', self) noteAction_2 = QtWidgets.QAction('Add', self) #I create the context menu self.popMenu = QtWidgets.QMenu(self) self.popMenu.addAction(noteAction_1) self.popMenu.addAction(noteAction_2) cursor = QtGui.QCursor() #self.connect(self.figure_canvas, SIGNAL("clicked()"), self.context_menu) #self.popMenu.exec_(self.mapToGlobal(event.globalPos())) noteAction_1.triggered.connect( lambda eventData=object: self.removeThisArea(eventData)) noteAction_2.triggered.connect( lambda eventData=object: self.classifyAsCurrentSelection( eventData)) self.popMenu.popup(cursor.pos()) def classifyAsCurrentSelection(self, caseNumber): # Get all the list values for this frame self.updateAllListFromAllBoxListDictionary() print("INSIDE classifyAsCurrentSelection") try: if caseNumber == 1: # green delete print(type(self.objectPicked)) X0 = self.objectPicked.get_xy()[0] Y0 = self.objectPicked.get_xy()[1] X1 = X0 + self.objectPicked.get_width() Y1 = Y0 + self.objectPicked.get_height() selectedBoxCoords = [X0, Y0, X1, Y1] if self.currentSelectedOption == "eraseBox": #self.autoDetectedBoxXYValues.remove(selectedBoxCoords) #self.eraseBoxXYValues.append(selectedBoxCoords) print("Use Delte Option! Right Click -> Delete Box") if self.currentSelectedOption == "autoDetcted": #self.autoDetectedBoxXYValues.remove(selectedBoxCoords) #self.addBoxXYValues.append(selectedBoxCoords) print("Already Selected!") if self.currentSelectedOption not in [ "oneWormLive", "multiWormLive", "oneWormDead", "multiWormDead", "autoDetcted" ]: self.autoDetectedBoxXYValues.remove(selectedBoxCoords) self.addBoxXYValues.append(selectedBoxCoords) self.rect.set_width(X1 - X0) self.rect.set_height(Y1 - Y0) self.rect.set_xy((X0, Y0)) self.rect = Rectangle((X0, Y0), 1, 1, picker=True) self.rect._alpha = 1 self.rect._edgecolor = (0, 1, 0, 1) self.rect._facecolor = (0, 0, 0, 0) self.canvas.draw() self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) if self.currentSelectedOption == "oneWormLive" and selectedBoxCoords not in self.oneWormLiveBoxXYValues: self.autoDetectedBoxXYValues.remove(selectedBoxCoords) self.oneWormLiveBoxXYValues.append(selectedBoxCoords) self.canvas.draw() self.rect.set_width(X1 - X0) self.rect.set_height(Y1 - Y0) self.rect.set_xy((X0, Y0)) self.rect = Rectangle((X0, Y0), 1, 1, picker=True) self.rect._alpha = 1 self.rect._edgecolor = (0, 0, 1, 1) self.rect._facecolor = (0, 0, 0, 0) self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) self.canvas.draw() if self.currentSelectedOption == "multiWormLive" and selectedBoxCoords not in self.multiWormLiveBoxXYValues: self.autoDetectedBoxXYValues.remove(selectedBoxCoords) self.multiWormLiveBoxXYValues.append(selectedBoxCoords) self.rect.set_width(X1 - X0) self.rect.set_height(Y1 - Y0) self.rect.set_xy((X0, Y0)) self.rect = Rectangle((X0, Y0), 1, 1, picker=True) self.rect._alpha = 1 self.rect._edgecolor = (1, 1, 0, 1) self.rect._facecolor = (0, 0, 0, 0) self.canvas.draw() self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) if self.currentSelectedOption == "oneWormDead" and selectedBoxCoords not in self.oneWormDeadBoxXYValues: self.autoDetectedBoxXYValues.remove(selectedBoxCoords) self.oneWormDeadBoxXYValues.append(selectedBoxCoords) self.rect.set_width(X1 - X0) self.rect.set_height(Y1 - Y0) self.rect.set_xy((X0, Y0)) self.rect = Rectangle((X0, Y0), 1, 1, picker=True) self.rect._alpha = 1 self.rect._edgecolor = (1, 0, 0, 1) self.rect._facecolor = (0, 0, 0, 0) self.canvas.draw() self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) if self.currentSelectedOption == "multiWormDead" and selectedBoxCoords not in self.multiWormDeadBoxXYValues: self.autoDetectedBoxXYValues.remove(selectedBoxCoords) self.multiWormDeadBoxXYValues.append(selectedBoxCoords) self.rect.set_width(X1 - X0) self.rect.set_height(Y1 - Y0) self.rect.set_xy((X0, Y0)) self.rect = Rectangle((X0, Y0), 1, 1, picker=True) self.rect._alpha = 1 self.rect._edgecolor = (1, 1, 1, 1) self.rect._facecolor = (0, 0, 0, 0) self.canvas.draw() self.rect._linewidth = 1 self.rect.set_linestyle('dashed') self.rect.addName = self.typeOfAnnotation self.pressevent = 1 self.canvas.axes.add_patch(self.rect) except: print("Delete and Redraw!") # updateAllBoxListDictionary(self) self.updateAllBoxListDictionary() def removeThisArea(self, caseNumber): # Get all the list values for this frame self.updateAllListFromAllBoxListDictionary() if caseNumber == 1: # green delete print(type(self.objectPicked)) X0 = self.objectPicked.get_xy()[0] Y0 = self.objectPicked.get_xy()[1] X1 = X0 + self.objectPicked.get_width() Y1 = Y0 + self.objectPicked.get_height() removeBoxCoords = [X0, Y0, X1, Y1] #print(removeBoxCoords) self.objectPicked.remove() self.patchesTotal = self.patchesTotal - 1 try: if removeBoxCoords in self.eraseBoxXYValues: self.eraseBoxXYValues.remove(removeBoxCoords) if removeBoxCoords in self.addBoxXYValues: self.addBoxXYValues.remove(removeBoxCoords) if removeBoxCoords in self.oneWormLiveBoxXYValues: #print(self.oneWormLiveBoxXYValues) self.oneWormLiveBoxXYValues.remove(removeBoxCoords) #print(self.oneWormLiveBoxXYValues) if removeBoxCoords in self.multiWormLiveBoxXYValues: self.multiWormLiveBoxXYValues.remove(removeBoxCoords) if removeBoxCoords in self.oneWormDeadBoxXYValues: self.oneWormDeadBoxXYValues.remove(removeBoxCoords) if removeBoxCoords in self.multiWormDeadBoxXYValues: self.multiWormDeadBoxXYValues.remove(removeBoxCoords) if removeBoxCoords in self.autoDetectedBoxXYValues: print(len(self.autoDetectedBoxXYValues)) self.autoDetectedBoxXYValues.remove(removeBoxCoords) print(len(self.autoDetectedBoxXYValues)) except: pass # elif caseNumber == 2: # orange add all # self.objectPicked._facecolor = (1.0, 0.64, 0.0,0.5) # self.objectPicked._alpha = 0.5 # self.objectPicked.addName ="addAll" # elif caseNumber == 3: # black # self.objectPicked._facecolor = (0,0, 0, 0.8) # self.objectPicked._alpha = 0.8 # self.objectPicked.addName ="eraseBox" # elif caseNumber == 4: # self.objectPicked._facecolor = ( 0, 0, 0, 0.2) # self.objectPicked._alpha = 0.2 # self.objectPicked.addName ="deleteAll" # elif caseNumber == 5: # self.objectPicked.set_color("C2") # self._edgecolor = (0, 0, 0, 0) # self.objectPicked.addName ="addBox" self.canvas.draw() #print(len(self.canvas.axes.patches)) #self.canvas.draw() #self.on_release_for_annotate(None) def initializeAnnotationDictionary(self): self.currentAnnotationFrame = None self.annotationRecordDictionary = {} def updateAnnotationDictionary(self): # When you move away from current Frame call this previousFrame = self.currentAnnotationFrame if previousFrame is not None: self.annotationRecordDictionary[str( previousFrame)] = self.canvas.axes.patches def getAnnotationDictionary(self): return self.annotationRecordDictionary def applyAnnotationDictionary(self, frameNumber): self.currentAnnotationFrame = frameNumber self.canvas.axes.patches = [] if str(frameNumber) in self.annotationRecordDictionary.keys(): for patch in self.annotationRecordDictionary[str(frameNumber)]: self.canvas.axes.add_patch(patch) def setAnnotationDictionary(self): pass