pastebin - collaborative debugging tool
pi.kpaste.net RSS


IR Camera Server
Posted by Anonymous on Sun 8th Apr 2018 00:20
raw | new post

  1. #!/usr/bin/python
  2. #python -m CGIHTTPServer
  3.  
  4. import sys
  5. sys.path.append('.')
  6. from Adafruit_AMG88xx import Adafruit_AMG88xx
  7. #import pygame
  8. import os
  9. import math
  10. import time
  11.  
  12. import numpy as np
  13. from scipy.interpolate import griddata
  14.  
  15. from colour import Color
  16.  
  17. #low range of the sensor (this will be blue on the screen)
  18. MINTEMP = 15
  19.  
  20. #high range of the sensor (this will be red on the screen)
  21. MAXTEMP = 30
  22.  
  23. #how many color values we can have
  24. COLORDEPTH = 1024
  25.  
  26.  
  27. #sys.stdout.write("Content-type: text/html \r\n\r\n")
  28. #sys.stdout.write("<!doctype html><html><head><title>Hello CGI</title></head>")
  29. #sys.stdout.write("<body>")
  30.  
  31. sys.stdout.write("Content-type: image/svg+xml \r\n\r\n")
  32.  
  33.  
  34.  
  35. #initialize the sensor
  36. sensor = Adafruit_AMG88xx()
  37.  
  38. points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)]
  39. grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j]
  40.  
  41. #sensor is an 8x8 grid so lets do a square
  42. height = 240
  43. width = 240
  44.  
  45. #the list of colors we can choose from
  46. blue = Color("indigo")
  47. colors = list(blue.range_to(Color("red"), COLORDEPTH))
  48.  
  49. #create the array of colors
  50. colors = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in colors]
  51.  
  52. displayPixelWidth = width / 30
  53. displayPixelHeight = height / 30
  54.  
  55.  
  56.  
  57. #some utility functions
  58. def constrain(val, min_val, max_val):
  59.     return min(max_val, max(min_val, val))
  60.  
  61. def map(x, in_min, in_max, out_min, out_max):
  62.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
  63.  
  64. #let the sensor initialize
  65. time.sleep(.1)
  66.        
  67. #read the pixels
  68. pixels = sensor.readPixels()
  69. pixels = [map(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels]
  70.  
  71. #perdorm interpolation
  72. bicubic = griddata(points, pixels, (grid_x, grid_y), method='cubic')
  73.  
  74. print("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"240\"  height=\"240\"  id=\"yourimage\" >")
  75.  
  76. #draw everything
  77. for ix, row in enumerate(bicubic):
  78.     for jx, pixel in enumerate(row):
  79.         theColor=colors[constrain(int(pixel), 0, COLORDEPTH- 1)]
  80.         theX=(displayPixelHeight * ix)
  81.         theY=(displayPixelWidth * jx)
  82.         dispColor=str(theColor[0])+","+str(theColor[1])+","+str(theColor[2]);
  83.         #print("dispColor="+dispColor);
  84.         #print("theColor["+str(theColor)+"] "+str(theX)+","+str(theY)+" ")
  85.         print("<rect x=\""+str(theX)+"\" y=\""+str(theY)+"\" width=\"8\" height=\"8\"  style=\"fill:rgb("+dispColor+");\"/>")
  86.  
  87.         #pygame.draw.rect(lcd, colors[constrain(int(pixel), 0, COLORDEPTH- 1)], (displayPixelHeight * ix, displayPixelWidth * jx, displayPixelHeight, #displayPixelWidth))
  88.  
  89.        
  90. #print("</svg>")
  91. sys.stdout.write("</svg>")
  92.  
  93. #python -m CGIHTTPServer # run from command line
  94. #index.html
  95. #<head>
  96. # <nometa http-equiv="refresh" content="2" />
  97. #</head>
  98. #<img src="./cgi-bin/cam.py" id="yourimage">
  99. #<script>
  100. #window.setInterval(function()
  101. #{
  102. #    document.getElementById('yourimage').src = "./cgi-bin/cam.py?random="+new Date().getTime();
  103. #},5000);
  104. #</script>
  105. #</body>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at