def get_scores(image, east='frozen_east_text_detection.pb', min_confidence=0.5, width=320, height=320): if TESTING: cv.imshow('SCORES INPUT', image) (imageHeight, imageWidth) = image.shape[:2] layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"] # load the pre-trained EAST text detector log("[INFO] loading EAST text detector...") log('east:', east) net = cv.dnn.readNetFromTensorflow(east) blob = cv.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() log("[INFO] text detection took {:.6f} seconds".format(end - start)) confidenceThreshold = min_confidence return decode(scores, geometry, confidenceThreshold)
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)
def text_detect(image, 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 = text_model # 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() # 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] drawpolys_count, drawrects_count = 0, 0 for i, function in enumerate(functions): indicies = nms.boxes(rects, confidences, nms_function=function, confidence_threshold=confidenceThreshold, nsm_threshold=nmsThreshold) indicies = np.array(indicies).reshape(-1) if len(indicies) != 0: drawrects = np.array(rects)[indicies] drawpolys_count = len(drawrects) # convert rects to polys polygons = utils.rects2polys(rects, thetas, offsets, ratioWidth, ratioHeight) 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) if len(indicies) != 0: drawpolys = np.array(polygons)[indicies] drawrects_count = len(drawpolys) return [drawpolys_count, drawrects_count]