Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import numpy as np
  4. import time
  5. import cv2
  6. # import paho.mqtt.client as mqtt
  7.  
  8. # Global settings
  9. debug = 0 # 0 = Off, 1 = text, 2 = with images
  10. start_x = 188 # x-position to start cropping
  11. start_y = 428 # x-position to start cropping
  12. crop_size = 40 # size of crop area in pixels
  13. ramp_frames = 40 # Number of frames to throw away while the camera adjusts to light levels
  14.  
  15.  
  16. # Global variables
  17. lasttrigger = 1
  18. lasthist = 1200
  19. liter_current = 0
  20. debug_counter = 1
  21.  
  22.  
  23.  
  24. # ==============================================
  25. # ===== Rotate Image ===========================
  26. # ==============================================
  27. def rotate(image, angle, center = None, scale = 1.0):
  28. (h, w) = image.shape[:2]
  29.  
  30. if center is None:
  31. center = (w / 2, h / 2)
  32.  
  33. # Perform the rotation
  34. M = cv2.getRotationMatrix2D(center, angle, scale)
  35. rotated = cv2.warpAffine(image, M, (w, h))
  36.  
  37. return rotated
  38.  
  39.  
  40. # ==============================================
  41. # ===== Write debug image ======================
  42. # ==============================================
  43. def write_debug_img(thresh, reduce, img):
  44. global debug_counter
  45. print "Logging image ... " + str(debug_counter)
  46. cv2.imwrite("temp/thresh_" + str(debug_counter) + ".jpg", thresh)
  47. cv2.imwrite("temp/reduce_" + str(debug_counter) + ".jpg", reduce)
  48. cv2.imwrite("temp/img_" + str(debug_counter) + ".jpg", img)
  49. debug_counter = debug_counter + 1
  50.  
  51.  
  52. # ==============================================
  53. # ===== MQTT Upload ============================
  54. # ==============================================
  55. # def mqtt_upload ( liter ):
  56. # current_milli_time = int(round(time.time() * 1000))
  57. # line_string = "Wasser_Zaehler amount=1,value=" + str(liter)
  58. # line_string = line_string + " " + str(current_milli_time) + "000000"
  59.  
  60. # res = client.publish("logger/wasserzaehler", line_string, 2, True)
  61. # print ("MQTT: " + line_string)
  62. # return
  63.  
  64. # def on_connect(client, userdata, flags, rc):
  65. # print("Connected with result code " + str(rc))
  66.  
  67. # def on_publish(mosq, obj, mid):
  68. # print("Publish: " + str(mid))
  69.  
  70. #client.on_publish = on_publish
  71. #client.on_connect = on_connect
  72.  
  73.  
  74.  
  75. # ==============================================
  76. # ===== Main ===================================
  77. # ==============================================
  78. print "Init stream ..."
  79. vc = cv2.VideoCapture(0) # capture from first /dev/video device
  80.  
  81. if vc.isOpened(): # try to get the first frame
  82. rval, frame = vc.read() # read frame
  83. print "Stream initialized ..." + str(frame.shape) # frame shape gives width, heigh, color
  84. else:
  85. rval = False
  86. print "Stream NOT initialized ..."
  87.  
  88. # to adjust light levels, if necessary
  89. print "Ramping up ... "
  90. for i in xrange(ramp_frames):
  91. temp = vc.read()
  92. if debug:
  93. print "Ramping up ... " + str(i) + " / " + str(ramp_frames)
  94.  
  95.  
  96. # ==============================================
  97. # ===== Loop ===================================
  98. # ==============================================
  99.  
  100. while rval:
  101. ts = time.strftime("%d.%m.%Y %H:%M:%S - ", time.gmtime())
  102.  
  103. img = rotate(frame, 180) # rotating image 180 degrees
  104.  
  105. crop_img = img[start_y:start_y+crop_size, start_x:start_x+crop_size] # crop the image to a 50 x 50 rectangle
  106. crop_img=cv2.GaussianBlur(crop_img, (9,9), 9) # blur the image
  107. reduce=cv2.resize(crop_img, (40,30)) # reduce the size for faster processing
  108.  
  109. hsv = cv2.cvtColor(reduce, cv2.COLOR_BGR2HSV) # change color scheme to HSV
  110. red_lower=np.array([0,150,0],np.uint8) # between this and
  111. red_upper=np.array([190,255,255],np.uint8) # this red
  112. thresh = cv2.inRange(hsv, red_lower, red_upper) # check for redness
  113. hist = cv2.calcHist([thresh],[0],None,[1],[0,1]) # transform red to white, anything else to black
  114.  
  115.  
  116. if debug and hist != lasthist:
  117. print ts + "Hist ..." + str(hist[0])
  118. lasthist = hist
  119.  
  120. # if debug == 2 and hist[0] < 1050:
  121. # write_debug_img(thresh, reduce, img)
  122.  
  123. if hist[0] > 1120 and lasttrigger == 1: # if we triggered and it's very black, trigger to 0
  124. lasttrigger=0
  125. if debug:
  126. print ts + "Trigger 0"
  127. if debug == 2:
  128. write_debug_img(thresh, reduce, img)
  129. elif hist[0] < 1050 and lasttrigger == 0: # if it's less black, trigger to 1
  130. lasttrigger=1
  131. liter_current = liter_current + 1
  132. if debug:
  133. print ts + "Trigger 1"
  134. print '###{"count": ' + str(liter_current) + '}' # One liter was counted
  135. if debug == 2:
  136. write_debug_img(thresh, reduce, img)
  137. # mqtt_upload (liter_current)
  138.  
  139. time.sleep(0.25) # Sleep for 100 ms to reduce load
  140. rval, frame = vc.read() # next frame
  141.  
  142. key = cv2.waitKey(10) # exit on escape key
  143. if key == 27: # exit on ESC
  144. break
  145.  
  146.  
  147. vc.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement