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 @@
import six
import tensorflow as tf
import re
import io
from six.moves import range
from contextlib import contextmanager
......@@ -71,20 +70,25 @@ def create_image_summary(name, val):
s = tf.Summary()
for k in range(n):
arr = val[k]
if arr.shape[2] == 1: # scipy doesn't accept (h,w,1)
arr = arr[:, :, 0]
#CV2 will only write correctly in BGR chanel order
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)
buf = io.BytesIO()
# scipy assumes RGB
scipy.misc.toimage(arr).save(buf, format='png')
retval, img_str = cv2.imencode('.png', arr)
if not retval:
# Encoding has failed.
continue
img_str = img_str.tostring()
img = tf.Summary.Image()
img.height = h
img.width = w
# 1 - grayscale 3 - RGB 4 - RGBA
img.colorspace = c
img.encoded_image_string = buf.getvalue()
img.encoded_image_string = img_str
s.value.add(tag=tag, image=img)
return s
......@@ -261,7 +265,7 @@ def add_moving_summary(*args, **kwargs):
try:
import scipy.misc
import cv2
except ImportError:
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