File size: 2,529 Bytes
6af4017
 
 
 
 
 
 
 
 
 
 
2fc3151
6af4017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import pytesseract
import requests
import numpy as np
import os



# Set the Tesseract command path
pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'
st.write(f'Tesseract Path: {pytesseract.pytesseract.tesseract_cmd}')

# Detecting characters using PyTesseract
def detectChar(img):
    hImg, wImg, _ = img.shape
    boxes = pytesseract.image_to_boxes(img)

    # Create an image for displaying boxes
    img_with_boxes = img.copy()

    for b in boxes.splitlines():
        b = b.split(' ')
        x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
        char_text = b[0]

        # Draw boxes on the image
        cv2.rectangle(img_with_boxes, (x, hImg - y), (w, hImg - h), (0, 255, 0), 2)  # Green rectangle for individual character
        cv2.putText(img_with_boxes, char_text, (x, hImg - y + 25), cv2.FONT_HERSHEY_COMPLEX, 1, (50, 50, 255), 2)

    # Display the image with character boxes using Streamlit
    st.image(img_with_boxes, caption="Image with Character Boxes", use_column_width=True)

    # Extract text for spell-checking
    detected_text = ''.join([b.split()[0] for b in boxes.splitlines()])
    print(detected_text)

    # Perform spell-check using LanguageTool API
    suggestions = spell_check(detected_text)

    # Display spell-check suggestions
    if suggestions:
     #   st.write("Spell Check only")
        for suggestion in suggestions:
            st.write(suggestion)
         #   print(suggestion)
    else:
        st.write("No suggestions or error in spell-checking.")

# Function to perform spell-check using LanguageTool API
def spell_check(text):
    api_url = "https://api.languagetool.org/v2/check"
    data = {
        'text': text,
        'language': 'en-US',
    }

    response = requests.post(api_url, data=data)
    result = response.json()

    if 'matches' in result:
        suggestions = [match['message'] for match in result['matches']]
        return suggestions
    else:
        return []

# Streamlit UI
st.title("Pyresearch checker Detection  and Spell-checking")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    image_data = np.frombuffer(uploaded_file.read(), np.uint8)
    image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    st.image(image_rgb, caption="Uploaded Image.", use_column_width=True)

    if st.button("Detect Characters and Spell-check"):
        detectChar(image_rgb)