# -*- coding: utf-8 -*-
"""EasyOCR for Optical Character Recognition

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1lsez0P0MJReN9YlK-UBhtKzpHGABV5Wg
"""



"""**EasyOCR for Optical Character Recognition**

EasyOCR can OCR text in supporting  70+ languages, including English, German, Hindi, Russian,Arabic  and more! The EasyOCR maintainers plan to add additional languages in the future. You can find the full list of languages EasyOCR supports on the following page.

https://www.jaided.ai/easyocr
"""

!pip install opencv-python # NOTE: *not* opencv-contrib-python
!pip install easyocr

from easyocr import Reader
import argparse
import cv2

def cleanup_text(text):
	# strip out non-ASCII text so we can draw the text on the image
	# using OpenCV
	return "".join([c if ord(c) < 128 else "" for c in text]).strip()

# break the input languages into a comma separated list
langs = "ar,en".split(",")
gpu1=-1
print("[INFO] OCR'ing with the following languages: {}".format(langs))
# load the input image from disk
image = cv2.imread("2.png")
# OCR the input image using EasyOCR
print("[INFO] OCR'ing input image...")
reader = Reader(langs, gpu=-1 > 0)
results = reader.readtext(image)

results

# loop over the results
filename="out.jpg"
for (bbox, text, prob) in results:
	# display the OCR'd text and associated probability
	print("[INFO] {:.4f}: {}".format(prob, text))
	# unpack the bounding box
	(tl, tr, br, bl) = bbox
	tl = (int(tl[0]), int(tl[1]))
	tr = (int(tr[0]), int(tr[1]))
	br = (int(br[0]), int(br[1]))
	bl = (int(bl[0]), int(bl[1]))
	# cleanup the text and draw the box surrounding the text along
	# with the OCR'd text itself
	text = cleanup_text(text)
	cv2.rectangle(image, tl, br, (0, 255, 0), 2)
	cv2.putText(image, text, (tl[0], tl[1] - 10),
		cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
# show the output image
cv2.imwrite(filename, image) 
#cv2.imshow("Image", image)
#cv2.waitKey(0)

import easyocr
reader = easyocr.Reader(['ar','en']) # need to run only once to load model into memory
result = reader.readtext('2.png')

result

pip install -r requirements.txt

from easyocr import Reader
import cv2
def cleanup_text(text):
	# strip out non-ASCII text so we can draw the text on the image
	# using OpenCV
	return "".join([c if ord(c) < 128 else "" for c in text]).strip()

def arabic_ocr(image_path,out_image):
  # break the input languages into a comma separated list
  langs = "ar,en".split(",")
  gpu1=-1
  #print("[INFO] OCR'ing with the following languages: {}".format(langs))
# load the input image from disk
  image = cv2.imread(image_path)
# OCR the input image using EasyOCR
  print("[INFO] OCR'ing input image...")
  reader = Reader(langs, gpu=-1 > 0)
  results = reader.readtext(image)

  #print(result)
  # loop over the results
  filename=out_image
  for (bbox, text, prob) in results:
    # display the OCR'd text and associated probability
    print("[INFO] {:.4f}: {}".format(prob, text))
    # unpack the bounding box
    (tl, tr, br, bl) = bbox
    tl = (int(tl[0]), int(tl[1]))
    tr = (int(tr[0]), int(tr[1]))
    br = (int(br[0]), int(br[1]))
    bl = (int(bl[0]), int(bl[1]))
    # cleanup the text and draw the box surrounding the text along
    # with the OCR'd text itself
    text = cleanup_text(text)
    cv2.rectangle(image, tl, br, (0, 255, 0), 2)
    cv2.putText(image, text, (tl[0], tl[1] - 10),
      cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
  # show the output image
  cv2.imwrite(filename, image)
  return results

image_path='1.jpg'
out_image='arabic.jpg'
arabic_ocr(image_path,out_image)

from google.colab.patches import cv2_imshow

img = cv2.imread('arabic.jpg', cv2.IMREAD_UNCHANGED)
cv2_imshow(img)