Created
November 24, 2017 23:01
-
-
Save dnlcrl/9c4bc1c88a36bb4ef29a5c73df4345db to your computer and use it in GitHub Desktop.
easy image labeling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
def point_in_circle(xp, yp, xc, yc, r): | |
return (xp - xc)**2 + (yp - yc)**2 < r**2 | |
def draw_circles(ax, centers, classes, colors): | |
for i in range(centers.shape[0]): | |
circle = plt.Circle((centers[i, 1], centers[i, 0]), 20, color=colors[classes[i]], fill=False) | |
ax.add_patch(circle) | |
class Labeler(object): | |
"""label candidates easily""" | |
@staticmethod | |
def onclick(event, args): | |
ax, centers, radii, classes, colors = args | |
for i, center in enumerate(centers): | |
incircle = point_in_circle( | |
event.ydata, event.xdata, center[0], center[1], radii[i]) | |
if incircle: | |
classes[i] = int(not classes[i]) | |
draw_circles(ax, centers, classes, colors) | |
plt.draw() | |
@classmethod | |
def label(cls, image, centers, radii, classes, colors): | |
fig, ax = plt.subplots(1) | |
ax.imshow(image, cmap='gray') | |
draw_circles(ax, centers, classes, colors) | |
args = ax, centers, radii, classes, colors | |
fig.canvas.mpl_connect('button_press_event', lambda event: cls.onclick(event, args)) | |
plt.show(block=True) | |
return classes |
lene3232
commented
Nov 24, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment