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
12345---title: docTRemoji: 📑colorFrom: purplecolorTo: pink12345---title: Text Extractoremoji: 📑colorFrom: purplecolorTo: pink
123451 unmodified line7891011121336 unmodified lines5051525354555657585960616263646566671 unmodified line69707172737421 unmodified lines96979899100101import cv2import matplotlib.pyplot as pltimport numpy as npimport streamlit as stimport torch1 unmodified linefrom doctr.io import DocumentFilefrom doctr.utils.visualization import visualize_pagefrom backend.pytorch import DET_ARCHS, RECO_ARCHS, forward_image, load_predictorforward_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")36 unmodified linesdet_arch = st.sidebar.selectbox("Text detection model", det_archs)reco_arch = st.sidebar.selectbox("Text recognition model", reco_archs)# For newlinest.sidebar.write("\n")# Only straight pages or possible rotationst.sidebar.title("Parameters")assume_straight_pages = st.sidebar.checkbox("Assume straight pages", value=True)st.sidebar.write("\n")# Straighten pagesstraighten_pages = st.sidebar.checkbox("Straighten pages", value=False)st.sidebar.write("\n")# Binarization thresholdbin_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 lineelse:with st.spinner("Loading model..."):predictor = load_predictor(det_arch, reco_arch, assume_straight_pages, straighten_pages, bin_thresh, forward_device)21 unmodified linesimg = out.pages[0].synthesize()cols[2].image(img, clamp=True)# Display JSONst.markdown("\nHere are your analysis results in JSON format:")st.json(page_export, expanded=False)123451 unmodified line7891011121336 unmodified lines5051525354555657585960616263646566671 unmodified line69707172737475767721 unmodified lines99100101102103104105106107108109# 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_pagefrom backend.pytorch import DET_ARCHS, RECO_ARCHS, load_predictor #forward_imageforward_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")36 unmodified linesdet_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")if st.sidebar.button("Analyze page"):if uploaded_file is None:1 unmodified lineelse:with st.spinner("Loading model..."):# Default Valuesassume_straight_pages, straighten_pages, bin_thresh = True, False, 0.3predictor = load_predictor(det_arch, reco_arch, assume_straight_pages, straighten_pages, bin_thresh, forward_device)21 unmodified linesimg = out.pages[0].synthesize()cols[2].image(img, clamp=True)print('out',out)print('\n')print('page_export',page_export)print('\n')# Display JSONst.markdown("\nHere are your analysis results in JSON format:")st.json(page_export, expanded=False)
59 unmodified lines6061626364656667686970717273747576777879808159 unmodified linesreturn predictordef forward_image(predictor: OCRPredictor, image: np.ndarray, device: torch.device) -> np.ndarray:"""Forward an image through the predictorArgs:----predictor: instance of OCRPredictorimage: image to processdevice: torch.device, the device to process the image onReturns:-------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_map59 unmodified lines6061626364656667686970717273747576777879808159 unmodified linesreturn 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# 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
README.md+1-1
app.py+23-15
backend/pytorch.py+16-16