This commit is contained in:
Hamza-Ayed
2023-11-25 14:05:02 +03:00
parent 1d2ceed031
commit a8d6c509c1
37 changed files with 679 additions and 307 deletions

View File

@@ -1,10 +1,11 @@
import 'dart:convert';
import 'dart:io';
import 'dart:ui';
import 'package:get/get.dart';
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:ride/constant/colors.dart';
import 'package:ride/controller/functions/llama_ai.dart';
class CarRegistrationRecognizerController extends GetxController {
@override
@@ -15,7 +16,6 @@ class CarRegistrationRecognizerController extends GetxController {
// The ImagePicker instance
final ImagePicker _imagePicker = ImagePicker();
Map extractedData = {};
// The GoogleMlKit TextRecognizer instance
final TextRecognizer _textRecognizer = TextRecognizer();
@@ -24,47 +24,57 @@ class CarRegistrationRecognizerController extends GetxController {
String? scannedText;
String? jsonOutput;
final List<Map<String, dynamic>> lines = [];
Map extracted = {};
XFile? image;
Map decode = {};
CroppedFile? croppedFile;
// Picks an image from the camera or gallery and extracts the text
final List<Map<String, dynamic>> extractedTextWithCoordinates = [];
Future<void> scanText() async {
// Pick an image from the camera or gallery
image = await _imagePicker.pickImage(source: ImageSource.gallery);
update();
// If no image was picked, return
if (image == null) {
return;
}
// Convert the XFile object to an InputImage object
final InputImage inputImage = InputImage.fromFile(File(image!.path));
// Crop the image
croppedFile = await ImageCropper().cropImage(
sourcePath: image!.path,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio16x9
],
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'Cropper',
toolbarColor: AppColor.blueColor,
toolbarWidgetColor: AppColor.yellowColor,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false),
IOSUiSettings(
title: 'Cropper',
),
],
);
// If no cropped image was obtained, return
if (croppedFile == null) {
return;
}
// Convert the cropped file to an InputImage object
final InputImage inputImage = InputImage.fromFile(File(croppedFile!.path));
// Recognize the text in the image
final RecognizedText recognizedText =
await _textRecognizer.processImage(inputImage);
scannedText = recognizedText.text;
final Map<String, dynamic> extractedData = {};
for (TextBlock block in recognizedText.blocks) {
for (TextLine line in block.lines) {
final String lineText = line.text;
final Rect lineBoundingBox = line.boundingBox!;
extractedTextWithCoordinates.add({
'text': lineText,
'boundingBox': {
'left': lineBoundingBox.left,
'top': lineBoundingBox.top,
'width': lineBoundingBox.width,
'height': lineBoundingBox.height,
},
});
}
update();
}
print(jsonEncode(extractedTextWithCoordinates));
// Extract the scanned text line by line
final List<Map<String, dynamic>> lines = [];
@@ -73,15 +83,20 @@ class CarRegistrationRecognizerController extends GetxController {
i.toString(): recognizedText.blocks[i].text,
});
}
// print(jsonEncode(lines));
// Convert the list of lines to a JSON string
jsonOutput = jsonEncode(extractedData);
decode = jsonDecode(jsonOutput!);
String result = lines.map((map) => map.values.first.toString()).join(' ');
// print(result.length);
if (result.length > 2200) {
result = result.substring(0, 2200);
}
// print('jsonOutput------------------------------');
// print(result);
Map result2 = await LlamaAi().getCarRegistrationData(result);
// Assign the result to the extracted variable
extracted = result2;
print('extracted is ' + extracted.toString());
update();
print('jsonOutput------------------------------');
print(decode);
// print(jsonEncode(lines));
}
}