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

feat: #494 - added OCR for packaging #497

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
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
24 changes: 13 additions & 11 deletions lib/model/OcrIngredientsResult.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import '../interface/JsonObject.dart';

part 'OcrIngredientsResult.g.dart';

/// Result from OCR applied to ingredients.
@JsonSerializable()
class OcrIngredientsResult extends JsonObject {
const OcrIngredientsResult({
this.status,
this.ingredientsTextFromImageOrig,
this.ingredientsTextFromImage,
});

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

@override
Map<String, dynamic> toJson() => _$OcrIngredientsResultToJson(this);

final int? status;

@JsonKey(name: 'ingredients_text_from_image_orig')
final String? ingredientsTextFromImageOrig;

@JsonKey(name: 'ingredients_text_from_image')
final String? ingredientsTextFromImage;

const OcrIngredientsResult(
{this.status,
this.ingredientsTextFromImageOrig,
this.ingredientsTextFromImage});

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

@override
Map<String, dynamic> toJson() => _$OcrIngredientsResultToJson(this);
}
28 changes: 28 additions & 0 deletions lib/model/OcrPackagingResult.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:json_annotation/json_annotation.dart';
import '../interface/JsonObject.dart';

part 'OcrPackagingResult.g.dart';

/// Result from OCR applied to packaging.
@JsonSerializable()
class OcrPackagingResult extends JsonObject {
const OcrPackagingResult({
this.status,
this.textFromImageOrig,
this.textFromImage,
});

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

@override
Map<String, dynamic> toJson() => _$OcrPackagingResultToJson(this);

final int? status;

@JsonKey(name: 'packaging_text_from_image_orig')
final String? textFromImageOrig;

@JsonKey(name: 'packaging_text_from_image')
final String? textFromImage;
}
21 changes: 21 additions & 0 deletions lib/model/OcrPackagingResult.g.dart

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

45 changes: 37 additions & 8 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:http/http.dart';
import 'package:openfoodfacts/interface/JsonObject.dart';
import 'package:openfoodfacts/model/KnowledgePanels.dart';
import 'package:openfoodfacts/model/OcrIngredientsResult.dart';
import 'package:openfoodfacts/model/OcrPackagingResult.dart';
import 'package:openfoodfacts/model/OrderedNutrients.dart';
import 'package:openfoodfacts/model/ProductFreshness.dart';
import 'package:openfoodfacts/model/ProductImage.dart';
Expand Down Expand Up @@ -700,26 +701,54 @@ class OpenFoodAPIClient {
OcrField ocrField = OcrField.GOOGLE_CLOUD_VISION,
QueryType? queryType,
}) async {
var ocrUri = UriHelper.getPostUri(
final Uri uri = UriHelper.getPostUri(
path: '/cgi/ingredients.pl',
queryType: queryType,
);
Map<String, String> queryParameters = <String, String>{
final Map<String, String> queryParameters = <String, String>{
'code': barcode,
'process_image': '1',
'id': 'ingredients_${language.code}',
'ocr_engine': OcrField.GOOGLE_CLOUD_VISION.key
'ocr_engine': ocrField.key
};
Response response = await HttpHelper().doPostRequest(
ocrUri,
final Response response = await HttpHelper().doPostRequest(
uri,
queryParameters,
user,
queryType: queryType,
);
return OcrIngredientsResult.fromJson(
json.decode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>,
);
}

OcrIngredientsResult result = OcrIngredientsResult.fromJson(
json.decode(utf8.decode(response.bodyBytes)));
return result;
/// Extracts the text from packaging image with OCR.
static Future<OcrPackagingResult> extractPackaging(
final User user,
final String barcode,
final OpenFoodFactsLanguage language, {
final OcrField ocrField = OcrField.GOOGLE_CLOUD_VISION,
final QueryType? queryType,
}) async {
final Uri uri = UriHelper.getPostUri(
path: '/cgi/packaging.pl',
queryType: queryType,
);
final Map<String, String> queryParameters = <String, String>{
'code': barcode,
'process_image': '1',
'id': 'packaging_${language.code}',
'ocr_engine': ocrField.key
};
final Response response = await HttpHelper().doPostRequest(
uri,
queryParameters,
user,
queryType: queryType,
);
return OcrPackagingResult.fromJson(
json.decode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>,
);
}

/// Give user suggestion based on autocompleted outputs
Expand Down