【问题标题】:why i am getting an attribute error for numpy.nd array?为什么我收到 numpy.nd 数组的属性错误?
【发布时间】:2022-01-05 07:45:55
【问题描述】:

这是我的代码:

import cv2
import numpy as np
import glob
import random
from PIL import Image
import   pytesseract

# Load Yolo
net = cv2.dnn.readNet("yolov3_training_last.weights", "yolov3_testing.cfg")

# Name custom object
classes = [""]

# Images path
images_path = glob.glob(r"D:\python beginner projects\yolov3\dataset\4.jpg")

#tesseract path
path_to_tesseract = r"C:\Users\Owner\Tesseract-OCR\tesseract.exe"

layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

# Insert here the path of your images
random.shuffle(images_path)
pytesseract.tesseract_cmd = path_to_tesseract

# loop through all the images
for img_path in images_path:
    # Loading image
    img = cv2.imread(img_path)
    img = cv2.resize(img, None, fx=0.9, fy=0.9)
    height, width, channels = img.shape

    # Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
text = pytesseract.image_to_string(Image.open(img), lang="hin") 
net.setInput(blob)
outs = net.forward(output_layers)
text = pytesseract.image_to_string(img)
# Showing information on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
    for detection in out:
        scores = detection[5:]
        class_id = np.argmax(scores)
        confidence = scores[class_id]

        if confidence > 0.3:
            # Object detected
            print(class_id)
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)
            print(text)                

            # Rectangle coordinates
            x = int(center_x - w / 2)
            y = int(center_y - h / 2)

            boxes.append([x, y, w, h])
            confidences.append(float(confidence))
            class_ids.append(class_id)

indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.4)
print(indexes)
font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        color = colors[class_ids[i]]
        cv2.rectangle(img, (x, y), (x + w + 29, y + h + 10), color, 2)
        cv2.putText(img, label, (x + 10, y + 10), font, 3, color, 2)


cv2.imshow("Image", img)

key = cv2.waitKey(0)

它给了我以下错误: 回溯(最近一次通话最后): 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py”,第 2979 行,打开 fp.seek(0) AttributeError: 'numpy.ndarray' 对象没有属性 'seek'

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“D:\python 初学者项目\yolov3\object_detection.py”,第 37 行,在 text = pytesseract.image_to_string(Image.open(img), lang="hin") 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py”,第 2981 行,打开 fp = io.BytesIO(fp.read()) AttributeError:“numpy.ndarray”对象没有“读取”属性。你的意思是:“真实的”?

【问题讨论】:

  • text = pytesseract.image_to_string(Image.open(img), lang="hin") 这一行,img不应该是字符串还是路径?现在是 cv2.resize() 最后返回的任何内容
  • @Dennis 你能帮我完成我的项目吗?上面的代码来自那个项目

标签: python python-tesseract


【解决方案1】:

Image.open(img) 正在尝试使用图像而不是该图像的路径。换句话说,输入是矩阵而不是路径。而且我看到image_to_string的几个应用都使用了OpenCV,所以建议改一下。

试试这个:

text = pytesseract.image_to_string(img, lang="hin") 

PD。请注意,您只是从图像中提取文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多