You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by ke...@apache.org on 2019/06/04 13:37:40 UTC
[incubator-mxnet] branch master updated: [clojure] clojurify
function names in image.clj namespace (#15121)
This is an automated email from the ASF dual-hosted git repository.
kedarb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push:
new 28c528e [clojure] clojurify function names in image.clj namespace (#15121)
28c528e is described below
commit 28c528e16be70b31287b65d949205396bbfec6e8
Author: Arthur Caillau <Ch...@users.noreply.github.com>
AuthorDate: Tue Jun 4 15:37:04 2019 +0200
[clojure] clojurify function names in image.clj namespace (#15121)
* [clojure] clojurify function names in image.clj namespace
* move deprecated to the proper location for defn
* rename color-flag to color and use :color :grayscale as values
* add rm dest-path in with-file
* change `color-flag` to `color` in `color->int`
---
.../src/org/apache/clojure_mxnet/image.clj | 116 +++++++++++++++++++--
.../test/org/apache/clojure_mxnet/image_test.clj | 63 +++++++----
2 files changed, 151 insertions(+), 28 deletions(-)
diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj
index f81a358..68dcbfe 100644
--- a/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj
+++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj
@@ -17,6 +17,7 @@
(ns org.apache.clojure-mxnet.image
"Image API of Clojure package."
+ (:refer-clojure :exclude [read])
(:require [t6.from-scala.core :refer [$ $$] :as $]
[org.apache.clojure-mxnet.dtype :as dtype]
[org.apache.clojure-mxnet.ndarray :as ndarray]
@@ -38,8 +39,10 @@
(s/def ::decode-image-opts
(s/keys :opt-un [::color-flag ::to-rgb ::output]))
-(defn decode-image
- "Decodes an image from an input stream with OpenCV
+(defn ^:deprecated decode-image
+ "DEPRECATED: use `decode` instead.
+
+ Decodes an image from an input stream with OpenCV
`input-stream`: `InputStream` - Contains the binary encoded image
`color-flag`: 0 or 1 - Convert decoded image to grayscale (0) or color (1)
`to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB
@@ -60,14 +63,47 @@
([input-stream]
(decode-image input-stream {})))
+(s/def ::color #{:grayscale :color})
+(s/def ::decode-image-opts-2 (s/keys :opt-un [::color ::to-rgb ::output]))
+
+(defn- color->int [color]
+ (case color
+ :grayscale 0
+ :color 1))
+
+(defn decode
+ "Decodes an image from an input stream with OpenCV.
+ `input-stream`: `InputStream` - Contains the binary encoded image
+ `color`: keyword in `#{:color :grayscale}` - Convert decoded image to
+ grayscale or color
+ `to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB
+ format (instead of opencv's default BGR)
+ `output`: nil or `NDArray`
+ returns: `NDArray` with dtype uint8
+
+ Ex:
+ (decode input-stream)
+ (decode input-stream {:color :color})
+ (decode input-stream {:color :grayscale :output nd})"
+ ([input-stream {:keys [color to-rgb output]
+ :or {color :color to-rgb true output nil}
+ :as opts}]
+ (util/validate! ::input-stream input-stream "Invalid input stream")
+ (util/validate! ::decode-image-opts-2 opts "Invalid options for decoding")
+ (Image/imDecode input-stream (color->int color) to-rgb ($/option output)))
+ ([input-stream]
+ (decode input-stream {})))
+
(s/def ::filename string?)
(s/def ::optional-color-flag
(s/or :none nil? :some ::color-flag))
(s/def ::optional-to-rgb
(s/or :none nil? :some ::to-rgb))
-(defn read-image
- "Reads an image file and returns an ndarray with OpenCV. It returns image in
+(defn ^:deprecated read-image
+ "DEPRECATED: use `read` instead.
+
+ Reads an image file and returns an ndarray with OpenCV. It returns image in
RGB by default instead of OpenCV's default BGR.
`filename`: string - Name of the image file to be loaded
`color-flag`: 0 or 1 - Convert decoded image to grayscale (0) or color (1)
@@ -95,11 +131,43 @@
([filename]
(read-image filename {})))
+(defn read
+ "Reads an image file and returns an ndarray with OpenCV. It returns image in
+ RGB by default instead of OpenCV's default BGR.
+ `filename`: string - Name of the image file to be loaded
+ `color`: keyword in `#{:color :grayscale}` - Convert decoded image to
+ grayscale or color
+ `to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB
+ format (instead of opencv's default BGR)
+ `output`: nil or `NDArray`
+ returns: `NDArray` with dtype uint8
+
+ Ex:
+ (read \"cat.jpg\")
+ (read \"cat.jpg\" {:color :grayscale})
+ (read \"cat.jpg\" {:color :color :output nd})"
+ ([filename {:keys [color to-rgb output]
+ :or {color :color to-rgb nil output nil}
+ :as opts}]
+ (util/validate! ::filename filename "Invalid filename")
+ (util/validate! ::color color "Invalid color")
+ (util/validate! ::optional-to-rgb to-rgb "Invalid conversion flag")
+ (util/validate! ::output output "Invalid output")
+ (Image/imRead
+ filename
+ ($/option (when color (color->int color)))
+ ($/option to-rgb)
+ ($/option output)))
+ ([filename]
+ (read filename {})))
+
(s/def ::int int?)
(s/def ::optional-int (s/or :none nil? :some int?))
-(defn resize-image
- "Resizes the image array to (width, height)
+(defn ^:deprecated resize-image
+ "DEPRECATED: use `resize` instead.
+
+ Resizes the image array to (width, height)
`input`: `NDArray` - source image in NDArray
`w`: int - Width of resized image
`h`: int - Height of resized image
@@ -122,6 +190,30 @@
([input w h]
(resize-image input w h {})))
+(defn resize
+ "Resizes the image array to (width, height)
+ `input`: `NDArray` - source image in NDArray
+ `w`: int - Width of resized image
+ `h`: int - Height of resized image
+ `interpolation`: Interpolation method. Default is INTER_LINEAR
+ `ouput`: nil or `NDArray`
+ returns: `NDArray`
+
+ Ex:
+ (resize nd-img 300 300)
+ (resize nd-img 28 28 {:output nd})"
+ ([input w h {:keys [interpolation output]
+ :or {interpolation nil output nil}
+ :as opts}]
+ (util/validate! ::ndarray input "Invalid input array")
+ (util/validate! ::int w "Invalid width")
+ (util/validate! ::int h "Invalid height")
+ (util/validate! ::optional-int interpolation "Invalid interpolation")
+ (util/validate! ::output output "Invalid output")
+ (Image/imResize input w h ($/option interpolation) ($/option output)))
+ ([input w h]
+ (resize input w h {})))
+
(defn apply-border
"Pad image border with OpenCV.
`input`: `NDArray` - source image in NDArray
@@ -193,7 +285,17 @@
(s/def ::to-image-ndarray
(s/and ::ndarray ::all-bytes ::rgb-array))
-(defn to-image
+(defn ^:deprecated to-image
+ "DEPRECATED: user `ndarray->image` instead.
+
+ Convert a NDArray image in RGB format to a real image.
+ `input`: `NDArray` - Source image in NDArray
+ returns: `BufferedImage`"
+ [input]
+ (util/validate! ::to-image-ndarray input "Invalid input array")
+ (Image/toImage input))
+
+(defn ndarray->image
"Convert a NDArray image in RGB format to a real image.
`input`: `NDArray` - Source image in NDArray
returns: `BufferedImage`"
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj
index b543b2d..fd200f1 100644
--- a/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/image_test.clj
@@ -50,50 +50,71 @@
(use-fixtures :once (with-file image-src-path image-path))
(deftest test-decode-image
- (let [img-arr (image/decode-image
- (io/input-stream image-path))
- img-arr-2 (image/decode-image
- (io/input-stream image-path)
- {:color-flag image/GRAYSCALE})]
+ (let [img-arr (image/decode-image (io/input-stream image-path))
+ img-arr-2 (image/decode-image (io/input-stream image-path)
+ {:color-flag image/GRAYSCALE})]
+ (is (= [576 1024 3] (ndarray/shape-vec img-arr)))
+ (is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))
+
+(deftest test-decode
+ (let [img-arr (image/decode (io/input-stream image-path))
+ img-arr-2 (image/decode (io/input-stream image-path)
+ {:color :grayscale})]
(is (= [576 1024 3] (ndarray/shape-vec img-arr)))
(is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))
(deftest test-read-image
(let [img-arr (image/read-image image-path)
- img-arr-2 (image/read-image
- image-path
- {:color-flag image/GRAYSCALE})]
+ img-arr-2 (image/read-image image-path {:color-flag image/GRAYSCALE})]
+ (is (= [576 1024 3] (ndarray/shape-vec img-arr)))
+ (is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))
+
+(deftest test-read
+ (let [img-arr (image/read image-path)
+ img-arr-2 (image/read image-path {:color :grayscale})]
(is (= [576 1024 3] (ndarray/shape-vec img-arr)))
(is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))
(deftest test-resize-image
- (let [img-arr (image/read-image image-path)
+ (let [img-arr (image/read image-path)
resized-arr (image/resize-image img-arr 224 224)]
(is (= [224 224 3] (ndarray/shape-vec resized-arr)))))
-(deftest test-crop-image
- (let [img-arr (image/read-image image-path)
+(deftest test-resize
+ (let [img-arr (image/read image-path)
+ resized-arr (image/resize img-arr 224 224)]
+ (is (= [224 224 3] (ndarray/shape-vec resized-arr)))))
+
+(deftest test-fixed-crop
+ (let [img-arr (image/read image-path)
cropped-arr (image/fixed-crop img-arr 0 0 224 224)]
(is (= [224 224 3] (ndarray/shape-vec cropped-arr)))))
(deftest test-apply-border
- (let [img-arr (image/read-image image-path)
+ (let [img-arr (image/read image-path)
padded-arr (image/apply-border img-arr 1 1 1 1)]
(is (= [578 1026 3] (ndarray/shape-vec padded-arr)))))
(deftest test-to-image
- (let [img-arr (image/read-image image-path)
- resized-arr (image/resize-image img-arr 224 224)
+ (let [img-arr (image/read image-path)
+ resized-arr (image/resize img-arr 224 224)
new-img (image/to-image resized-arr)]
(is (ImageIO/write new-img "png" (io/file tmp-dir "out.png")))))
+(deftest test-ndarray->image
+ (let [img-arr (image/read image-path)
+ resized-arr (image/resize img-arr 224 224)
+ new-img (image/ndarray->image resized-arr)]
+ (is (ImageIO/write new-img "png" (io/file tmp-dir "out.png")))))
+
(deftest test-draw-bounding-box!
(let [orig-img (ImageIO/read (new File image-path))
- new-img (-> orig-img
- (image/draw-bounding-box! [{:x-min 190 :x-max 850 :y-min 50 :y-max 450}
- {:x-min 200 :x-max 350 :y-min 440 :y-max 530}]
- {:stroke 2
- :names ["pug" "cookie"]
- :transparency 0.8
- :font-size-mult 2.0}))]
+ new-img (image/draw-bounding-box!
+ orig-img
+ [{:x-min 190 :x-max 850 :y-min 50 :y-max 450}
+ {:x-min 200 :x-max 350 :y-min 440 :y-max 530}]
+ {:stroke 2
+ :names ["pug" "cookie"]
+ :transparency 0.8
+ :font-size-mult 2.0})]
(is (ImageIO/write new-img "png" (io/file tmp-dir "out.png")))))