Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2023 08 23 ocr image draw regions dicom draw regions #550

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 35 additions & 39 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -7,23 +7,22 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.6)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
addressable (2.8.1)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.11.1)
colorator (1.1.0)
commonmarker (0.23.6)
concurrent-ruby (1.1.10)
dnsruby (1.61.9)
simpleidn (~> 0.1)
commonmarker (0.23.10)
concurrent-ruby (1.2.2)
dnsruby (1.70.0)
simpleidn (~> 0.2.1)
elasticsearch (7.17.7)
elasticsearch-api (= 7.17.7)
elasticsearch-transport (= 7.17.7)
@@ -35,12 +34,12 @@ GEM
em-websocket (0.5.3)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0)
ethon (0.15.0)
ethon (0.16.0)
ffi (>= 1.15.0)
eventmachine (1.2.7)
eventmachine (1.2.7-x64-mingw32)
execjs (2.8.1)
faraday (1.10.2)
faraday (1.10.3)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
@@ -64,16 +63,14 @@ GEM
faraday-rack (1.0.0)
faraday-retry (1.0.3)
ffi (1.15.5)
ffi (1.15.5-x64-mingw-ucrt)
ffi (1.15.5-x64-mingw32)
forwardable-extended (2.6.0)
gemoji (3.0.1)
github-pages (227)
github-pages (228)
github-pages-health-check (= 1.17.9)
jekyll (= 3.9.2)
jekyll (= 3.9.3)
jekyll-avatar (= 0.7.0)
jekyll-coffeescript (= 1.1.1)
jekyll-commonmark-ghpages (= 0.2.0)
jekyll-commonmark-ghpages (= 0.4.0)
jekyll-default-layout (= 0.1.4)
jekyll-feed (= 0.15.1)
jekyll-gist (= 1.5.0)
@@ -107,7 +104,7 @@ GEM
jemoji (= 0.12.0)
kramdown (= 2.3.2)
kramdown-parser-gfm (= 1.1.0)
liquid (= 4.0.3)
liquid (= 4.0.4)
mercenary (~> 0.3)
minima (= 2.5.1)
nokogiri (>= 1.13.6, < 2.0)
@@ -123,13 +120,13 @@ GEM
activesupport (>= 2)
nokogiri (>= 1.4)
http_parser.rb (0.8.0)
i18n (0.9.5)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
jekyll (3.9.2)
jekyll (3.9.3)
addressable (~> 2.4)
colorator (~> 1.0)
em-websocket (~> 0.5)
i18n (~> 0.7)
i18n (>= 0.7, < 2)
jekyll-sass-converter (~> 1.0)
jekyll-watch (~> 2.0)
kramdown (>= 1.17, < 3)
@@ -145,11 +142,11 @@ GEM
coffee-script-source (~> 1.11.1)
jekyll-commonmark (1.4.0)
commonmarker (~> 0.22)
jekyll-commonmark-ghpages (0.2.0)
commonmarker (~> 0.23.4)
jekyll-commonmark-ghpages (0.4.0)
commonmarker (~> 0.23.7)
jekyll (~> 3.9.0)
jekyll-commonmark (~> 1.4.0)
rouge (>= 2.0, < 4.0)
rouge (>= 2.0, < 5.0)
jekyll-default-layout (0.1.4)
jekyll (~> 3.0)
jekyll-feed (0.15.1)
@@ -237,33 +234,33 @@ GEM
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
liquid (4.0.3)
listen (3.7.1)
liquid (4.0.4)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.3.6)
mini_portile2 (2.8.0)
mini_portile2 (2.8.4)
minima (2.5.1)
jekyll (>= 3.5, < 5.0)
jekyll-feed (~> 0.9)
jekyll-seo-tag (~> 2.1)
minitest (5.16.3)
minitest (5.19.0)
multi_json (1.15.0)
multipart-post (2.2.3)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
multipart-post (2.3.0)
nokogiri (1.15.4)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
octokit (4.25.1)
faraday (>= 1, < 3)
sawyer (~> 0.9)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.7)
racc (1.6.0)
racc (1.7.1)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rexml (3.2.5)
rexml (3.2.6)
rouge (3.26.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
@@ -280,20 +277,19 @@ GEM
unf (~> 0.1.4)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thread_safe (0.3.6)
typhoeus (1.4.0)
ethon (>= 0.9.0)
tzinfo (1.2.10)
thread_safe (~> 0.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.2)
unicode-display_width (1.8.0)
wdm (0.1.1)
webrick (1.7.0)
zeitwerk (2.6.1)
webrick (1.8.1)

PLATFORMS
arm64-darwin-22
x64-mingw-ucrt
x64-mingw32
x86_64-darwin-21
@@ -302,7 +298,7 @@ PLATFORMS

DEPENDENCIES
elasticsearch (~> 7.10)
github-pages (= 227)
github-pages (= 228)
jekyll (~> 3.9)
jekyll-incremental (= 0.1.0)!
jekyll-redirect-from
102 changes: 95 additions & 7 deletions docs/en/ocr_pipeline_components.md
Original file line number Diff line number Diff line change
@@ -1356,6 +1356,89 @@ data.select("dicom").show()

</div></div><div class="h3-box" markdown="1">

### DicomDrawRegions

`DicomDrawRegions` draw regions to Dicom Image.

</div><div class="h3-box" markdown="1">

##### Input Columns

{:.table-model-big}
| Param name | Type | Default | Column Data Description |
| --- | --- | --- | --- |
| inputCol | string | content | Binary dicom object |
| inputRegionsCol | string | regions | Detected Array[Coordinates] from PositionFinder |

</div><div class="h3-box" markdown="1">

#### Parameters

{:.table-model-big}
| Param name | Type | Default | Description |
| --- | --- | --- | --- |
| scaleFactor | float | 1.0 | Scaling factor for regions. |
| rotated | boolean | False | Enable/Disable support for rotated rectangles |
| keepInput | boolean | False | Keep the original input column |
| compression | string | RLELossless | Compression type |
| forceCompress | boolean | False | True - Force compress image. False - compress only if original image was compressed |
| aggCols | Array[string] | ['path'] | Sets the columns to be included in aggregation. These columns are preserved in the output DataFrame after transformations |

</div><div class="h3-box" markdown="1">

##### Output Columns

{:.table-model-big}
| Param name | Type | Default | Column Data Description |
| --- | --- | --- | --- |
| outputCol | string | image | Modified Dicom file data |

**Example:**

<div class="tabs-box tabs-new pt0" markdown="1">

{% include programmingLanguageSelectScalaPython.html %}

```python
from sparkocr.transformers import *

dicomPath = "path to dicom files"

# Read dicom file as binary file
df = spark.read.format("binaryFile").load(dicomPath)

dicomToImage = DicomToImage() \
.setInputCol("content") \
.setOutputCol("image") \
.setMetadataCol("meta")

position_finder = PositionFinder() \
# Usually chunks are created using the deidentification_nlp_pipeline
.setInputCols("ner_chunk") \
.setOutputCol("coordinates") \
.setPageMatrixCol("positions") \
.setPadding(0)

draw_regions = DicomDrawRegions() \
.setInputCol("content") \
.setInputRegionsCol("coordinates") \
.setOutputCol("dicom") \
.setKeepInput(True) \
.setScaleFactor(1/3.0) \
.setAggCols(["path", "content"])

data = dicomToImage.transform(df)

data.select("content", "dicom").show()
```

```scala
// Note: DicomDrawRegions class is not available in the Scala API
// This class is used in the Python API for DICOM image manipulation and transformation.
```

</div></div><div class="h3-box" markdown="1">

## Image pre-processing

Next section describes the transformers for image pre-processing: scaling, binarization, skew correction, etc.
@@ -2896,6 +2979,11 @@ val result = modelPipeline.transform(df)
| lineWidth | Int | 4 | Line width for draw rectangles |
| fontSize | Int | 12 | Font size for render labels and score |
| rotated | boolean | False | Support rotated regions |
| rectColor | Color | Color.black | Color outline for bounding box |
| filledRect | boolean | False | Enable/Disable filling rectangle |
| sourceImageHeightCol | Int | height_dimension | Original annotation reference height |
| sourceImageWidthCol | Int | width_dimension | Original annotation reference width |
| scaleBoundingBoxes | Boolean | True | sourceImage height & width are required for scaling. Necessary to ensure accurate regions despite image transformations.|

</div><div class="h3-box" markdown="1">

@@ -2915,13 +3003,12 @@ val result = modelPipeline.transform(df)
```python
from pyspark.ml import PipelineModel
from sparkocr.transformers import *
from sparkocr.enums import *

imagePath = "path to image"

# Read image file as binary file
df = spark.read
.format("binaryFile")
.load(imagePath)
df = spark.read.format("binaryFile").load(imagePath)

binary_to_image = BinaryToImage() \
.setInputCol("content") \
@@ -2935,6 +3022,7 @@ layout_analyzer = ImageLayoutAnalyzer() \
draw = ImageDrawRegions() \
.setInputCol("image") \
.setRegionCol("regions") \
.setRectColor(Color.red) \
.setOutputCol("image_with_regions")

# Define pipeline
@@ -2950,17 +3038,16 @@ data.show()

```scala
import org.apache.spark.ml.Pipeline
import java.awt.Color

import com.johnsnowlabs.ocr.transformers.{ImageSplitRegions, ImageLayoutAnalyzer}
import com.johnsnowlabs.ocr.OcrContext.implicits._


val imagePath = "path to image"

// Read image file as binary file
val df = spark.read
.format("binaryFile")
.load(imagePath)
.asImage("image")
val df = spark.read.format("binaryFile").load(imagePath).asImage("image")

// Define transformer for detect regions
val layoutAnalyzer = new ImageLayoutAnalyzer()
@@ -2970,6 +3057,7 @@ val layoutAnalyzer = new ImageLayoutAnalyzer()
val draw = new ImageDrawRegions()
.setInputCol("image")
.setRegionCol("regions")
.setRectColor(Color.RED)
.setOutputCol("image_with_regions")

// Define pipeline
Loading