Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import numpy as np
- import time
- import cv2
- # import paho.mqtt.client as mqtt
- # Global settings
- debug = 0 # 0 = Off, 1 = text, 2 = with images
- start_x = 188 # x-position to start cropping
- start_y = 428 # x-position to start cropping
- crop_size = 40 # size of crop area in pixels
- ramp_frames = 40 # Number of frames to throw away while the camera adjusts to light levels
- # Global variables
- lasttrigger = 1
- lasthist = 1200
- liter_current = 0
- debug_counter = 1
- # ==============================================
- # ===== Rotate Image ===========================
- # ==============================================
- def rotate(image, angle, center = None, scale = 1.0):
- (h, w) = image.shape[:2]
- if center is None:
- center = (w / 2, h / 2)
- # Perform the rotation
- M = cv2.getRotationMatrix2D(center, angle, scale)
- rotated = cv2.warpAffine(image, M, (w, h))
- return rotated
- # ==============================================
- # ===== Write debug image ======================
- # ==============================================
- def write_debug_img(thresh, reduce, img):
- global debug_counter
- print "Logging image ... " + str(debug_counter)
- cv2.imwrite("temp/thresh_" + str(debug_counter) + ".jpg", thresh)
- cv2.imwrite("temp/reduce_" + str(debug_counter) + ".jpg", reduce)
- cv2.imwrite("temp/img_" + str(debug_counter) + ".jpg", img)
- debug_counter = debug_counter + 1
- # ==============================================
- # ===== MQTT Upload ============================
- # ==============================================
- # def mqtt_upload ( liter ):
- # current_milli_time = int(round(time.time() * 1000))
- # line_string = "Wasser_Zaehler amount=1,value=" + str(liter)
- # line_string = line_string + " " + str(current_milli_time) + "000000"
- # res = client.publish("logger/wasserzaehler", line_string, 2, True)
- # print ("MQTT: " + line_string)
- # return
- # def on_connect(client, userdata, flags, rc):
- # print("Connected with result code " + str(rc))
- # def on_publish(mosq, obj, mid):
- # print("Publish: " + str(mid))
- #client.on_publish = on_publish
- #client.on_connect = on_connect
- # ==============================================
- # ===== Main ===================================
- # ==============================================
- print "Init stream ..."
- vc = cv2.VideoCapture(0) # capture from first /dev/video device
- if vc.isOpened(): # try to get the first frame
- rval, frame = vc.read() # read frame
- print "Stream initialized ..." + str(frame.shape) # frame shape gives width, heigh, color
- else:
- rval = False
- print "Stream NOT initialized ..."
- # to adjust light levels, if necessary
- print "Ramping up ... "
- for i in xrange(ramp_frames):
- temp = vc.read()
- if debug:
- print "Ramping up ... " + str(i) + " / " + str(ramp_frames)
- # ==============================================
- # ===== Loop ===================================
- # ==============================================
- while rval:
- ts = time.strftime("%d.%m.%Y %H:%M:%S - ", time.gmtime())
- img = rotate(frame, 180) # rotating image 180 degrees
- crop_img = img[start_y:start_y+crop_size, start_x:start_x+crop_size] # crop the image to a 50 x 50 rectangle
- crop_img=cv2.GaussianBlur(crop_img, (9,9), 9) # blur the image
- reduce=cv2.resize(crop_img, (40,30)) # reduce the size for faster processing
- hsv = cv2.cvtColor(reduce, cv2.COLOR_BGR2HSV) # change color scheme to HSV
- red_lower=np.array([0,150,0],np.uint8) # between this and
- red_upper=np.array([190,255,255],np.uint8) # this red
- thresh = cv2.inRange(hsv, red_lower, red_upper) # check for redness
- hist = cv2.calcHist([thresh],[0],None,[1],[0,1]) # transform red to white, anything else to black
- if debug and hist != lasthist:
- print ts + "Hist ..." + str(hist[0])
- lasthist = hist
- # if debug == 2 and hist[0] < 1050:
- # write_debug_img(thresh, reduce, img)
- if hist[0] > 1120 and lasttrigger == 1: # if we triggered and it's very black, trigger to 0
- lasttrigger=0
- if debug:
- print ts + "Trigger 0"
- if debug == 2:
- write_debug_img(thresh, reduce, img)
- elif hist[0] < 1050 and lasttrigger == 0: # if it's less black, trigger to 1
- lasttrigger=1
- liter_current = liter_current + 1
- if debug:
- print ts + "Trigger 1"
- print '###{"count": ' + str(liter_current) + '}' # One liter was counted
- if debug == 2:
- write_debug_img(thresh, reduce, img)
- # mqtt_upload (liter_current)
- time.sleep(0.25) # Sleep for 100 ms to reduce load
- rval, frame = vc.read() # next frame
- key = cv2.waitKey(10) # exit on escape key
- if key == 27: # exit on ESC
- break
- vc.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement