Download app.py from Nymbo/OCR: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/Nymbo/OCR/resolve/main/app.py
- Command line
-
hf download hf://spaces/Nymbo/OCR/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Nymbo/OCR/resolve/main/app.py
1.11 kB
| import streamlit as st | |
| import easyocr | |
| from PIL import Image | |
| import numpy as np | |
| def get_reader(lang='en'): | |
| return easyocr.Reader([lang], gpu=False) | |
| def extract_text(image_path, lang='en'): | |
| reader = get_reader(lang) | |
| results = reader.readtext(np.array(image_path.convert('RGB'))) | |
| all_text = '' | |
| confidences = [] | |
| for (bbox, text, prob) in results: | |
| all_text += ' ' + text | |
| confidences.append(prob) | |
| final_confidence = sum(confidences) / len(confidences) if confidences else 0 | |
| return all_text, final_confidence | |
| st.title(' Text from Image') | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption='Uploaded Image', use_container_width=True) | |
| st.write("") | |
| st.write("Hold tight..") | |
| extracted_text, confidence = extract_text(image, 'en') | |
| st.write("Extracted Text") | |
| st.text_area("Result", extracted_text, height=150) | |
| st.write(f"Confidence: {confidence*100:.2f}%") | |