Ejemplo n.º 1
0
    def generateThumbnail(original_resource):
        thumbnailDir = settings.MEDIA_ROOT + 'resource_thumbnail/tmp/'
        thumbnail = thumbnailDir + str(original_resource.id)

        (resource, resource_type) = ResourceThumbnail.get_resource_type(original_resource)

        import urllib

        if resource_type == "video":

            try:
                provider = VideoHelper.getVideoProvider(resource.url)
                video_tag = VideoHelper.getVideoID(resource.url, provider)

                thumbnailUrl = ResourceThumbnail.getThumbnailFromProvider(video_tag, provider)

                # Returns a 120x90 image
                urllib.urlretrieve(thumbnailUrl, thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT)

                call(
                    ["convert", "-size", str(ResourceThumbnail.THUMBNAIL_HEIGHT) + "x"
                        + str(ResourceThumbnail.THUMBNAIL_WIDTH), "xc:white", thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

                call(
                    ["composite", "-geometry", "-30-15", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT,
                        thumbnail + ResourceThumbnail.THUMBNAIL_EXT, thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

                # Now delete the temporary retrived image thumbnail
                call(["rm", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT])

            except:
                resource.type = 'url'
                ResourceThumbnail.generateURLThumbnail(resource, thumbnail)

        elif resource_type == "article":
            ResourceThumbnail.generate_thumbnail(resource)

        elif resource_type == "url":
            ResourceThumbnail.generateURLThumbnail(resource, thumbnail)

        elif resource_type == "attachment":

            # Figure out the extension of the attachment
            from os.path import splitext
            name, resource.extension = splitext(resource.file.name)

            ms = [".doc", ".docx", ".ppt", ".pptx", ".rtf", "odt", ".odp"]
            pdf = [".pdf"]
            image = [".jpg", ".jpeg", ".png", ".bmp", ".eps", ".ps", ".gif", ".tiff"]

            ext = str.lower(str(resource.extension))

            if ext in ms:
                thumbnailSrcName = "ms.jpg"
            elif ext in pdf:
                if not settings.DEBUG:
                    urllib.urlretrieve(settings.MEDIA_URL + resource.revision.content.file.name,
                        settings.MEDIA_ROOT + resource.revision.content.file.name)

                call(
                    ["convert", "-density", "300",
                    settings.MEDIA_ROOT + resource.revision.content.file.name + '[0]',
                    thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT])

                call(
                    ["convert", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT,
                        '-resize', '30%', '-gravity', 'center', '-crop', '120x120+0+0',
                        thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

                # Now delete the temporary retrived image thumbnail
                call(["rm", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT])

                if not settings.DEBUG:
                    import os
                    os.remove(settings.MEDIA_ROOT + resource.revision.content.file.name)

                thumbnailSrcName = "pdf.jpg"
            elif ext in image:
                thumbnailSrcName = "image.jpg"
            else:
                thumbnailSrcName = "blank.jpg"

            if ext not in pdf:
                call(
                    ["cp", settings.MEDIA_ROOT + 'resource_thumbnail/defaults/' + thumbnailSrcName,
                        thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

        from django.core.files.images import ImageFile
        image_file = open(thumbnail + ResourceThumbnail.THUMBNAIL_EXT)
        thumbnail_to_assign = ImageFile(image_file)

        # NOTE(Varun): It's really important to keep the save as false, to avoid
        #     a recursion here
        resource.image.save(
            thumbnail_to_assign.name, thumbnail_to_assign, save=False)

        image_file.close()

        # Now delete the temporary image thumbnail
        call(["rm", thumbnail + ResourceThumbnail.THUMBNAIL_EXT])
Ejemplo n.º 2
0
framespre=40
framespost=20
fps=30

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
#ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())

                   
# open the video file
cap = cv2.VideoCapture(args["video"])

# get the frame size
frameExtractor = VideoHelper()
size = frameExtractor.getFrameSize(args["video"])
xproc = size[0]  # x,y resolution for processing is taken from the video file
yproc = size[1] 
record = True  # should we record video output?
voname = 'track-out1.avi' # name of video output to save
vonamerep = 'track-out2.avi'  # color track with circle

if (record):
    fourcc_video = cv2.VideoWriter_fourcc(*'XVID')
    video = cv2.VideoWriter(voname, fourcc_video, fps, (xproc,yproc))  # open output video to record
    fourcc_videorep = cv2.VideoWriter_fourcc(*'XVID')
    videorep = cv2.VideoWriter(vonamerep, fourcc_videorep, fps, (xproc,yproc))  # open output video to record

history = 5
varThreshold = 18