Spaces:
Sleeping
Sleeping
feat: camera on-demand, fix all text visibility, show natural image before OCR
Browse files
app.py
CHANGED
|
@@ -81,8 +81,13 @@ html, body, [data-testid="stAppViewContainer"], .stApp {
|
|
| 81 |
|
| 82 |
.stApp > header { background: #f8fafc !important; }
|
| 83 |
|
| 84 |
-
/* Force light text color on
|
| 85 |
-
p, span, label,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
color: #1e293b !important;
|
| 87 |
}
|
| 88 |
|
|
@@ -93,10 +98,49 @@ section[data-testid="stSidebar"], [data-testid="stSidebarContent"], [data-testid
|
|
| 93 |
border-right: 1px solid #e2e8f0 !important;
|
| 94 |
box-shadow: 2px 0 16px rgba(15, 23, 42, 0.03) !important;
|
| 95 |
}
|
| 96 |
-
section[data-testid="stSidebar"] *
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
color: #334155 !important;
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
/* Cards */
|
| 101 |
.doc-card {
|
| 102 |
background: #ffffff !important;
|
|
@@ -465,16 +509,57 @@ with input_tab2:
|
|
| 465 |
|
| 466 |
with input_tab3:
|
| 467 |
st.markdown("""
|
| 468 |
-
<div style="
|
| 469 |
-
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
| 471 |
</div>
|
| 472 |
""", unsafe_allow_html=True)
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
|
| 480 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -506,10 +591,26 @@ if pil_img is None:
|
|
| 506 |
|
| 507 |
|
| 508 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 509 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 511 |
-
with st.spinner("π¨
|
| 512 |
-
# If camera capture, default to camera_enhance mode for optimal results
|
| 513 |
# Use user-selected mode; camera_enhance only activates if user explicitly picks it
|
| 514 |
effective_mode = preprocess_mode
|
| 515 |
processed_img, preprocess_meta = full_preprocess(pil_img, mode=effective_mode)
|
|
|
|
| 81 |
|
| 82 |
.stApp > header { background: #f8fafc !important; }
|
| 83 |
|
| 84 |
+
/* Force light text color on ALL elements */
|
| 85 |
+
*, p, span, label, h1, h2, h3, h4, h5, h6, div {
|
| 86 |
+
color: #1e293b;
|
| 87 |
+
}
|
| 88 |
+
div.stMarkdown, [data-testid="stWidgetLabel"] label,
|
| 89 |
+
[data-testid="stWidgetLabel"] p,
|
| 90 |
+
[data-testid="stMarkdownContainer"] p {
|
| 91 |
color: #1e293b !important;
|
| 92 |
}
|
| 93 |
|
|
|
|
| 98 |
border-right: 1px solid #e2e8f0 !important;
|
| 99 |
box-shadow: 2px 0 16px rgba(15, 23, 42, 0.03) !important;
|
| 100 |
}
|
| 101 |
+
section[data-testid="stSidebar"] *,
|
| 102 |
+
section[data-testid="stSidebar"] p,
|
| 103 |
+
section[data-testid="stSidebar"] span,
|
| 104 |
+
section[data-testid="stSidebar"] label,
|
| 105 |
+
section[data-testid="stSidebar"] div {
|
| 106 |
color: #334155 !important;
|
| 107 |
}
|
| 108 |
|
| 109 |
+
/* Toggle / Checkbox fix */
|
| 110 |
+
[data-testid="stToggle"] label, [data-testid="stToggle"] p,
|
| 111 |
+
[data-testid="stCheckbox"] label, [data-testid="stCheckbox"] p {
|
| 112 |
+
color: #334155 !important;
|
| 113 |
+
font-weight: 500 !important;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
/* Multiselect selected items text */
|
| 117 |
+
[data-baseweb="tag"] span { color: #4338ca !important; }
|
| 118 |
+
|
| 119 |
+
/* Slider value label */
|
| 120 |
+
[data-testid="stSlider"] [data-testid="stTickBarMin"],
|
| 121 |
+
[data-testid="stSlider"] [data-testid="stTickBarMax"],
|
| 122 |
+
[data-testid="stSlider"] p { color: #334155 !important; }
|
| 123 |
+
|
| 124 |
+
/* Tab labels β always visible */
|
| 125 |
+
.stTabs [role="tab"] span, .stTabs [role="tab"] p {
|
| 126 |
+
color: #64748b !important;
|
| 127 |
+
}
|
| 128 |
+
.stTabs [aria-selected="true"] span,
|
| 129 |
+
.stTabs [aria-selected="true"] p {
|
| 130 |
+
color: #ffffff !important;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
/* Expander header */
|
| 134 |
+
[data-testid="stExpander"] summary p {
|
| 135 |
+
color: #1e293b !important;
|
| 136 |
+
font-weight: 600 !important;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
/* Success / warning / error message text */
|
| 140 |
+
[data-testid="stAlert"] p, [data-testid="stAlert"] div {
|
| 141 |
+
color: #1e293b !important;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
/* Cards */
|
| 145 |
.doc-card {
|
| 146 |
background: #ffffff !important;
|
|
|
|
| 509 |
|
| 510 |
with input_tab3:
|
| 511 |
st.markdown("""
|
| 512 |
+
<div style="background:#eff6ff; border:1px solid #bfdbfe; border-radius:12px; padding:1rem 1.2rem; margin-bottom:1rem;">
|
| 513 |
+
<div style="font-weight:700; color:#1d4ed8; font-size:0.95rem; margin-bottom:4px;">πΈ Live Camera Scanner</div>
|
| 514 |
+
<div style="color:#1e40af; font-size:0.84rem; line-height:1.5;">
|
| 515 |
+
Click <strong>Enable Camera</strong> to turn on your webcam, take a photo of any document,
|
| 516 |
+
and DocIQ will extract all text from it. The image is shown in its natural colors β OCR runs on the original.
|
| 517 |
+
</div>
|
| 518 |
</div>
|
| 519 |
""", unsafe_allow_html=True)
|
| 520 |
+
|
| 521 |
+
# Camera is OFF by default β only enable when user explicitly clicks the button
|
| 522 |
+
if "camera_enabled" not in st.session_state:
|
| 523 |
+
st.session_state["camera_enabled"] = False
|
| 524 |
+
|
| 525 |
+
col_cam_btn, col_cam_info = st.columns([1, 3])
|
| 526 |
+
with col_cam_btn:
|
| 527 |
+
if not st.session_state["camera_enabled"]:
|
| 528 |
+
if st.button("π· Enable Camera", key="btn_enable_camera", use_container_width=True):
|
| 529 |
+
st.session_state["camera_enabled"] = True
|
| 530 |
+
st.rerun()
|
| 531 |
+
else:
|
| 532 |
+
if st.button("π΄ Disable Camera", key="btn_disable_camera", use_container_width=True):
|
| 533 |
+
st.session_state["camera_enabled"] = False
|
| 534 |
+
st.session_state.pop("camera_capture", None)
|
| 535 |
+
st.rerun()
|
| 536 |
+
with col_cam_info:
|
| 537 |
+
if st.session_state["camera_enabled"]:
|
| 538 |
+
st.markdown("""
|
| 539 |
+
<div style="color:#15803d; font-size:0.84rem; padding:0.4rem 0; font-weight:600;">
|
| 540 |
+
β
Camera is ON β point at your document and click the capture button below
|
| 541 |
+
</div>""", unsafe_allow_html=True)
|
| 542 |
+
else:
|
| 543 |
+
st.markdown("""
|
| 544 |
+
<div style="color:#64748b; font-size:0.84rem; padding:0.4rem 0;">
|
| 545 |
+
Camera is off. Click Enable Camera to start your webcam.
|
| 546 |
+
</div>""", unsafe_allow_html=True)
|
| 547 |
+
|
| 548 |
+
if st.session_state["camera_enabled"]:
|
| 549 |
+
cam_file = st.camera_input(
|
| 550 |
+
"π· Point at your document and capture",
|
| 551 |
+
key="camera_scanner_input",
|
| 552 |
+
help="Position the document clearly, ensure good lighting, then click the capture button."
|
| 553 |
+
)
|
| 554 |
+
if cam_file is not None:
|
| 555 |
+
pil_img = Image.open(cam_file).convert("RGB")
|
| 556 |
+
doc_name = "camera_capture.png"
|
| 557 |
+
is_camera_input = True
|
| 558 |
+
# Show preview of the natural captured image
|
| 559 |
+
st.markdown("""
|
| 560 |
+
<div style="color:#15803d; font-weight:600; font-size:0.9rem; margin:0.5rem 0 0.3rem;">β
Image captured! OCR will run on the natural original image below:</div>
|
| 561 |
+
""", unsafe_allow_html=True)
|
| 562 |
+
st.image(pil_img, caption="πΈ Captured Document (Natural Colors β OCR input)", use_container_width=True)
|
| 563 |
|
| 564 |
|
| 565 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 591 |
|
| 592 |
|
| 593 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 594 |
+
# Show Natural Image Preview (always original colors)
|
| 595 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 596 |
+
if not is_camera_input: # Camera tab already shows its own preview
|
| 597 |
+
st.markdown("""
|
| 598 |
+
<div style="background:#f0fdf4; border:1px solid #bbf7d0; border-radius:12px;
|
| 599 |
+
padding:0.8rem 1.2rem; margin-bottom:0.8rem; display:flex; align-items:center; gap:10px;">
|
| 600 |
+
<span style="font-size:1.2rem;">πΌοΈ</span>
|
| 601 |
+
<div>
|
| 602 |
+
<div style="font-weight:700; color:#15803d; font-size:0.9rem;">Image loaded β Natural original preview</div>
|
| 603 |
+
<div style="color:#166534; font-size:0.8rem;">OCR is applied to the original image. Enhancement only runs internally to improve text detection accuracy.</div>
|
| 604 |
+
</div>
|
| 605 |
+
</div>
|
| 606 |
+
""", unsafe_allow_html=True)
|
| 607 |
+
st.image(pil_img, caption=f"π {doc_name} β Original (Natural Colors)", use_container_width=True)
|
| 608 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 609 |
+
|
| 610 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 611 |
+
# Preprocessing Pipeline (for OCR accuracy only)
|
| 612 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 613 |
+
with st.spinner("π¨ Enhancing image for OCR inference..."):
|
|
|
|
| 614 |
# Use user-selected mode; camera_enhance only activates if user explicitly picks it
|
| 615 |
effective_mode = preprocess_mode
|
| 616 |
processed_img, preprocess_meta = full_preprocess(pil_img, mode=effective_mode)
|