data-writer-opencv.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import numpy.core.multiarray
  2. import cv2
  3. import pytesseract
  4. import numpy as np
  5. import time
  6. from datetime import datetime
  7. from picamera import PiCamera
  8. from pytesseract import Output
  9. import cv2
  10. import pytesseract
  11. from pytesseract import Output
  12. import numpy as np
  13. import re
  14. def parse(image):
  15. custom_oem=r'--oem 3 --psm 11'
  16. # https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital
  17. data = pytesseract.image_to_data(img, lang='lets', config=custom_oem, output_type=Output.DICT)
  18. print(data)
  19. results = []
  20. for i in range(len(data['text'])):
  21. text = data['text'][i].strip('.,-_')
  22. text = re.sub('[^0-9]', '', text)
  23. if text:
  24. results.append(text)
  25. if len(results) == 2:
  26. results = list(map(lambda x: int(x) / 10.,results ))
  27. if results[0] < 100 and results[1] < 1000:
  28. return results
  29. return None
  30. def thresholding(image):
  31. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  32. pixel_values = image.reshape((-1, 1))
  33. pixel_values = np.float32(pixel_values)
  34. print(pixel_values.shape)
  35. # define stopping criteria
  36. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
  37. # number of clusters (K)
  38. k = 3
  39. _, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
  40. # convert back to 8 bit values
  41. centers = np.uint8(centers)
  42. # flatten the labels array
  43. labels = labels.flatten()
  44. # convert all pixels to the color of the centroids
  45. segmented_image = centers[labels.flatten()]
  46. segmented_image = segmented_image.reshape(image.shape)
  47. # disable only the cluster number 2 (turn the pixel into black)
  48. masked_image = np.copy(image)
  49. # convert to the shape of a vector of pixel values
  50. masked_image = masked_image.reshape((-1, 1))
  51. # color (i.e cluster) to disable
  52. cluster = 2
  53. masked_image[labels != cluster] = [0]
  54. # convert back to original shape
  55. masked_image = masked_image.reshape(image.shape)
  56. return masked_image
  57. image = cv2.medianBlur(image,3)
  58. #image = np.invert(image)
  59. return cv2.threshold(image, 140, 255, cv2.THRESH_BINARY)[1]
  60. return image
  61. def thresholding2(image):
  62. image = cv2.medianBlur(image,7)
  63. #image = np.invert(image)
  64. return image
  65. def crop(image):
  66. top=314
  67. left=230
  68. height= 150
  69. width=400
  70. return image[top : (top + height) , left: (left + width)]
  71. camera = PiCamera(
  72. resolution=(800, 600)
  73. )
  74. camera.awb_mode = 'off'
  75. camera.awb_gains = (1.5, 2.0)
  76. camera.shutter_speed = 60000
  77. camera.iso = 800
  78. while True:
  79. file = "/home/pi/solar-monitor/camera/" + str(datetime.now()) + ".jpg"
  80. camera.capture(file)
  81. print("Captured.")
  82. img = cv2.imread(file)
  83. img = crop(img)
  84. img = thresholding2(img)
  85. print(parse(img))
  86. cv2.imwrite(file + '_r.jpg', img)
  87. time.sleep(2)