视觉/图像重磅干货,第一时间送达!
pip install torch torchvision torchaudio easyocr
import easyocr
reader = easyocr.Reader(['en'])
reader = easyocr.Reader(['en'],gpu=False)
reader = easyocr.Reader(['en'],model_storage_directory='path/to/directory'
)
[([[28, 22], [353, 22], [353, 72], [28, 72]], 'SERIAL NUMBER', 0.8874381662616708), ([[35, 75], [397, 75], [397, 137], [35, 137]], 'AOC1715821', 0.8521895819573561), ([[39, 255], [315, 255], [315, 299], [39, 299]], 'PART NUMBER', 0.9971079202290591), ([[42, 298], [370, 298], [370, 354], [42, 354]], '9-00864-01', 0.8142346378327698)]
import easyocr
reader = easyocr.Reader(['en'])
result = reader.readtext('/path/of/image',detail=0)
print(result)
['SERIAL NUMBER', 'AOC1715821', 'PART NUMBER', '9-00864-01']
result = reader.readtext('/path/of/image',detail=0, paragraph=True)
print(result)
['SERIAL NUMBER AOC1715821', 'PART NUMBER 9-00864-01']
import easyocr
reader = easyocr.Reader(['en'])
result = reader.readtext('/path/of/image')
for res in result:
coord=res[0]
text=res[1]
conf=res[2]
print(text)
SERIAL NUMBER
AOC1715821
PART NUMBER
9-00864-01
pip install supervision
import easyocr
import supervision as sv
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
# Image path
Image_path = '/path/to/image'
# Initialize EasyOCR reader (English language, CPU)
reader = easyocr.Reader(['en'], gpu=False, model_storage_directory='/path/to/directory')
# Perform text detection on the image
result = reader.readtext(Image_path)
# Load image using OpenCV
image = cv2.imread(Image_path)
# Prepare lists for bounding boxes, confidences, class IDs, and labels
xyxy, confidences, class_ids, label = [], [], [], []
# Extract data from OCR result
for detection in result:
bbox, text, confidence = detection[0], detection[1], detection[2]
# Convert bounding box format
x_min = int(min([point[0] for point in bbox]))
y_min = int(min([point[1] for point in bbox]))
x_max = int(max([point[0] for point in bbox]))
y_max = int(max([point[1] for point in bbox]))
# Append data to lists
xyxy.append([x_min, y_min, x_max, y_max])
label.append(text)
confidences.append(confidence)
class_ids.append(0)
# Convert to NumPy arrays
detections = sv.Detections(
xyxy=np.array(xyxy),
confidence=np.array(confidences),
class_id=np.array(class_ids)
)
# Annotate image with bounding boxes and labels
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
annotated_image = box_annotator.annotate(scene=image, detections=detections)
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections, labels=label)
# Display and save the annotated image
sv.plot_image(image=annotated_image)
cv2.imwrite("Output.jpg", annotated_image)
—THE END—
觉得有用,麻烦给个赞和在看