Update json-CR1.py
Browse files- json-CR1.py +145 -143
json-CR1.py
CHANGED
|
@@ -1,143 +1,145 @@
|
|
| 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\CR1"
|
| 12 |
-
# مجلد إخراج ملفات JSON
|
| 13 |
-
output_json_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR1\cr1_json"
|
| 14 |
-
|
| 15 |
-
# Ensure output folder exists
|
| 16 |
-
os.makedirs(output_json_folder, exist_ok=True)
|
| 17 |
-
|
| 18 |
-
# Exact same prompt
|
| 19 |
-
prompt = """
|
| 20 |
-
Please extract the following fields from the CR1 commercial registration document image
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
-
|
| 26 |
-
-
|
| 27 |
-
-
|
| 28 |
-
-
|
| 29 |
-
-
|
| 30 |
-
-
|
| 31 |
-
-
|
| 32 |
-
-
|
| 33 |
-
- Its
|
| 34 |
-
-
|
| 35 |
-
-
|
| 36 |
-
-
|
| 37 |
-
-
|
| 38 |
-
-
|
| 39 |
-
-
|
| 40 |
-
-
|
| 41 |
-
-
|
| 42 |
-
-
|
| 43 |
-
-
|
| 44 |
-
- Its
|
| 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 |
-
print(f"
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
| 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\CR1"
|
| 12 |
+
# مجلد إخراج ملفات JSON
|
| 13 |
+
output_json_folder = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch\classified\CR1\cr1_json"
|
| 14 |
+
|
| 15 |
+
# Ensure output folder exists
|
| 16 |
+
os.makedirs(output_json_folder, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
# Exact same prompt
|
| 19 |
+
prompt = """
|
| 20 |
+
Please extract the following fields from the CR1 commercial registration document image. Extract the Arabic text, but return the output as JSON using the exact English keys below. If any field is missing, set its value to null.
|
| 21 |
+
|
| 22 |
+
Fields to extract (in Arabic content):
|
| 23 |
+
- Unified number
|
| 24 |
+
- Establishment number
|
| 25 |
+
- Commercial name of the institution
|
| 26 |
+
- Its main center
|
| 27 |
+
- Phone
|
| 28 |
+
- Postal code
|
| 29 |
+
- Trader name
|
| 30 |
+
- Nationality
|
| 31 |
+
- Date of birth
|
| 32 |
+
- Civil registry number - residence
|
| 33 |
+
- Its date
|
| 34 |
+
- Its source
|
| 35 |
+
- Passport number
|
| 36 |
+
- Its date
|
| 37 |
+
- Issuer
|
| 38 |
+
- Activity
|
| 39 |
+
- Capital
|
| 40 |
+
- Name of the manager or authorized agent
|
| 41 |
+
- Nationality
|
| 42 |
+
- Date of birth
|
| 43 |
+
- Civil registry number - residence
|
| 44 |
+
- Its date
|
| 45 |
+
- Its source
|
| 46 |
+
- Certified by the commercial registration office in the city
|
| 47 |
+
- That the above-mentioned institution is registered in the city
|
| 48 |
+
- Certificates expire on
|
| 49 |
+
- Receipt number
|
| 50 |
+
- Date
|
| 51 |
+
|
| 52 |
+
Return the output in JSON format with these exact keys:
|
| 53 |
+
|
| 54 |
+
{
|
| 55 |
+
"unified_number": null,
|
| 56 |
+
"establishment_number": null,
|
| 57 |
+
"institution_name": null,
|
| 58 |
+
"main_office": null,
|
| 59 |
+
"phone": null,
|
| 60 |
+
"postal_code": null,
|
| 61 |
+
"merchant_name": null,
|
| 62 |
+
"nationality": null,
|
| 63 |
+
"birth_date": null,
|
| 64 |
+
"national_id": null,
|
| 65 |
+
"national_id_issue_date": null,
|
| 66 |
+
"national_id_issue_place": null,
|
| 67 |
+
"passport_number": null,
|
| 68 |
+
"passport_issue_date": null,
|
| 69 |
+
"passport_issuer": null,
|
| 70 |
+
"business_activity": null,
|
| 71 |
+
"capital": null,
|
| 72 |
+
"manager_name": null,
|
| 73 |
+
"manager_nationality": null,
|
| 74 |
+
"manager_birth_date": null,
|
| 75 |
+
"manager_national_id": null,
|
| 76 |
+
"manager_id_issue_date": null,
|
| 77 |
+
"manager_id_issue_place": null,
|
| 78 |
+
"registry_office_city": null,
|
| 79 |
+
"registered_in_city": null,
|
| 80 |
+
"certificate_expiry_date": null,
|
| 81 |
+
"receipt_number": null,
|
| 82 |
+
"receipt_date": null
|
| 83 |
+
}
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key={API_KEY}"
|
| 88 |
+
headers = {"Content-Type": "application/json"}
|
| 89 |
+
|
| 90 |
+
# Iterate over all images
|
| 91 |
+
for image_name in os.listdir(cr1_images_folder):
|
| 92 |
+
if not image_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
image_path = os.path.join(cr1_images_folder, image_name)
|
| 96 |
+
base_name = os.path.splitext(image_name)[0]
|
| 97 |
+
output_file = os.path.join(output_json_folder, base_name + ".json")
|
| 98 |
+
|
| 99 |
+
# Skip if JSON already exists
|
| 100 |
+
if os.path.exists(output_file):
|
| 101 |
+
print(f"Skipped {image_name} (JSON file already exists)")
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
# Read image and convert to base64
|
| 105 |
+
with open(image_path, "rb") as f:
|
| 106 |
+
image_b64 = base64.b64encode(f.read()).decode()
|
| 107 |
+
|
| 108 |
+
# Send request to Gemini API
|
| 109 |
+
data = {
|
| 110 |
+
"contents": [
|
| 111 |
+
{
|
| 112 |
+
"role": "user",
|
| 113 |
+
"parts": [
|
| 114 |
+
{"text": prompt},
|
| 115 |
+
{
|
| 116 |
+
"inline_data": {
|
| 117 |
+
"mime_type": "image/jpeg",
|
| 118 |
+
"data": image_b64
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
]
|
| 122 |
+
}
|
| 123 |
+
]
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
response = requests.post(url, headers=headers, json=data)
|
| 128 |
+
response.raise_for_status()
|
| 129 |
+
response_text = response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 130 |
+
|
| 131 |
+
match = re.search(r"```json\s*(\{.*\})\s*```", response_text, re.DOTALL)
|
| 132 |
+
if match:
|
| 133 |
+
json_text = match.group(1)
|
| 134 |
+
result = json.loads(json_text)
|
| 135 |
+
|
| 136 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 137 |
+
json.dump(result, f, ensure_ascii=False, indent=2)
|
| 138 |
+
|
| 139 |
+
print(f"✅ Processed: {image_name}")
|
| 140 |
+
else:
|
| 141 |
+
print(f"❌ Failed to extract JSON from: {image_name}")
|
| 142 |
+
print(response_text)
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
print(f"❌ Error processing image {image_name}: {e}")
|