Skip to content

Commit

Permalink
[clojure] clojurify function names in image.clj namespace (apache#15121)
Browse files Browse the repository at this point in the history
* [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`
  • Loading branch information
Chouffe authored and haohuw committed Jun 23, 2019
1 parent 1c4a421 commit 539e299
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 28 deletions.
116 changes: 109 additions & 7 deletions contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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`"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))

0 comments on commit 539e299

Please sign in to comment.