Files
tripz/lib/controller/auth/captin/ml_google_doc.dart
Hamza Aleghwairyeen dcf02cc36d 4/26/1
2024-04-26 18:23:45 +03:00

104 lines
3.1 KiB
Dart

import 'dart:io';
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:SEFER/constant/colors.dart';
import 'package:SEFER/controller/functions/llama_ai.dart';
class CarRegistrationRecognizerController extends GetxController {
@override
void onInit() {
// scanText();
super.onInit();
}
// The ImagePicker instance
final ImagePicker _imagePicker = ImagePicker();
// The GoogleMlKit TextRecognizer instance
final TextRecognizer _textRecognizer = TextRecognizer();
// The scanned text
String? scannedText;
String? jsonOutput;
final List<Map<String, dynamic>> lines = [];
Map extracted = {};
XFile? image;
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;
}
// 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'.tr,
toolbarColor: AppColor.blueColor,
toolbarWidgetColor: AppColor.yellowColor,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false),
IOSUiSettings(
title: 'Cropper'.tr,
),
],
);
// 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;
// Extract the scanned text line by line
final List<Map<String, dynamic>> lines = [];
for (var i = 0; i < recognizedText.blocks.length; i++) {
lines.add({
i.toString(): recognizedText.blocks[i].text,
});
}
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,
'vin,make,made,year,expiration_date,color,owner,registration_date'); //
// Assign the result to the extracted variable
extracted = result2;
print('extracted is ' + extracted.toString());
update();
}
}