Update json-CR3.py
Browse files- json-CR3.py +169 -165
json-CR3.py
CHANGED
|
@@ -1,165 +1,169 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import json
|
| 3 |
-
import re
|
| 4 |
-
import requests
|
| 5 |
-
import os
|
| 6 |
-
import time
|
| 7 |
-
|
| 8 |
-
API_KEY = "AIzaSyBr2-dUqHDZkk20hlWeEcpWnVVdkq9fqyE"
|
| 9 |
-
|
| 10 |
-
cr1_images_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR2"
|
| 11 |
-
output_json_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR2\cr2_json"
|
| 12 |
-
|
| 13 |
-
os.makedirs(output_json_folder, exist_ok=True)
|
| 14 |
-
|
| 15 |
-
prompt = """
|
| 16 |
-
Extract the following fields from the CR3 document image. Return both Arabic and English text where available:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
ال
|
| 20 |
-
|
| 21 |
-
ا
|
| 22 |
-
الج
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
مر
|
| 28 |
-
ها
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
ال
|
| 33 |
-
|
| 34 |
-
ال
|
| 35 |
-
ال
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
ال
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
و
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
-
"
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"
|
| 84 |
-
"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
result["
|
| 147 |
-
|
| 148 |
-
result["
|
| 149 |
-
result["اسم ال
|
| 150 |
-
result["ا
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
print(
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
API_KEY = "AIzaSyBr2-dUqHDZkk20hlWeEcpWnVVdkq9fqyE"
|
| 9 |
+
|
| 10 |
+
cr1_images_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR2"
|
| 11 |
+
output_json_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR2\cr2_json"
|
| 12 |
+
|
| 13 |
+
os.makedirs(output_json_folder, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
prompt = """
|
| 16 |
+
Extract the following fields from the CR3 document image. Return both Arabic text and English text where available:
|
| 17 |
+
|
| 18 |
+
Fields to extract (in Arabic):
|
| 19 |
+
- الرقم
|
| 20 |
+
- التاريخ
|
| 21 |
+
- الرقم الموحد للمنشأة
|
| 22 |
+
- اسم التاجر
|
| 23 |
+
- الجنسية
|
| 24 |
+
- تاريخ الميلاد
|
| 25 |
+
- رقم السجل المدني-الإقامة
|
| 26 |
+
- تاريخه
|
| 27 |
+
- مصدرة
|
| 28 |
+
- مركزها الرئيسي
|
| 29 |
+
- هاتف
|
| 30 |
+
- الرمز البريدي
|
| 31 |
+
- رقم سجل المركز الرئيسي
|
| 32 |
+
- الاسم التجاري للفرع
|
| 33 |
+
- العنوان
|
| 34 |
+
- الرمز البريدي
|
| 35 |
+
- الهاتف
|
| 36 |
+
- النشاط
|
| 37 |
+
- رأس المال
|
| 38 |
+
- إسم المدير او الوكيل المفوض
|
| 39 |
+
- الجنسية
|
| 40 |
+
- تاريخ الميلاد
|
| 41 |
+
- رقم السجل المدني-الإقامة
|
| 42 |
+
- تاريخه
|
| 43 |
+
- مصدره
|
| 44 |
+
- سلطات المدير
|
| 45 |
+
- يشهد مكتب السجل التجاري بمدينة
|
| 46 |
+
- بأنه تم تسجيل المؤسسة المذكورة أعلاة بمدينة
|
| 47 |
+
- وتنتهي صلاحية الشهادات في
|
| 48 |
+
- بموجب الإيصال رقم
|
| 49 |
+
- وتاريخ
|
| 50 |
+
- مدير السجل التجاري
|
| 51 |
+
|
| 52 |
+
Return the result as JSON with the following keys in English:
|
| 53 |
+
|
| 54 |
+
{
|
| 55 |
+
"document_number": null,
|
| 56 |
+
"document_date": null,
|
| 57 |
+
"unified_establishment_number": null,
|
| 58 |
+
"merchant_name": null,
|
| 59 |
+
"nationality": null,
|
| 60 |
+
"birth_date": null,
|
| 61 |
+
"national_id": null,
|
| 62 |
+
"national_id_issue_date": null,
|
| 63 |
+
"national_id_issue_place": null,
|
| 64 |
+
"main_office": null,
|
| 65 |
+
"phone": null,
|
| 66 |
+
"postal_code": null,
|
| 67 |
+
"main_office_registry_number": null,
|
| 68 |
+
"branch_commercial_name": null,
|
| 69 |
+
"branch_address": null,
|
| 70 |
+
"branch_postal_code": null,
|
| 71 |
+
"branch_phone": null,
|
| 72 |
+
"business_activity": null,
|
| 73 |
+
"capital": null,
|
| 74 |
+
"manager_name": null,
|
| 75 |
+
"manager_nationality": null,
|
| 76 |
+
"manager_birth_date": null,
|
| 77 |
+
"manager_national_id": null,
|
| 78 |
+
"manager_id_issue_date": null,
|
| 79 |
+
"manager_id_issue_place": null,
|
| 80 |
+
"manager_authority": null,
|
| 81 |
+
"registry_office_city": null,
|
| 82 |
+
"registered_in_city": null,
|
| 83 |
+
"certificate_expiry_date": null,
|
| 84 |
+
"receipt_number": null,
|
| 85 |
+
"receipt_date": null,
|
| 86 |
+
"registry_manager": null
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
If a field is missing, set its value to null.
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key={API_KEY}"
|
| 93 |
+
headers = {"Content-Type": "application/json"}
|
| 94 |
+
|
| 95 |
+
def split_address(address):
|
| 96 |
+
parts = [p.strip() for p in address.split("،")]
|
| 97 |
+
while len(parts) < 4:
|
| 98 |
+
parts.append(None)
|
| 99 |
+
return parts
|
| 100 |
+
|
| 101 |
+
for image_name in os.listdir(cr1_images_folder):
|
| 102 |
+
if not image_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 103 |
+
continue
|
| 104 |
+
|
| 105 |
+
image_path = os.path.join(cr1_images_folder, image_name)
|
| 106 |
+
base_name = os.path.splitext(image_name)[0]
|
| 107 |
+
output_file = os.path.join(output_json_folder, base_name + ".json")
|
| 108 |
+
|
| 109 |
+
if os.path.exists(output_file):
|
| 110 |
+
print(f"Skipped {image_name} (JSON file already exists)")
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
with open(image_path, "rb") as f:
|
| 114 |
+
image_b64 = base64.b64encode(f.read()).decode()
|
| 115 |
+
|
| 116 |
+
data = {
|
| 117 |
+
"contents": [
|
| 118 |
+
{
|
| 119 |
+
"role": "user",
|
| 120 |
+
"parts": [
|
| 121 |
+
{"text": prompt},
|
| 122 |
+
{
|
| 123 |
+
"inline_data": {
|
| 124 |
+
"mime_type": "image/jpeg",
|
| 125 |
+
"data": image_b64
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
]
|
| 129 |
+
}
|
| 130 |
+
]
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
try:
|
| 134 |
+
response = requests.post(url, headers=headers, json=data)
|
| 135 |
+
response.raise_for_status()
|
| 136 |
+
response_text = response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 137 |
+
|
| 138 |
+
# استخدام التعبير المنتظم الصحيح
|
| 139 |
+
match = re.search(r"```json\s*(\{.*?\})\s*```", response_text, re.DOTALL)
|
| 140 |
+
if match:
|
| 141 |
+
json_text = match.group(1)
|
| 142 |
+
result = json.loads(json_text)
|
| 143 |
+
|
| 144 |
+
# تقسيم العنوان إلى أجزاء منفصلة
|
| 145 |
+
if "مركزها الرئيسي" in result and result["مركزها الرئيسي"]:
|
| 146 |
+
parts = split_address(result["مركزها الرئيسي"])
|
| 147 |
+
result["رقم المبنى"] = parts[0]
|
| 148 |
+
result["اسم الشارع"] = parts[1]
|
| 149 |
+
result["اسم الحي"] = parts[2]
|
| 150 |
+
result["الرقم الإضافي"] = parts[3]
|
| 151 |
+
else:
|
| 152 |
+
result["رقم المبنى"] = None
|
| 153 |
+
result["اسم الشارع"] = None
|
| 154 |
+
result["اسم الحي"] = None
|
| 155 |
+
result["الرقم الإضافي"] = None
|
| 156 |
+
|
| 157 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 158 |
+
json.dump(result, f, ensure_ascii=False, indent=2)
|
| 159 |
+
|
| 160 |
+
print(f"✅ Processed: {image_name}")
|
| 161 |
+
|
| 162 |
+
else:
|
| 163 |
+
print(f"❌ Failed to extract JSON from: {image_name}")
|
| 164 |
+
print(response_text)
|
| 165 |
+
|
| 166 |
+
time.sleep(3) # ينتظر 3 ثواني قبل إرسال الصورة التالية
|
| 167 |
+
|
| 168 |
+
except Exception as e:
|
| 169 |
+
print(f"❌ Error processing image {image_name}: {e}")
|