def main(): global sensor sensor = Sensor() try: sensor.connect() except: print("Error connecting to sensor") raise return #cv.namedWindow("Threshold", cv.WINDOW_AUTOSIZE); cv.namedWindow("Contours", cv.WINDOW_AUTOSIZE); nfunc = np.vectorize(normalize) images = get_next_image() baseline_frame = images[-1] baseline = baseline_frame['image'] width = baseline_frame['cols'] height = baseline_frame['rows'] while 1: images = get_next_image(); frame = images[-1] pixels = np.array(frame['image']) pixels_zeroed = np.subtract(baseline, pixels); min = np.amin(pixels_zeroed) max = np.amax(pixels_zeroed) pixels_normalized = nfunc(pixels_zeroed, min, max) large = sp.ndimage.zoom(pixels_normalized, 10, order=1) large = large.astype(np.uint8); large = cv.medianBlur(large,7) #large = cv.GaussianBlur(large,(7,7),0) #stuff, blobs = cv.threshold(large,150,255,cv.THRESH_BINARY) stuff, blobs = cv.threshold(large,160,255,cv.THRESH_BINARY+cv.THRESH_OTSU) contours, hierarchy = cv.findContours(blobs, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) out = np.zeros((height*10,width*10,3), np.uint8) cv.drawContours(out,contours,-1,(255,255,0),3) regions_found = len(contours) contours = np.vstack(contours).squeeze() rect = cv.minAreaRect(contours) box = cv.cv.BoxPoints(rect) box = np.int0(box) cv.drawContours(out,[box],0,(0,255,255),2) #cv.imshow("Threshold", blobs); cv.imshow("Contours", out) cv.waitKey(1) x = rect[0][0] y = rect[0][1] angle = rect[2]; if(regions_found < 10): send(x, y, angle) time.sleep(0.4) sensor.close() print "Done. Everything cleaned up."
def sensor_data(): sensor = Sensor.connect(device) data = Sensor.read(sensor) payload = { 'time_local': datetime.datetime.now().isoformat(), 'pm_2_5': Sensor.to_pm_2_5(data), 'pm_10': Sensor.to_pm_10(data) } return jsonify(payload)
from sense_hat import SenseHat import time import json from azure.iot.device import IoTHubDeviceClient, Message import configparser from sensor import Sensor config = configparser.ConfigParser() config.read('config.ini') conn = config['iot_conn']['conn'] sensor = Sensor(conn) sensor.connect() sensor.setData('temperature') i = 0 while i <= 10: sensor.showMessage() sensor.sendMessage() time.sleep(10) i += 1