Skip to content

Instantly share code, notes, and snippets.

@shreyanshp-cactus
Created May 19, 2026 10:14
Show Gist options
  • Select an option

  • Save shreyanshp-cactus/64640183859940366fef1f9e709347c1 to your computer and use it in GitHub Desktop.

Select an option

Save shreyanshp-cactus/64640183859940366fef1f9e709347c1 to your computer and use it in GitHub Desktop.
Extract text using easyocr
import easyocr
import cv2
import numpy as np
def main():
reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)
image_path = './input/sample.png'
results = reader.readtext(image_path)
# Load image with OpenCV
image = cv2.imread(image_path)
for (bbox, text, confidence) in results:
pts = [tuple(map(int, point)) for point in bbox]
cv2.polylines(image, [np.array(pts)], isClosed=True, color=(0,255,0), thickness=2)
# Save the result image
cv2.imwrite('./output/ocr_result.png', image)
print("Result saved to ./output/ocr_result.png")
# Save detected texts to a file
with open('./output/ocr_texts.txt', 'w', encoding='utf-8') as f:
for (_, text, _) in results:
f.write(text + '\n')
print("Extracted texts saved to ./output/ocr_texts.txt")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment