def caract_ocr(imagem, boxs): imagem = Image.fromarray(im) #print(type(imagem)) #print(boxs[3][0],boxs[3][1],(boxs[3][0]+boxs[3][2]),(boxs[3][1]+boxs[3][3])) id_caracteres = [] for i in range(len(boxs)): #print(i) #print(boxs[i][0],boxs[i][1],(boxs[i][0]+boxs[i][2]),(boxs[i][1]+boxs[i][3])) etiqueta = imagem.crop( (boxs[i][0], boxs[i][1], (boxs[i][0] + boxs[i][2]), (boxs[i][1] + boxs[i][3]))) display_img(etiqueta) etiqueta_w, etiqueta_h = etiqueta.size #0.60 e 0.70 da largura e altura da imagem sao baseados na posição relativa estimada das letras alvo. letras = etiqueta.crop( (etiqueta_w * 0.60, etiqueta_h * 0.70, etiqueta_w, etiqueta_h)) display_img(letras) letras = np.array(letras) reader = Reader(['en']) results = reader.readtext(letras) #letters = if len(results) != 0: id_caracteres.append(results[0][1]) return id_caracteres
def ocr_image(image_path, langs='en', gpu=-1): # break the input languages into a comma separated list langs = langs.split(",") 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=gpu > 0) results = reader.readtext(image) all_text = '' # loop over the results 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) all_text += ' ' + text # resize image for UI purpose h, w, _ = image.shape # image = cv2.resize(image, (700, 700)) return image, all_text
def upload_image(): file = request.files['file'] if not file: return {'error': 'Missing file'}, 400 now = datetime.now() filename = now.strftime("%Y%m%d_%H%M%S") + "_" + str(randint(000, 999)) file.save(os.path.join(uploads_dir, secure_filename(filename + '.jpg'))) images = cv2.imread(uploads_dir + '/' + filename + '.jpg') reader = Reader(['en']) results = reader.readtext(images) text_array = [] confidence_level = [] for result in results: text_array.append(result[1]) confidence_level.append(result[2]) response = { 'text': text_array, 'confidence': confidence_level, } return jsonify({"data": response}), 200
def get_text_areas(pixels): """ Easy OCR function. Gets an image at the path below and returns the text of the picture. """ reader = Reader(['fr'], gpu=False, verbose=False) ocr_data = reader.readtext(pixels) # ocr data[0][2] is the level of confidence of the result # If the result is near 0, it is very likely that there is no text try: if ocr_data[0][2] > 0.3: return ocr_data except Exception: return []
ap.add_argument("-g", "--gpu", type=int, default=-1, help="whether or not GPU should be used") args = vars(ap.parse_args()) # break the input languages into a comma separated list langs = args["langs"].split(",") print("[INFO] OCR'ing with the following languages: {}".format(langs)) image = cv2.imread(args["image"]) print("[INFO] OCR'ing input image...") reader = Reader(langs, gpu=args["gpu"] > 0) results = reader.readtext(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)