here is my code:
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)
it gives me the following error: Traceback (most recent call last): File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 2979, in open fp.seek(0) AttributeError: 'numpy.ndarray' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "D:\python beginner projects\yolov3\object_detection.py", line 37, in text = pytesseract.image_to_string(Image.open(img), lang="hin") File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 2981, in open fp = io.BytesIO(fp.read()) AttributeError: 'numpy.ndarray' object has no attribute 'read'. Did you mean: 'real'?
Image.open(img) is trying to use an image and not a path to that image. In other words, the input is a matrix instead of a path. Moreover, I saw that several applications of image_to_string use OpenCV, so I suggest changing it.
Try this:
text = pytesseract.image_to_string(img, lang="hin")
PD. Notice that you are extracting the text only from an image.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.