我正在编写代码,将图像中的字符绘制到与图像相同位置的可编辑文件中。我计划提取文本坐标并将它们绘制在 dxf 文件上。代码是:
import cv2
import pytesseract
import numpy as np
import ezdxf
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR'
# Load the image
img = cv2.imread(r'C:\Users\kshet\OneDrive\Desktop\python oce\example.jpg')
# Preprocess the image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# Perform OCR using Pytesseract
text = pytesseract.image_to_string(gray, lang='eng', config='--psm 6')
# Split the text into words
words = text.split()
# Extract the coordinates of each word
dxf_entities = []
for word in words:
result = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DICT, config='--psm 6')
n_boxes = len(result['level'])
for i in range(n_boxes):
if result['text'][i] == word:
(x, y, w, h) = (result['left'][i], result['top'][i], result['width'][i], result['height'][i])
x_center = x + w / 2
y_center = y + h / 2
dxf_entities.append({
'type': 'text',
'text': word,
'insert': (x_center, y_center),
'height': h / 2,
})
# Plot the words on an EZDXF file
doc = ezdxf.new('R2010')
msp = doc.modelspace()
for entity in dxf_entities:
if entity['type'] == 'text':
msp.add_text(entity['text'], dxfattribs={
'layer': 'TEXT',
'height': entity['height'],
'insert': entity['insert'],
})
doc.saveas('output.dxf')
我得到的错误如下:
试过以管理员身份运行它没有用
PermissionError: [WinError 5] Access is denied