text: import cv2 as cv
  import math
  import numpy as np
  from numpy.linalg import norm
  def decode(scores, geometry, scoreThresh):
      detections = []
      confidences = []
  
      ############ CHECK DIMENSIONS AND SHAPES OF geometry AND scores ############
      assert len(scores.shape) == 4, "Incorrect dimensions of scores"
      assert len(geometry.shape) == 4, "Incorrect dimensions of geometry"
      assert scores.shape[0] == 1, "Invalid dimensions of scores"
      assert geometry.shape[0] == 1, "Invalid dimensions of geometry"
      assert scores.shape[1] == 1, "Invalid dimensions of scores"
      assert geometry.shape[1] == 5, "Invalid dimensions of geometry"
      assert scores.shape[2] == geometry.shape[2], "Invalid dimensions of scores and geometry"
      assert scores.shape[3] == geometry.shape[3], "Invalid dimensions of scores and geometry"
      height = scores.shape[2]
      width = scores.shape[3]
      for y in range(0, height):
  
          # Extract data from scores
          scoresData = scores[0][0][y]
          x0_data = geometry[0][0][y]
          x1_data = geometry[0][1][y]
          x2_data = geometry[0][2][y]
          x3_data = geometry[0][3][y]
          anglesData = geometry[0][4][y]
          for x in range(0, width):
              score = scoresData[x]
  
              # If score is lower than threshold score, move to next x
              if(score < scoreThresh):
                  continue
  
              # Calculate offset
              offsetX = x * 4.0
              offsetY = y * 4.0
              angle = anglesData[x]
  
              # Calculate cos and sin of angle
              cosA = math.cos(angle)
              sinA = math.sin(angle)
              h = x0_data[x] + x2_data[x]
              w = x1_data[x] + x3_data[x]
  
              # Calculate offset
              offset = ([offsetX + cosA * x1_data[x] + sinA * x2_data[x], offsetY - sinA * x1_data[x] + cosA * x2_data[x]])
  
              # Find points for rectangle
              p1 = (-sinA * h + offset[0], -cosA * h + offset[1])
              p3 = (-cosA * w + offset[0],  sinA * w + offset[1])
              center = (0.5*(p1[0]+p3[0]), 0.5*(p1[1]+p3[1]))
              detections.append((center, (w,h), -1*angle * 180.0 / math.pi))
              confidences.append(float(score))
  
      # Return detections and confidences
      return [detections, confidences]
  
  def text_detector(frame):
      # Read and store arguments
      confThreshold = 0.5
      nmsThreshold = 0.4
      inpWidth = 1024
      inpHeight = 768
      model = 'input/frozen_east_text_detection.pb'
  
      # Load network
      net = cv.dnn.readNet(model)
  
      # Create a new named window
      outNames = []
      outNames.append("feature_fusion/Conv_7/Sigmoid")
      outNames.append("feature_fusion/concat_3")
  
      # Open a video file or an image file or a camera stream
  
      while cv.waitKey(1) < 0:
          # Read frame
  
          # Get frame height and width
          height_ = frame.shape[0]
          width_ = frame.shape[1]
          rW = width_ / float(inpWidth)
          rH = height_ / float(inpHeight)
  
          # Create a 4D blob from frame.
          blob = cv.dnn.blobFromImage(frame, 1.0, (inpWidth, inpHeight), (123.68, 116.78, 103.94), True, False)
  
          # Run the model
          net.setInput(blob)
          outs = net.forward(outNames)
          t, _ = net.getPerfProfile()
          label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
  
          # Get scores and geometry
          scores = outs[0]
          geometry = outs[1]
          [boxes, confidences] = decode(scores, geometry, confThreshold)
  
          # Apply NMS
          indices = cv.dnn.NMSBoxesRotated(boxes, confidences, confThreshold,nmsThreshold)
          # cropped items are gonna be in result
          result = None
  
          # meli card card meli estimation point
          # arz kart meli 5.3 , toolesh 8.5
          # makane taghribi code meli az bala chap 1.5 arz va 6 tool
          p3 = np.asarray((width_ * 6/8.5, height_ * 1.5/5.3))
  
          min_distance = 100000000000000;
          for i in indices:
              # get 4 corners of the rotated rect
              vertices = cv.boxPoints(boxes[i[0]])
              # scale the bounding box coordinates based on the respective ratios
              for j in range(4):
                  vertices[j][0] *= rW
                  vertices[j][1] *= rH
  
              p1 = np.asarray((int(vertices[1][0]), int(vertices[1][1])))
              p2 = np.asarray((int(vertices[(2 + 1) % 4][0]), int(vertices[(2 + 1) % 4][1])))
              current_distance = norm(np.cross(p2 - p1, p1 - p3)) / norm(p2 - p1)
              if(current_distance < min_distance):
                  min_distance = current_distance
                  result = frame[p1[1]:p2[1], p1[0]:p2[0]];
              # cv.line(frame, p1, p2, (0, 255, 0), 1);
  
          # Put efficiency information
          # cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
          return result