2016-08-01 43 views
0

Ich versuche, anyline OCR SDK zu integrieren, um URL-Links aus einem Bild zu extrahieren. Von ihrer documentation Ich habe verstanden, dass durch die Änderung der iban Scanner Beispiel kann ich die gleichen Ergebnisse für eine URL erreichen. HierAnyline OCR SDK Integration zum Scannen von URLs in einem Bild

ist die Konfigurationsdatei für Scan-Ansicht

{ 
    "captureResolution":"1080", 
    "cutout": { 
    "style": "rect", 
    "maxWidthPercent": "80%", 
    "maxHeightPercent": "80%", 
    "alignment": "top_half", 
    "width": 900, 
    "ratioFromSize": { 
     "width": 10, 
     "height": 1 
    }, 
    "strokeWidth": 2, 
    "cornerRadius": 10, 
    "strokeColor": "FFFFFF", 
    "outerColor": "000000", 
    "outerAlpha": 0.3, 
    "feedbackStrokeColor": "0099FF" 
    }, 
    "flash": { 
    "mode": "manual", 
    "alignment": "bottom_right" 
    }, 
    "beepOnResult": true, 
    "vibrateOnResult": true, 
    "blinkAnimationOnResult": true, 
    "cancelOnResult": true, 
    "visualFeedback": { 
    "style": "contour_point", 
    "strokeColor": "0099FF", 
    "strokeWidth": 2, 
    "fillColor": "110099FF" 
    } 
} 

-Code für ScanURLActivity.java

public class ScanURLActivity extends AppCompatActivity { 
    private static final String TAG = ScanURLActivity.class.getSimpleName(); 
    private AnylineOcrScanView scanView; 
    private URLResultView urlResultView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Set the flag to keep the screen on (otherwise the screen may go dark during scanning) 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

     setContentView(R.layout.activity_anyline_ocr); 

     addURLResultView(); 

     String license = getString(R.string.anyline_license_key); 

     // Copies given traineddata-file to a place where the core can access it. 
     // This MUST be called for every traineddata file that is used (before startScanning() is called). 
     // The file must be located directly in the assets directory (or in tessdata/ but no other folders are allowed) 
     scanView = new AnylineOcrScanView(getApplicationContext(),null); 
     scanView.copyTrainedData("tessdata/eng_no_dict.traineddata", "d142032d86da1be4dbe22dce2eec18d7"); 
     scanView.copyTrainedData("tessdata/deu.traineddata", "2d5190b9b62e28fa6d17b728ca195776"); 

     //Configure the OCR for URLs 
     AnylineOcrConfig anylineOcrConfig = new AnylineOcrConfig(); 
     // use the line mode (line length and font may vary) 
     anylineOcrConfig.setScanMode(AnylineOcrConfig.ScanMode.LINE); 
     // set the languages used for OCR 
     anylineOcrConfig.setTesseractLanguages("eng_no_dict", "deu"); 
     // allow only capital letters and numbers 
     anylineOcrConfig.setCharWhitelist("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.\\:"); 
     // set the height range the text can have 
     anylineOcrConfig.setMinCharHeight(20); 
     anylineOcrConfig.setMaxCharHeight(60); 
     // The minimum confidence required to return a result, a value between 0 and 100. 
     // (higher confidence means less likely to get a wrong result, but may be slower to get a result) 
     anylineOcrConfig.setMinConfidence(65); 
     // a simple regex for a basic validation of the URL, results that don't match this, will not be returned 
     // (full validation is more complex, as different countries have different formats) 
     anylineOcrConfig.setValidationRegex("^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$"); 
     // removes small contours (helpful in this case as no letters with small artifacts are allowed, like iöäü) 
     anylineOcrConfig.setRemoveSmallContours(true); 
     // removes whitespaces from the result 
     // (also causes faster processing, because optimizations can be made if whitespaces are not relevant) 
     anylineOcrConfig.setRemoveWhitespaces(true); 
     // Experimental parameter to set the minimum sharpness (value between 0-100; 0 to turn sharpness detection off) 
     // The goal of the minimum sharpness is to avoid a time consuming ocr step, 
     // if the image is blurry and good results are therefor not likely. 
     anylineOcrConfig.setMinSharpness(66); 
     // set the ocr config 
     scanView.setAnylineOcrConfig(anylineOcrConfig); 

     // set an individual focus configuration for this example 
     FocusConfig focusConfig = new FocusConfig.Builder() 
       .setDefaultMode(Camera.Parameters.FOCUS_MODE_AUTO) // set default focus mode to be auto focus 
       .setAutoFocusInterval(8000) // set an interval of 8 seconds for auto focus 
       .setEnableFocusOnTouch(true) // enable focus on touch functionality 
       .setEnablePhaseAutoFocus(true) // enable phase focus for faster focusing on new devices 
       .setEnableFocusAreas(true) // enable focus areas to coincide with the cutout 
       .build(); 
     // set the focus config 
     scanView.setFocusConfig(focusConfig); 
     // set the highest possible preview fps range 
     scanView.setUseMaxFpsRange(true); 
     // set sports scene mode to try and bump up the fps count even more 
     scanView.setSceneMode(Camera.Parameters.SCENE_MODE_SPORTS); 

     // initialize with the license and a listener 
     scanView.initAnyline(license, new AnylineOcrListener() { 
      @Override 
      public void onReport(String identifier, Object value) { 
       // Called with interesting values, that arise during processing. 
       // Some possibly reported values: 
       // 
       // $brightness - the brightness of the center region of the cutout as a float value 
       // $confidence - the confidence, an Integer value between 0 and 100 
       // $thresholdedImage - the current image transformed into black and white 
       // $sharpness - the detected sharpness value (only reported if minSharpness > 0) 
      } 

      @Override 
      public boolean onTextOutlineDetected(List<PointF> list) { 
       // Called when the outline of a possible text is detected. 
       // If false is returned, the outline is drawn automatically. 
       return false; 
      } 

      @Override 
      public void onResult(AnylineOcrResult result) { 
       // Called when a valid result is found (minimum confidence is exceeded and validation with regex was ok) 
       urlResultView.setResult(result.getText()); 
       urlResultView.setVisibility(View.VISIBLE); 
      } 

      @Override 
      public void onAbortRun(AnylineOcrError code, String message) { 
       // Is called when no result was found for the current image. 
       // E.g. if no text was found or the result is not valid. 
      } 
     }); 

     // disable the reporting if set to off in preferences 
     if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
       SettingsFragment.KEY_PREF_REPORTING_ON, true)) { 
      // The reporting of results - including the photo of a scanned meter - 
      // helps us in improving our product, and the customer experience. 
      // However, if you wish to turn off this reporting feature, you can do it like this: 
      scanView.setReportingEnabled(false); 
     } 
     urlResultView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       urlResultView.setVisibility(View.INVISIBLE); 
       scanView.startScanning(); 
      } 
     }); 

    } 
    private void addURLResultView() { 
     RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout); 

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 

     urlResultView = new URLResultView(this); 
     urlResultView.setVisibility(View.INVISIBLE); 

     mainLayout.addView(urlResultView, params); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     scanView.startScanning(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     scanView.cancelScanning(); 
     scanView.releaseCameraInBackground(); 
    } 

    @Override 
    public void onBackPressed() { 
     if (urlResultView.getVisibility() == View.VISIBLE) { 
      urlResultView.setVisibility(View.INVISIBLE); 
      scanView.startScanning(); 
     } else { 
      super.onBackPressed(); 
     } 

    } 
} 

Die AnylineOCRlistener kann in keinem Fall ocr Ergebnisse trotz Zuversicht bei 65.

gesetzt erkennen Code für URLResultView.java Klasse

public class URLResultView extends RelativeLayout { 

    private TextView resultText; 

    public URLResultView(Context context) { 
     super(context); 
     init(); 
    } 

    public URLResultView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public URLResultView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 

     init(); 
    } 

    private void init() { 

     setPadding(DimensUtil.getPixFromDp(getContext(), 4), DimensUtil.getPixFromDp(getContext(), 16), 
       DimensUtil.getPixFromDp(getContext(), 4), DimensUtil.getPixFromDp(getContext(), 16)); 

     //setBackgroundResource(R.drawable.); 

     inflate(getContext(), R.layout.url_result, this); 

     resultText = (TextView) findViewById(R.id.text_result); 
    } 

    public void setResult(String result) { 
     resultText.setText(result.trim()); 
    } 
} 

Kann mir jemand bei dieser Integration helfen, da ich keine andere Ressource/Anleitung finden kann als ihre Dokumentation und SDK-Beispiele.

+0

Hallo, haben Sie es geschafft URL aus Bildern zu extrahieren? – Dake

Antwort

0

Eigentlich gibt es zwei Probleme, die Sie in lauteten:

  1. Sie nur den umgekehrten Schrägstrich \ zum charWhitelist hinzugefügt, nicht der Schrägstrich /
  2. Sie haben removeSmallContours zu false

einstellen removeSmallContours entfernt alles von der Linie, die kleiner ist als die minCharHeight. In Ihrem Fall wird es die : und . entfernen, weil sie zu klein für das SDK sind.

Nachdem diese beiden Einstellungen zu ändern, funktioniert das Scannen mit einem feinen minConfidence von 85.