def text_detection(image, east, min_confidence, width, height): image = cv2.imread(image) orig = image.copy() (origHeight, origWidth) = image.shape[:2] (newW, newH) = (width, height) ratioWidth = origWidth / float(newW) ratioHeight = origHeight / float(newH) image = cv2.resize(image, (newW, newH)) (imageHeight, imageWidth) = image.shape[:2] layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"] # original slika cv2.imshow("Original", orig) print("[INFO] Učitavanje EAST text detektora...") net = cv2.dnn.readNet(east) blob = cv2.dnn.blobFromImage(image, 1.0, (imageWidth, imageHeight), (123.68, 116.78, 103.94), swapRB=True, crop=False) start = time.time() net.setInput(blob) (scores, geometry) = net.forward(layerNames) end = time.time() print("[INFO] Trajanje detekcije: {:.6f} sekundi".format(end - start)) confidenceThreshold = min_confidence nmsThreshold = 0.4 (rects, confidences, baggage) = decode(scores, geometry, confidenceThreshold) offsets = [] thetas = [] for b in baggage: offsets.append(b['offset']) thetas.append(b['angle']) functions = [nms.felzenszwalb.nms, nms.fast.nms, nms.malisiewicz.nms] #print("[INFO] Running nms.boxes . . .") for i, function in enumerate(functions): start = time.time() indicies = nms.boxes(rects, confidences, nms_function=function, confidence_threshold=confidenceThreshold, nsm_threshold=nmsThreshold) end = time.time() indicies = np.array(indicies).reshape(-1) drawrects = np.array(rects)[indicies] name = function.__module__.split('.')[-1].title() print( "[INFO] Trajanje izvođenja {} metode: {:.6f} seconds i prondađeno je {} okvira" .format(name, end - start, len(drawrects))) drawOn = orig.copy() drawBoxes(drawOn, drawrects, ratioWidth, ratioHeight, (0, 255, 0), 2) title = "nms.boxes {}".format(name) cv2.imshow(title, drawOn) cv2.moveWindow(title, 150 + i * 300, 350) cv2.waitKey(0) """
def text_detection(image, east, min_confidence, width, height): # load the input image and grab the image dimensions image = cv2.imread(image) orig = image.copy() (origHeight, origWidth) = image.shape[:2] # set the new width and height and then determine the ratio in change # for both the width and height (newW, newH) = (width, height) ratioWidth = origWidth / float(newW) ratioHeight = origHeight / float(newH) # resize the image and grab the new image dimensions image = cv2.resize(image, (newW, newH)) (imageHeight, imageWidth) = image.shape[:2] # define the two output layer names for the EAST detector model that # we are interested -- the first is the output probabilities and the # second can be used to derive the bounding box coordinates of text layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"] # load the pre-trained EAST text detector print("[INFO] loading EAST text detector...") net = cv2.dnn.readNet(east) # construct a blob from the image and then perform a forward pass of # the model to obtain the two output layer sets blob = cv2.dnn.blobFromImage(image, 1.0, (imageWidth, imageHeight), (123.68, 116.78, 103.94), swapRB=True, crop=False) start = time.time() net.setInput(blob) (scores, geometry) = net.forward(layerNames) end = time.time() # show timing information on text prediction print("[INFO] text detection took {:.6f} seconds".format(end - start)) # NMS on the the unrotated rects confidenceThreshold = min_confidence nmsThreshold = 0.4 # decode the blob info (rects, confidences, baggage) = decode(scores, geometry, confidenceThreshold) offsets = [] thetas = [] for b in baggage: offsets.append(b['offset']) thetas.append(b['angle']) ########################################################## functions = [nms.felzenszwalb.nms, nms.fast.nms, nms.malisiewicz.nms] print("[INFO] Running nms.boxes . . .") for i, function in enumerate(functions): start = time.time() indicies = nms.boxes(rects, confidences, nms_function=function, confidence_threshold=confidenceThreshold, nsm_threshold=nmsThreshold) end = time.time() indicies = np.array(indicies).reshape(-1) drawrects = np.array(rects)[indicies] name = function.__module__.split('.')[-1].title() print("[INFO] {} NMS took {:.6f} seconds and found {} boxes".format( name, end - start, len(drawrects))) drawOn = orig.copy() drawBoxes(drawOn, drawrects, ratioWidth, ratioHeight, (0, 255, 0), 2) title = "nms.boxes {}".format(name) cv2.imshow(title, drawOn) cv2.moveWindow(title, 150 + i * 300, 150) #cv2.waitKey(0) # convert rects to polys polygons = utils.rects2polys(rects, thetas, offsets, ratioWidth, ratioHeight) print("[INFO] Running nms.polygons . . .") for i, function in enumerate(functions): start = time.time() indicies = nms.polygons(polygons, confidences, nms_function=function, confidence_threshold=confidenceThreshold, nsm_threshold=nmsThreshold) end = time.time() indicies = np.array(indicies).reshape(-1) drawpolys = np.array(polygons)[indicies] name = function.__module__.split('.')[-1].title() print("[INFO] {} NMS took {:.6f} seconds and found {} boxes".format( name, end - start, len(drawpolys))) drawOn = orig.copy() drawPolygons(drawOn, drawpolys, ratioWidth, ratioHeight, (0, 255, 0), 2) title = "nms.polygons {}".format(name) cv2.imshow(title, drawOn) cv2.moveWindow(title, 150 + i * 300, 150) cv2.waitKey(0)
for i, function in enumerate(functions): start = time.time() indicies = nms.boxes(rects, confidences, nms_function=function, confidence_threshold=confidenceThreshold, nsm_threshold=nmsThreshold) end = time.time() indicies = np.array(indicies).reshape(-1) drawrects = np.array(rects)[indicies] name = function.__module__.split('.')[-1].title() print("[INFO] {} NMS took {:.6f} seconds and found {} boxes".format(name, end - start, len(drawrects))) drawOn = orig.copy() drawBoxes(drawOn, drawrects, ratioWidth, ratioHeight, (0, 255, 0), 2) ############## Lo mío ################ print(drawrects, indicies, ratioHeight, ratioWidth) # drawrects contiene las coordenadas de las cajas de texto detectadas en formato (x, y, w, h) # Usarlas para recortar la imagen y pasárselo a tesseract y = [int(drawrects[0][1]*ratioHeight), int((drawrects[0][1]+drawrects[0][3])*ratioHeight)] x = [int(drawrects[0][0]*ratioWidth), int((drawrects[0][0]+drawrects[0][2])*ratioWidth)] print(x, y) crop = orig[y[0]:y[1], x[0]:x[1]] cv2.imshow('Prueba', crop) cv2.waitKey(0) ####################################### title = "nms.boxes {}".format(name) cv2.imshow(title, drawOn)