gpt4 book ai didi

python - 裁剪检测到的区域 python

转载 作者:行者123 更新时间:2023-11-30 09:30:27 25 4
gpt4 key购买 nike

import cv2
import pytesseract
import numpy as np
from pytesseract import Output
import re
from pytesseract import image_to_string
import matplotlib.pyplot as plt

kernel = np.ones((5,5),np.uint8)

# noise removal
#this works on date
image = cv2.imread('1.Chase Bank_test.jpg')
result = image.copy()

gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)


thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (255,255,255), 5)

# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (255,255,255), 5)


date_list= []

d = pytesseract.image_to_data(thresh, output_type=Output.DICT)
#d = pytesseract.image_to_data(thresh)


keys = list(d.keys())

#date_pattern = '([\d]+\/[\d]+)'

#Regular expression to get date
date_pattern = '^(0[1-9]|[12]|[1-9]|3[02])/'
#amount= '((\d+)(\,)(\d+)(\.))+\d+'

description = ''

n_boxes = len(d['text']) # make the boxes around till the lenght text, put n_boxes in for loop
for i in range(n_boxes):
if int(d['conf'][i]) > 10:
if re.match(date_pattern, d['text'][i]):
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
detect_img = cv2.rectangle(result, (x, y), (x + w, y + h), (0, 300, 0), 2) #dates works good with opening




#logic for crop the detected date and append to date_list
#for i in range(n_boxes):
# crop_detect_img = detect_img[y:y+h+8, x:x+w+8]
# test_list=(image_to_string((crop_detect_img)))
# print(test_list)



#date_list.append(crop_detect_img)
#print(test_list)

crop_detect_img = detect_img[y:y+h+10, x:x+w+10]
crop_date_gray = cv2.cvtColor(crop_detect_img,cv2.COLOR_BGR2GRAY)


crop_date_thresh = cv2.threshold(crop_date_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]




date=(image_to_string((crop_date_thresh)))

print(date)



plt.figure(figsize = (20,20))
plt.imshow(crop_date_thresh)

#plt.imshow()
plt.imshow(result)
#print(result)


contours = contours[1] if imutils.is_cv3() else contours[0]



ROI_number = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
cv2.rectangle(result,(x,y),(x+w,y+h),(36,255,12),2)
ROI_number += 1

plt.imshow('thresh', thresh)

**

I can not figure out how crop to the detected dates from image in a loop so I could pass the cropped images to tesseract OCR and append to the date list. And please suggest me better OCR in python which is free. SO i could generate better results. I am using OpenCV for my problem with regix.

**正如您所看到的,它从图像中查找所有日期并制作边界框

enter image description here

最佳答案

您可以绘制边界框,但不知道如何裁剪它?这很奇怪。

无论如何,我在下面添加了相关代码。如果这不是正确的循环,至少您现在知道如何在 numpy 中裁剪图像:

n_boxes = len(d['text']) # make the boxes around till the lenght text, put n_boxes in for loop
for i in range(n_boxes):
if int(d['conf'][i]) > 10:
if re.match(date_pattern, d['text'][i]):
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])

# crop ROI and dump to a file
cropped = gray[y : y+h, x : x+w]
imwrite('crop_' + str(i) + '.png', cropped)

detect_img = cv2.rectangle(result, (x, y), (x + w, y + h), (0, 300, 0), 2)

关于python - 裁剪检测到的区域 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60042219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com