Python Pygame text overlay printing on top of itself each time - python

I'm currently wanting to use an old piece of Python code called python slideshow with time and weather (github). I'm using a Raspberry Pi and I've got it all setup and it works perfectly in landscape (normal) mode. This is a slideshow application which has a weather and time overlay developed on Pygame.
I want to use the application in portrait, however when I rotate my screen in the /boot/config.txt using display_rotate=1 (or 3) and restart the slideshow, everything appears to work, the weather overlay appears in the bottom left/right as it should, however as soon as any part of the weather or clock or day update, it prints the text over itself.
Has anyone got an idea of what might be causing this behaviour? I can't see anything in the code. Here's the python file from Git below and I have attached a screenshot of the strange behavour[image showing text overlay issue in the clock in this instance][1]:
Python slideshow with time and weather on github
#!/usr/bin/env python
"""
A pygame program to show a slideshow of all images buried in a given directory.
Originally Released: 2007.10.31 (Happy halloween!)
"""
from __future__ import division
import argparse
import os
import stat
import sys
import time
import datetime
import pygame
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE
#weather
import pyowm
file_list = [] # a list of all images being shown
title = "pgSlideShow | My Slideshow!" # caption of the window...
waittime = 5 # default time to wait between images (in seconds)
def walktree(top, callback):
"""recursively descend the directory tree rooted at top, calling the
callback function for each regular file. Taken from the module-stat
example at: http://docs.python.org/lib/module-stat.html
"""
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[stat.ST_MODE]
if stat.S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif stat.S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname
def addtolist(file, extensions=['.png', '.jpg', '.jpeg', '.gif', '.bmp']):
"""Add a file to a global list of image files."""
global file_list # ugh
filename, ext = os.path.splitext(file)
e = ext.lower()
# Only add common image types to the list.
if e in extensions:
print 'Adding to list: ', file
file_list.append(file)
else:
print 'Skipping: ', file, ' (NOT a supported image)'
def input(events):
"""A function to handle keyboard/mouse/device input events. """
for event in events: # Hit the ESC key to quit the slideshow.
if (event.type == QUIT or
(event.type == KEYDOWN and event.key == K_ESCAPE)):
pygame.quit()
def timeSince(lastTime,interval):
if (time.time() - lastTime)>=interval:
return True
else:
return False
def main(startdir="."):
global file_list, title, waittime
lastSwitch=time.time()
lastWeather=time.time()
owm = pyowm.OWM('4cc9ae1d116c7e70c145252ab605f260')
observation = owm.weather_at_place('Ottawa,CA')
w = observation.get_weather()
temperature=(w.get_temperature('celsius'))['temp']
status=w.get_status()
#print w
pygame.init()
# Test for image support
if not pygame.image.get_extended():
print "Your Pygame isn't built with extended image support."
print "It's likely this isn't going to work."
sys.exit(1)
walktree(startdir, addtolist) # this may take a while...
if len(file_list) == 0:
print "Sorry. No images found. Exiting."
sys.exit(1)
modes = pygame.display.list_modes()
print max(modes)
pygame.display.set_mode(max(modes))
screen = pygame.display.get_surface()
screen_width, screen_height= screen.get_size()
pygame.display.set_caption(title)
#pygame.display.toggle_fullscreen()
pygame.display.set_mode(max(modes),pygame.FULLSCREEN)
pygame.mouse.set_visible(0)
#create font
timeFont = pygame.font.Font("indulta/Indulta-SemiSerif-boldFFP.otf", 100)
dateFont = pygame.font.Font("indulta/Indulta-SemiSerif-boldFFP.otf", 60)
weatherFont = pygame.font.Font("indulta/Indulta-SemiSerif-boldFFP.otf", 60)
print str(waittime) +"wait time"
current = 0
num_files = len(file_list)
while(True):
try:
img = pygame.image.load(file_list[current])
img = img.convert()
tempX,tempY=img.get_size()
ratio =tempX/tempY
tempSize=(screen_width,int(screen_width/ratio))
print str(img.get_size())+" to "+ str(tempSize) +"and ratio: "+str(ratio)
# rescale the image to fit the current display
img = pygame.transform.scale(img, tempSize)
screen.blit(img, (0, 0))
#gets current weather
if timeSince(lastWeather,30):
observation = owm.weather_at_place('Ottawa,CA')
w = observation.get_weather()
temperature=(w.get_temperature('celsius'))['temp']
status=w.get_status()
print status
lastWeather=time.time()
print "updateing weather"
#gets the current time and displays it
timeText=datetime.datetime.now().strftime("%I:%M%p")
dateText=datetime.datetime.now().strftime("%B %d, %Y")
weatherText=str(int(temperature))+"`C "+status
timeLabel = timeFont.render(timeText, 1, (255,255,255))
dateLabel = dateFont.render(dateText, 1, (255,255,255))
weatherLabel = weatherFont.render(weatherText, 1, (255,255,255))
timeWidth, timeHeight= timeLabel.get_size()
dateWidth, dateHeight= dateLabel.get_size()
weatherWidth, weatherHeight= weatherLabel.get_size()
screen.blit(weatherLabel, (0, screen_height-weatherHeight))
screen.blit(timeLabel, (screen_width-timeWidth, screen_height-timeHeight-dateHeight))
screen.blit(dateLabel, (screen_width-dateWidth, screen_height-dateHeight))
pygame.display.flip()
input(pygame.event.get())
time.sleep(1/60)
except pygame.error as err:
print "Failed to display %s: %s" % (file_list[current], err)
sys.exit(1)
# When we get to the end, re-start at the beginning
if timeSince(lastSwitch,waittime):
current = (current + 1) % num_files;
lastSwitch=time.time()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Recursively loads images '
'from a directory, then displays them in a Slidshow.'
)
parser.add_argument(
'path',
metavar='ImagePath',
type=str,
default='.',
nargs="?",
help='Path to a directory that contains images'
)
parser.add_argument(
'--waittime',
type=int,
dest='waittime',
action='store',
default=1,
help='Amount of time to wait before showing the next image.'
)
parser.add_argument(
'--title',
type=str,
dest='title',
action='store',
default="pgSlidShow | My Slideshow!",
help='Set the title for the display window.'
)
args = parser.parse_args()
#waittime = args.waittime
title = args.title
main(startdir=args.path)

I have found the answer. Adding the line screen.fill((0,0,0)) just above img = pygame.image.load(file_list[current])has fixed the issue.

Related

Python - add multiple files to folder but run event only once

I'm trying to make a watchdog to listen to a folder changes (adding/deleting) files.
My problem is, that every time I copy-create/delete several files from this folder (and its subfolders), the event chain starts one by one for each and every file.
How can I make the on_event() method to be invoked only once, after multiple files creation/deletion?
Let's say I'm copying to this folders two images.
I want the event handler to be invoked only once after file transfer finishes, and not twice - once for each image - as it currently works.
Thanks!
The code runs on a raspberry pi 3 with python 3.7.
Here's the code:
import os
import time
import psutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
i = 0
def show_stats():
global i
read = "read #" + str(i) + ":"
mem = "\nmemory in use: " + str(psutil.virtual_memory().percent)+"%"
cpu = "\ncpu load: " + str(psutil.cpu_percent())+"%"
temp = "\ncurrent " + \
os.popen("vcgencmd measure_temp").readline().replace(
"=", ": ").replace("'C", " C°")
end = "\n=================="
i += 1
stats = read + mem + cpu + temp + end
return stats
class Watcher:
DIRECTORY_TO_WATCH = r'/home/pi/Desktop/jsSlider/images'
def __init__(self):
self.observer = Observer()
print("watching ", self.DIRECTORY_TO_WATCH, "...")
def run(self):
event_handler = Handler()
self.observer.schedule(
event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
print(show_stats())
except Exception as e:
self.observer.stop()
print(e)
self.observer.join()
class Handler(FileSystemEventHandler):
#staticmethod
def on_event(event):
wait = 1
elif event.event_type == 'created' or event.event_type == 'deleted':
print("Received event - %s. " %event.src_path, str(event.event_type))
time.sleep(wait) #i found that its best to give some timeout between commands because it overwhelmed the pi for some reason (one second seems to be enough)...
os.system('python /home/pi/Desktop/Slider/scripts/arr_edit.py') #recreate the JS array
time.sleep(wait)
os.system('cp -r /home/pi/Desktop/jsSlider/scripts/imgArr.js /home/pi/Desktop/jsSlider/themes/1') #copy the newly created JS array to its place
time.sleep(wait)
os.system('sudo pkill chromium') #"refresh" the page -the kiosk mode reactivates the process...
# os.system('cls')
print('done!')
if __name__ == '__main__':
w = Watcher()
w.run()
Edit I
There is a poor rpi3 connected to a tv in some clinic, working in kiosk mode to display images from a local html file (with some js code - the slide show run with an existing JS script - i can upload everything if requested | the images are also on the pi itself).
What I'm trying to achieve is to automatically:
rebulid the JS array (with a working python script - code below (arr_edit.py)).
copy the new array to its desired location. (shell command)
and restart chromium with "pkill chromium". (shell command)
Now, I cannot allow that every time someone copies/deletes multiple images, the commands will run each time - which means:
whenever 2+ images are being added, i cannot "restart" the kiosk
(sudo pkill chromium) each and every time a file is created.
Every time you copy multiple files (images in that case), for each individual image that was created in the folder, an entirely individual event.created is invoked, therefore for 5 images, there will be 5 different event.created events that will fire the on_event() method each on its own turn, making the kiosk restart 5 times in a row. (now think of what will happen if a 50 files transfer occurs - the pi will just crash)
Therefore, I need a method to invoke the command only 1 time after file transfer finishes, regardless of how many files has changed/created/deleted in the folder.
arr_edit.py (not entirely my code):
import os
dir_path = r'/home/pi/Desktop/jsSlider/images'
file_path = r'/home/pi/Desktop/jsSlider/scripts/imgArr.js'
directory = os.fsencode(dir_path)
arr_name = 'images=[\n'
start_str = '{"img":"./images/'
end_str = '"},\n'
images = ''
def writer(array, imagesList):
str_to_write = array + imagesList + ']'
f = open(file_path, 'w')
f.write(str_to_write)
f.close
file_list = os.listdir(directory)
for file in file_list:
filename = os.fsdecode(file)
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".webp") or filename.endswith(".webp"):
if file == file_list[len(file_list)-1]:
end_str = '"}\n'
images += start_str + filename + end_str
continue
else:
continue
writer(arr_name, images)
output JS array (sample from inside imgArr.js):
images=[
{"img":"./images/246.jpg"},
{"img":"./images/128.jpg"},
{"img":"./images/238.webp"},
{"img":"./images/198.jpg"},
{"img":"./images/247.webp"}
]
As Mark suggested in the comments,
i added a check to see if the js file has changed in the past 5 minutes.
if the file changed,
wait for another 5 minutes and re-initiate the cange (if more files have been added to the folder) so the new, larger files will also be shown in this run.
Works like a charm!
many thanks!!
here's the final watchdog.py
import os
import time
import psutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
i = 0
def show_stats():
global i
read = "read #" + str(i) + ":"
mem = "\nmemory in use: " + str(psutil.virtual_memory().percent)+"%"
cpu = "\ncpu load: " + str(psutil.cpu_percent())+"%"
temp = "\ncurrent " + \
os.popen("vcgencmd measure_temp").readline().replace(
"=", ": ").replace("'C", " C°")
end = "\n=================="
i += 1
stats = read + mem + cpu + temp + end
return stats
def wait_for_file(file):
time.sleep(300)
if age(file) >= 5:
modify()
def modify():
os.system('python /home/pi/Desktop/jsSlider/scripts/arr_edit.py')
os.system(
'cp -r /home/pi/Desktop/jsSlider/scripts/imgArr.js /home/pi/Desktop/jsSlider/themes/1')
time.sleep(1)
os.system('sudo pkill chromium')
# os.system('cls')
print("done!\nwatching...")
def age(filename):
return ((time.time() - os.path.getmtime(filename))//60)
class Watcher:
DIRECTORY_TO_WATCH = r'/home/pi/Desktop/jsSlider/images'
def __init__(self):
self.observer = Observer()
print("watching ", self.DIRECTORY_TO_WATCH, "...")
def run(self):
event_handler = Handler()
self.observer.schedule(
event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
print(show_stats())
except Exception as e:
self.observer.stop()
print(e)
self.observer.join()
class Handler(FileSystemEventHandler):
# staticmethod
def on_any_event(event):
file = r'/home/pi/Desktop/jsSlider/scripts/imgArr.js'
if event.event_type == 'created' or event.event_type == 'deleted':
print("Received event - %s. " %
event.src_path, str(event.event_type))
time.sleep(5)
if age(file) < 5:
wait_for_file(file)
else:
modify()
if __name__ == '__main__':
w = Watcher()
w.run()

How to integrate barcode scanner with tkinter GUI application in python?

When I run the below code. The camera opens and we can read the barcode. What I need is that the camera window remains at the side of my Tkinter GUI application rather than poping up. Here's the code
from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
from datetime import datetime
import imutils
import time
import cv2
import winsound
frequency = 600 # Set Frequency To 2500 Hertz
duration = 800 # Set Duration To 1000 ms == 1 second
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, default="barcodesData.csv",
help="path to output CSV file ")
args = vars(ap.parse_args())
print("Starting webcam")
vs = VideoStream(src=0).start()
time.sleep(2.0)
csvWrite = open(args["output"], "w")
found = set()
while True:
frameData = vs.read()
frameData = imutils.resize(frameData, width=600)
barcodes = pyzbar.decode(frameData)
for barcode in barcodes:
(x, y, width, height) = barcode.rect
cv2.rectangle(frameData, (x, y), (x + width, y + height), (0, 0, 255), 2)
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
textData = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(frameData, textData, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
if barcodeData not in found:
csvWrite.write("{},{}\n".format(datetime.today().strftime('%Y-%m-%d'),
barcodeData))
csvWrite.flush()
found.add(barcodeData)
winsound.Beep(frequency, duration)
cv2.imshow("Barcode Scanner", frameData)
key = cv2.waitKey(1) & 0xFF
if key == ord("e"):
break
# close the output CSV file do a bit of cleanup
print("\nWait while we calculate cost...")
csvWrite.close()
cv2.destroyAllWindows()
vs.stop()
time.sleep(1.0)
TO be specific. I'm making a billing software where I can read the barcodes of the products and make a bill. The camera separate screen is annoying so if the camera is on any side of the canvas all the time. It would be more quick.
I encoded the IDs of each product/item in my database in a QR code. When that particular item is being sought for, I used CV2 to detect and decode the QR code.
Here is the code:
def encode_qr():
import qrcode
import random as r
item_code = r.randint(00000000,99999999)
data = item_code
qrfile = "qr_image_name_for_specified_item.png"
# generate qrcode
qrimage = qrcode.make(data)
# save image
fl = qrimage.save(qrfile)
print("Done generating qrcode")
def decode_qr():
import cv2
filename="qr_image_name_for_specified_item.png"
# alternatively webcam cv2.VideoCapture(0)
# read image
image = cv2.imread(filename)
# initialize qrcode detector
detector = cv2.QRCodeDetector()
# detect and decode
info, v_array, binary_qrcode=detector.detectAndDecode(image)
# if null?
if v_array is None:
print("No qrcode detected or probably some technical issues occurred")
else:
print("QRCODE data")
print(info)
# below i am working with the import sqlite3 as sql3
sqldb = sql3.connect("your_database.db")
cur = sqldb.cursor()
cur.execute("select * from table where ID=?, (info,))
rows = cur.fetchall()
for r in rows:
print(r) # this will loop all the item details with the itemcode
print(r[1]) # a specific detail with the item_code

tmux - how to display an image in a pane?

I'd like to display an animated GIF or any other image in a pane in tmux.
I'm playing with asciimatics to do this, and have modded one of the sample programs (images.py) to:
display a single image
show no error messages on "Ctrl+C"
accept a single command line arg "image filename"
Here's the script I have and the only issue is that it seems to do a slow refresh of the image being displayed ~ every 10 seconds. How do I remove this refresh, since the image is a static image?
image.py
from __future__ import division
from asciimatics.effects import BannerText, Print, Scroll
from asciimatics.renderers import ColourImageFile, FigletText, ImageFile
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError
import sys
total = len(sys.argv)-1
if (total < 1):
print ("Usage: IMG")
sys.exit(1)
# Parsing args one by one
IMG = str(sys.argv[1])
def demo(screen):
scenes = []
effects = [
Print(screen,
ColourImageFile(
screen, IMG,
screen.height-2,
uni=screen.unicode_aware,
dither=screen.unicode_aware),
0,
stop_frame=200
)
]
scenes.append(Scene(effects))
screen.play(scenes, stop_on_resize=True)
# capture ctrl+c and exit nicely
import signal
import sys
def signal_handler(sig, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == "__main__":
while True:
try:
Screen.wrapper(demo)
#Screen.wrapper(demo, catch_interrupt=True)
sys.exit(0)
except ResizeScreenError:
sys.exit(0)
I just had to set stop_frame=0 and it works as hoped, as specified in the docs
Example usage
$ python image.py /images/fox.jpg
image.py
from __future__ import division
from asciimatics.effects import BannerText, Print, Scroll
from asciimatics.renderers import ColourImageFile, FigletText, ImageFile
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError
import sys
total = len(sys.argv)-1
if (total < 1):
print ("Usage: IMG")
sys.exit(1)
# Parsing args one by one
IMG = str(sys.argv[1])
def demo(screen):
scenes = []
effects = [
Print(screen,
ColourImageFile(
screen, IMG,
screen.height-2,
uni=screen.unicode_aware,
dither=screen.unicode_aware),
0,
stop_frame=200
)
]
scenes.append(Scene(effects))
screen.play(scenes, stop_on_resize=True)
# capture ctrl+c and exit nicely
import signal
import sys
def signal_handler(sig, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == "__main__":
while True:
try:
Screen.wrapper(demo)
#Screen.wrapper(demo, catch_interrupt=True)
sys.exit(0)
except ResizeScreenError:
sys.exit(0)

close current and open a new openCV window if file modified Python

I was wondering if anyone could help me out a bit with this problem I cannot solve. I am using Pafy to search Youtube from a text file that has a song name written in it and that gets a new song every few minutes.
I am using Watchdog to watch for file modification and when i first run the script, watchdog catches the file modification and runs the pafy and opencv script, but it won't do the same when the following modification occurs.
#watchdog file change monitoring
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print ("Received modified event - %s." % event.src_path)
cv2.destroyAllWindows()
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='//PLAYING', recursive=False)
observer.start()
try:
while True:
#read PLAYING.txt
PLAYING = open('//PLAYING.txt').readline()
PLAYING = PLAYING[7:]
print (PLAYING)
#search youtube based on NowOnAir.txt
query_string = urllib.parse.urlencode({"search_query" : PLAYING})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
link = ('http://www.youtube.com/watch?v=' + search_results[0])
videoPafy = pafy.new(link)
best = videoPafy.getbestvideo()
videompv = best.url
#opencv youtube video output
video = cv2.VideoCapture(videompv)
while(video.isOpened()):
ret, frame = video.read()
resize = cv2.resize(frame, (1680, 1050))
gray = cv2.cvtColor(resize, cv2.COLOR_BGR2GRAY)
result = cv2.addWeighted(image, 0.2, resize, 0.8, 0)
cv2.namedWindow('frame', 0)
cv2.resizeWindow('frame', 1680, 1050)
cv2.imshow('frame', result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
So, I what I want to happen is, when file gets modified, I want openCV to close the window and open a new one with the new youtube query result.
Any suggestions would be quite welcome, thank You in advance.
If the file is just updated once per track change, then you could check the timestamp of the file for modification and use that to trigger your search.
import os.path
import time
last_modified = time.ctime(os.path.getmtime(file))
while True:
time.sleep(1)
if last_modified != time.ctime(os.path.getmtime(file)):
# search for the track on youtube
last_modified = time.ctime(os.path.getmtime(file))

Python FTP Upload - Errorno -2, Name or service not known

i am implementing a FTP upload with my python script. Everything works except for my ftp upload. I have used chmod to change the permissions of the folder that i have got my images and script in to chmod -R 777. And the permissions on the FTP server are all set at 777. Please note that ftpDir is intentionally left blank as this is the root folder for the account on the ftp server. I do not know what else i can do to fix this issue. 3
This is my error code that i get:
placeFile - Start FTP to ftp.brisbaneskycams.com
placeFile - ERROR FTP transfer Failed ...
Filename : /home/pi/pimotion/images/capture-20160503-044806.jpg
Error Msg: ['[Errno', '-2] Name or service not known']
Please Investigate Problem ...
This is the script:
#!/usr/bin/python
#
# Lightweight Motion Detection using python picamera libraries
# based on code from raspberry pi forum by user utpalc
# modified by Claude Pageau for this working example
# ------------------------------------------------------------
# original code on github https://github.com/pageauc/picamera-motion
# This is sample code that can be used for further development
verbose = True
if verbose:
print "Loading python libraries ....."
else:
print "verbose output has been disabled verbose=False"
import picamera
import picamera.array
import datetime
import time
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from fractions import Fraction
import ftplib
from ftplib import FTP
import os
#Constants
SECONDS2MICRO = 1000000 # Constant for converting Shutter Speed in Seconds to Microseconds
# User Customizable Settings
imageDir = "images"
imagePath = "/home/pi/pimotion/" + imageDir
imageNamePrefix = 'capture-' # Prefix for all image file names. Eg front-
imageWidth = 1980
imageHeight = 1080
imageVFlip = False # Flip image Vertically
imageHFlip = False # Flip image Horizontally
imagePreview = False
# FTP Server location variables
ftpOn = True
ftpServer = "ftphostname"
ftpDir = ""
ftpUser = "ftpusername"
ftpPass = "XXXXXXXXXX"
numberSequence = False
threshold = 10 # How Much pixel changes
sensitivity = 100 # How many pixels change
nightISO = 800
nightShutSpeed = 6 * SECONDS2MICRO # seconds times conversion to microseconds constant
# Advanced Settings not normally changed
testWidth = 100
testHeight = 75
def checkImagePath(imagedir):
# Find the path of this python script and set some global variables
mypath=os.path.abspath(__file__)
baseDir=mypath[0:mypath.rfind("/")+1]
baseFileName=mypath[mypath.rfind("/")+1:mypath.rfind(".")]
# Setup imagePath and create folder if it Does Not Exist.
imagePath = baseDir + imagedir # Where to save the images
# if imagePath does not exist create the folder
if not os.path.isdir(imagePath):
if verbose:
print "%s - Image Storage folder not found." % (progName)
print "%s - Creating image storage folder %s " % (progName, imagePath)
os.makedirs(imagePath)
return imagePath
def takeDayImage(imageWidth, imageHeight, filename):
if verbose:
print "takeDayImage - Working ....."
with picamera.PiCamera() as camera:
camera.resolution = (imageWidth, imageHeight)
# camera.rotation = cameraRotate #Note use imageVFlip and imageHFlip variables
if imagePreview:
camera.start_preview()
camera.vflip = imageVFlip
camera.hflip = imageHFlip
# Day Automatic Mode
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.capture(filename)
def placeFile(filepath):
path, filename = os.path.split(filepath)
os.chdir(path)
print filename
print path
if verbose:
print("placeFile - Start FTP to %s" % ftpServer )
try:
ftp = FTP(ftpServer)
ftp.login(user=ftpUser, passwd = ftpPass)
ftp.cwd(ftpDir)
ftp.storbinary('STOR ' + filename, open(filename, 'rb'))
ftp.quit()
if verbose:
print("placeFile - SUCCESSFUL FTP Transfer")
print(" Filename : %s " % (filepath))
except ftplib.all_errors as e:
errorcode_string = str(e).split(None, 1)
if verbose:
print("placeFile - ERROR FTP transfer Failed ...")
print(" Filename : %s " % (filepath))
print(" Error Msg: %s" % ( errorcode_string ))
print(" Please Investigate Problem ...")
def takeNightImage(imageWidth, imageHeight, filename):
if verbose:
print "takeNightImage - Working ....."
with picamera.PiCamera() as camera:
camera.resolution = (imageWidth, imageHeight)
if imagePreview:
camera.start_preview()
camera.vflip = imageVFlip
camera.hflip = imageHFlip
# Night time low light settings have long exposure times
# Settings for Low Light Conditions
# Set a frame rate of 1/6 fps, then set shutter
# speed to 6s and ISO to approx 800 per nightISO variable
camera.framerate = Fraction(1, 6)
camera.shutter_speed = nightShutSpeed
camera.exposure_mode = 'off'
camera.iso = nightISO
# Give the camera a good long time to measure AWB
# (you may wish to use fixed AWB instead)
time.sleep(10)
camera.capture(filename)
if verbose:
print "checkNightMode - Captured %s" % (filename)
return filename
def takeMotionImage(width, height, daymode):
with picamera.PiCamera() as camera:
time.sleep(1)
camera.resolution = (width, height)
with picamera.array.PiRGBArray(camera) as stream:
if daymode:
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
else:
# Take Low Light image
# Set a framerate of 1/6 fps, then set shutter
# speed to 6s and ISO to 800
camera.framerate = Fraction(1, 6)
camera.shutter_speed = nightShutSpeed
camera.exposure_mode = 'off'
camera.iso = nightISO
# Give the camera a good long time to measure AWB
# (you may wish to use fixed AWB instead)
time.sleep( 10 )
camera.capture(stream, format='rgb')
return stream.array
def scanIfDay(width, height, daymode):
data1 = takeMotionImage(width, height, daymode)
while not motionFound:
data2 = takeMotionImage(width, height, daymode)
pCnt = 0L;
diffCount = 0L;
for w in range(0, width):
for h in range(0, height):
# get the diff of the pixel. Conversion to int
# is required to avoid unsigned short overflow.
diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
if diff > threshold:
diffCount += 1
if diffCount > sensitivity:
break; #break outer loop.
if diffCount > sensitivity:
motionFound = True
else:
# print "Sum of all pixels=", pxCnt
data2 = data1
return motionFound
def scanMotion(width, height, daymode):
motionFound = False
data1 = takeMotionImage(width, height, daymode)
while not motionFound:
data2 = takeMotionImage(width, height, daymode)
diffCount = 0L;
for w in range(0, width):
for h in range(0, height):
# get the diff of the pixel. Conversion to int
# is required to avoid unsigned short overflow.
diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
if diff > threshold:
diffCount += 1
if diffCount > sensitivity:
break; #break outer loop.
if diffCount > sensitivity:
motionFound = True
else:
data2 = data1
return motionFound
def getFileName(imagePath, imageNamePrefix, currentCount):
rightNow = datetime.datetime.now()
if numberSequence :
filename = imagePath + "/" + imageNamePrefix + str(currentCount) + ".jpg"
else:
filename = "%s/%s%04d%02d%02d-%02d%02d%02d.jpg" % ( imagePath, imageNamePrefix ,rightNow.year, rightNow.month, rightNow.day, rightNow.hour, rightNow.minute, rightNow.second)
return filename
def motionDetection():
print "Scanning for Motion threshold=%i sensitivity=%i ......" % (threshold, sensitivity)
isDay = True
currentCount= 1000
while True:
if scanMotion(testWidth, testHeight, isDay):
filename = getFileName(imagePath, imageNamePrefix, currentCount)
if numberSequence:
currentCount += 1
if isDay:
takeDayImage( imageWidth, imageHeight, filename )
else:
takeNightImage( imageWidth, imageHeight, filename )
if verbose:
print("MotionDetection - Saved %s" % filename)
if ftpOn:
placeFile(filename)
if __name__ == '__main__':
try:
motionDetection()
finally:
print ""
print "+++++++++++++++"
print "Exiting Program"
print "+++++++++++++++"
print ""

Categories