/ocr/ocrservice/src/com/googlecode/eyesfree/textdetect/HydrogenTextDetector.java

http://eyes-free.googlecode.com/ · Java · 260 lines · 141 code · 76 blank · 43 comment · 4 complexity · 08598ac417933171ef94c14bb102489c MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.textdetect;
  17. import android.os.Environment;
  18. import com.googlecode.leptonica.android.Pix;
  19. import com.googlecode.leptonica.android.Pixa;
  20. /**
  21. * @author alanv@google.com (Alan Viverette)
  22. */
  23. public class HydrogenTextDetector {
  24. private final int mNative;
  25. static {
  26. System.loadLibrary("lept");
  27. System.loadLibrary("hydrogen");
  28. }
  29. private Parameters mParams;
  30. public HydrogenTextDetector() {
  31. mNative = nativeConstructor();
  32. mParams = new Parameters();
  33. setParameters(mParams);
  34. }
  35. public void setSize(int width, int height) {
  36. // TODO(alanv): Set up native buffers
  37. }
  38. @Override
  39. protected void finalize() throws Throwable {
  40. try {
  41. nativeDestructor(mNative);
  42. } finally {
  43. super.finalize();
  44. }
  45. }
  46. public void setParameters(Parameters params) {
  47. mParams = params;
  48. nativeSetParameters(mNative, mParams);
  49. }
  50. public Parameters getParameters() {
  51. return mParams;
  52. }
  53. public Pixa getTextAreas() {
  54. int nativePixa = nativeGetTextAreas(mNative);
  55. if (nativePixa == 0) {
  56. return null;
  57. }
  58. int width = nativeGetSourceWidth(mNative);
  59. int height = nativeGetSourceHeight(mNative);
  60. return new Pixa(nativePixa, width, height);
  61. }
  62. public float getSkewAngle() {
  63. return nativeGetSkewAngle(mNative);
  64. }
  65. public float[] getTextConfs() {
  66. return nativeGetTextConfs(mNative);
  67. }
  68. public Pix getSourceImage() {
  69. int nativePix = nativeGetSourceImage(mNative);
  70. if (nativePix == 0) {
  71. return null;
  72. }
  73. return new Pix(nativePix);
  74. }
  75. /**
  76. * Sets the text detection source image to be a clone of the supplied source
  77. * image. The supplied image may be recycled after calling this method.
  78. *
  79. * @param pixs The source image on which to perform text detection.
  80. */
  81. public void setSourceImage(Pix pixs) {
  82. nativeSetSourceImage(mNative, pixs.getNativePix());
  83. }
  84. public void detectText() {
  85. nativeDetectText(mNative);
  86. }
  87. public void clear() {
  88. nativeClear(mNative);
  89. }
  90. // ******************
  91. // * PUBLIC CLASSES *
  92. // ******************
  93. public class Parameters {
  94. public boolean debug;
  95. public String out_dir;
  96. // Edge-based thresholding
  97. public int edge_tile_x;
  98. public int edge_tile_y;
  99. public int edge_thresh;
  100. public int edge_avg_thresh;
  101. // Skew angle correction
  102. public boolean skew_enabled;
  103. public float skew_min_angle;
  104. public float skew_sweep_range;
  105. public float skew_sweep_delta;
  106. public int skew_sweep_reduction;
  107. public int skew_search_reduction;
  108. public float skew_search_min_delta;
  109. // Singleton filter
  110. public float single_min_aspect;
  111. public float single_max_aspect;
  112. public int single_min_area;
  113. public float single_min_density;
  114. // Quick pair filter
  115. public float pair_h_ratio;
  116. public float pair_d_ratio;
  117. public float pair_h_dist_ratio;
  118. public float pair_v_dist_ratio;
  119. public float pair_h_shared;
  120. // Cluster pair filter
  121. public int cluster_width_spacing;
  122. public float cluster_shared_edge;
  123. public float cluster_h_ratio;
  124. // Finalized cluster filter
  125. public int cluster_min_blobs;
  126. public float cluster_min_aspect;
  127. public float cluster_min_fdr;
  128. public int cluster_min_edge;
  129. public int cluster_min_edge_avg;
  130. public Parameters() {
  131. debug = false;
  132. out_dir = Environment.getExternalStorageDirectory().toString();
  133. // Edge-based thresholding
  134. edge_tile_x = 32;
  135. edge_tile_y = 64;
  136. edge_thresh = 64;
  137. edge_avg_thresh = 4;
  138. // Skew angle correction
  139. skew_enabled = true;
  140. skew_min_angle = 1.0f;
  141. skew_sweep_range = 30.0f;
  142. skew_sweep_delta = 5.0f;
  143. skew_sweep_reduction = 8;
  144. skew_search_reduction = 4;
  145. skew_search_min_delta = 0.01f;
  146. // Singleton filter
  147. single_min_aspect = 0.1f;
  148. single_max_aspect = 4.0f;
  149. single_min_area = 4;
  150. single_min_density = 0.2f;
  151. // Quick pair filter
  152. pair_h_ratio = 1.0f;
  153. pair_d_ratio = 1.5f;
  154. pair_h_dist_ratio = 2.0f;
  155. pair_v_dist_ratio = 0.25f;
  156. pair_h_shared = 0.25f;
  157. // Cluster pair filter
  158. cluster_width_spacing = 2;
  159. cluster_shared_edge = 0.5f;
  160. cluster_h_ratio = 1.0f;
  161. // Finalized cluster filter
  162. cluster_min_blobs = 5;
  163. cluster_min_aspect = 2;
  164. cluster_min_fdr = 2.5f;
  165. cluster_min_edge = 32;
  166. cluster_min_edge_avg = 1;
  167. }
  168. }
  169. // ******************
  170. // * NATIVE METHODS *
  171. // ******************
  172. private static native int nativeConstructor();
  173. private static native void nativeDestructor(int nativePtr);
  174. private static native void nativeSetParameters(int nativePtr, Parameters params);
  175. private static native int nativeGetTextAreas(int nativePtr);
  176. private static native float nativeGetSkewAngle(int nativePtr);
  177. private static native int nativeGetSourceWidth(int nativePtr);
  178. private static native int nativeGetSourceHeight(int nativePtr);
  179. private static native float[] nativeGetTextConfs(int nativePtr);
  180. private static native int nativeGetSourceImage(int nativePtr);
  181. private static native int nativeSetSourceImage(int nativePtr, int nativePix);
  182. private static native void nativeDetectText(int nativePtr);
  183. private static native void nativeClear(int nativePtr);
  184. }