Ankitajadhav commited on
Commit
1888df6
1 Parent(s): 66b31a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import cv2
3
+ import numpy as np
4
+ import pytesseract
5
+ from pytesseract import Output
6
+ import gradio as gr
7
+
8
+ #image preprocessing function
9
+
10
+ #grayscale image
11
+ def get_grayscale(image):
12
+ return cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
13
+
14
+ # noise removal
15
+ def remove_noise(image):
16
+ return cv2.medianBlur(image,5)
17
+
18
+ #thresholding
19
+ def thresholding(image):
20
+ return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
21
+
22
+ #dilation
23
+ def dilate(image):
24
+ kernel = np.ones((5,5),np.uint8)
25
+ return cv2.dilate(image, kernel, iterations = 1)
26
+
27
+ #erosion
28
+ def erode(image):
29
+ kernel = np.ones((5,5),np.uint8)
30
+ return cv2.erode(image, kernel, iterations = 1)
31
+
32
+ #opening - erosion followed by dilation
33
+ def opening(image):
34
+ kernel = np.ones((5,5),np.uint8)
35
+ return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
36
+
37
+ #canny edge detection
38
+ def canny(image):
39
+ return cv2.Canny(image, 100, 200)
40
+
41
+ #skew correction
42
+ def deskew(image):
43
+ coords = np.column_stack(np.where(image > 0))
44
+ angle = cv2.minAreaRect(coords)[-1]
45
+ if angle < -45:
46
+ angle = -(90 + angle)
47
+ else:
48
+ angle = -angle
49
+ (h, w) = image.shape[:2]
50
+ center = (w // 2, h // 2)
51
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
52
+ rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
53
+ return rotated
54
+
55
+ #template matching
56
+ def match_template(image, template):
57
+ return cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
58
+
59
+
60
+ def extract_text_from_image(img):
61
+
62
+ # Convert To PIL Image
63
+ im_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
64
+ gray = get_grayscale(im_rgb)
65
+ noise_remove = remove_noise(gray)
66
+ # thresh = thresholding(noise_remove)
67
+ text = pytesseract.image_to_string(noise_remove)
68
+ return text
69
+
70
+ app = gr.Interface(
71
+ fn=extract_text_from_image,
72
+ inputs=gr.Image(label="upload image"),
73
+ outputs= gr.Textbox()
74
+ )
75
+ app.launch()