Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,9 @@ def detect_book_regions(image: np.ndarray, min_area=10000, eps_coef=0.02):
|
|
21 |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
|
22 |
closed = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
|
23 |
|
24 |
-
contours, _ = cv2.findContours(
|
|
|
|
|
25 |
boxes = []
|
26 |
|
27 |
for cnt in contours:
|
@@ -98,7 +100,7 @@ def query_openlibrary(title_text: str, author_text: str = None):
|
|
98 |
def process_image(image_file):
|
99 |
"""
|
100 |
Gradio passes a PIL image or numpy array. Convert to OpenCV BGR, detect covers β OCR β OpenLibrary.
|
101 |
-
Return a DataFrame and
|
102 |
"""
|
103 |
img = np.array(image_file)[:, :, ::-1].copy() # PIL to OpenCV BGR
|
104 |
boxes = detect_book_regions(img)
|
@@ -126,15 +128,10 @@ def process_image(image_file):
|
|
126 |
}
|
127 |
)
|
128 |
|
129 |
-
# Build DataFrame
|
130 |
-
|
131 |
-
df_empty = pd.DataFrame(columns=["title", "author_name", "publisher", "first_publish_year"])
|
132 |
-
csv_bytes = df_empty.to_csv(index=False).encode()
|
133 |
-
return df_empty, csv_bytes
|
134 |
-
|
135 |
-
df = pd.DataFrame(records)
|
136 |
csv_bytes = df.to_csv(index=False).encode()
|
137 |
-
return df, csv_bytes
|
138 |
|
139 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
140 |
# 5. Build the Gradio Interface
|
@@ -162,9 +159,8 @@ def build_interface():
|
|
162 |
download_file = gr.File(label="Download CSV")
|
163 |
|
164 |
def on_run(image):
|
165 |
-
df,
|
166 |
-
|
167 |
-
return df, {"name": "books.csv", "data": csv_bytes}
|
168 |
|
169 |
run_button.click(
|
170 |
fn=on_run,
|
@@ -176,5 +172,4 @@ def build_interface():
|
|
176 |
|
177 |
if __name__ == "__main__":
|
178 |
demo_app = build_interface()
|
179 |
-
|
180 |
-
demo_app.launch()
|
|
|
21 |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
|
22 |
closed = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
|
23 |
|
24 |
+
contours, _ = cv2.findContours(
|
25 |
+
closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
26 |
+
)
|
27 |
boxes = []
|
28 |
|
29 |
for cnt in contours:
|
|
|
100 |
def process_image(image_file):
|
101 |
"""
|
102 |
Gradio passes a PIL image or numpy array. Convert to OpenCV BGR, detect covers β OCR β OpenLibrary.
|
103 |
+
Return a DataFrame and a (filename, bytes) tuple for CSV.
|
104 |
"""
|
105 |
img = np.array(image_file)[:, :, ::-1].copy() # PIL to OpenCV BGR
|
106 |
boxes = detect_book_regions(img)
|
|
|
128 |
}
|
129 |
)
|
130 |
|
131 |
+
# Build DataFrame (even if empty)
|
132 |
+
df = pd.DataFrame(records, columns=["title", "author_name", "publisher", "first_publish_year"])
|
|
|
|
|
|
|
|
|
|
|
133 |
csv_bytes = df.to_csv(index=False).encode()
|
134 |
+
return df, ("books.csv", csv_bytes)
|
135 |
|
136 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
137 |
# 5. Build the Gradio Interface
|
|
|
159 |
download_file = gr.File(label="Download CSV")
|
160 |
|
161 |
def on_run(image):
|
162 |
+
df, file_tuple = process_image(image)
|
163 |
+
return df, file_tuple
|
|
|
164 |
|
165 |
run_button.click(
|
166 |
fn=on_run,
|
|
|
172 |
|
173 |
if __name__ == "__main__":
|
174 |
demo_app = build_interface()
|
175 |
+
demo_app.launch()
|
|