-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebcam.py
More file actions
57 lines (51 loc) · 1.87 KB
/
Webcam.py
File metadata and controls
57 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import CamUtils
import SimpleHTTPServer
import SocketServer
import Sensors
def spew_simple_webpage(snaplist):
outfile = open("index.html", "w")
outfile.write("<html>\n")
outfile.write("<head><title>Simple webcam v 0.2</title></head>\n")
outfile.write("<body>\n")
tmp = snaplist
tmp.reverse()
for (title, filename) in tmp:
outfile.write("<br/><br/>\n<img src=\"" + filename + "\"/>\n")
outfile.write("<a href=\"" + filename + "\">" + title + "</a>\n")
outfile.write("<hr/>\n")
outfile.write("<a href=\"shots\">shots</a>\n")
outfile.write("</body></html>")
class Webcam:
"""Manages snapshots and launches a simple web server
Does it in such a way that would never fly in a commercial
web environemnt"""
def __init__(self, maxsnaps=10, handlesnaps = spew_simple_webpage):
self.snapper = CamUtils.TimedSnapshotter()
self.snapnames = []
self.maxsnaps = maxsnaps
self.handlesnaps = handlesnaps
def webcam_looponce(self):
(timestamp, filename) = self.snapper.work_and_wait()
print "Wrote to file \"" + filename + "\""
self.snapnames.append((timestamp.display_string(), filename))
lop_off = max(0, len(self.snapnames) - self.maxsnaps)
self.snapnames = self.snapnames[lop_off:]
self.handlesnaps(self.snapnames)
if __name__ == "__main__":
def runwebcam():
w = Webcam()
s = Sensors.GPIOSensor()
s.simple_loop(w.webcam_looponce)
def runwebserver():
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
port = 8002
httpd = SocketServer.TCPServer(("", port), handler)
print "serving at port " + str(port)
httpd.serve_forever()
import multiprocessing as mp
p1 = mp.Process(target=runwebcam)
p2 = mp.Process(target=runwebserver)
p2.start()
p1.start()
p2.join()
p1.join()