From 998378a64f890920150ff13087d7c1acfa372530 Mon Sep 17 00:00:00 2001 From: Kenta Kubo Date: Tue, 4 Jul 2017 02:46:40 +0900 Subject: [PATCH] Fix Python 3 compatibilities (#6908) --- tools/im2rec.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/im2rec.py b/tools/im2rec.py index 380ad1e43162..a05bfb04621b 100644 --- a/tools/im2rec.py +++ b/tools/im2rec.py @@ -12,6 +12,9 @@ import time import traceback +if sys.version_info[0] == 3: + xrange = range + try: import multiprocessing except ImportError: @@ -89,7 +92,7 @@ def read_list(path_in): continue try: item = [int(line[0])] + [line[-1]] + [float(i) for i in line[1:-1]] - except Exception, e: + except Exception as e: print('Parsing lst met error for %s, detail: %s' %(line, e)) continue yield item @@ -108,7 +111,7 @@ def image_encode(args, i, item, q_out): img = fin.read() s = mx.recordio.pack(header, img) q_out.put((i, s, item)) - except Exception, e: + except Exception as e: traceback.print_exc() print('pack_img error:', item[1], e) q_out.put((i, None, item)) @@ -142,7 +145,7 @@ def image_encode(args, i, item, q_out): try: s = mx.recordio.pack_img(header, img, quality=args.quality, img_fmt=args.encoding) q_out.put((i, s, item)) - except Exception, e: + except Exception as e: traceback.print_exc() print('pack_img error on file: %s' % fullpath, e) q_out.put((i, None, item)) @@ -279,8 +282,11 @@ def parse_args(): write_process.join() else: print('multiprocessing not available, fall back to single threaded encoding') - import Queue - q_out = Queue.Queue() + try: + import Queue as queue + except ImportError: + import queue + q_out = queue.Queue() fname = os.path.basename(fname) fname_rec = os.path.splitext(fname)[0] + '.rec' fname_idx = os.path.splitext(fname)[0] + '.idx'