Skip to content

Commit

Permalink
PythonAPI/coco.py updated showAnns() for keypoints visualization. Dem…
Browse files Browse the repository at this point in the history
…o also updated to include keypoints.
  • Loading branch information
tylin committed Jul 14, 2016
1 parent 812e11c commit de2ee0e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 13,325 deletions.
13,409 changes: 88 additions & 13,321 deletions PythonAPI/pycocoDemo.ipynb

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions PythonAPI/pycocotools/coco.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = 'tylin'
__version__ = '1.0.1'
__version__ = '2.0'
# Interface for accessing the Microsoft COCO dataset.

# Microsoft COCO is a large image dataset designed for object detection,
Expand Down Expand Up @@ -245,15 +245,16 @@ def showAnns(self, anns):
datasetType = 'captions'
if datasetType == 'instances':
ax = plt.gca()
ax.set_autoscale_on(False)
polygons = []
color = []
for ann in anns:
c = np.random.random((1, 3)).tolist()[0]
c = (np.random.random((1, 3))*0.6+0.4).tolist()[0]
if type(ann['segmentation']) == list:
# polygon
for seg in ann['segmentation']:
poly = np.array(seg).reshape((len(seg)/2, 2))
polygons.append(Polygon(poly, True,alpha=0.4))
polygons.append(Polygon(poly))
color.append(c)
else:
# mask
Expand All @@ -271,7 +272,21 @@ def showAnns(self, anns):
for i in range(3):
img[:,:,i] = color_mask[i]
ax.imshow(np.dstack( (img, m*0.5) ))
p = PatchCollection(polygons, facecolors=color, edgecolors=(0,0,0,1), linewidths=3, alpha=0.4)
if 'keypoints' in ann and type(ann['keypoints']) == list:
# turn skeleton into zero-based index
sks = np.array(self.loadCats(ann['category_id'])[0]['skeleton'])-1
kp = np.array(ann['keypoints'])
x = kp[0::3]
y = kp[1::3]
v = kp[2::3]
for sk in sks:
if np.all(v[sk]>0):
plt.plot(x[sk],y[sk], linewidth=3, color=c)
plt.plot(x[v==1], y[v==1],'o',markersize=8, markerfacecolor=c, markeredgecolor='k',markeredgewidth=2)
plt.plot(x[v==2], y[v==2],'o',markersize=8, markerfacecolor=c, markeredgecolor=c, markeredgewidth=2)
p = PatchCollection(polygons, facecolor=color, linewidths=0, alpha=0.4)
ax.add_collection(p)
p = PatchCollection(polygons, facecolor="none", edgecolors=color, linewidths=2)
ax.add_collection(p)
elif datasetType == 'captions':
for ann in anns:
Expand Down

0 comments on commit de2ee0e

Please sign in to comment.