try: from PIL import Image except ImportError: import Image from cv2 import cv2 import pytesseract import argparse import numpy as np from imutils.object_detection import non_max_suppression pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' faceCascade = cv2.CascadeClassifier( 'haarcascade/haarcascade_frontalface_default.xml') def decode_predictions(scores, geometry): # grab the number of rows and columns from the scores volume, then # initialize our set of bounding box rectangles and corresponding # confidence scores (numRows, numCols) = scores.shape[2:4] rects = [] confidences = [] # loop over the number of rows for y in range(0, numRows): # extract the scores (probabilities), followed by the # geometrical data used to derive potential bounding box # coordinates that surround text scoresData = scores[0, 0, y] xData0 = geometry[0, 0, y] xData1 = geometry[0, 1, y] xData2 = geometry[0, 2, y] xData3 = geometry[0, 3, y] anglesData = geometry[0, 4, y] # loop over the number of columns for x in range(0, numCols): # if our score does not have sufficient probability, # ignore it if scoresData[x] < 0.5: continue # compute the offset factor as our resulting feature # maps will be 4x smaller than the input image (offsetX, offsetY) = (x * 4.0, y * 4.0) # extract the rotation angle for the prediction and # then compute the sin and cosine angle = anglesData[x] cos = np.cos(angle) sin = np.sin(angle) # use the geometry volume to derive the width and height # of the bounding box h = xData0[x] + xData2[x] w = xData1[x] + xData3[x] # compute both the starting and ending (x, y)-coordinates # for the text prediction bounding box endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x])) endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x])) startX = int(endX - w) startY = int(endY - h) # add the bounding box coordinates and probability score # to our respective lists rects.append((startX, startY, endX, endY)) confidences.append(scoresData[x]) # return a tuple of the bounding boxes and associated confidences return (rects, confidences) def findNumber(url): image = cv2.imread("./"+url) orig = image.copy() (origH, origW) = image.shape[:2] (newW, newH) = (320,320) rW = origW / float(newW) rH = origH / float(newH) image = cv2.resize(image, (newW, newH)) (H, W) = image.shape[:2] layerNames = [ "feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"] net = cv2.dnn.readNet("../EAST/frozen_east_text_detection.pb") blob = cv2.dnn.blobFromImage(image, 1.0, (W, H), (123.68, 116.78, 103.94), swapRB=True, crop=False) net.setInput(blob) (scores, geometry) = net.forward(layerNames) (rects, confidences) = decode_predictions(scores, geometry) boxes = non_max_suppression(np.array(rects), probs=confidences) results = [] for (startX, startY, endX, endY) in boxes: startX = int(startX * rW) startY = int(startY * rH) endX = int(endX * rW) endY = int(endY * rH) dX = int((endX - startX) * 0.0) dY = int((endY - startY) * 0.0) startX = max(0, startX - dX) startY = max(0, startY - dY) endX = min(origW, endX + (dX * 2)) endY = min(origH, endY + (dY * 2)) roi = orig[startY:endY, startX:endX] config = ("-l eng --oem 1 --psm 7") text = pytesseract.image_to_string(roi, config=config) results.append(((startX, startY, endX, endY), text)) results = sorted(results, key=lambda r: r[0][1]) wyniki = [] for ((startX, startY, endX, endY), text) in results: if( text.isdigit() ): wyniki.append(text) # print("OCR TEXT") # print("========") # print("{}\n".format(text)) # text = "".join([c if ord(c) < 128 else "" for c in text]).strip() # output = orig.copy() # cv2.rectangle(output, (startX, startY), (endX, endY), # (0, 0, 255), 2) # cv2.putText(output, text, (startX, startY - 20), # cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3) # cv2.imshow("Text Detection", output) # cv2.waitKey(0) return wyniki