Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,94 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import tensorflow as tf
|
3 |
-
import keras_ocr
|
4 |
-
import requests
|
5 |
import cv2
|
6 |
-
import os
|
7 |
-
import csv
|
8 |
-
import numpy as np
|
9 |
-
import pandas as pd
|
10 |
-
import huggingface_hub
|
11 |
-
from huggingface_hub import Repository
|
12 |
-
from datetime import datetime
|
13 |
-
import scipy.ndimage.interpolation as inter
|
14 |
import easyocr
|
15 |
-
import
|
16 |
-
|
17 |
-
from
|
18 |
-
from
|
19 |
-
from
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
"""
|
22 |
-
|
23 |
"""
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
result = ocr.ocr(img)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
"""
|
36 |
-
|
37 |
"""
|
38 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
output_text += ' '+ text
|
46 |
-
return output_text
|
47 |
|
48 |
"""
|
49 |
-
|
50 |
"""
|
51 |
-
#
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
# Thresholding or Binarization
|
56 |
-
def thresholding(src):
|
57 |
-
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
|
58 |
-
def ocr_with_easy(img):
|
59 |
-
gray_scale_image=get_grayscale(img)
|
60 |
-
thresholding(gray_scale_image)
|
61 |
-
cv2.imwrite('image.png',gray_scale_image)
|
62 |
-
reader = easyocr.Reader(['th','en'])
|
63 |
-
bounds = reader.readtext('image.png',paragraph="False",detail = 0)
|
64 |
-
bounds = ''.join(bounds)
|
65 |
-
return bounds
|
66 |
-
|
67 |
"""
|
68 |
-
|
69 |
"""
|
70 |
-
def
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
if
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
if
|
78 |
-
text_output = ocr_with_easy(img)
|
79 |
-
if Method == 'KerasOCR':
|
80 |
-
text_output = ocr_with_keras(img)
|
81 |
-
if Method == 'PaddleOCR':
|
82 |
-
text_output = ocr_with_paddle(img)
|
83 |
-
|
84 |
-
try:
|
85 |
-
flag(Method,text_output,img)
|
86 |
-
except Exception as e:
|
87 |
-
print(e)
|
88 |
-
return text_output
|
89 |
else:
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
# print("Error in ocr generation ==>",e)
|
94 |
-
# text_output = "Something went wrong"
|
95 |
-
# return text_output
|
96 |
-
|
97 |
|
98 |
"""
|
99 |
-
Create
|
100 |
"""
|
101 |
-
|
102 |
-
# image = gr.Image(shape=(300, 300))
|
103 |
image = gr.Image()
|
104 |
-
|
105 |
-
|
106 |
|
107 |
demo = gr.Interface(
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
title="
|
112 |
-
|
113 |
-
|
114 |
-
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
115 |
-
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
116 |
-
|
117 |
-
|
118 |
)
|
119 |
-
|
120 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
|
|
|
|
|
|
2 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import easyocr
|
4 |
+
import pandas as pd
|
5 |
+
import nltk
|
6 |
+
from nltk.tokenize import word_tokenize
|
7 |
+
from nltk.corpus import stopwords
|
8 |
+
from nltk.stem import PorterStemmer
|
9 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
10 |
+
from sklearn.ensemble import RandomForestClassifier
|
11 |
+
|
12 |
+
# Download necessary NLTK data
|
13 |
+
nltk.data.path.append("/usr/local/lib/nltk_data")
|
14 |
+
nltk.download('punkt')
|
15 |
+
nltk.download('stopwords')
|
16 |
+
|
17 |
"""
|
18 |
+
EasyOCR for Text Extraction
|
19 |
"""
|
20 |
+
def ocr_with_easy(img):
|
21 |
+
# Convert image to grayscale
|
22 |
+
gray_scale_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
23 |
+
cv2.imwrite('image.png', gray_scale_image)
|
|
|
24 |
|
25 |
+
# Use EasyOCR to read text from the image
|
26 |
+
reader = easyocr.Reader(['en'])
|
27 |
+
bounds = reader.readtext('image.png', paragraph="False", detail=0)
|
28 |
+
extracted_text = ' '.join(bounds)
|
29 |
+
return extracted_text
|
30 |
|
31 |
"""
|
32 |
+
Text Preprocessing for Spam Classification
|
33 |
"""
|
34 |
+
def preprocess_text(text):
|
35 |
+
tokens = word_tokenize(text.lower())
|
36 |
+
stop_words = set(stopwords.words('english'))
|
37 |
+
filtered_tokens = [word for word in tokens if word.isalnum() and word not in stop_words]
|
38 |
+
stemmer = PorterStemmer()
|
39 |
+
stemmed_tokens = [stemmer.stem(word) for word in filtered_tokens]
|
40 |
+
return ' '.join(stemmed_tokens)
|
|
|
|
|
41 |
|
42 |
"""
|
43 |
+
Load and Train Spam Classifier
|
44 |
"""
|
45 |
+
# Load the dataset
|
46 |
+
data = pd.read_csv('spam.csv', encoding='latin-1')
|
47 |
+
data['v2'] = data['v2'].apply(preprocess_text)
|
48 |
+
|
49 |
+
# Feature Extraction (TF-IDF)
|
50 |
+
tfidf_vectorizer = TfidfVectorizer()
|
51 |
+
tfidf_matrix = tfidf_vectorizer.fit_transform(data['v2'])
|
52 |
+
|
53 |
+
# Label Encoding
|
54 |
+
data['v1'] = data['v1'].map({'ham': 0, 'spam': 1})
|
55 |
+
|
56 |
+
# Create a Random Forest classifier
|
57 |
+
rf_classifier = RandomForestClassifier(random_state=42)
|
58 |
+
rf_classifier.fit(tfidf_matrix, data['v1'])
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
"""
|
61 |
+
OCR and Spam Classification Pipeline
|
62 |
"""
|
63 |
+
def ocr_and_classify_spam(img):
|
64 |
+
# Step 1: Extract text from the image using EasyOCR
|
65 |
+
extracted_text = ocr_with_easy(img)
|
66 |
|
67 |
+
# Step 2: Preprocess and classify the extracted text
|
68 |
+
if extracted_text:
|
69 |
+
processed_text = preprocess_text(extracted_text)
|
70 |
+
input_tfidf = tfidf_vectorizer.transform([processed_text])
|
71 |
+
prediction = rf_classifier.predict(input_tfidf)
|
72 |
+
spam_result = "SPAM" if prediction[0] == 1 else "NOT SPAM"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
else:
|
74 |
+
spam_result = "No text found in the image."
|
75 |
+
|
76 |
+
return extracted_text, spam_result
|
|
|
|
|
|
|
|
|
77 |
|
78 |
"""
|
79 |
+
Create User Interface with Gradio
|
80 |
"""
|
|
|
|
|
81 |
image = gr.Image()
|
82 |
+
output_text = gr.Textbox(label="Extracted Text")
|
83 |
+
output_classification = gr.Textbox(label="Spam Classification")
|
84 |
|
85 |
demo = gr.Interface(
|
86 |
+
fn=ocr_and_classify_spam,
|
87 |
+
inputs=image,
|
88 |
+
outputs=[output_text, output_classification],
|
89 |
+
title="OCR and Spam Classifier",
|
90 |
+
description="Upload an image with text. The text will be extracted using EasyOCR and then classified as SPAM or NOT SPAM.",
|
91 |
+
css=".gradio-container {background-color: lightgray}"
|
|
|
|
|
|
|
|
|
92 |
)
|
93 |
+
|
94 |
+
demo.launch()
|