在学习OCR识别时,我拿到一串代码,但是看不出来要怎么让它跑起来拿到结果(只是一个类,原本是通过接口访问的代码)
import re
import numpy as np
from PIL import Image
from utils import order_point, crop_image, calc_distance
class BusinessCerts:
def __init__(self, img='', dbnet_det='', ocr_recognition=''):
self.img = img
self.dbnet_det = dbnet_det
self.ocr_recognition = ocr_recognition
self.det_result = []
self.box_result = []
self.left_uppers = []
self.line_space = -1
self.line_height = -1
self.box_detect()
self.ocr_recognize()
self.left_uppers = np.array([[point['pts'][0], point['pts'][1]] for point in self.box_result])
self.left_lowers = np.array([[point['pts'][6], point['pts'][7]] for point in self.box_result])
self.results = {'名称': '', '统一社会信用代码': '', '住所': ''}
self.ocr_pipline()
# DBNet文字检测
def box_detect(self):
det_result = self.dbnet_det.predict(self.img)
det_result = det_result.reshape(-1, 8)
self.det_result = det_result[det_result[:, 1].argsort()]
return det_result[det_result[:, 1].argsort()]
# 识别文字
def ocr_recognize(self):
box_result = []
for i in range(self.det_result.shape[0]):
box_dict = {}
pts = order_point(self.det_result[i])
image_crop = crop_image(self.img, pts)
image_crop = Image.fromarray(image_crop)
result = self.ocr_recognition.rec(image_crop)
pts = pts.reshape(-1).astype(int)
box_dict['pts'] = pts
box_dict['text'] = result
box_result.append(box_dict)
self.box_result = box_result
# 根据标签索引,通过计算距离确定对应值
def get_item_value(self, key_idx):
curr_pts = self.box_result[key_idx]['pts']
curr_right_upper = np.array([curr_pts[2], curr_pts[3]])
dists = calc_distance(curr_right_upper, self.left_uppers)
dists[key_idx] = float('inf')
value_idx = np.argmin(dists)
return value_idx, self.box_result[value_idx]['text']
# 获取名称对应Box索引
def get_name_index(self):
index = -1
for i, temp in enumerate(self.box_result):
if '名称' == temp['text'] or '称' == temp['text']:
index = i
return index
def get_credit_code_index(self):
index = -1
for i, temp in enumerate(self.box_result):
if '统一社会信用代码' == temp['text'] or '信用代码' == temp['text'] or '代码' == temp['text']:
index = i
return index
def get_address_index(self):
index = -1
for i, temp in enumerate(self.box_result):
if '住所' == temp['text'] or '经营场所' == temp['text']:
index = i
return index
def ocr_pipline(self):
address_index = self.get_address_index()
if address_index != -1:
address_value_idx, address_result = self.get_item_value(address_index)
address_value_pts = self.box_result[address_value_idx]['pts']
self.line_height = address_value_pts[7] - address_value_pts[1]
address_value_left_lower = np.array([address_value_pts[6], address_value_pts[7]])
# 计算两行之间的距离,住所Box左下角点和其他Box左上角点的距离
line_space_dists = calc_distance(address_value_left_lower, self.left_uppers)
line_space_dists[address_value_idx] = float('inf')
# 计算行距
left_lowers = [[point['pts'][6], point['pts'][7]] for point in self.box_result]
left_upper = np.array([address_value_pts[0], address_value_pts[1]])
line_height_dists = calc_distance(left_upper, left_lowers)
line_height_dists[address_value_idx] = float('inf')
self.line_space = min(line_height_dists)
# 如果最小行距小于正常行距的 1/2 ,认为有换行
if min(line_space_dists) < (self.line_space / 2):
min_idx = np.argmin(line_space_dists)
next_line = self.box_result[min_idx]['text']
address_result += next_line
self.results['住所'] = address_result
name_index = self.get_name_index()
if name_index != -1:
name_value_idx, name_result = self.get_item_value(name_index)
name_value_pts = self.box_result[name_value_idx]['pts']
name_value_left_lower = np.array([name_value_pts[6], name_value_pts[7]])
# 计算两行之间的距离,名称Box左下角点和其他Box左上角点的距离
line_space_dists = calc_distance(name_value_left_lower, self.left_uppers)
line_space_dists[name_value_idx] = float('inf')
# 如果最小行距小于正常行距的 1/2 ,认为有换行
if min(line_space_dists) < (self.line_space / 2):
min_idx = np.argmin(line_space_dists)
next_line = self.box_result[min_idx]['text']
address_result += next_line
self.results['名称'] = name_result
credit_code_index = self.get_credit_code_index()
if credit_code_index != -1:
_, credit_code_result = self.get_item_value(credit_code_index)
re_result = re.findall(r'[0-9A-Z]{15,}', credit_code_result)
if re_result:
self.results['统一社会信用代码'] = re_result[0]
else:
for box in self.box_result:
re_result = re.findall(r'[0-9A-Z]{15,}', box['text'])
if re_result:
self.results['统一社会信用代码'] = re_result[0]
break
if __name__ == "__main__":
我的IF后要怎么写才能单独把这串代码运行起来?
我想到达成的目的就是把这个类给它实例化,然后运行拿到结果
new BusinessCerts()对象,并传递这三个参数
if __name__ == "__main__":
img=''
ocr_recognition = OCRRecognition()
dbnet_det = DbNetInfer()
certs = BusinessCerts(img,dbnet_det,ocr_recognition)
“Devil组”引证GPT后的撰写:
的公司名称、统一社会信用代码和住所信息。如果只有代码本身而不知道其接口的调用方法,可能需要一些额外的准备工作来让代码可以运行。以下是一些建议:
确保已经安装了必要的依赖库。代码中使用了Numpy、Pillow(PIL)等库,要确保这些库已经被正确安装在您的Python环境中。
确认代码中的依赖项是否被正确导入。在代码的开头,有这样一行代码:from utils import order_point, crop_image, calc_distance,这表示代码中还引用了一个名为utils的模块。要确认这个模块已经被正确导入或者存在于您的工作目录中。
了解代码的调用方式。通过查看代码,我们可以看到,这个类是需要传递三个参数的,分别是img、dbnet_det和ocr_recognition。这些参数的类型和具体含义可能需要根据代码的具体实现来确定,可以尝试在代码中寻找这些参数的定义和使用来了解它们的作用。
熟悉代码中的方法。在这个类中,有一些方法是被调用的,例如box_detect、ocr_recognize等等。要查看这些方法的实现,并了解它们在整个识别流程中的作用,以便能够正确地调用它们。
确认代码的输入和输出。最后,要确保代码的输入和输出符合您的预期。这个类最终会提取出公司名称、统一社会信用代码和住所信息,并存储在self.results中。要查看这些结果的数据类型和具体内容,以便能够正确地使用这些信息。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
这段代码是OCR文字识别的一部分,包括了文字检测(DBNet)和文字识别(OCR)两个步骤。这个类的实例化需要提供一张图片,以及已经训练好的DBNet和OCR模型。
实例化之后,先进行文字检测,将图片中的文字框出来,然后针对每个文字框进行文字识别,最后返回识别出来的结果。
具体来说,这个类的主要方法包括:
需要注意的是,这段代码中用到了一些自定义的函数和类,比如order_point、crop_image和calc_distance等,这些函数和类应该在其他文件中定义。因此,如果你想让这段代码跑起来,你需要找到这些函数和类的定义并将其导入到当前文件中。