7/17/2
This commit is contained in:
@@ -136,6 +136,7 @@ class CRUD {
|
||||
// 'https://eastus.api.cognitive.microsoft.com/vision/v3.2/ocr'
|
||||
'https://eastus.api.cognitive.microsoft.com/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2024-02-01'));
|
||||
request.body = json.encode({"url": imagePathFull});
|
||||
// request.body = json.encode({"url": imagePathFull});
|
||||
request.headers.addAll(headers);
|
||||
|
||||
http.StreamedResponse response = await request.send();
|
||||
|
||||
48
lib/controller/functions/face_detect.dart
Normal file
48
lib/controller/functions/face_detect.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'dart:convert';
|
||||
import 'package:SEFER/constant/api_key.dart';
|
||||
import 'package:SEFER/constant/box_name.dart';
|
||||
import 'package:SEFER/main.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../../constant/links.dart';
|
||||
import 'upload_image.dart';
|
||||
|
||||
Future<String> faceDetector() async {
|
||||
await ImageController().choosFace(AppLink.uploadEgypt, 'face_detect');
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
var headers = {
|
||||
'Authorization': 'Basic ${AK.basicCompareFaces}',
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
var request = http.Request(
|
||||
'POST',
|
||||
Uri.parse(
|
||||
'https://face-detect-f6924392c4c7.herokuapp.com/compare_faces'));
|
||||
|
||||
request.body = json.encode({
|
||||
"url1":
|
||||
"https://api.sefer.live/sefer/card_image/id_front-${box.read(BoxName.driverID)}.jpg",
|
||||
"url2":
|
||||
"https://api.sefer.live/sefer/card_image/face_detect-${box.read(BoxName.driverID)}.jpg"
|
||||
});
|
||||
print('request.body: ${request.body}');
|
||||
request.headers.addAll(headers);
|
||||
|
||||
try {
|
||||
http.StreamedResponse response = await request.send();
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
String result = await response.stream.bytesToString();
|
||||
print('result: ${result}');
|
||||
return result;
|
||||
} else {
|
||||
print('Error: ${response.reasonPhrase}');
|
||||
return 'Error: ${response.reasonPhrase}';
|
||||
}
|
||||
} catch (e) {
|
||||
print('Exception occurred: $e');
|
||||
return 'Error: $e';
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import 'package:SEFER/controller/functions/crud.dart';
|
||||
import 'package:SEFER/controller/functions/device_info.dart';
|
||||
import 'package:SEFER/main.dart';
|
||||
import 'package:SEFER/views/home/Captin/home_captain/home_captin.dart';
|
||||
import 'package:SEFER/views/widgets/mydialoug.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:image_cropper/image_cropper.dart';
|
||||
@@ -21,6 +22,21 @@ import '../../constant/colors.dart';
|
||||
import 'tts.dart';
|
||||
import 'upload_image.dart';
|
||||
|
||||
enum LicenseSide {
|
||||
front,
|
||||
back,
|
||||
unknown,
|
||||
}
|
||||
|
||||
enum DocumentType {
|
||||
carLicenseFront,
|
||||
carLicenseBack,
|
||||
idCardFront,
|
||||
idCardBack,
|
||||
driverLicense,
|
||||
unknown,
|
||||
}
|
||||
|
||||
class AI extends GetxController {
|
||||
bool approved = false;
|
||||
bool isDriverSaved = false;
|
||||
@@ -306,90 +322,6 @@ class AI extends GetxController {
|
||||
// }
|
||||
}
|
||||
|
||||
// Future<void> addDriverEgypt() async {
|
||||
// // try {
|
||||
// // Extract values from box or set defaults
|
||||
// var firstName = responseIdEgyptDriverLicense['firstName'] ?? '';
|
||||
// var lastName = responseIdEgyptDriverLicense['lastName'] ?? '';
|
||||
// var email = box.read(BoxName.emailDriver) ?? '';
|
||||
// var phone = box.read(BoxName.phoneDriver)?.toString() ?? '';
|
||||
// var driverId = box.read(BoxName.driverID)?.toString() ?? '';
|
||||
// var password = '123456';
|
||||
// var gender = responseIdEgyptBack['gender'] == 'ذكر' ? 'Male' : 'Female';
|
||||
// var licenseType = responseIdEgyptDriverLicense['license_type'] ?? '';
|
||||
// var nationalNumber = responseIdEgyptBack['nationalID'] ?? '';
|
||||
// var nameArabic = responseIdEgyptDriverLicense['name_arabic'] ?? '';
|
||||
// var nameEnglish = responseIdEgyptDriverLicense['name_english'] ?? '';
|
||||
// var issueDate = responseIdEgyptDriverLicense['issue_date'] ?? '';
|
||||
// var expiryDate = responseIdEgyptDriverLicense['expiry_date'] ?? '';
|
||||
// var licenseCategories =
|
||||
// responseIdEgyptDriverLicense['license_categories'] ?? '';
|
||||
// var address = responseIdEgyptFront['address'] ?? '';
|
||||
// var cardId = responseIdEgyptFront['card_id'] ?? '';
|
||||
// var occupation = responseIdEgyptBack['occupation'] ?? '';
|
||||
// var education = responseIdEgyptBack['occupation'] ?? '';
|
||||
// var licenseIssueDate =
|
||||
// responseIdEgyptDriverLicense['issue_date'].toString() ?? '';
|
||||
// var religion = responseIdEgyptBack['religion'] ?? '';
|
||||
// var status = responseIdEgyptBack['fullName'] ?? '';
|
||||
// var birthdate = responseIdEgyptFront['dob'] != null
|
||||
// ? DateTime.parse(responseIdEgyptFront['dob'] + '-01-01').toString()
|
||||
// : '1960-01-01';
|
||||
//
|
||||
// var maritalStatus = responseIdEgyptBack['maritalStatus'] ?? '';
|
||||
// var site = responseIdEgyptDriverLicense['address'] ?? '';
|
||||
// var employmentType = responseIdEgyptDriverLicense['employmentType'] ?? '';
|
||||
//
|
||||
// // Create payload
|
||||
// var payload = {
|
||||
// 'first_name': firstName,
|
||||
// 'last_name': lastName,
|
||||
// 'email': email,
|
||||
// 'phone': phone,
|
||||
// 'id': driverId,
|
||||
// 'password': password,
|
||||
// 'gender': gender,
|
||||
// 'license_type': licenseType,
|
||||
// 'national_number': nationalNumber,
|
||||
// 'name_arabic': nameArabic,
|
||||
// 'name_english': nameEnglish,
|
||||
// 'issue_date': issueDate,
|
||||
// 'expiry_date': expiryDate,
|
||||
// 'license_categories': licenseCategories,
|
||||
// 'address': address,
|
||||
// 'card_id': cardId,
|
||||
// 'occupation': occupation,
|
||||
// 'education': education,
|
||||
// 'licenseIssueDate': licenseIssueDate,
|
||||
// 'religion': religion,
|
||||
// 'status': status,
|
||||
// 'birthdate': birthdate,
|
||||
// 'maritalStatus': maritalStatus,
|
||||
// 'site': site,
|
||||
// 'employmentType': employmentType,
|
||||
// };
|
||||
// isLoading = true;
|
||||
// update();
|
||||
// // Make POST request
|
||||
// var res = await CRUD().post(link: AppLink.signUpCaptin, payload: payload);
|
||||
// var status1 = jsonDecode(res);
|
||||
// isLoading = false;
|
||||
// update();
|
||||
// // Handle response
|
||||
//
|
||||
// if (status1['status'] == 'success') {
|
||||
// isDriverSaved = true;
|
||||
// Get.snackbar('Success', 'Driver data saved successfully',
|
||||
// backgroundColor: AppColor.greenColor);
|
||||
// } else {
|
||||
// Get.snackbar('Error', 'Failed to save driver data',
|
||||
// backgroundColor: Colors.red);
|
||||
// }
|
||||
// // } catch (e) {
|
||||
// // Get.snackbar('Error', 'An error occurred while saving driver data',
|
||||
// // backgroundColor: Colors.red);
|
||||
// // }
|
||||
// }
|
||||
addCriminalDeocuments() async {
|
||||
var res = await CRUD().post(link: AppLink.addCriminalDocuments, payload: {
|
||||
"driverId": box.read(BoxName.driverID),
|
||||
@@ -448,9 +380,11 @@ class AI extends GetxController {
|
||||
Map<String, dynamic> responseIdEgyptDriverLicense = {};
|
||||
String? responseIdCardDriverEgypt1;
|
||||
bool isloading = false;
|
||||
bool isLoading = false;
|
||||
var image;
|
||||
CroppedFile? croppedFile;
|
||||
DateTime now = DateTime.now();
|
||||
|
||||
Future<void> pickImage() async {
|
||||
final pickedImage = await picker.pickImage(source: ImageSource.gallery);
|
||||
|
||||
@@ -498,23 +432,121 @@ class AI extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
bool isLoading = false;
|
||||
Future allMethodForAI(String prompt, linkPHP, imagePath) async {
|
||||
DocumentType checkDocumentType(String text) {
|
||||
// Convert text to lowercase and remove all spaces and new lines
|
||||
text = text.toLowerCase().replaceAll(RegExp(r'\s+'), '');
|
||||
|
||||
// Keywords for each document type
|
||||
final Map<DocumentType, List<String>> keywords = {
|
||||
DocumentType.carLicenseBack: ['شاسيه', 'موتور', 'سم٣'],
|
||||
DocumentType.carLicenseFront: ['وزارةالداخلية', 'رخصةتسيير'],
|
||||
DocumentType.idCardFront: ['بطاقةتحقيقالشخصية'],
|
||||
DocumentType.idCardBack: ['البطاقةساريةحتى'],
|
||||
DocumentType.driverLicense: ['قيادةخاصة', 'خاصه'],
|
||||
};
|
||||
|
||||
// Check each document type
|
||||
for (var entry in keywords.entries) {
|
||||
if (entry.value.any((keyword) => text.contains(keyword))) {
|
||||
return entry.key;
|
||||
}
|
||||
}
|
||||
|
||||
// If no match is found
|
||||
return DocumentType.unknown;
|
||||
}
|
||||
|
||||
Future<void> allMethodForAI(
|
||||
String prompt, String linkPHP, String imagePath) async {
|
||||
isLoading = true;
|
||||
update();
|
||||
await ImageController().choosImage(linkPHP, imagePath);
|
||||
Future.delayed(const Duration(seconds: 2));
|
||||
// print(imagePath);
|
||||
var extractedString =
|
||||
await CRUD().arabicTextExtractByVisionAndAI(imagePath: imagePath);
|
||||
print(extractedString);
|
||||
var json = jsonDecode(extractedString);
|
||||
var textValues = CRUD().extractTextFromLines(json);
|
||||
print(textValues);
|
||||
// await Get.put(AI()).geminiAiExtraction(prompt, textValues, imagePath);
|
||||
await Get.put(AI()).anthropicAI(textValues, prompt, imagePath);
|
||||
isLoading = false;
|
||||
update();
|
||||
|
||||
try {
|
||||
await ImageController().choosImage(linkPHP, imagePath);
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
var extractedString =
|
||||
await CRUD().arabicTextExtractByVisionAndAI(imagePath: imagePath);
|
||||
var json = jsonDecode(extractedString);
|
||||
var textValues = CRUD().extractTextFromLines(json);
|
||||
print('textValues: ${textValues}');
|
||||
|
||||
DocumentType detectedType = checkDocumentType(textValues);
|
||||
String expectedDocument = getExpectedDocument(imagePath);
|
||||
String detectedDocument = getDetectedDocument(detectedType);
|
||||
|
||||
bool isCorrectDocument = (detectedType == getExpectedType(imagePath));
|
||||
|
||||
if (!isCorrectDocument) {
|
||||
MyDialog().getDialog('incorrect_document_title'.tr,
|
||||
'${'expected'.tr}: $expectedDocument\n${'detected'.tr}: $detectedDocument',
|
||||
() {
|
||||
Get.back();
|
||||
});
|
||||
} else {
|
||||
// Process the correct document
|
||||
await Get.put(AI()).anthropicAI(textValues, prompt, imagePath);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error in allMethodForAI: $e');
|
||||
MyDialog().getDialog('error'.tr, 'error_processing_document'.tr, () {
|
||||
Get.back();
|
||||
});
|
||||
} finally {
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
String getExpectedDocument(String imagePath) {
|
||||
switch (imagePath) {
|
||||
case 'car_front':
|
||||
return 'car_license_front'.tr;
|
||||
case 'car_back':
|
||||
return 'car_license_back'.tr;
|
||||
case 'id_back':
|
||||
return 'id_card_back'.tr;
|
||||
case 'id_front':
|
||||
return 'id_card_front'.tr;
|
||||
case 'driver_license':
|
||||
return 'driver_license'.tr;
|
||||
default:
|
||||
return 'unknown_document'.tr;
|
||||
}
|
||||
}
|
||||
|
||||
DocumentType getExpectedType(String imagePath) {
|
||||
switch (imagePath) {
|
||||
case 'car_front':
|
||||
return DocumentType.carLicenseFront;
|
||||
case 'car_back':
|
||||
return DocumentType.carLicenseBack;
|
||||
case 'id_back':
|
||||
return DocumentType.idCardBack;
|
||||
case 'id_front':
|
||||
return DocumentType.idCardFront;
|
||||
case 'driver_license':
|
||||
return DocumentType.driverLicense;
|
||||
default:
|
||||
return DocumentType.unknown;
|
||||
}
|
||||
}
|
||||
|
||||
String getDetectedDocument(DocumentType type) {
|
||||
switch (type) {
|
||||
case DocumentType.carLicenseFront:
|
||||
return 'car_license_front'.tr;
|
||||
case DocumentType.carLicenseBack:
|
||||
return 'car_license_back'.tr;
|
||||
case DocumentType.idCardFront:
|
||||
return 'id_card_front'.tr;
|
||||
case DocumentType.idCardBack:
|
||||
return 'id_card_back'.tr;
|
||||
case DocumentType.driverLicense:
|
||||
return 'driver_license'.tr;
|
||||
default:
|
||||
return 'unknown_document'.tr;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> generateContent() async {
|
||||
|
||||
@@ -8,6 +8,10 @@ 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 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:path_provider/path_provider.dart' as path_provider;
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/colors.dart';
|
||||
import '../../main.dart';
|
||||
@@ -53,11 +57,12 @@ class ImageController extends GetxController {
|
||||
isloading = true;
|
||||
update();
|
||||
// Save the cropped image
|
||||
File savedCroppedImage = File(croppedFile!.path);
|
||||
// File savedCroppedImage = File(croppedFile!.path);
|
||||
File compressedImage = await compressImage(File(croppedFile!.path));
|
||||
print('link =$link');
|
||||
try {
|
||||
await uploadImage(
|
||||
savedCroppedImage,
|
||||
compressedImage,
|
||||
{
|
||||
'driverID':
|
||||
box.read(BoxName.driverID) ?? box.read(BoxName.passengerID),
|
||||
@@ -74,6 +79,41 @@ class ImageController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
choosFace(String link, String imageType) async {
|
||||
final pickedImage = await picker.pickImage(
|
||||
source: ImageSource.camera,
|
||||
preferredCameraDevice: CameraDevice.front,
|
||||
);
|
||||
if (pickedImage != null) {
|
||||
image = File(pickedImage.path);
|
||||
isloading = true;
|
||||
update();
|
||||
// Compress the image
|
||||
File compressedImage = await compressImage(File(pickedImage.path));
|
||||
|
||||
// Save the picked image directly
|
||||
// File savedImage = File(pickedImage.path);
|
||||
print('link =$link');
|
||||
try {
|
||||
await uploadImage(
|
||||
compressedImage,
|
||||
{
|
||||
'driverID':
|
||||
box.read(BoxName.driverID) ?? box.read(BoxName.passengerID),
|
||||
'imageType': imageType
|
||||
},
|
||||
link,
|
||||
);
|
||||
} catch (e) {
|
||||
Get.snackbar('Image Upload Failed'.tr, e.toString(),
|
||||
backgroundColor: AppColor.redColor);
|
||||
} finally {
|
||||
isloading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uploadImage(File file, Map data, String link) async {
|
||||
var request = http.MultipartRequest(
|
||||
'POST',
|
||||
@@ -149,11 +189,12 @@ class ImageController extends GetxController {
|
||||
isloading = true;
|
||||
update();
|
||||
// Save the cropped image
|
||||
File savedCroppedImage = File(croppedFile!.path);
|
||||
// File savedCroppedImage = File(croppedFile!.path);
|
||||
File compressedImage = await compressImage(File(croppedFile!.path));
|
||||
print('link =$link');
|
||||
try {
|
||||
await uploadImage(
|
||||
savedCroppedImage,
|
||||
compressedImage,
|
||||
{
|
||||
'driverID':
|
||||
box.read(BoxName.driverID) ?? box.read(BoxName.passengerID),
|
||||
@@ -210,3 +251,18 @@ class ImageController extends GetxController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<File> compressImage(File file) async {
|
||||
final dir = await path_provider.getTemporaryDirectory();
|
||||
final targetPath = "${dir.absolute.path}/temp.jpg";
|
||||
|
||||
var result = await FlutterImageCompress.compressAndGetFile(
|
||||
file.absolute.path,
|
||||
targetPath,
|
||||
quality: 70,
|
||||
minWidth: 1024,
|
||||
minHeight: 1024,
|
||||
);
|
||||
|
||||
return File(result!.path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user