Пример #1
0
# Read config form files
config = ConfigParser.RawConfigParser()
config.read(join(ROOT_DIRECTORY, 'config', 'file_format.cfg'))
labelDirectory = join(ROOT_DIRECTORY, config.get('Directory', 'Label'))
videoDirectory = join(ROOT_DIRECTORY, config.get('Directory', 'Video'))
skeletonDirectory = join(ROOT_DIRECTORY, config.get('Directory', 'Skeleton'))
resultDirectory = join(ROOT_DIRECTORY, config.get('Directory', 'Result'))
videoFilenameExtension = config.get('File', 'Video Extension')
skeletonFilenameExtension = config.get('File', 'Skeleton Extension')
skeletonVideoSuffix = config.get('File', 'Skeleton Suffix')
depthVideoSuffix = config.get('File', 'Depth Video Suffix')
colorVideoSuffix = config.get('File', 'Color Video Suffix')

firstFile = ( f for f in listdir(labelDirectory) if isfile(join(labelDirectory,f)) & f.endswith('.csv') ).next()

labelFrameData = CSVFrameData()
labelFrameData.load(join(labelDirectory, firstFile))

firstFile = firstFile[:-4]

skeletonFrameData = CSVFrameData()
skeletonFrameData.load(join(skeletonDirectory, firstFile+skeletonVideoSuffix+skeletonFilenameExtension))

srcVideoPath = join(videoDirectory,firstFile+depthVideoSuffix+videoFilenameExtension)
depthFrameData = VideoFrameData()
depthFrameData.load(srcVideoPath)

srcVideoPath = join(videoDirectory,firstFile+colorVideoSuffix+videoFilenameExtension)
colorFrameData = VideoFrameData()
colorFrameData.load(srcVideoPath)
Пример #2
0
def readVideo(fileName, frameCallback, result):
    frameConverter = FrameConverter()

    labelFrameData = CSVFrameData()
    labelFrameData.load(join(labelDirectory, fileName))
    labelFrameData.setDebug(DEBUG_MODE)

    fileName = fileName[:-4]

    skeletonFrameData = CSVFrameData()
    skeletonFrameData.load(
        join(skeletonDirectory,
             fileName + skeletonVideoSuffix + skeletonFilenameExtension))
    skeletonFrameData.setDebug(DEBUG_MODE)

    srcVideoPath = join(videoDirectory,
                        fileName + depthVideoSuffix + videoFilenameExtension)
    depthFrameData = VideoFrameData()
    depthFrameData.load(srcVideoPath)

    i = 0
    resultImages = []

    depthRetval, depthFrame = depthFrameData.readFrame()
    labelRetval, labelFrame = labelFrameData.readFrame()
    skeletonRetval, skeletonFrame = skeletonFrameData.readFrame()
    if not depthRetval or not labelRetval or not skeletonRetval:
        return

    h, w = depthFrame.shape[0:2]
    encodedFrame = frameConverter.encode(depthFrame, labelFrame, skeletonFrame)

    if SAVE_RESULT_VIDEO:
        videoPath = join(resultDirectory,
                         'croppedHand-' + fileName + videoFilenameExtension)
        videoWriter = cv2.VideoWriter()
        fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
        videoWriter.open(videoPath, fourcc, 30, (w * 2, h))
    else:
        videoWriter = None

    while (labelRetval and depthRetval and skeletonRetval):
        encodedFrame = frameConverter.encode(depthFrame, labelFrame,
                                             skeletonFrame)
        res, resultImage = frameCallback(depthFrame, labelFrame, videoWriter)
        resultImages.append(resultImage)

        if VISUALIZE_RESULT:
            if cv2.waitKey(1) & 0xFF == ord('q'):
                depthRetval = False
                continue

        depthRetval, depthFrame = depthFrameData.readFrame()
        labelRetval, labelFrame = labelFrameData.readFrame()
        skeletonRetval, skeletonFrame = skeletonFrameData.readFrame()
    depthFrameData.close()
    labelFrameData.close()
    skeletonFrameData.close()

    if SAVE_RESULT_VIDEO & (videoWriter is not None):
        message = "Saving Video..."
        savingImage = np.zeros((h, w * 2, 3), np.uint8)
        savingImageTextSize, _ = cv2.getTextSize(message,
                                                 cv2.FONT_HERSHEY_SIMPLEX, 1,
                                                 2)
        savingImageTextLocation = (w - savingImageTextSize[0] / 2,
                                   h / 2 - savingImageTextSize[1] / 2)
        for i in xrange(0, len(resultImages)):
            savingImage[:] = 0
            cv2.rectangle(
                savingImage, (0, savingImageTextLocation[1]),
                (i * w * 2 / len(resultImages),
                 savingImageTextLocation[1] + savingImageTextSize[1]),
                (0, 255, 0), cv2.cv.CV_FILLED)
            cv2.putText(savingImage, message, savingImageTextLocation,
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
                        cv2.CV_AA)
            cv2.imshow('Depth Video', savingImage)
            videoWriter.write(resultImages[i])
Пример #3
0
def readVideo(fileName, frameCallback, result):
    frameConverter = FrameConverter()

    labelFrameData = CSVFrameData()
    labelFrameData.load(join(labelDirectory, fileName))
    labelFrameData.setDebug(DEBUG_MODE)

    fileName = fileName[:-4]

    skeletonFrameData = CSVFrameData()
    skeletonFrameData.load(join(skeletonDirectory, fileName+skeletonVideoSuffix+skeletonFilenameExtension))
    skeletonFrameData.setDebug(DEBUG_MODE)

    srcVideoPath = join(videoDirectory,fileName+depthVideoSuffix+videoFilenameExtension)
    depthFrameData = VideoFrameData()
    depthFrameData.load(srcVideoPath)

    i = 0
    resultImages = []

    depthRetval,depthFrame = depthFrameData.readFrame()
    labelRetval,labelFrame = labelFrameData.readFrame()
    skeletonRetval,skeletonFrame = skeletonFrameData.readFrame()
    if not depthRetval or not labelRetval or not skeletonRetval:
        return

    h,w = depthFrame.shape[0:2]
    encodedFrame = frameConverter.encode(depthFrame, labelFrame, skeletonFrame)

    if SAVE_RESULT_VIDEO:
        videoPath = join(resultDirectory, 'croppedHand-'+fileName+videoFilenameExtension)
        videoWriter = cv2.VideoWriter()
        fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
        videoWriter.open(videoPath, fourcc, 30, (w*2,h))
    else:
        videoWriter = None

    while(labelRetval and depthRetval and skeletonRetval):
        encodedFrame = frameConverter.encode(depthFrame, labelFrame, skeletonFrame)
        res,resultImage = frameCallback(depthFrame, labelFrame, videoWriter)
        resultImages.append(resultImage)

        if VISUALIZE_RESULT:
            if cv2.waitKey(1) & 0xFF == ord('q'):
                depthRetval = False
                continue

        depthRetval,depthFrame = depthFrameData.readFrame()
        labelRetval,labelFrame = labelFrameData.readFrame()
        skeletonRetval,skeletonFrame = skeletonFrameData.readFrame()
    depthFrameData.close()
    labelFrameData.close()
    skeletonFrameData.close()

    if SAVE_RESULT_VIDEO & (videoWriter is not None):
        message = "Saving Video..."
        savingImage = np.zeros((h,w*2,3), np.uint8)
        savingImageTextSize, _ = cv2.getTextSize(message, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
        savingImageTextLocation = (w-savingImageTextSize[0]/2, h/2-savingImageTextSize[1]/2)
        for i in xrange(0,len(resultImages)):
            savingImage[:] = 0
            cv2.rectangle(savingImage, (0,savingImageTextLocation[1]), (i*w*2/len(resultImages),savingImageTextLocation[1]+savingImageTextSize[1]), (0,255,0), cv2.cv.CV_FILLED)
            cv2.putText(savingImage, message, savingImageTextLocation, cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.CV_AA)
            cv2.imshow('Depth Video', savingImage)
            videoWriter.write(resultImages[i])