Browse files

Added directory functionality to VirtualCamera

  • Loading branch information...
1 parent 0f3088a commit da0a59a249c8a6c012180201372f457be38f4caa @xamox xamox committed Sep 23, 2012
Showing with 53 additions and 1 deletion.
  1. +53 −1 SimpleCV/Camera.py
View
54 SimpleCV/Camera.py
@@ -603,6 +603,7 @@ class VirtualCamera(FrameSource):
* For image, pass the filename or URL to the image
* For the video, the filename
* For imageset, you can pass either a path or a list of [path, extension]
+ * For directory you treat a directory to show the latest file, an example would be where a security camera logs images to the directory, calling .getImage() will get the latest in the directory
"""
source = ""
@@ -623,13 +624,16 @@ def __init__(self, s, st, start=1):
* "image" - a single still image.
* "video" - a video file.
* "imageset" - a SimpleCV image set.
+ * "directory" - a VirtualCamera for loading a directory
**EXAMPLE**
>>> vc = VirtualCamera("img.jpg", "image")
>>> vc = VirtualCamera("video.mpg", "video")
>>> vc = VirtualCamera("./path_to_images/", "imageset")
>>> vc = VirtualCamera("video.mpg", "video", 300)
+ >>> vc = VirtualCamera("./imgs", "directory")
+
"""
self.source = s
@@ -639,11 +643,12 @@ def __init__(self, s, st, start=1):
start=1
self.start = start
- if not (self.sourcetype == "video" or self.sourcetype == "image" or self.sourcetype == "imageset"):
+ if not (self.sourcetype == "video" or self.sourcetype == "image" or self.sourcetype == "imageset" or self.sourcetype == "directory"):
print 'Error: In VirtualCamera(), Incorrect Source option. "%s" \nUsage:' % self.sourcetype
print '\tVirtualCamera("filename","video")'
print '\tVirtualCamera("filename","image")'
print '\tVirtualCamera("./path_to_images","imageset")'
+ print '\tVirtualCamera("./path_to_images","directory")'
return None
if (type(self.source) == list):
@@ -666,6 +671,9 @@ def __init__(self, s, st, start=1):
if (self.sourcetype == 'video'):
self.capture = cv.CaptureFromFile(self.source)
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_POS_FRAMES, self.start-1)
+
+ if (self.sourcetype == 'directory'):
+ pass
def getImage(self):
@@ -694,6 +702,10 @@ def getImage(self):
if (self.sourcetype == 'video'):
return Image(cv.QueryFrame(self.capture), self)
+
+ if (self.sourcetype == 'directory'):
+ img = self.findLastestImage(self.source, 'bmp')
+ return Image(img, self)
def rewind(self, start=None):
"""
@@ -814,6 +826,46 @@ def getFrameNumber(self):
if (self.sourcetype == 'video'):
number_frame = int(cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_POS_FRAMES))
return number_frame
+
+ def findLastestImage(self, directory='.', extension='png'):
+ """
+ **SUMMARY**
+
+ This function finds the latest file in a directory
+ with a given extension.
+
+ **PARAMETERS**
+
+ directory - The directory you want to load images from (defaults to current directory)
+ extension - The image extension you want to use (defaults to .png)
+
+ **RETURNS**
+
+ The filename of the latest image
+
+ **USAGE**
+
+ >>> cam = VirtualCamera('imgs/', 'png') #find all .png files in 'img' directory
+ >>> cam.getImage() # Grab the latest image from that directory
+
+ """
+ max_mtime = 0
+ max_dir = None
+ max_file = None
+ max_full_path = None
+ for dirname,subdirs,files in os.walk(directory):
+ for fname in files:
+ if fname.split('.')[-1] == extension:
+ full_path = os.path.join(dirname, fname)
+ mtime = os.stat(full_path).st_mtime
+ if mtime > max_mtime:
+ max_mtime = mtime
+ max_dir = dirname
+ max_file = fname
+ max_full_path = os.path.abspath(os.path.join(dirname, fname))
+
+
+ return max_full_path
class Kinect(FrameSource):
"""

0 comments on commit da0a59a

Please sign in to comment.