Skip to content

Commit

Permalink
Merge pull request #11 from HighLiuk/main
Browse files Browse the repository at this point in the history
Add Block, Paragraph, Word, Symbol models for Full Text Annotation
  • Loading branch information
faithoflifedev authored Sep 5, 2023
2 parents 5b40d5e + 9590f92 commit c1e1aaf
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/google_vision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export 'src/model/annotate_image_response.dart';
export 'src/model/annotated_responses.dart';
export 'src/model/annotation_request.dart';
export 'src/model/annotation_requests.dart';
export 'src/model/block.dart';
export 'src/model/bounding_poly.dart';
export 'src/model/color_info.dart';
export 'src/model/color.dart';
Expand All @@ -44,17 +45,20 @@ export 'src/model/localized_object_annotation.dart';
export 'src/model/location_info.dart';
export 'src/model/normalized_vertex.dart';
export 'src/model/page.dart';
export 'src/model/paragraph.dart';
export 'src/model/position.dart';
export 'src/model/product_search_params.dart';
export 'src/model/property.dart';
export 'src/model/safe_search_annotation.dart';
export 'src/model/status.dart';
export 'src/model/symbol.dart';
export 'src/model/text_annotation.dart';
export 'src/model/text_detection_params.dart';
export 'src/model/text_property.dart';
export 'src/model/token.dart';
export 'src/model/vertex.dart';
export 'src/model/web_detection_params.dart';
export 'src/model/word.dart';

export 'src/provider/oauth.dart';
export 'src/provider/vision.dart';
Expand Down
78 changes: 78 additions & 0 deletions lib/src/model/block.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ignore_for_file: constant_identifier_names

import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import 'bounding_poly.dart';
import 'paragraph.dart';
import 'text_property.dart';

part 'block.g.dart';

/// Logical element on the page.
@JsonSerializable(explicitToJson: true)
class Block {
/// Additional information detected for the block.
final TextProperty? property;

/// The bounding box for the block. The vertices are in the order of top-left,
/// top-right, bottom-right, bottom-left. When a rotation of the bounding box
/// is detected the rotation is represented as around the top-left corner as
/// defined when the text is read in the 'natural' orientation. For example:
///
/// - when the text is horizontal it might look like:
///
/// ```
/// 0----1
/// | |
/// 3----2
/// ```
///
/// - when it's rotated 180 degrees around the top-left corner it becomes:
///
/// ```
/// 2----3
/// | |
/// 1----0
/// ```
///
/// and the vertex order will still be (0, 1, 2, 3).
final BoundingPoly? boundingBox;

/// List of paragraphs in this block (if this blocks is of type text).
final List<Paragraph>? paragraphs;

/// Detected block type (text, image etc) for this block.
@JsonKey(unknownEnumValue: BlockType.UNKNOWN)
final BlockType blockType;

/// Confidence of the OCR results on the block. Range [0, 1\].
final double? confidence;

Block({
required this.property,
required this.boundingBox,
this.blockType = BlockType.UNKNOWN,
required this.paragraphs,
required this.confidence,
});

factory Block.fromJson(Map<String, dynamic> json) => _$BlockFromJson(json);

Map<String, dynamic> toJson() => _$BlockToJson(this);

@override
String toString() => jsonEncode(toJson());
}

/// Type of a block (text, image etc) as identified by OCR.
@JsonEnum()
enum BlockType {
UNKNOWN,
TEXT,
TABLE,
PICTURE,
RULER,
BARCODE,
}
40 changes: 40 additions & 0 deletions lib/src/model/block.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/model/detected_break.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DetectedBreak {
BreakType type;

/// True if break prepends the element.
final bool isPrefix;
final bool? isPrefix;

DetectedBreak({
this.type = BreakType.UNKNOWN,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/detected_break.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion lib/src/model/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import 'block.dart';
import 'text_property.dart';

part 'page.g.dart';

/// Additional information detected on the structural component.
/// Detected page from OCR.
@JsonSerializable(explicitToJson: true)
class Page {
/// Additional information detected on the page.
Expand All @@ -18,13 +19,17 @@ class Page {
/// Page height. For PDFs the unit is points. For images (including TIFFs) the unit is pixels.
final int height;

/// List of blocks of text, images etc on this page.
final List<Block>? blocks;

/// Confidence of the OCR results on the page. Range [0, 1\].
final double? confidence;

Page({
required this.property,
required this.width,
required this.height,
required this.blocks,
required this.confidence,
});

Expand Down
4 changes: 4 additions & 0 deletions lib/src/model/page.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions lib/src/model/paragraph.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import 'bounding_poly.dart';
import 'text_property.dart';
import 'word.dart';

part 'paragraph.g.dart';

/// Structural unit of text representing a number of words in certain order.
@JsonSerializable(explicitToJson: true)
class Paragraph {
/// Additional information detected for the paragraph.
final TextProperty? property;

/// The bounding box for the paragraph. The vertices are in the order of
/// top-left, top-right, bottom-right, bottom-left. When a rotation of the
/// bounding box is detected the rotation is represented as around the
/// top-left corner as defined when the text is read in the 'natural'
/// orientation. For example:
///
/// - when the text is horizontal it might look like:
///
/// ```
/// 0----1
/// | |
/// 3----2
/// ```
///
/// - when it's rotated 180 degrees around the top-left corner it becomes:
///
/// ```
/// 2----3
/// | |
/// 1----0
/// ```
///
/// and the vertex order will still be (0, 1, 2, 3).
final BoundingPoly? boundingBox;

/// List of all words in this paragraph.
final List<Word>? words;

/// Confidence of the OCR results for the paragraph. Range [0, 1\].
final double? confidence;

Paragraph({
required this.property,
required this.boundingBox,
required this.words,
required this.confidence,
});

factory Paragraph.fromJson(Map<String, dynamic> json) =>
_$ParagraphFromJson(json);

Map<String, dynamic> toJson() => _$ParagraphToJson(this);

@override
String toString() => jsonEncode(toJson());
}
27 changes: 27 additions & 0 deletions lib/src/model/paragraph.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions lib/src/model/symbol.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import 'bounding_poly.dart';
import 'text_property.dart';

part 'symbol.g.dart';

/// A single symbol representation.
@JsonSerializable(explicitToJson: true)
class Symbol {
/// Additional information detected for the symbol.
final TextProperty? property;

/// The bounding box for the symbol. The vertices are in the order of
/// top-left, top-right, bottom-right, bottom-left. When a rotation of the
/// bounding box is detected the rotation is represented as around the
/// top-left corner as defined when the text is read in the 'natural'
/// orientation. For example:
///
/// - when the text is horizontal it might look like:
///
/// ```
/// 0----1
/// | |
/// 3----2
/// ```
///
/// - when it's rotated 180 degrees around the top-left corner it becomes:
///
/// ```
/// 2----3
/// | |
/// 1----0
/// ```
///
/// and the vertex order will still be (0, 1, 2, 3).
final BoundingPoly? boundingBox;

/// The actual UTF-8 representation of the symbol.
final String? text;

/// Confidence of the OCR results for the symbol. Range [0, 1\].
final double? confidence;

Symbol({
required this.property,
required this.boundingBox,
required this.text,
required this.confidence,
});

factory Symbol.fromJson(Map<String, dynamic> json) => _$SymbolFromJson(json);

Map<String, dynamic> toJson() => _$SymbolToJson(this);

@override
String toString() => jsonEncode(toJson());
}
25 changes: 25 additions & 0 deletions lib/src/model/symbol.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c1e1aaf

Please sign in to comment.