Last active
December 1, 2017 22:16
-
-
Save refugeesus/9e2448e91754fb387c20e3777aea221e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
import time | |
from matplotlib import pyplot as plt | |
from Adafruit_AMG88xx import Adafruit_AMG88xx | |
from scipy.interpolate import griddata | |
import cv2 | |
from helium_client import Helium | |
import json | |
helium = Helium("/dev/serial0") | |
helium.connect() | |
channel = helium.create_channel("aws_test") | |
# Access an instance of Configuration | |
config = channel.config() | |
# Start sensor | |
sensor = Adafruit_AMG88xx() | |
state = config.get("channel.state") | |
config.set("channel.state", state) | |
''' pixels = [28.75, 28.25, 27.75, 27.5, 27.5, 27.25, 28.5, 29, | |
28.25, 29.25, 27.25, 27, 27, 26.5, 29.75, 29.5, | |
29, 27, 27.25, 27, 26.5, 28, 30.25, 28.25, | |
28.5, 27.25, 27, 26.75, 26.5, 28.25, 30.25, 30.25, | |
29, 27, 27, 26.5, 26.75, 29.25, 31, 30.25, | |
27, 26.5, 26.75, 26.5, 26, 28.25, 29.75, 28.25, | |
27.25, 26.25, 26, 26, 26.75, 27, 27.5, 26.5, | |
27.5, 26, 26.25, 25.75, 25.75, 26, 26.75, 26.5]''' | |
while(1): | |
if state == True: | |
# Read pixels, convert them to values between 0 and 1, map them to an 8x8 grid | |
pixels = sensor.readPixels() | |
pixmax = max(pixels) | |
pixels = [x / pixmax for x in pixels] | |
points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)] | |
grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j] | |
# bicubic interpolation of 8x8 grid to make a 32x32 grid | |
bicubic = griddata(points, pixels, (grid_x, grid_y), method='cubic') | |
image = np.array(bicubic) | |
image = np.reshape(image, (32, 32)) | |
# print(image) | |
plt.imsave('color_img.jpg', image) | |
# Read image | |
img = cv2.imread("color_img.jpg", cv2.IMREAD_GRAYSCALE) | |
img = cv2.bitwise_not(img) | |
# Setup SimpleBlobDetector parameters. | |
params = cv2.SimpleBlobDetector_Params() | |
# Change thresholds | |
params.minThreshold = 10 | |
params.maxThreshold = 255 | |
# Filter by Area. | |
params.filterByArea = True | |
params.minArea = 5 | |
# Filter by Circularity | |
params.filterByCircularity = True | |
params.minCircularity = 0.1 | |
# Filter by Convexity | |
params.filterByConvexity = False | |
params.minConvexity = 0.87 | |
# Filter by Inertia | |
params.filterByInertia = False | |
params.minInertiaRatio = 0.01 | |
# Set up the detector with default parameters. | |
detector = cv2.SimpleBlobDetector(params) | |
# Detect blobs. | |
keypoints = detector.detect(img) | |
# Draw detected blobs as red circles. | |
for i in range (0, len(keypoints)): | |
x = keypoints[i].pt[0] | |
y = keypoints[i].pt[1] | |
print(x, y) | |
payload = json.dumps({"objects":str(len(keypoints))}) | |
json.loads(payload) | |
# Get the channel setting for interval_ms | |
interval = config.get("channel.interval_ms") | |
config.set("channel.interval_ms", interval) | |
state = config.get("channel.state") | |
config.set("channel.state", state) | |
# Report the device having set interval_ms | |
channel.send(payload) | |
time.sleep(int(interval)) | |
else: | |
print("idle") | |
time.sleep(5) | |
state = config.get("channel.state") | |
config.set("channel.state", state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment