Commit 4e1d0816 authored by kalpani omalka's avatar kalpani omalka

crop fields

parent cac962d9
Pipeline #3280 canceled with stages
from cv2 import cv2
import random
scale = 0.5
circles = []
counter = 0
counter2 = 0
point1 = []
point2 = []
myPoints = []
myColor = []
def mousePoints(event,x,y,flags,params):
global counter,point1,point2,counter2,circles,myColor
if event == cv2.EVENT_LBUTTONDOWN:
if counter == 0:
point1=int(x//scale),int(y//scale)
counter +=1
myColor = (random.randint(0,2)*200,random.randint(0,2)*200,random.randint(0,2)*200)
elif counter ==1:
point2=int(x//scale),int(y//scale)
types = input('Enter Type ')
name = input('Enter Name ')
myPoints.append([point1,point2,types,name])
counter=0
circles.append([x,y,myColor])
counter2 += 1
img = cv2.imread('template.jpg')
h,w,c = img.shape
img = cv2.resize(img, (w//3,h//3), None, scale, scale)
while True:
for x,y,color in circles:
cv2.circle(img,(x,y),3,color,cv2.FILLED)
cv2.imshow("Original Image ", img)
cv2.setMouseCallback("Original Image ", mousePoints)
if cv2.waitKey(1) & 0xFF == ord('s'):
print(myPoints)
break
\ No newline at end of file
import numpy as np
from cv2 import cv2
import pytesseract
import os
# Declaring Paths
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
current_path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
test_images_path = current_path+"\\Test_images"
per = 25
scale = 0.5
roi = [[(204, 196), (3304, 364), 'text', 'Validation'], #(x,y) (width+x,height+y)
[(154, 668), (2544, 765), 'text', 'Name'],
[(2654, 650), (3400, 758), 'text', 'RegNo'],
[(754, 960), (2380, 1065), 'text', 'Course'],
[(2412, 1370), (3350, 1450), 'text', 'Amount']]
imageT = cv2.imread("template.jpg")
h,w,c = imageT.shape
#imageT = cv2.resize(imageT, (w//6,h//6))
# text = pytesseract.image_to_string(image)
orb = cv2.ORB_create(1000)
# unique key points/elements to image, decsripters are the representation of these key points that will be easier for
#the computer to understand
kp1, des1 = orb.detectAndCompute(imageT, None)
Image_list = os.listdir(test_images_path)
print(Image_list)
for j,y in enumerate(Image_list):
img = cv2.imread(test_images_path + "\\"+y)
h,w,c = img.shape
kp2, des2 = orb.detectAndCompute(img, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
matches = bf.match(des2,des1)
matches.sort(key= lambda x: x.distance)
good = matches[:int(len(matches)*(per/100))]
imgMatch = cv2.drawMatches(img,kp2,imageT,kp1,good[:100],None,flags=2)
srcPoints = np.float32([kp2[m.queryIdx].pt for m in good]).reshape(-1,1,2)
dstPoints = np.float32([kp1[m.trainIdx].pt for m in good]).reshape(-1,1,2)
M, _ = cv2.findHomography(srcPoints,dstPoints,cv2.RANSAC,5.0)
imgScan = cv2.warpPerspective(img,M,(w,h))
imgShow = imgScan.copy()
imgMask = np.zeros_like(imgShow)
Data_list = []
for x,r in enumerate(roi):
cv2.rectangle(imgMask, ((r[0][0]), r[0][1]), ((r[1][0]), r[1][1]), (0,255,0), cv2.FILLED)
imgShow = cv2.addWeighted(imgShow, 0.99, imgMask, 0.1,0)
imgCrop = imgScan[r[0][1]:r[1][1], r[0][0]:r[1][0]]
cv2.imshow(str(x), imgCrop)
if r[2] == 'text':
#print(f'{r[3]} : {pytesseract.image_to_string(imgCrop)}')
Data_list.append(pytesseract.image_to_string(imgCrop))
print(Data_list)
with open('outputFields.csv', 'a+') as f:
for data in Data_list:
print(data)
f.write((str(data)+','))
f.write('\n')
img = cv2.resize(img, (w//4,h//4), None, scale, scale)
imgShow = cv2.resize(imgShow, (w//4,h//4), None, scale, scale)
cv2.imshow(y+"2", imgShow)
#imgKp1 = cv2.drawKeypoints(image, kp1, None)
#cv2.imshow("KeyPoints", imgKp1)
#cv2.imshow("Output", image)
cv2.waitKey(0)
\ No newline at end of file
Validations,Name,RegNo,Course,Amount
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment