This commit is contained in:
Hamza-Ayed
2024-05-11 00:58:11 +03:00
parent b473a9ce09
commit 750d980fcb
38 changed files with 329 additions and 1814 deletions

View File

@@ -28,9 +28,9 @@ class CRUD {
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}',
},
);
print("-----request----" + response.request.toString());
// print("-----request----" + response.request.toString());
// print("-----headers-----" + response.headers.toString());
print("-----payload-----" + payload.toString());
// print("-----payload-----" + payload.toString());
// if (response.statusCode == 200) {
// print(response.body);
var jsonData = jsonDecode(response.body);
@@ -104,40 +104,79 @@ class CRUD {
Future allMethodForAI(String prompt, linkPHP, imagePath) async {
await ImageController().choosImage(linkPHP, imagePath);
Future.delayed(const Duration(seconds: 2));
String extracted =
var extractedString =
await arabicTextExtractByVisionAndAI(imagePath: imagePath);
// print('extractedString');
var json = jsonDecode(extractedString);
// print(extractedString);
// print(json);
var textValues = getAllTextValuesWithLineNumbers(json);
// List<String> textValues = getAllTextValues(json);
Map<String, dynamic> extractedData = extractText(extracted);
print(extractedData);
// await AI().geminiAiExtraction(prompt, extractedData);
print('extractedData');
print(textValues);
await AI().geminiAiExtraction(prompt, textValues);
}
Map<String, dynamic> extractText(String jsonData) {
Map<String, dynamic> data = jsonDecode(jsonData);
List<List<String>> textLines = [];
Map<String, List<Map<String, String>>> getAllTextValuesWithLineNumbers(
Map json) {
Map<String, List<Map<String, String>>> output = {};
int lineNumber = 1;
for (var region in data['regions']) {
List<String> lineTexts = [];
for (var line in region['lines']) {
String lineText = "";
for (var word in line['words']) {
lineText += word['text'] + " ";
if (json.containsKey('regions')) {
List<dynamic> regions = json['regions'];
for (Map<String, dynamic> region in regions) {
if (region.containsKey('lines')) {
List<dynamic> lines = region['lines'];
List<Map<String, String>> linesWithText = [];
for (Map<String, dynamic> line in lines) {
if (line.containsKey('words')) {
List<dynamic> words = line['words'];
String lineText = "";
for (Map<String, dynamic> word in words) {
if (word.containsKey('text')) {
lineText += word['text'] + " ";
}
}
lineText = lineText.trim();
linesWithText.add(
{"line_number": lineNumber.toString(), "text": lineText});
lineNumber++;
}
}
output["region_${region.hashCode}"] = linesWithText;
}
lineText = lineText.trim();
lineTexts.add(lineText);
}
textLines.add(lineTexts);
}
// You can choose between returning a new JSON or a List based on your needs
// Option 1: Return a new JSON with "textLines" key
return {"textLines": textLines};
// Option 2: Return a List of List<String> (lines with words)
// return textLines;
return output;
}
// List<String> getAllTextValues(Map json) {
// List<String> textValues = [];
// if (json.containsKey('regions')) {
// List<dynamic> regions = json['regions'];
// for (Map<String, dynamic> region in regions) {
// if (region.containsKey('lines')) {
// List<dynamic> lines = region['lines'];
// for (Map<String, dynamic> line in lines) {
// if (line.containsKey('words')) {
// List<dynamic> words = line['words'];
// for (Map<String, dynamic> word in words) {
// if (word.containsKey('text')) {
// textValues.add(word['text']);
// }
// }
// }
// }
// }
// }
// }
// return textValues;
// }
Future<dynamic> arabicTextExtractByVisionAndAI({
required String imagePath,
}) async {
@@ -145,27 +184,27 @@ class CRUD {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '21010e54b50f41a4904708c526e102df'
};
var url = Uri.parse(
'https://ocrhamza.cognitiveservices.azure.com/vision/v2.1/ocr?language=ar',
);
// var url = Uri.parse(
// 'https://ocrhamza.cognitiveservices.azure.com/vision/v2.1/ocr?language=ar',
// );
String imagePathFull =
'${AppLink.server}card_image/$imagePath-${box.read(BoxName.driverID) ?? box.read(BoxName.passengerID)}.jpg';
'${AppLink.server}card_image/$imagePath-${box.read(BoxName.driverID)}.jpg';
// print('imagePath=$imagePathFull');
var requestBody = {"url": imagePathFull};
var response = await http.post(
url,
body: jsonEncode(requestBody), // Encode the JSON object to a string
headers: headers,
);
var request = http.Request(
'POST',
Uri.parse(
'https://ocrhamza.cognitiveservices.azure.com/vision/v2.1/ocr?language=ar'));
request.body = json.encode({"url": imagePathFull});
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
var responseBody = jsonDecode(response.body);
print(responseBody);
print('imagePath=$imagePathFull');
return responseBody.toString();
// print(await response.stream.bytesToString());
return await response.stream.bytesToString();
} else {
print(response.reasonPhrase);
}
return response.statusCode;
}
Future<dynamic> getChatGPT({

View File

@@ -171,7 +171,7 @@ class AI extends GetxController {
// },
{
'text':
"Extract the desired information from the following passage as json decoded like $prompt just in this:\n\n$payload"
"Extract the desired information from the following passage as json decoded like $prompt .and look for this instruction first name in line 3or 2 ,fullname in line 4 from it written left to right but you modify it rtl,address in line 5 it written left to right but you modify it rtl and 6,dob,nationalid in the last line as just in this:\n\n$payload"
},
],
},
@@ -196,7 +196,7 @@ class AI extends GetxController {
},
]
});
print(requestBody);
// print(requestBody);
final response = await http.post(
Uri.parse(

View File

@@ -81,7 +81,7 @@ class LocationController extends GetxController {
// )));
// }
Get.find<HomeCaptainController>()
.mapHomeCaptainController
.mapHomeCaptainController!
.animateCamera(CameraUpdate.newLatLng(LatLng(
Get.find<LocationController>().myLocation.latitude,
Get.find<LocationController>().myLocation.longitude,

View File

@@ -0,0 +1,80 @@
import 'package:SEFER/constant/api_key.dart';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/info.dart';
import 'package:SEFER/constant/links.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
class SmsEgypt extends GetxController {
var headers = {'Content-Type': 'application/json'};
Future sendSmsEgypt(String phone, otp) async {
var res = await http.post(
Uri.parse(AppLink.sendSms),
body: {
"username": AppInformation.appName,
"password": AK.smsPassword,
"message": "${AppInformation.appName} app code is $otp\ncopy it to app",
"language": box.read(BoxName.lang) == 'en' ? "e" : 'r',
"sender": "Kazumi", // todo add sefer sender name
"receiver": "2$phone"
},
headers: headers,
);
if (res.statusCode == 200) {
Get.defaultDialog(
title: 'You will recieve code in sms message'.tr,
middleText: '',
confirm: MyElevatedButton(
title: 'OK'.tr,
onPressed: () {
Get.back();
}));
}
}
Future checkCredit(String phone, otp) async {
var res = await http.post(
Uri.parse(AppLink.checkCredit),
body: {
"username": AppInformation.appName,
"password": AK.smsPassword,
},
headers: headers,
);
print(res);
}
Future sendSmsWithValidaty(String phone, otp) async {
var res = await http.post(
Uri.parse(AppLink.checkCredit),
body: {
"username": AppInformation.appName,
"password": AK.smsPassword,
"message": "This is an example SMS message.",
"language": box.read(BoxName.lang) == 'en' ? "e" : 'r',
"sender": "Kazumi", // todo add sefer sender name
"receiver": "2$phone",
"validity": "10",
"StartTime": DateTime.now().toString() // "1/1/2024 10:00:00"
},
headers: headers,
);
print(res);
}
Future sendSmsStatus(String smsid) async {
var res = await http.post(
Uri.parse(AppLink.checkCredit),
body: {
"username": AppInformation.appName,
"password": AK.smsPassword,
"smsid": smsid //"00b77dfc-5b8f-474d-9def-9f0158b70f98"
},
headers: headers,
);
print(res);
}
}

View File

@@ -7,7 +7,7 @@ import 'package:http/http.dart' as http;
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';
import 'package:image/image.dart' as image_package;
import '../../constant/box_name.dart';
import '../../constant/colors.dart';
import '../../main.dart';
@@ -21,6 +21,7 @@ class ImageController extends GetxController {
choosImage(String link, String imageType) async {
final pickedImage = await picker.pickImage(source: ImageSource.gallery);
image = File(pickedImage!.path);
croppedFile = await ImageCropper().cropImage(
sourcePath: image!.path,
aspectRatioPresets: [