Update json-V3.py
Browse files- json-V3.py +105 -104
json-V3.py
CHANGED
|
@@ -1,104 +1,105 @@
|
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
1. اسم المكلف
|
| 23 |
-
2. عنوان المركز الرئيسي
|
| 24 |
-
3. المدينة
|
| 25 |
-
4. الحي
|
| 26 |
-
5. صندوق البريد
|
| 27 |
-
6. الرمز البريدي
|
| 28 |
-
7. الهاتف
|
| 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 |
-
if
|
| 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 |
-
response.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
print(
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
| 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 |
+
Extract the following fields from the document (in Arabic only) and return them in JSON format:
|
| 21 |
+
|
| 22 |
+
1. اسم المكلف
|
| 23 |
+
2. عنوان المركز الرئيسي
|
| 24 |
+
3. المدينة
|
| 25 |
+
4. الحي
|
| 26 |
+
5. صندوق البريد
|
| 27 |
+
6. الرمز البريدي
|
| 28 |
+
7. الهاتف
|
| 29 |
+
|
| 30 |
+
The required JSON format:
|
| 31 |
+
|
| 32 |
+
{
|
| 33 |
+
"taxpayer_name": "...",
|
| 34 |
+
"headquarters_address": "...",
|
| 35 |
+
"city": "...",
|
| 36 |
+
"district": "...",
|
| 37 |
+
"po_box": "...",
|
| 38 |
+
"postal_code": "...",
|
| 39 |
+
"phone": "..."
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
If any field is missing, return it as null.
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key={API_KEY}"
|
| 48 |
+
headers = {"Content-Type": "application/json"}
|
| 49 |
+
|
| 50 |
+
# Iterate over all images
|
| 51 |
+
for image_name in os.listdir(cr1_images_folder):
|
| 52 |
+
if not image_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 53 |
+
continue
|
| 54 |
+
|
| 55 |
+
image_path = os.path.join(cr1_images_folder, image_name)
|
| 56 |
+
base_name = os.path.splitext(image_name)[0]
|
| 57 |
+
output_file = os.path.join(output_json_folder, base_name + ".json")
|
| 58 |
+
|
| 59 |
+
# Skip if JSON already exists
|
| 60 |
+
if os.path.exists(output_file):
|
| 61 |
+
print(f"Skipped {image_name} (JSON file already exists)")
|
| 62 |
+
continue
|
| 63 |
+
|
| 64 |
+
# Read image and convert to base64
|
| 65 |
+
with open(image_path, "rb") as f:
|
| 66 |
+
image_b64 = base64.b64encode(f.read()).decode()
|
| 67 |
+
|
| 68 |
+
# Send request to Gemini API
|
| 69 |
+
data = {
|
| 70 |
+
"contents": [
|
| 71 |
+
{
|
| 72 |
+
"role": "user",
|
| 73 |
+
"parts": [
|
| 74 |
+
{"text": prompt},
|
| 75 |
+
{
|
| 76 |
+
"inline_data": {
|
| 77 |
+
"mime_type": "image/jpeg",
|
| 78 |
+
"data": image_b64
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
]
|
| 82 |
+
}
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
response = requests.post(url, headers=headers, json=data)
|
| 88 |
+
response.raise_for_status()
|
| 89 |
+
response_text = response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 90 |
+
|
| 91 |
+
match = re.search(r"```json\s*(\{.*\})\s*```", response_text, re.DOTALL)
|
| 92 |
+
if match:
|
| 93 |
+
json_text = match.group(1)
|
| 94 |
+
result = json.loads(json_text)
|
| 95 |
+
|
| 96 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 97 |
+
json.dump(result, f, ensure_ascii=False, indent=2)
|
| 98 |
+
|
| 99 |
+
print(f"✅ Processed: {image_name}")
|
| 100 |
+
else:
|
| 101 |
+
print(f"❌ Failed to extract JSON from: {image_name}")
|
| 102 |
+
print(response_text)
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"❌ Error processing image {image_name}: {e}")
|