File size: 10,133 Bytes
430a6c7
 
 
 
 
 
bbc77e0
 
430a6c7
 
cece48d
430a6c7
cece48d
430a6c7
cece48d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430a6c7
 
cece48d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d668e84
cece48d
 
 
 
b97942e
 
 
cece48d
b97942e
cece48d
b97942e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721608
b97942e
 
 
 
 
 
 
cece48d
b97942e
 
 
cece48d
 
 
 
b97942e
d668e84
 
b97942e
cece48d
 
d668e84
cece48d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b97942e
0475645
 
b97942e
bbc77e0
d668e84
bbc77e0
 
 
 
 
 
b97942e
 
cece48d
b97942e
 
cece48d
b97942e
 
cece48d
 
 
 
 
 
 
 
 
 
 
 
 
b97942e
 
 
 
cece48d
b97942e
 
 
 
cece48d
4721608
b97942e
4721608
b97942e
 
bbc77e0
 
b97942e
4721608
 
 
 
 
b97942e
 
 
 
 
0475645
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import cv2
import numpy as np
import pytesseract
import requests
import pandas as pd
import gradio as gr
import uuid
import os

# ──────────────────────────────────────────────────────────────
# 1. Utility: Detect rectangular contours (approximate book covers)
# ──────────────────────────────────────────────────────────────
def detect_book_regions(image: np.ndarray, min_area=5000, eps_coef=0.02):
    """
    Detect rectangular regions in an image that likely correspond to book covers.
    Returns a list of bounding boxes: (x, y, w, h).
    """
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edges = cv2.Canny(blurred, 50, 150)

    # Dilate + erode to close gaps
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    closed = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

    contours, _ = cv2.findContours(
        closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )
    boxes = []

    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area < min_area:
            continue

        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, eps_coef * peri, True)

        # Keep only quadrilaterals
        if len(approx) == 4:
            x, y, w, h = cv2.boundingRect(approx)
            ar = w / float(h)
            # Filter by typical book-cover aspect ratios
            # (you can loosen/tighten these ranges if needed)
            if 0.4 < ar < 0.9 or 1.0 < ar < 1.6:
                boxes.append((x, y, w, h))

    # Sort left→right, then top→bottom
    boxes = sorted(boxes, key=lambda b: (b[1], b[0]))
    return boxes

# ──────────────────────────────────────────────────────────────
# 2. OCR on a cropped region
# ──────────────────────────────────────────────────────────────
def ocr_on_region(image: np.ndarray, box: tuple):
    """
    Crop the image to the given box and run Tesseract OCR.
    Return the raw OCR text.
    """
    x, y, w, h = box
    cropped = image[y : y + h, x : x + w]
    gray_crop = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
    _, thresh_crop = cv2.threshold(
        gray_crop, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU
    )
    custom_config = r"--oem 3 --psm 6"
    text = pytesseract.image_to_string(thresh_crop, config=custom_config)
    return text.strip()

# ──────────────────────────────────────────────────────────────
# 3. OCR on the full image (fallback)
# ──────────────────────────────────────────────────────────────
def ocr_full_image(image: np.ndarray):
    """
    Run OCR on the entire image if no covers were detected.
    Return the full OCR text (string).
    """
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Optionally threshold entire image as well
    _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    custom_config = r"--oem 3 --psm 6"
    text = pytesseract.image_to_string(thresh, config=custom_config)
    return text.strip()

# ──────────────────────────────────────────────────────────────
# 4. Query OpenLibrary API
# ──────────────────────────────────────────────────────────────
def query_openlibrary(title_text: str, author_text: str = None):
    """
    Search OpenLibrary by title (and optional author).
    Return a dict with title, author_name, publisher, first_publish_year, or None.
    """
    base_url = "https://openlibrary.org/search.json"
    params = {"title": title_text}
    if author_text:
        params["author"] = author_text

    try:
        resp = requests.get(base_url, params=params, timeout=5)
        resp.raise_for_status()
        data = resp.json()
        if data.get("docs"):
            doc = data["docs"][0]
            return {
                "title": doc.get("title", ""),
                "author_name": ", ".join(doc.get("author_name", [])),
                "publisher": ", ".join(doc.get("publisher", [])),
                "first_publish_year": doc.get("first_publish_year", ""),
            }
    except Exception as e:
        print(f"OpenLibrary query failed: {e}")

    return None

# ──────────────────────────────────────────────────────────────
# 5. Process one uploaded image
# ──────────────────────────────────────────────────────────────
def process_image(image_file):
    """
    Gradio passes a PIL image or numpy array. Convert to OpenCV BGR,
    detect covers β†’ OCR β†’ OpenLibrary.
    If no covers are found, fall back to OCR on the full image once.
    Write CSV to a temp file and return (DataFrame, filepath).
    """
    # Convert PIL to OpenCV BGR
    img = np.array(image_file)[:, :, ::-1].copy()

    # 1) Try to detect individual covers
    boxes = detect_book_regions(img)
    records = []

    if boxes:
        # If we found boxes, run OCR + lookup for each
        for box in boxes:
            ocr_text = ocr_on_region(img, box)
            lines = [l.strip() for l in ocr_text.splitlines() if l.strip()]
            if not lines:
                continue

            title_guess = lines[0]
            author_guess = lines[1] if len(lines) > 1 else None
            meta = query_openlibrary(title_guess, author_guess)
            if meta:
                records.append(meta)
            else:
                # No OpenLibrary match β†’ still include OCR result
                records.append(
                    {
                        "title": title_guess,
                        "author_name": author_guess or "",
                        "publisher": "",
                        "first_publish_year": "",
                    }
                )
    else:
        # 2) FALLBACK: no boxes detected β†’ OCR on full image once
        full_text = ocr_full_image(img)
        lines = [l.strip() for l in full_text.splitlines() if l.strip()]
        if lines:
            # Use first line as title guess, second (if any) as author guess
            title_guess = lines[0]
            author_guess = lines[1] if len(lines) > 1 else None
            meta = query_openlibrary(title_guess, author_guess)
            if meta:
                records.append(meta)
            else:
                records.append(
                    {
                        "title": title_guess,
                        "author_name": author_guess or "",
                        "publisher": "",
                        "first_publish_year": "",
                    }
                )
        # If lines is empty, records remains empty

    # Build DataFrame (even if empty)
    df = pd.DataFrame(records, columns=["title", "author_name", "publisher", "first_publish_year"])
    csv_bytes = df.to_csv(index=False).encode()

    # Write CSV to a unique temporary file
    unique_name = f"books_{uuid.uuid4().hex}.csv"
    temp_path = os.path.join("/tmp", unique_name)
    with open(temp_path, "wb") as f:
        f.write(csv_bytes)

    return df, temp_path

# ──────────────────────────────────────────────────────────────
# 6. Build the Gradio Interface
# ──────────────────────────────────────────────────────────────
def build_interface():
    with gr.Blocks(title="Book Cover Scanner") as demo:
        gr.Markdown(
            """
            ## Book Cover Scanner + Metadata Lookup

            1. Upload a photo containing one or multiple book covers  
            2. The app will:
               - Detect individual covers (rectangles).  
               - If any are found, OCR each one and query OpenLibrary for metadata.  
               - If **no** rectangles are detected, OCR the **entire image** once.  
            3. Display all detected/guessed books in a table.  
            4. Download a CSV of the results.  

            **Tips:**  
            - For best cover detection: use a flat, well-lit photo with minimal glare/obstructions.  
            - You can also place each cover on a plain background (e.g., a white tabletop).  
            """
        )

        with gr.Row():
            img_in = gr.Image(type="pil", label="Upload Image of Book Covers")
            run_button = gr.Button("Scan & Lookup")

        output_table = gr.Dataframe(
            headers=["title", "author_name", "publisher", "first_publish_year"],
            label="Detected Books + Metadata",
            datatype="pandas",
        )
        download_file = gr.File(label="Download CSV")

        def on_run(image):
            df, filepath = process_image(image)
            return df, filepath

        run_button.click(
            fn=on_run,
            inputs=[img_in],
            outputs=[output_table, download_file],
        )

    return demo

if __name__ == "__main__":
    demo_app = build_interface()
    demo_app.launch()