Spaces:
Build error
Build error
Commit ·
d812fea
1
Parent(s): cdd9cb4
Updates in readme, parameters
Browse filesFiles changed (3)
- README.md+1-1
- app.py+23-15
- backend/pytorch.py+16-16
122345---title: docTRtitle: Text Extractoremoji: 📑colorFrom: purplecolorTo: pink12123451 unmodified line789101011121336 unmodified lines5051525354555657585960616263645354555657585960616263646566671 unmodified line69707172737475767721 unmodified lines99100101102103104105106107108109import cv2import matplotlib.pyplot as plt# import cv2# import matplotlib.pyplot as pltimport numpy as npimport streamlit as stimport torch1 unmodified linefrom doctr.io import DocumentFilefrom doctr.utils.visualization import visualize_page
from backend.pytorch import DET_ARCHS, RECO_ARCHS, forward_image, load_predictorfrom backend.pytorch import DET_ARCHS, RECO_ARCHS, load_predictor #forward_image
forward_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
36 unmodified lines det_arch = st.sidebar.selectbox("Text detection model", det_archs) reco_arch = st.sidebar.selectbox("Text recognition model", reco_archs)
# For newline st.sidebar.write("\n") # Only straight pages or possible rotation st.sidebar.title("Parameters") assume_straight_pages = st.sidebar.checkbox("Assume straight pages", value=True) st.sidebar.write("\n") # Straighten pages straighten_pages = st.sidebar.checkbox("Straighten pages", value=False) st.sidebar.write("\n") # Binarization threshold bin_thresh = st.sidebar.slider("Binarization threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.1) st.sidebar.write("\n") # # For newline # st.sidebar.write("\n") # # Only straight pages or possible rotation # st.sidebar.title("Parameters") # assume_straight_pages = st.sidebar.checkbox("Assume straight pages", value=True) # st.sidebar.write("\n") # # Straighten pages # straighten_pages = st.sidebar.checkbox("Straighten pages", value=False) # st.sidebar.write("\n") # # Binarization threshold # bin_thresh = st.sidebar.slider("Binarization threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.1) # st.sidebar.write("\n")
if st.sidebar.button("Analyze page"): if uploaded_file is None:1 unmodified line
else: with st.spinner("Loading model..."): # Default Values assume_straight_pages, straighten_pages, bin_thresh = True, False, 0.3
predictor = load_predictor( det_arch, reco_arch, assume_straight_pages, straighten_pages, bin_thresh, forward_device )21 unmodified lines img = out.pages[0].synthesize() cols[2].image(img, clamp=True)
print('out',out) print('\n') print('page_export',page_export) print('\n')
# Display JSON st.markdown("\nHere are your analysis results in JSON format:") st.json(page_export, expanded=False)59 unmodified lines606162636463646566676869706667686970717273747576777879727374757677787980818159 unmodified lines return predictor
def forward_image(predictor: OCRPredictor, image: np.ndarray, device: torch.device) -> np.ndarray: """Forward an image through the predictor# def forward_image(predictor: OCRPredictor, image: np.ndarray, device: torch.device) -> np.ndarray:# """Forward an image through the predictor
Args: ---- predictor: instance of OCRPredictor image: image to process device: torch.device, the device to process the image on# Args:# ----# predictor: instance of OCRPredictor# image: image to process# device: torch.device, the device to process the image on
Returns: ------- segmentation map """ with torch.no_grad(): processed_batches = predictor.det_predictor.pre_processor([image]) out = predictor.det_predictor.model(processed_batches[0].to(device), return_model_output=True) seg_map = out["out_map"].to("cpu").numpy()# Returns:# -------# segmentation map# """# with torch.no_grad():# processed_batches = predictor.det_predictor.pre_processor([image])# out = predictor.det_predictor.model(processed_batches[0].to(device), return_model_output=True)# seg_map = out["out_map"].to("cpu").numpy()
return seg_map# return seg_mapREADME.md+1-1
app.py+23-15
backend/pytorch.py+16-16