Skip to content

Instantly share code, notes, and snippets.

@linnil1
Created December 4, 2018 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linnil1/33fb832f38002f0b874ef6fb7b77321d to your computer and use it in GitHub Desktop.
Save linnil1/33fb832f38002f0b874ef6fb7b77321d to your computer and use it in GitHub Desktop.
Making annotation for location of target in Image.
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import numpy as np
import os
name = "IMG_9781"
fig, ax = plt.subplots(figsize=(12, 8), dpi=100)
plt.subplots_adjust(bottom=0, top=1, left=0, right=1)
plt.title(name)
img = plt.imread(name + ".JPG")
ann_filename = name + '.npz'
count = 0
circle_size = 5
ann = []
def Read():
global ann
if os.path.exists(ann_filename):
print("Recover")
ann = list(np.load(ann_filename)['ann'])
print(ann)
Load()
def Load():
ax.clear()
ax.imshow(img)
ax.axis('off')
for an in ann:
plotCircle(an[0], an[1])
plt.draw()
def plotCircle(x, y):
circle1 = plt.Circle((x, y), circle_size, color='r')
ax.add_artist(circle1)
plt.draw()
print("""
Right click to redo
Left click to point
Middle click to save
Shift + any to do whatever you want(ex, enlarge)
""")
def onclick(event):
print(event)
"""
print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.xdata, event.ydata))
"""
if event.key:
print(event.key)
return
if event.button == 3:
print("Redo")
ann.pop()
Load()
elif event.button == 2:
print("Save")
np.savez(ann_filename, ann=ann)
else:
print("Add")
plotCircle(event.xdata, event.ydata)
ann.append([event.xdata, event.ydata])
cid = fig.canvas.mpl_connect('button_press_event', onclick)
Read()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment