Update json-CR6.py
Browse files- json-CR6.py +140 -138
json-CR6.py
CHANGED
|
@@ -1,138 +1,140 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import json
|
| 3 |
-
import re
|
| 4 |
-
import requests
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
# مفتاح Gemini API
|
| 8 |
-
API_KEY = "AIzaSyBr2-dUqHDZkk20hlWeEcpWnVVdkq9fqyE"
|
| 9 |
-
|
| 10 |
-
# المجلد الذي يحتوي صور cr1
|
| 11 |
-
cr1_images_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR6"
|
| 12 |
-
|
| 13 |
-
# مجلد إخراج ملفات JSON
|
| 14 |
-
output_json_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR6\cr6_json"
|
| 15 |
-
|
| 16 |
-
# Ensure output folder exists
|
| 17 |
-
os.makedirs(output_json_folder, exist_ok=True)
|
| 18 |
-
|
| 19 |
-
# Exact same prompt
|
| 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 |
-
result["
|
| 120 |
-
result["ال
|
| 121 |
-
result["ر
|
| 122 |
-
|
| 123 |
-
result["رقم ال
|
| 124 |
-
|
| 125 |
-
result["
|
| 126 |
-
result["ال
|
| 127 |
-
result["ر
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
print(f"
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# مفتاح Gemini API
|
| 8 |
+
API_KEY = "AIzaSyBr2-dUqHDZkk20hlWeEcpWnVVdkq9fqyE"
|
| 9 |
+
|
| 10 |
+
# المجلد الذي يحتوي صور cr1
|
| 11 |
+
cr1_images_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR6"
|
| 12 |
+
|
| 13 |
+
# مجلد إخراج ملفات JSON
|
| 14 |
+
output_json_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR6\cr6_json"
|
| 15 |
+
|
| 16 |
+
# Ensure output folder exists
|
| 17 |
+
os.makedirs(output_json_folder, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# Exact same prompt
|
| 20 |
+
# Exact same prompt
|
| 21 |
+
prompt = """
|
| 22 |
+
Please extract the following fields from the CR6 commercial registration document image, all in Arabic only:
|
| 23 |
+
|
| 24 |
+
- الكيان التجاري
|
| 25 |
+
- حالة السجل
|
| 26 |
+
- مدة المنشأة
|
| 27 |
+
- الرقم الوطني الموحد للمنشأة
|
| 28 |
+
- رابط المتجر الإكتروني
|
| 29 |
+
- رأس المال
|
| 30 |
+
- المدينة
|
| 31 |
+
- صندوق البريد
|
| 32 |
+
- الرمز البريدي
|
| 33 |
+
- هاتف
|
| 34 |
+
- تاريخ اصدار السجل
|
| 35 |
+
- تاريخ انتهاء السجل
|
| 36 |
+
- الموقع الاكتروني
|
| 37 |
+
- العنوان
|
| 38 |
+
- النشاط التجاري
|
| 39 |
+
|
| 40 |
+
Please return the result as JSON with the following exact keys, and if any field is missing, set its value to null:
|
| 41 |
+
|
| 42 |
+
{
|
| 43 |
+
"commercial_entity": null,
|
| 44 |
+
"registry_status": null,
|
| 45 |
+
"establishment_duration": null,
|
| 46 |
+
"unified_national_number": null,
|
| 47 |
+
"online_store_link": null,
|
| 48 |
+
"capital": null,
|
| 49 |
+
"city": null,
|
| 50 |
+
"po_box": null,
|
| 51 |
+
"postal_code": null,
|
| 52 |
+
"phone": null,
|
| 53 |
+
"registry_issue_date": null,
|
| 54 |
+
"registry_expiry_date": null,
|
| 55 |
+
"website": null,
|
| 56 |
+
"address": null,
|
| 57 |
+
"business_activity": null,
|
| 58 |
+
"building_number": null,
|
| 59 |
+
"additional_number": null,
|
| 60 |
+
"unit_number": null
|
| 61 |
+
}
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key={API_KEY}"
|
| 66 |
+
headers = {"Content-Type": "application/json"}
|
| 67 |
+
|
| 68 |
+
# Iterate over all images
|
| 69 |
+
for image_name in os.listdir(cr1_images_folder):
|
| 70 |
+
if not image_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
image_path = os.path.join(cr1_images_folder, image_name)
|
| 74 |
+
base_name = os.path.splitext(image_name)[0]
|
| 75 |
+
output_file = os.path.join(output_json_folder, base_name + ".json")
|
| 76 |
+
|
| 77 |
+
# Skip if JSON already exists
|
| 78 |
+
if os.path.exists(output_file):
|
| 79 |
+
print(f"⏩ Skipped {image_name} (JSON file already exists)")
|
| 80 |
+
continue
|
| 81 |
+
|
| 82 |
+
# Read image and convert to base64
|
| 83 |
+
with open(image_path, "rb") as f:
|
| 84 |
+
image_b64 = base64.b64encode(f.read()).decode()
|
| 85 |
+
|
| 86 |
+
# Send request to Gemini API
|
| 87 |
+
data = {
|
| 88 |
+
"contents": [
|
| 89 |
+
{
|
| 90 |
+
"role": "user",
|
| 91 |
+
"parts": [
|
| 92 |
+
{"text": prompt},
|
| 93 |
+
{
|
| 94 |
+
"inline_data": {
|
| 95 |
+
"mime_type": "image/jpeg",
|
| 96 |
+
"data": image_b64
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
]
|
| 100 |
+
}
|
| 101 |
+
]
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
response = requests.post(url, headers=headers, json=data)
|
| 106 |
+
response.raise_for_status()
|
| 107 |
+
response_text = response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 108 |
+
|
| 109 |
+
match = re.search(r"```json\s*(\{.*\})\s*```", response_text, re.DOTALL)
|
| 110 |
+
if match:
|
| 111 |
+
json_text = match.group(1)
|
| 112 |
+
result = json.loads(json_text)
|
| 113 |
+
|
| 114 |
+
# تقسيم حقل العنوان إذا كان موجود
|
| 115 |
+
address = result.get("العنوان")
|
| 116 |
+
if address:
|
| 117 |
+
parts = address.strip().split()
|
| 118 |
+
if len(parts) == 5:
|
| 119 |
+
result["رقم المبنى"] = parts[0]
|
| 120 |
+
result["المدينة"] = parts[1]
|
| 121 |
+
result["الرمز البريدي"] = parts[2]
|
| 122 |
+
result["الرقم الإضافي"] = parts[3]
|
| 123 |
+
result["رقم الوحدة"] = parts[4]
|
| 124 |
+
else:
|
| 125 |
+
result["رقم المبنى"] = None
|
| 126 |
+
result["المدينة"] = None
|
| 127 |
+
result["الرمز البريدي"] = None
|
| 128 |
+
result["الرقم الإضافي"] = None
|
| 129 |
+
result["رقم الوحدة"] = None
|
| 130 |
+
|
| 131 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 132 |
+
json.dump(result, f, ensure_ascii=False, indent=2)
|
| 133 |
+
|
| 134 |
+
print(f"✅ Processed: {image_name}")
|
| 135 |
+
else:
|
| 136 |
+
print(f"❌ Failed to extract JSON from: {image_name}")
|
| 137 |
+
print(response_text)
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"❌ Error processing image {image_name}: {e}")
|