Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. placeFile - Start FTP to ftp.brisbaneskycams.com
  2. placeFile - ERROR FTP transfer Failed ...
  3. Filename : /home/pi/pimotion/images/capture-20160503-044806.jpg
  4. Error Msg: ['[Errno', '-2] Name or service not known']
  5. Please Investigate Problem ...
  6.  
  7. #!/usr/bin/python
  8. #
  9. # Lightweight Motion Detection using python picamera libraries
  10. # based on code from raspberry pi forum by user utpalc
  11. # modified by Claude Pageau for this working example
  12. # ------------------------------------------------------------
  13. # original code on github https://github.com/pageauc/picamera-motion
  14.  
  15. # This is sample code that can be used for further development
  16.  
  17. verbose = True
  18. if verbose:
  19. print "Loading python libraries ....."
  20. else:
  21. print "verbose output has been disabled verbose=False"
  22.  
  23. import picamera
  24. import picamera.array
  25. import datetime
  26. import time
  27. from PIL import Image
  28. from PIL import ImageFont
  29. from PIL import ImageDraw
  30. from fractions import Fraction
  31. import ftplib
  32. from ftplib import FTP
  33. import os
  34.  
  35. #Constants
  36. SECONDS2MICRO = 1000000 # Constant for converting Shutter Speed in Seconds to Microseconds
  37.  
  38. # User Customizable Settings
  39. imageDir = "images"
  40. imagePath = "/home/pi/pimotion/" + imageDir
  41. imageNamePrefix = 'capture-' # Prefix for all image file names. Eg front-
  42. imageWidth = 1980
  43. imageHeight = 1080
  44. imageVFlip = False # Flip image Vertically
  45. imageHFlip = False # Flip image Horizontally
  46. imagePreview = False
  47.  
  48. # FTP Server location variables
  49. ftpOn = True
  50. ftpServer = "ftphostname"
  51. ftpDir = ""
  52. ftpUser = "ftpusername"
  53. ftpPass = "XXXXXXXXXX"
  54.  
  55. numberSequence = False
  56.  
  57. threshold = 10 # How Much pixel changes
  58. sensitivity = 100 # How many pixels change
  59.  
  60. nightISO = 800
  61. nightShutSpeed = 6 * SECONDS2MICRO # seconds times conversion to microseconds constant
  62.  
  63. # Advanced Settings not normally changed
  64. testWidth = 100
  65. testHeight = 75
  66.  
  67. def checkImagePath(imagedir):
  68. # Find the path of this python script and set some global variables
  69. mypath=os.path.abspath(__file__)
  70. baseDir=mypath[0:mypath.rfind("/")+1]
  71. baseFileName=mypath[mypath.rfind("/")+1:mypath.rfind(".")]
  72.  
  73. # Setup imagePath and create folder if it Does Not Exist.
  74. imagePath = baseDir + imagedir # Where to save the images
  75. # if imagePath does not exist create the folder
  76. if not os.path.isdir(imagePath):
  77. if verbose:
  78. print "%s - Image Storage folder not found." % (progName)
  79. print "%s - Creating image storage folder %s " % (progName, imagePath)
  80. os.makedirs(imagePath)
  81. return imagePath
  82.  
  83. def takeDayImage(imageWidth, imageHeight, filename):
  84. if verbose:
  85. print "takeDayImage - Working ....."
  86. with picamera.PiCamera() as camera:
  87. camera.resolution = (imageWidth, imageHeight)
  88. # camera.rotation = cameraRotate #Note use imageVFlip and imageHFlip variables
  89. if imagePreview:
  90. camera.start_preview()
  91. camera.vflip = imageVFlip
  92. camera.hflip = imageHFlip
  93. # Day Automatic Mode
  94. camera.exposure_mode = 'auto'
  95. camera.awb_mode = 'auto'
  96. camera.capture(filename)
  97.  
  98. def placeFile(filepath):
  99. path, filename = os.path.split(filepath)
  100. os.chdir(path)
  101. print filename
  102. print path
  103. if verbose:
  104. print("placeFile - Start FTP to %s" % ftpServer )
  105. try:
  106. ftp = FTP(ftpServer)
  107. ftp.login(user=ftpUser, passwd = ftpPass)
  108. ftp.cwd(ftpDir)
  109. ftp.storbinary('STOR ' + filename, open(filename, 'rb'))
  110. ftp.quit()
  111. if verbose:
  112. print("placeFile - SUCCESSFUL FTP Transfer")
  113. print(" Filename : %s " % (filepath))
  114. except ftplib.all_errors as e:
  115. errorcode_string = str(e).split(None, 1)
  116. if verbose:
  117. print("placeFile - ERROR FTP transfer Failed ...")
  118. print(" Filename : %s " % (filepath))
  119. print(" Error Msg: %s" % ( errorcode_string ))
  120. print(" Please Investigate Problem ...")
  121.  
  122.  
  123. def takeNightImage(imageWidth, imageHeight, filename):
  124. if verbose:
  125. print "takeNightImage - Working ....."
  126. with picamera.PiCamera() as camera:
  127. camera.resolution = (imageWidth, imageHeight)
  128. if imagePreview:
  129. camera.start_preview()
  130. camera.vflip = imageVFlip
  131. camera.hflip = imageHFlip
  132. # Night time low light settings have long exposure times
  133. # Settings for Low Light Conditions
  134. # Set a frame rate of 1/6 fps, then set shutter
  135. # speed to 6s and ISO to approx 800 per nightISO variable
  136. camera.framerate = Fraction(1, 6)
  137. camera.shutter_speed = nightShutSpeed
  138. camera.exposure_mode = 'off'
  139. camera.iso = nightISO
  140. # Give the camera a good long time to measure AWB
  141. # (you may wish to use fixed AWB instead)
  142. time.sleep(10)
  143. camera.capture(filename)
  144. if verbose:
  145. print "checkNightMode - Captured %s" % (filename)
  146. return filename
  147.  
  148. def takeMotionImage(width, height, daymode):
  149. with picamera.PiCamera() as camera:
  150. time.sleep(1)
  151. camera.resolution = (width, height)
  152. with picamera.array.PiRGBArray(camera) as stream:
  153. if daymode:
  154. camera.exposure_mode = 'auto'
  155. camera.awb_mode = 'auto'
  156. else:
  157. # Take Low Light image
  158. # Set a framerate of 1/6 fps, then set shutter
  159. # speed to 6s and ISO to 800
  160. camera.framerate = Fraction(1, 6)
  161. camera.shutter_speed = nightShutSpeed
  162. camera.exposure_mode = 'off'
  163. camera.iso = nightISO
  164. # Give the camera a good long time to measure AWB
  165. # (you may wish to use fixed AWB instead)
  166. time.sleep( 10 )
  167. camera.capture(stream, format='rgb')
  168. return stream.array
  169.  
  170. def scanIfDay(width, height, daymode):
  171. data1 = takeMotionImage(width, height, daymode)
  172. while not motionFound:
  173. data2 = takeMotionImage(width, height, daymode)
  174. pCnt = 0L;
  175. diffCount = 0L;
  176. for w in range(0, width):
  177. for h in range(0, height):
  178. # get the diff of the pixel. Conversion to int
  179. # is required to avoid unsigned short overflow.
  180. diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
  181. if diff > threshold:
  182. diffCount += 1
  183. if diffCount > sensitivity:
  184. break; #break outer loop.
  185. if diffCount > sensitivity:
  186. motionFound = True
  187. else:
  188. # print "Sum of all pixels=", pxCnt
  189. data2 = data1
  190. return motionFound
  191.  
  192. def scanMotion(width, height, daymode):
  193. motionFound = False
  194. data1 = takeMotionImage(width, height, daymode)
  195. while not motionFound:
  196. data2 = takeMotionImage(width, height, daymode)
  197. diffCount = 0L;
  198. for w in range(0, width):
  199. for h in range(0, height):
  200. # get the diff of the pixel. Conversion to int
  201. # is required to avoid unsigned short overflow.
  202. diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
  203. if diff > threshold:
  204. diffCount += 1
  205. if diffCount > sensitivity:
  206. break; #break outer loop.
  207. if diffCount > sensitivity:
  208. motionFound = True
  209. else:
  210. data2 = data1
  211. return motionFound
  212.  
  213. def getFileName(imagePath, imageNamePrefix, currentCount):
  214. rightNow = datetime.datetime.now()
  215. if numberSequence :
  216. filename = imagePath + "/" + imageNamePrefix + str(currentCount) + ".jpg"
  217. else:
  218. filename = "%s/%s%04d%02d%02d-%02d%02d%02d.jpg" % ( imagePath, imageNamePrefix ,rightNow.year, rightNow.month, rightNow.day, rightNow.hour, rightNow.minute, rightNow.second)
  219. return filename
  220.  
  221. def motionDetection():
  222. print "Scanning for Motion threshold=%i sensitivity=%i ......" % (threshold, sensitivity)
  223. isDay = True
  224. currentCount= 1000
  225. while True:
  226. if scanMotion(testWidth, testHeight, isDay):
  227. filename = getFileName(imagePath, imageNamePrefix, currentCount)
  228. if numberSequence:
  229. currentCount += 1
  230. if isDay:
  231. takeDayImage( imageWidth, imageHeight, filename )
  232. else:
  233. takeNightImage( imageWidth, imageHeight, filename )
  234. if verbose:
  235. print("MotionDetection - Saved %s" % filename)
  236. if ftpOn:
  237. placeFile(filename)
  238.  
  239. if __name__ == '__main__':
  240. try:
  241. motionDetection()
  242.  
  243. finally:
  244. print ""
  245. print "+++++++++++++++"
  246. print "Exiting Program"
  247. print "+++++++++++++++"
  248. print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement