Commit fa1dccdd authored by Aaron Gokaslan's avatar Aaron Gokaslan Committed by Yuxin Wu

Use cv2 to replace scipy.misc (fix #592) (#667)

* Converted scipy.misc to cv2

Scipy.misc is being deprecated so we should probably switch to OpenCV to reduce the number of needed dependencies.

* Removed unused import.

* Made Travis happy

* Travis fix

* Added cv2 dummy func and remove grayscale hack
parent b5702366
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
import six import six
import tensorflow as tf import tensorflow as tf
import re import re
import io
from six.moves import range from six.moves import range
from contextlib import contextmanager from contextlib import contextmanager
...@@ -71,20 +70,25 @@ def create_image_summary(name, val): ...@@ -71,20 +70,25 @@ def create_image_summary(name, val):
s = tf.Summary() s = tf.Summary()
for k in range(n): for k in range(n):
arr = val[k] arr = val[k]
if arr.shape[2] == 1: # scipy doesn't accept (h,w,1) #CV2 will only write correctly in BGR chanel order
arr = arr[:, :, 0] if c == 3:
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
elif c == 4:
arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
tag = name if n == 1 else '{}/{}'.format(name, k) tag = name if n == 1 else '{}/{}'.format(name, k)
buf = io.BytesIO() retval, img_str = cv2.imencode('.png', arr)
# scipy assumes RGB if not retval:
scipy.misc.toimage(arr).save(buf, format='png') # Encoding has failed.
continue
img_str = img_str.tostring()
img = tf.Summary.Image() img = tf.Summary.Image()
img.height = h img.height = h
img.width = w img.width = w
# 1 - grayscale 3 - RGB 4 - RGBA # 1 - grayscale 3 - RGB 4 - RGBA
img.colorspace = c img.colorspace = c
img.encoded_image_string = buf.getvalue() img.encoded_image_string = img_str
s.value.add(tag=tag, image=img) s.value.add(tag=tag, image=img)
return s return s
...@@ -261,7 +265,7 @@ def add_moving_summary(*args, **kwargs): ...@@ -261,7 +265,7 @@ def add_moving_summary(*args, **kwargs):
try: try:
import scipy.misc import cv2
except ImportError: except ImportError:
from ..utils.develop import create_dummy_func from ..utils.develop import create_dummy_func
create_image_summary = create_dummy_func('create_image_summary', 'scipy.misc') # noqa create_image_summary = create_dummy_func('create_image_summary', 'cv2') # noqa
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment