Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv
- import sys
- import datetime
- import os
- import threading
- import time
- import shutil
- # This threaded class monitors the src and dst folders for files
- # If it finds some files in the spool folder, it copies them over to
- # the snapshots folder and removes the originals
- class FolderMonitor (threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.setDaemon(True)
- def run(self):
- while True:
- dirlist = os.listdir("spool/")
- print "Attempting to move spooled images to server"
- for filename in dirlist:
- try:
- if os.path.exists("snapshots/"):
- src = "spool/" + filename
- dst = "snapshots/" + filename
- os.system("cp " + src + " " + dst)
- os.system("rm " + src)
- except IOError, e:
- print "Error Jim- ", e
- except:
- print "Unkown Error"
- time.sleep(20)
- capture = cv.CaptureFromCAM(0)
- frame = cv.QueryFrame(capture)
- if frame == None:
- print "Could not capture image from camera"
- sys.exit(0)
- snapshot = cv.CloneImage(frame)
- grey = cv.CreateImage( cv.GetSize(frame), 8, 1)
- last = cv.CloneImage(grey)
- diff = cv.CloneImage(grey)
- cv.NamedWindow('frame')
- cv.NamedWindow('snapshot')
- cv.NamedWindow('detect')
- font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, 4)
- # Start the folder monitor
- monitor = FolderMonitor()
- monitor.start()
- def countWhitePixels(img):
- (w,h) = cv.GetSize(img)
- size = float(w * h)
- white = float(cv.CountNonZero(img))
- return white / size
- def main():
- while True:
- # Captures the current frame, greys and blurs it
- frame = cv.QueryFrame(capture)
- cv.CvtColor( frame, grey, cv.CV_BGR2GRAY)
- cv.EqualizeHist(grey,grey)
- cv.Smooth(grey, grey, cv.CV_BLUR,17)
- cv.Smooth(grey, grey, cv.CV_MEDIAN,17)
- # Calculate the difference between the last frame and the
- # current blurred frame
- cv.AbsDiff(grey, last, diff)
- threshold = 15
- color = 255
- cv.Threshold( diff, diff, threshold, color, cv.CV_THRESH_BINARY)
- weight = countWhitePixels(diff)
- if weight > 0.04:
- now = datetime.datetime.now()
- cv.Copy(frame, snapshot)
- cv.Rectangle(snapshot, (0,snapshot.height-30), (snapshot.width,snapshot.height), (0,0,0), cv.CV_FILLED)
- cv.PutText(snapshot, now.strftime("%d-%m-%Y %H:%M:%S"), (10,snapshot.height- 10), font, (255,255,255))
- filename = now.strftime("%d-%m-%Y_%H%M%S.jpg")
- if not os.path.exists("spool/" + filename):
- cv.SaveImage("spool/" + filename, snapshot)
- # Show all images because I am nice
- cv.ShowImage("frame", frame)
- cv.ShowImage("snapshot", snapshot)
- cv.ShowImage('detect', diff)
- # Copy the existing blurred image (last will always be 1 frame)
- # behind
- cv.Copy(grey, last)
- c = cv.WaitKey(2)
- if c==27:
- break
- return 0
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement