【发布时间】: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 你能帮我完成我的项目吗?上面的代码来自那个项目