Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
doc bash 2-5, for pack, unpack, pack_img and unpack_img (#6140)
Browse files Browse the repository at this point in the history
* doc bash for pack, unpack, pack_img and unpack_img

* Add comments for labels could be 1d list

* Update recordio.py

* Update recordio.py

* Update recordio.py

fixing text

* Update recordio.py

fixing text

* remove empty line
  • Loading branch information
jiayue666 authored and piiswrong committed May 25, 2017
1 parent a4ce7f2 commit 132544d
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions python/mxnet/recordio.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,23 @@ def pack(header, s):
----------
header : IRHeader
Header of the image record.
``header.label`` can be a number or an array.
``header.label`` can be a number or an array. See more detail in ``IRHeader``.
s : str
string to pack
Raw image string to be packed.
Returns
-------
s : str
The packed string.
Examples
--------
>>> label = 4 # label can also be a 1-D array, for example: label = [1,2,3]
>>> id = 2574
>>> header = mx.recordio.IRHeader(0, label, id, 0)
>>> with open(path, 'r') as file:
... s = file.read()
>>> packed_s = mx.recordio.pack(header, s)
"""
header = IRHeader(*header)
if isinstance(header.label, numbers.Number):
Expand All @@ -324,6 +338,14 @@ def unpack(s):
Header of the image record.
s : str
Unpacked string.
Examples
--------
>>> record = mx.recordio.MXRecordIO('test.rec', 'r')
>>> item = record.read()
>>> header, s = mx.recordio.unpack(item)
>>> header
HEADER(flag=0, label=14.0, id=20129312, id2=0)
"""
header = IRHeader(*struct.unpack(_IR_FORMAT, s[:_IR_SIZE]))
s = s[_IR_SIZE:]
Expand All @@ -340,14 +362,34 @@ def unpack_img(s, iscolor=-1):
s : str
String buffer from ``MXRecordIO.read``.
iscolor : int
image format option for ``cv2.imdecode``.
Image format option for ``cv2.imdecode``.
Returns
-------
header : IRHeader
Header of the image record.
img : numpy.ndarray
Unpacked image.
Examples
--------
>>> record = mx.recordio.MXRecordIO('test.rec', 'r')
>>> item = record.read()
>>> header, img = mx.recordio.unpack_img(item)
>>> header
HEADER(flag=0, label=14.0, id=20129312, id2=0)
>>> img
array([[[ 23, 27, 45],
[ 28, 32, 50],
...,
[ 36, 40, 59],
[ 35, 39, 58]],
...,
[[ 91, 92, 113],
[ 97, 98, 119],
...,
[168, 169, 167],
[166, 167, 165]]], dtype=uint8)
"""
header, s = unpack(s)
img = np.fromstring(s, dtype=np.uint8)
Expand All @@ -362,9 +404,9 @@ def pack_img(header, img, quality=95, img_fmt='.jpg'):
----------
header : IRHeader
Header of the image record.
``header.label`` can be a number or an array.
``header.label`` can be a number or an array. See more detail in ``IRHeader``.
img : numpy.ndarray
image to pack
Image to be packed.
quality : int
Quality for JPEG encoding in range 1-100, or compression for PNG encoding in range 1-9.
img_fmt : str
Expand All @@ -374,6 +416,14 @@ def pack_img(header, img, quality=95, img_fmt='.jpg'):
-------
s : str
The packed string.
Examples
--------
>>> label = 4 # label can also be a 1-D array, for example: label = [1,2,3]
>>> id = 2574
>>> header = mx.recordio.IRHeader(0, label, id, 0)
>>> img = cv2.imread('test.jpg')
>>> packed_s = mx.recordio.pack_img(header, img)
"""
assert cv2 is not None
jpg_formats = ['.JPG', '.JPEG']
Expand Down

0 comments on commit 132544d

Please sign in to comment.