fork download
  1. # Import required modules
  2. import cv2 as cv
  3. import math
  4. import argparse
  5.  
  6. parser = argparse.ArgumentParser(description='Use this script to run text detection deep learning networks using OpenCV.')
  7. # Input argument
  8. parser.add_argument('--input', help='Path to input image or video file. Skip this argument to capture frames from a camera.')
  9. # Model argument
  10. parser.add_argument('--model', default="frozen_east_text_detection.pb",
  11. help='Path to a binary .pb file of model contains trained weights.'
  12. )
  13. # Width argument
  14. parser.add_argument('--width', type=int, default=320,
  15. help='Preprocess input image by resizing to a specific width. It should be multiple by 32.'
  16. )
  17. # Height argument
  18. parser.add_argument('--height',type=int, default=320,
  19. help='Preprocess input image by resizing to a specific height. It should be multiple by 32.'
  20. )
  21. # Confidence threshold
  22. parser.add_argument('--thr',type=float, default=0.5,
  23. help='Confidence threshold.'
  24. )
  25. # Non-maximum suppression threshold
  26. parser.add_argument('--nms',type=float, default=0.4,
  27. help='Non-maximum suppression threshold.'
  28. )
  29.  
  30. args = parser.parse_args()
  31.  
  32.  
  33. ############ Utility functions ############
  34. def decode(scores, geometry, scoreThresh):
  35. detections = []
  36. confidences = []
  37.  
  38. ############ CHECK DIMENSIONS AND SHAPES OF geometry AND scores ############
  39. assert len(scores.shape) == 4, "Incorrect dimensions of scores"
  40. assert len(geometry.shape) == 4, "Incorrect dimensions of geometry"
  41. assert scores.shape[0] == 1, "Invalid dimensions of scores"
  42. assert geometry.shape[0] == 1, "Invalid dimensions of geometry"
  43. assert scores.shape[1] == 1, "Invalid dimensions of scores"
  44. assert geometry.shape[1] == 5, "Invalid dimensions of geometry"
  45. assert scores.shape[2] == geometry.shape[2], "Invalid dimensions of scores and geometry"
  46. assert scores.shape[3] == geometry.shape[3], "Invalid dimensions of scores and geometry"
  47. height = scores.shape[2]
  48. width = scores.shape[3]
  49. for y in range(0, height):
  50.  
  51. # Extract data from scores
  52. scoresData = scores[0][0][y]
  53. x0_data = geometry[0][0][y]
  54. x1_data = geometry[0][1][y]
  55. x2_data = geometry[0][2][y]
  56. x3_data = geometry[0][3][y]
  57. anglesData = geometry[0][4][y]
  58. for x in range(0, width):
  59. score = scoresData[x]
  60.  
  61. # If score is lower than threshold score, move to next x
  62. if(score < scoreThresh):
  63. continue
  64.  
  65. # Calculate offset
  66. offsetX = x * 4.0
  67. offsetY = y * 4.0
  68. angle = anglesData[x]
  69.  
  70. # Calculate cos and sin of angle
  71. cosA = math.cos(angle)
  72. sinA = math.sin(angle)
  73. h = x0_data[x] + x2_data[x]
  74. w = x1_data[x] + x3_data[x]
  75.  
  76. # Calculate offset
  77. offset = ([offsetX + cosA * x1_data[x] + sinA * x2_data[x], offsetY - sinA * x1_data[x] + cosA * x2_data[x]])
  78.  
  79. # Find points for rectangle
  80. p1 = (-sinA * h + offset[0], -cosA * h + offset[1])
  81. p3 = (-cosA * w + offset[0], sinA * w + offset[1])
  82. center = (0.5*(p1[0]+p3[0]), 0.5*(p1[1]+p3[1]))
  83. detections.append((center, (w,h), -1*angle * 180.0 / math.pi))
  84. confidences.append(float(score))
  85.  
  86. # Return detections and confidences
  87. return [detections, confidences]
  88.  
  89. if __name__ == "__main__":
  90. # Read and store arguments
  91. confThreshold = args.thr
  92. nmsThreshold = args.nms
  93. inpWidth = args.width
  94. inpHeight = args.height
  95. model = args.model
  96.  
  97. # Load network
  98. net = cv.dnn.readNet(model)
  99.  
  100. # Create a new named window
  101. kWinName = "EAST: An Efficient and Accurate Scene Text Detector"
  102. cv.namedWindow(kWinName, cv.WINDOW_NORMAL)
  103. outputLayers = []
  104. outputLayers.append("feature_fusion/Conv_7/Sigmoid")
  105. outputLayers.append("feature_fusion/concat_3")
  106.  
  107. # Open a video file or an image file or a camera stream
  108. cap = cv.VideoCapture(args.input if args.input else 0)
  109.  
  110. while cv.waitKey(1) < 0:
  111. # Read frame
  112. hasFrame, frame = cap.read()
  113. if not hasFrame:
  114. cv.waitKey()
  115. break
  116.  
  117. # Get frame height and width
  118. height_ = frame.shape[0]
  119. width_ = frame.shape[1]
  120. rW = width_ / float(inpWidth)
  121. rH = height_ / float(inpHeight)
  122.  
  123. # Create a 4D blob from frame.
  124. blob = cv.dnn.blobFromImage(frame, 1.0, (inpWidth, inpHeight), (123.68, 116.78, 103.94), True, False)
  125.  
  126. # Run the model
  127. net.setInput(blob)
  128. output = net.forward(outputLayers)
  129. t, _ = net.getPerfProfile()
  130. label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
  131.  
  132. # Get scores and geometry
  133. scores = output[0]
  134. geometry = output[1]
  135. [boxes, confidences] = decode(scores, geometry, confThreshold)
  136. # Apply NMS
  137. indices = cv.dnn.NMSBoxesRotated(boxes, confidences, confThreshold,nmsThreshold)
  138. for i in indices:
  139. # get 4 corners of the rotated rect
  140. vertices = cv.boxPoints(boxes[i[0]])
  141. # scale the bounding box coordinates based on the respective ratios
  142. for j in range(4):
  143. vertices[j][0] *= rW
  144. vertices[j][1] *= rH
  145. for j in range(4):
  146. p1 = (vertices[j][0], vertices[j][1])
  147. p2 = (vertices[(j + 1) % 4][0], vertices[(j + 1) % 4][1])
  148. cv.line(frame, p1, p2, (0, 255, 0), 2, cv.LINE_AA);
  149. # cv.putText(frame, "{:.3f}".format(confidences[i[0]]), (vertices[0][0], vertices[0][1]), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1, cv.LINE_AA)
  150.  
  151. # Put efficiency information
  152. cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
  153.  
  154. # Display the frame
  155. cv.imshow(kWinName,frame)
  156. cv.imwrite("out-{}".format(args.input),frame)
Runtime error #stdin #stdout #stderr 0.03s 27664KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 2, in <module>
ImportError: No module named 'cv2'