Skip to content

Instantly share code, notes, and snippets.

@pelrun
Created February 13, 2017 06:46
Show Gist options
  • Save pelrun/519ddb66d0ad8016bba45e2a2d6426f6 to your computer and use it in GitHub Desktop.
Save pelrun/519ddb66d0ad8016bba45e2a2d6426f6 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import urllib
import ssl
import time
maxWidth = 320
maxHeight = 240
maxClean = 71000.0
minClean = maxClean/2
tableCorners = np.array([[867, 629], [1137, 533], [1254, 627], [1013, 780] ], dtype = np.float32)
dstCorners = np.array([[0,0], [maxWidth-1,0], [maxWidth-1,maxHeight-1], [0,maxHeight-1]], dtype=np.float32)
M = cv2.getPerspectiveTransform(tableCorners, dstCorners)
while True:
timestamp = time.strftime("%Y%m%d%H%M%S")
image_file, _ = urllib.urlretrieve("https://10.0.1.100:8848/camera/49424/image.jpg?height=720&width=1280", "frames/" + timestamp + ".jpg", context=ssl.SSLContext(ssl.PROTOCOL_SSLv23))
image = cv2.imread(image_file)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
cv2.imwrite("warped/" + timestamp + ".jpg", warped)
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lighting = np.mean(grey)
grey = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(grey, 220, 1, cv2.THRESH_BINARY)
clean_raw = thresh.sum()
clean_pc = ((clean_raw-minClean) / (maxClean-minClean)) * 100
if clean_pc<0:
clean_pc = 0
if clean_pc>100:
clean_pc = 100
print timestamp+":", lighting, np.median(grey), np.std(grey), clean_pc, np.median(warped), np.std(warped)
urllib.urlretrieve("https://api.thingspeak.com/update.json?api_key=PRIVATEKEY&field2={:.2f}&field1={:.2f}&field3={:.2f}".format(lighting, clean_pc, clean_raw))
time.sleep(10*60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment