Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import csv
|
|
5 |
import os
|
6 |
import cv2
|
7 |
import numpy as np
|
|
|
8 |
import easyocr
|
9 |
import keras_ocr
|
10 |
from paddleocr import PaddleOCR
|
@@ -78,32 +79,23 @@ def generate_ocr(method, img):
|
|
78 |
|
79 |
return text_output, label
|
80 |
|
81 |
-
# Save extracted text to JSON & CSV
|
82 |
def save_results(text, label):
|
83 |
data = {"text": text, "label": label}
|
84 |
|
85 |
-
# Ensure the repository exists
|
86 |
-
repo_path = "./winamnd/ocr-llm-test"
|
87 |
-
if not os.path.exists(repo_path):
|
88 |
-
os.makedirs(repo_path)
|
89 |
-
|
90 |
-
# Define the full file paths for JSON and CSV
|
91 |
-
results_json = os.path.join(repo_path, "ocr_results.json")
|
92 |
-
results_csv = os.path.join(repo_path, "ocr_results.csv")
|
93 |
-
|
94 |
# Save to JSON
|
95 |
-
if not os.path.exists(
|
96 |
-
with open(
|
97 |
json.dump([], f)
|
98 |
-
with open(
|
99 |
content = json.load(f)
|
100 |
content.append(data)
|
101 |
f.seek(0)
|
102 |
json.dump(content, f, indent=4)
|
103 |
|
104 |
# Save to CSV
|
105 |
-
file_exists = os.path.exists(
|
106 |
-
with open(
|
107 |
writer = csv.DictWriter(f, fieldnames=["text", "label"])
|
108 |
if not file_exists:
|
109 |
writer.writeheader()
|
|
|
5 |
import os
|
6 |
import cv2
|
7 |
import numpy as np
|
8 |
+
import pandas as pd
|
9 |
import easyocr
|
10 |
import keras_ocr
|
11 |
from paddleocr import PaddleOCR
|
|
|
79 |
|
80 |
return text_output, label
|
81 |
|
82 |
+
# Save extracted text to JSON & CSV
|
83 |
def save_results(text, label):
|
84 |
data = {"text": text, "label": label}
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
# Save to JSON
|
87 |
+
if not os.path.exists(RESULTS_JSON):
|
88 |
+
with open(RESULTS_JSON, "w") as f:
|
89 |
json.dump([], f)
|
90 |
+
with open(RESULTS_JSON, "r+") as f:
|
91 |
content = json.load(f)
|
92 |
content.append(data)
|
93 |
f.seek(0)
|
94 |
json.dump(content, f, indent=4)
|
95 |
|
96 |
# Save to CSV
|
97 |
+
file_exists = os.path.exists(RESULTS_CSV)
|
98 |
+
with open(RESULTS_CSV, "a", newline="") as f:
|
99 |
writer = csv.DictWriter(f, fieldnames=["text", "label"])
|
100 |
if not file_exists:
|
101 |
writer.writeheader()
|