Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. # USAGE
  2. # python recognize_digits.py
  3.  
  4. # import the necessary packages
  5. from imutils.perspective import four_point_transform
  6. from imutils import contours
  7. import imutils
  8. import cv2
  9.  
  10. import matplotlib
  11. #matplotlib.use('GTKAgg')
  12. import matplotlib.pyplot as plt
  13.  
  14.  
  15. # define the dictionary of digit segments so we can identify
  16. # each digit on the thermostat
  17. DIGITS_LOOKUP = {
  18. (1, 1, 1, 0, 1, 1, 1): 0,
  19. (0, 0, 1, 0, 0, 1, 0): 1,
  20. (1, 0, 1, 1, 1, 1, 0): 2,
  21. (1, 0, 1, 1, 0, 1, 1): 3,
  22. (0, 1, 1, 1, 0, 1, 0): 4,
  23. (1, 1, 0, 1, 0, 1, 1): 5,
  24. (1, 1, 0, 1, 1, 1, 1): 6,
  25. (1, 0, 1, 0, 0, 1, 0): 7,
  26. (1, 1, 1, 1, 1, 1, 1): 8,
  27. (1, 1, 1, 1, 0, 1, 1): 9
  28. }
  29.  
  30. # load the example image
  31. image = cv2.imread("example.jpg")
  32.  
  33. # pre-process the image by resizing it, converting it to
  34. # graycale, blurring it, and computing an edge map
  35. image = imutils.resize(image, height=500)
  36. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  37. blurred = cv2.GaussianBlur(gray, (5, 5), 0)
  38. edged = cv2.Canny(blurred, 50, 200, 255)
  39.  
  40. # find contours in the edge map, then sort them by their
  41. # size in descending order
  42. cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
  43. cv2.CHAIN_APPROX_SIMPLE)
  44. cnts = cnts[0] if imutils.is_cv2() else cnts[1]
  45. cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
  46. displayCnt = None
  47.  
  48. # loop over the contours
  49. for c in cnts:
  50. # approximate the contour
  51. peri = cv2.arcLength(c, True)
  52. approx = cv2.approxPolyDP(c, 0.02 * peri, True)
  53.  
  54. # if the contour has four vertices, then we have found
  55. # the thermostat display
  56. if len(approx) == 4:
  57. displayCnt = approx
  58. break
  59.  
  60. # extract the thermostat display, apply a perspective transform
  61. # to it
  62. warped = four_point_transform(gray, displayCnt.reshape(4, 2))
  63. output = four_point_transform(image, displayCnt.reshape(4, 2))
  64.  
  65. # threshold the warped image, then apply a series of morphological
  66. # operations to cleanup the thresholded image
  67. thresh = cv2.threshold(warped, 0, 255,
  68. cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
  69. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
  70. thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
  71.  
  72. # find contours in the thresholded image, then initialize the
  73. # digit contours lists
  74. cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
  75. cv2.CHAIN_APPROX_SIMPLE)
  76. cnts = cnts[0] if imutils.is_cv2() else cnts[1]
  77. digitCnts = []
  78.  
  79. # loop over the digit area candidates
  80. for c in cnts:
  81. # compute the bounding box of the contour
  82. (x, y, w, h) = cv2.boundingRect(c)
  83.  
  84. # if the contour is sufficiently large, it must be a digit
  85. if w >= 15 and (h >= 30 and h <= 40):
  86. digitCnts.append(c)
  87.  
  88. # sort the contours from left-to-right, then initialize the
  89. # actual digits themselves
  90. digitCnts = contours.sort_contours(digitCnts,
  91. method="left-to-right")[0]
  92. digits = []
  93.  
  94. # loop over each of the digits
  95. for c in digitCnts:
  96. # extract the digit ROI
  97. (x, y, w, h) = cv2.boundingRect(c)
  98. roi = thresh[y:y + h, x:x + w]
  99.  
  100. # compute the width and height of each of the 7 segments
  101. # we are going to examine
  102. (roiH, roiW) = roi.shape
  103. (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))
  104. dHC = int(roiH * 0.05)
  105.  
  106. # define the set of 7 segments
  107. segments = [
  108. ((0, 0), (w, dH)), # top
  109. ((0, 0), (dW, h // 2)), # top-left
  110. ((w - dW, 0), (w, h // 2)), # top-right
  111. ((0, (h // 2) - dHC) , (w, (h // 2) + dHC)), # center
  112. ((0, h // 2), (dW, h)), # bottom-left
  113. ((w - dW, h // 2), (w, h)), # bottom-right
  114. ((0, h - dH), (w, h)) # bottom
  115. ]
  116. on = [0] * len(segments)
  117.  
  118. # loop over the segments
  119. for (i, ((xA, yA), (xB, yB))) in enumerate(segments):
  120. # extract the segment ROI, count the total number of
  121. # thresholded pixels in the segment, and then compute
  122. # the area of the segment
  123. segROI = roi[yA:yB, xA:xB]
  124. total = cv2.countNonZero(segROI)
  125. area = (xB - xA) * (yB - yA)
  126.  
  127. # if the total number of non-zero pixels is greater than
  128. # 50% of the area, mark the segment as "on"
  129. if total / float(area) > 0.5:
  130. on[i]= 1
  131.  
  132. # lookup the digit and draw it on the image
  133. digit = DIGITS_LOOKUP[tuple(on)]
  134. digits.append(digit)
  135. cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 1)
  136. cv2.putText(output, str(digit), (x - 10, y - 10),
  137. cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 255, 0), 2)
  138.  
  139. # display the digits
  140. print(u"{}{}.{} \u00b0C".format(*digits))
  141.  
  142. import numpy as np
  143. import matplotlib.pyplot as plt
  144. from PIL import Image
  145.  
  146. fname = 'example.jpg'
  147. image = Image.open(fname).convert("L")
  148. arr = np.asarray(image)
  149. plt.imshow(arr, cmap='gray')
  150. plt.show()
  151.  
  152.  
  153. plt.imshow(output)
  154. plt.show()
  155. #cv2.imshow("Input", image)
  156. #cv2.imshow("Output", output)
  157. #cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement