Skip to content

Commit

Permalink
Add image size information into json file
Browse files Browse the repository at this point in the history
With checking its consitency with the actual image.
  • Loading branch information
wkentaro committed Nov 16, 2018
1 parent 0276993 commit 63c451a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,8 @@ def format_shape(s):
shapes=shapes,
imagePath=imagePath,
imageData=imageData,
imageHeight=self.image.height(),
imageWidth=self.image.width(),
lineColor=self.lineColor.getRgb(),
fillColor=self.fillColor.getRgb(),
otherData=self.otherData,
Expand Down
47 changes: 44 additions & 3 deletions labelme/label_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os.path
import sys

from . import logger
from . import utils
from ._version import __version__


Expand Down Expand Up @@ -33,6 +35,8 @@ def load(self, filename):
'fillColor',
'shapes', # polygonal annotations
'flags', # image level flags
'imageHeight',
'imageWidth',
]
try:
with open(filename, 'rb' if PY2 else 'r') as f:
Expand All @@ -47,6 +51,11 @@ def load(self, filename):
imageData = f.read()
flags = data.get('flags')
imagePath = data['imagePath']
self._check_image_height_and_width(
base64.b64encode(imageData).decode('utf-8'),
data.get('imageHeight'),
data.get('imageWidth'),
)
lineColor = data['lineColor']
fillColor = data['fillColor']
shapes = (
Expand Down Expand Up @@ -77,11 +86,41 @@ def load(self, filename):
self.filename = filename
self.otherData = otherData

def save(self, filename, shapes, imagePath, imageData=None,
lineColor=None, fillColor=None, otherData=None,
flags=None):
@staticmethod
def _check_image_height_and_width(imageData, imageHeight, imageWidth):
img_arr = utils.img_b64_to_arr(imageData)
if imageHeight is not None and img_arr.shape[0] != imageHeight:
logger.error(
'imageHeight does not match with imageData or imagePath, '
'so getting imageHeight from actual image.'
)
imageHeight = img_arr.shape[0]
if imageWidth is not None and img_arr.shape[1] != imageWidth:
logger.error(
'imageWidth does not match with imageData or imagePath, '
'so getting imageWidth from actual image.'
)
imageWidth = img_arr.shape[1]
return imageHeight, imageWidth

def save(
self,
filename,
shapes,
imagePath,
imageHeight,
imageWidth,
imageData=None,
lineColor=None,
fillColor=None,
otherData=None,
flags=None,
):
if imageData is not None:
imageData = base64.b64encode(imageData).decode('utf-8')
imageHeight, imageWidth = self._check_image_height_and_width(
imageData, imageHeight, imageWidth
)
if otherData is None:
otherData = {}
if flags is None:
Expand All @@ -94,6 +133,8 @@ def save(self, filename, shapes, imagePath, imageData=None,
fillColor=fillColor,
imagePath=imagePath,
imageData=imageData,
imageHeight=imageHeight,
imageWidth=imageWidth,
)
for key, value in otherData.items():
data[key] = value
Expand Down

0 comments on commit 63c451a

Please sign in to comment.