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."
q.put(distance) if __name__ == '__main__': try: q = multiprocessing.Queue() p1 = multiprocessing.Process(target = go) p1.start() while True: p2 = multiprocessing.Process(target = force_to_stop,args=[q]) p2.start() p2.join() if q.get() < 10 : p1.terminate() print('force to stop') sensor.close() #motor.stop() for i in pin : GPIO.setup(i,GPIO.OUT) pwm = GPIO.PWM(i,100) GPIO.setup(23,GPIO.OUT) GPIO.setup(4,GPIO.IN) GPIO.cleanup() print('cleaned up!') sys.exit(0) break except KeyboardInterrupt: print('KeyboardInterrupt')
metrics['tags'] = m_tags if m_name in sen_data: metrics["data_point"]["value"] = sen_data[m_name] else: logging.warn("no key in sensor data: {}".format(m_name)) continue if timestamp: metrics['data_point']['timestamp'] = timestamp content['metrics'].append(metrics) return content if __name__ == "__main__": logging.debug('start') m = Machinist(MACHINIST_APIKEY) sen = Sensor(SENSOR_SERIAL_DEVICE) signal.signal(signal.SIGTERM, lambda *args: sen.close()) signal.signal(signal.SIGINT, lambda *args: sen.close()) sen.open() try: while sen.isopen(): now = datetime.datetime.now() try: metrics = metrics_data(sen, measured_at=now) except SensorSerialError: logging.error('Sensor serial error occurred.', exc_info=True) time.sleep(10) continue try: set_result = m.set_latest(metrics) sys.stderr.write(json.dumps(set_result, sort_keys=True, indent=4) + "\n") except MachinistHTTPError: