manik-hossain commited on
Commit
e4b6ad9
·
1 Parent(s): 8f92826

page 1 name

Browse files
Files changed (4) hide show
  1. app.py +86 -0
  2. packages.txt +2 -0
  3. r.png +0 -0
  4. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import cv2 as cv
3
+ import numpy as np
4
+ import streamlit as st
5
+ from PIL import Image
6
+ import pytesseract
7
+
8
+
9
+ act = [(150,240), (610,260)]
10
+
11
+
12
+ def align_images(ref_gray, input_gray):
13
+ """
14
+ Aligns the input image to the reference image using homography.
15
+
16
+ Parameters:
17
+ reference_image (numpy.ndarray): The reference image.
18
+ input_image (numpy.ndarray): The input image to be aligned.
19
+
20
+ Returns:
21
+ numpy.ndarray: The aligned version of the input image.
22
+ """
23
+ # # Convert images to grayscale
24
+ # ref_gray = cv2.cvtColor(reference_image, cv2.COLOR_BGR2GRAY)
25
+ # input_gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
26
+
27
+
28
+ # Detect ORB keypoints and descriptors
29
+ orb = cv2.ORB_create(nfeatures=500)
30
+ keypoints1, descriptors1 = orb.detectAndCompute(ref_gray, None)
31
+ keypoints2, descriptors2 = orb.detectAndCompute(input_gray, None)
32
+
33
+ # Match descriptors using BFMatcher
34
+ bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
35
+ matches = bf.match(descriptors1, descriptors2)
36
+ matches = sorted(matches, key=lambda x: x.distance)
37
+
38
+ # Extract location of good matches
39
+ ref_points = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
40
+ input_points = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
41
+
42
+ # Compute homography matrix
43
+ H, mask = cv2.findHomography(input_points, ref_points, cv2.RANSAC, 5.0)
44
+
45
+ # Warp input image to align with reference image
46
+ height, width = ref_gray.shape
47
+ aligned_image = cv2.warpPerspective(input_gray, H, (width, height))
48
+
49
+ return aligned_image
50
+
51
+ def ocr_with_crop(aligned_image):
52
+ # Open the image
53
+ # img = Image.open(image_path)
54
+ # img = cv2.imread(image_path,0)
55
+ # img = enhance(img)
56
+ # st.image(img)
57
+ # st.write(type(img))
58
+ # enh = enhance(np.array(img))
59
+ # st.image(enh)
60
+ # Define the coordinates for cropping
61
+ crop_coordinates = act
62
+
63
+ # Convert to rectangular bounds (x1, y1, x2, y2)
64
+ x1, y1 = crop_coordinates[0]
65
+ x2, y2 = crop_coordinates[1]
66
+ # Crop the image using the defined coordinates
67
+ # cropped_img = img.crop((x1, y1, x2, y2))
68
+ cropped_img = aligned_image[y1:y2,x1:x2]
69
+ st.image(cropped_img)
70
+ # Perform OCR on the cropped image
71
+ text = pytesseract.image_to_string(cropped_img)
72
+
73
+ # Print the extracted text
74
+ st.write(text)
75
+
76
+ if __name__== "__main__":
77
+ ref = cv.imread("r.png",0)
78
+ if inp:= st.file_uploader("upload your form in image format", type=['png', 'jpg']):
79
+ image = Image.open(inp)
80
+ gray_image_pil = image.convert('L')
81
+ image_array = np.array(gray_image_pil)
82
+ st.image(image_array)
83
+ align_image = align_images(ref,image_array)
84
+ ocr_with_crop(align_image)
85
+
86
+
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ tesseract-ocr
2
+ pytesseract
r.png ADDED
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ pillow
3
+ matplotlib
4
+ numpy
5
+ opencv-python
6
+ pymupdf
7
+ pillow
8
+ PyPDF2
9
+ pdf2image
10
+ pytesseract