-
Notifications
You must be signed in to change notification settings - Fork 2
/
mnist_show.py
52 lines (46 loc) · 1.68 KB
/
mnist_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#/usr/bin/env python2.7
#coding:utf-8
import sys
import cv2
import struct
import numpy
def usage_show():
print "python mnist_helper show image_file image_index"
print "\t display image from image_file"
print ""
pass
def mnist_show(image_file, image_index):
try:
fd = open(image_file, 'rb')
try:
magic, image_count = struct.unpack(">II", fd.read(8))
if magic not in [2049, 2051]:
raise Exception("Wrong file format!")
if (image_index < 0) or (image_index >= image_count):
raise Exception("index must between 0 and {0}".format(image_count-1))
if magic == 2051:
rows, cols = struct.unpack(">II", fd.read(8))
print "this file is image file, image count is {0}, rows={1}, cols={1}".\
format(image_count, rows, cols)
offset = image_index*rows*cols + 16
fd.seek(offset)
data = fd.read(cols*rows)
image = numpy.fromstring(data, dtype=numpy.uint8).reshape(cols, rows)
cv2.imshow("image", image)
cv2.waitKey(0)
else:
print ""
print "this file is label file, label count is {0}".format(image_count)
print ""
offset = image_index + 8
fd.seek(offset)
data = fd.read(1)
print "the {0}th label is {1}".format(image_index, ord(data))
print ""
except Exception, ex:
print "error:", ex.message
fd.close()
except Exception, err:
print "error:", err.message
usage_show()
pass