SHOW:
|
|
- or go back to the newest paste.
1 | import cv | |
2 | import sys | |
3 | import datetime | |
4 | import os | |
5 | import threading | |
6 | import time | |
7 | import shutil | |
8 | ||
9 | # This threaded class monitors the src and dst folders for files | |
10 | # If it finds some files in the spool folder, it copies them over to | |
11 | # the snapshots folder and removes the originals | |
12 | class FolderMonitor (threading.Thread): | |
13 | def __init__(self): | |
14 | threading.Thread.__init__(self) | |
15 | self.setDaemon(True) | |
16 | def run(self): | |
17 | ||
18 | while True: | |
19 | dirlist = os.listdir("spool/") | |
20 | print "Attempting to move spooled images to server" | |
21 | for filename in dirlist: | |
22 | try: | |
23 | if os.path.exists("snapshots/"): | |
24 | src = "spool/" + filename | |
25 | dst = "snapshots/" + filename | |
26 | os.system("cp " + src + " " + dst) | |
27 | os.system("rm " + src) | |
28 | ||
29 | except IOError, e: | |
30 | print "Error Jim- ", e | |
31 | except: | |
32 | print "Unkown Error" | |
33 | ||
34 | time.sleep(20) | |
35 | ||
36 | capture = cv.CaptureFromCAM(0) | |
37 | frame = cv.QueryFrame(capture) | |
38 | ||
39 | if frame == None: | |
40 | print "Could not capture image from camera" | |
41 | sys.exit(0) | |
42 | ||
43 | snapshot = cv.CloneImage(frame) | |
44 | ||
45 | grey = cv.CreateImage( cv.GetSize(frame), 8, 1) | |
46 | last = cv.CloneImage(grey) | |
47 | diff = cv.CloneImage(grey) | |
48 | ||
49 | cv.NamedWindow('frame') | |
50 | cv.NamedWindow('snapshot') | |
51 | cv.NamedWindow('detect') | |
52 | ||
53 | font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, 4) | |
54 | ||
55 | # Start the folder monitor | |
56 | monitor = FolderMonitor() | |
57 | monitor.start() | |
58 | ||
59 | def countWhitePixels(img): | |
60 | (w,h) = cv.GetSize(img) | |
61 | size = float(w * h) | |
62 | white = float(cv.CountNonZero(img)) | |
63 | return white / size | |
64 | ||
65 | ||
66 | def main(): | |
67 | while True: | |
68 | # Captures the current frame, greys and blurs it | |
69 | frame = cv.QueryFrame(capture) | |
70 | cv.CvtColor( frame, grey, cv.CV_BGR2GRAY) | |
71 | cv.EqualizeHist(grey,grey) | |
72 | cv.Smooth(grey, grey, cv.CV_BLUR,17) | |
73 | cv.Smooth(grey, grey, cv.CV_MEDIAN,17) | |
74 | ||
75 | # Calculate the difference between the last frame and the | |
76 | # current blurred frame | |
77 | cv.AbsDiff(grey, last, diff) | |
78 | ||
79 | threshold = 15 | |
80 | color = 255 | |
81 | cv.Threshold( diff, diff, threshold, color, cv.CV_THRESH_BINARY) | |
82 | ||
83 | weight = countWhitePixels(diff) | |
84 | if weight > 0.04: | |
85 | now = datetime.datetime.now() | |
86 | ||
87 | cv.Copy(frame, snapshot) | |
88 | cv.Rectangle(snapshot, (0,snapshot.height-30), (snapshot.width,snapshot.height), (0,0,0), cv.CV_FILLED) | |
89 | cv.PutText(snapshot, now.strftime("%d-%m-%Y %H:%M:%S"), (10,snapshot.height- 10), font, (255,255,255)) | |
90 | ||
91 | filename = now.strftime("%d-%m-%Y_%H%M%S.jpg") | |
92 | if not os.path.exists("spool/" + filename): | |
93 | cv.SaveImage("spool/" + filename, snapshot) | |
94 | ||
95 | ||
96 | # Show all images because I am nice | |
97 | cv.ShowImage("frame", frame) | |
98 | cv.ShowImage("snapshot", snapshot) | |
99 | cv.ShowImage('detect', diff) | |
100 | ||
101 | # Copy the existing blurred image (last will always be 1 frame) | |
102 | # behind | |
103 | cv.Copy(grey, last) | |
104 | ||
105 | - | c = cv.WaitKey(2) |
105 | + | c = cv.WaitKey(2) |
106 | if c==27: | |
107 | break | |
108 | ||
109 | ||
110 | return 0 | |
111 | ||
112 | if __name__ == '__main__': | |
113 | main() |