webcam upload to ftp

Python

Public Domain

Get's an image from any opencv-supported webcam and uploads it to an ftp site

Download (right click, save as, rename as appropriate)

Embed

 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from datetime import datetime, date, time
import time
from time import strftime
import Image
import ImageDraw
import ImageFont
import ImageFilter
import math
import opencv
import sys
import os
from opencv import highgui 
from opencv import cvGetMat
from opencv.adaptors import Ipl2PIL
from ftplib import FTP

import psyco; psyco.full()

ftpSite = "ftp.example.com"
ftpDirectory = "/directory/subdirectory"
ftpUsername = "username"
ftpPassword = "password"
uploadInterval = 300
filenamePrefix = "myProject"
srcImageWidth = 640
srcImageHeight = 480
imageWidth = 600
imageHeight = 450

# If you want to use another font, drop it next to webcam.py and change/uncomment here
# font = ImageFont.truetype("VeraMono.ttf", 12, encoding="unic")

class WebcamControl:
    def get_image(self, camera):
        im = highgui.cvQueryFrame(camera)
        im = opencv.cvGetMat(im)
        return opencv.adaptors.Ipl2PIL(im) 

    def startWebcam(self):
        camera = highgui.cvCreateCameraCapture(0)
        highgui.cvSetCaptureProperty(camera, highgui.CV_CAP_PROP_FRAME_WIDTH, srcImageWidth);
        highgui.cvSetCaptureProperty(camera, highgui.CV_CAP_PROP_FRAME_HEIGHT, srcImageHeight);
        filename = filenamePrefix + "_" + strftime("%Y%m%d-%H%M%S") + ".jpg"

        # This needs to be done, otherwise the first image will be all black.
        # Webcams need to be initialized, I suppose. Don't use crappy microsoft cams, kids.
        self.get_image(camera).save(filename, 'JPEG')
        time.sleep(10)
        os.remove(filename)
        
        while(True):
            try:
                filename = filenamePrefix + "_" + strftime("%Y%m%d-%H%M%S") + ".jpg"
                myimage = self.get_image(camera)
                myimage = myimage.resize((imageWidth, imageHeight), Image.ANTIALIAS)
                draw = ImageDraw.Draw(myimage)
                sizex, sizey = draw.textsize(strftime("%d.%m.%Y %H:%M:%S"), font=font)
                draw.rectangle([5, (imageHeight - 5) - sizey, sizex + 15, 445], fill=(0,0,0))
                draw.text((10, (imageHeight - 5) - sizey), strftime("%d.%m.%Y %H:%M:%S"), font=font)
                myimage.save(filename, 'JPEG', quality=90, optimize=False)
                ftp = FTP(ftpSite)
                ftp.set_debuglevel(0)
                ftp.set_pasv(False)
                ftp.login(ftpUsername, ftpPassword)
                ftp.cwd(ftpDirectory) 
                f = open(filename, 'rb')
                print datetime.now(), ": Uploading " + filename
                ftp.storbinary("STOR " + filename, f)
                f.close()
                os.remove(filename)
                ftp.quit()
                print datetime.now(), ": Waiting", uploadInterval ," seconds..."
                for i in xrange(uploadInterval):
                    time.sleep(1) # Yeah, I know...
                print "-"
            except:
                exit()

if __name__ == '__main__':
    wbc = WebcamControl()
    wbc.startWebcam()