Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import pytesseract
  2. from PIL import Image
  3. print (pytesseract.image_to_string(Image.open('test.jpg')))
  4.  
  5.  
  6.  
  7.  
  8. # USAGE
  9. # python order_coordinates.py
  10.  
  11. # import the necessary packages
  12. from __future__ import print_function
  13. from imutils import perspective
  14. from imutils import contours
  15. import numpy as np
  16. import argparse
  17. import imutils
  18. import cv2
  19.  
  20. def order_points_old(pts):
  21. # initialize a list of coordinates that will be ordered
  22. # such that the first entry in the list is the top-left,
  23. # the second entry is the top-right, the third is the
  24. # bottom-right, and the fourth is the bottom-left
  25. rect = np.zeros((4, 2), dtype="float32")
  26.  
  27. # the top-left point will have the smallest sum, whereas
  28. # the bottom-right point will have the largest sum
  29. s = pts.sum(axis=1)
  30. rect[0] = pts[np.argmin(s)]
  31. rect[2] = pts[np.argmax(s)]
  32.  
  33. # now, compute the difference between the points, the
  34. # top-right point will have the smallest difference,
  35. # whereas the bottom-left will have the largest difference
  36. diff = np.diff(pts, axis=1)
  37. rect[1] = pts[np.argmin(diff)]
  38. rect[3] = pts[np.argmax(diff)]
  39.  
  40. # return the ordered coordinates
  41. return rect
  42.  
  43. # construct the argument parse and parse the arguments
  44. ap = argparse.ArgumentParser()
  45. ap.add_argument("-n", "--new", type=int, default=-1,
  46. help="whether or not the new order points should should be used")
  47. args = vars(ap.parse_args())
  48.  
  49. # load our input image, convert it to grayscale, and blur it slightly
  50. image = cv2.imread("exemple.jpg")
  51. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  52. gray = cv2.GaussianBlur(gray, (7, 7), 0)
  53.  
  54. # perform edge detection, then perform a dilation + erosion to
  55. # close gaps in between object edges
  56. edged = cv2.Canny(gray, 50, 100)
  57. edged = cv2.dilate(edged, None, iterations=1)
  58. edged = cv2.erode(edged, None, iterations=1)
  59.  
  60. # find contours in the edge map
  61. cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
  62. cv2.CHAIN_APPROX_SIMPLE)
  63. cnts = imutils.grab_contours(cnts)
  64.  
  65. # sort the contours from left-to-right and initialize the bounding box
  66. # point colors
  67. (cnts, _) = contours.sort_contours(cnts)
  68. colors = ((0, 0, 255), (240, 0, 159), (255, 0, 0), (255, 255, 0))
  69.  
  70. # loop over the contours individually
  71. for (i, c) in enumerate(cnts):
  72. # if the contour is not sufficiently large, ignore it
  73. if cv2.contourArea(c) < 1500:
  74. continue
  75.  
  76. # compute the rotated bounding box of the contour, then
  77. # draw the contours
  78. box = cv2.minAreaRect(c)
  79. box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
  80. box = np.array(box, dtype="int")
  81. cv2.drawContours(image, [box], -1, (0, 255, 0), 2)
  82.  
  83. # show the original coordinates
  84. print("Object #{}:".format(i + 1))
  85. print(box)
  86.  
  87. # order the points in the contour such that they appear
  88. # in top-left, top-right, bottom-right, and bottom-left
  89. # order, then draw the outline of the rotated bounding
  90. # box
  91. rect = order_points_old(box)
  92.  
  93. # check to see if the new method should be used for
  94. # ordering the coordinates
  95. if args["new"] > 0:
  96. rect = perspective.order_points(box)
  97.  
  98. # show the re-ordered coordinates
  99. print(rect.astype("int"))
  100. print("")
  101.  
  102. # loop over the original points and draw them
  103. for ((x, y), color) in zip(rect, colors):
  104. cv2.circle(image, (int(x), int(y)), 5, color, -1)
  105.  
  106. # draw the object num at the top-left corner
  107. cv2.putText(image, "Object #{}".format(i + 1),
  108. (int(rect[0][0] - 15), int(rect[0][1] - 15)),
  109. cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 2)
  110.  
  111. # show the image
  112. cv2.imshow("Image", image)
  113. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement