Пример #1
0
def IterationStandaloneMSH(index):
    #capture = g.capture;
    harlocsFolder = g.harlocsFolder
    fileNamePrefix = g.fileNamePrefix
    fileNameExtension = g.fileNameExtension

    common.DebugPrint("Entered IterationStandaloneMSH(index=%d)" % index)
    #img = MyImageReadMSH(capture, index);
    img = MyImageReadMSH(index)

    im = img
    pp = multi_scale_harris.multi_scale_harris(im, nos, disp=0)
    # n=0:nos-1

    if False:
        #harlocs = pp
        harlocs.append(pp)

    multi_scale_harris.StoreMultiScaleHarrisFeatures( \
            harlocsFolder + "/" + fileNamePrefix + "%05d%s" % \
                                (index, fileNameExtension),
            pp)
    if False:
        counter += counterStep
        # If we try to seek to a frame out-of-bounds frame it gets to the last one
        if config.OCV_OLD_PY_BINDINGS:
            capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, counter)
        else:
            capture.set(cv2.CAP_PROP_POS_FRAMES, counter)

    return 1
Пример #2
0
def IterationStandaloneMSH(index):
    #capture = g.capture;
    harlocsFolder = g.harlocsFolder;
    fileNamePrefix = g.fileNamePrefix;
    fileNameExtension = g.fileNameExtension;

    common.DebugPrint("Entered IterationStandaloneMSH(index=%d)" % index);
    #img = MyImageReadMSH(capture, index);
    img = MyImageReadMSH(index);

    im = img;
    pp = multi_scale_harris.multi_scale_harris(im, nos, disp=0); # n=0:nos-1

    if False:
        #harlocs = pp
        harlocs.append(pp);

    multi_scale_harris.StoreMultiScaleHarrisFeatures( \
            harlocsFolder + "/" + fileNamePrefix + "%05d%s" % \
                                (index, fileNameExtension),
            pp);
    if False:
        counter += counterStep;
        # If we try to seek to a frame out-of-bounds frame it gets to the last one
        if config.OCV_OLD_PY_BINDINGS:
            capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, counter);
        else:
            capture.set(cv2.CAP_PROP_POS_FRAMES, counter);

    return 1;
Пример #3
0
def ComputeHarlocs(capture,
                   counterStep,
                   folderName,
                   fileNamePrefix,
                   fileNameExtension=".csv",
                   indexVideo=-1):
    print( \
            "Entered ComputeHarlocs(capture=%s, counterStep=%d, folderName=%s, " \
                                     "indexVideo=%d)" % \
                            (str(capture), counterStep, folderName, indexVideo))

    harlocsFolder = config.VIDEOS_FOLDER + "/" + folderName

    t1 = float(cv2.getTickCount())

    harlocs = []

    if config.OCV_OLD_PY_BINDINGS:
        numFrames = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    else:
        numFrames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    common.DebugPrint("ComputeHarlocs(): numFrames = %d" % numFrames)

    if not os.path.exists(harlocsFolder):
        os.makedirs(harlocsFolder)
    else:
        #!!!!TODO: check that the loaded Harlocs are complete - same frame numbers as in the videos
        # Folder with precomputed Harris features exists
        folderContent = os.listdir(harlocsFolder)
        sortedFolderContent = sorted(folderContent)

        for fileName in sortedFolderContent:
            pathFileName = harlocsFolder + "/" + fileName
            """
            common.DebugPrint("ComputeHarlocs(): pathFileName = %s" % pathFileName);
            common.DebugPrint("ComputeHarlocs(): fileName = %s" % fileName);
            """
            if os.path.isfile(pathFileName) and \
                            fileName.startswith(fileNamePrefix) and \
                            pathFileName.endswith(fileNameExtension):
                common.DebugPrint("ComputeHarlocs(): Loading %s" %
                                  pathFileName)
                harrisFeatures = multi_scale_harris.LoadMultiScaleHarrisFeatures(
                    pathFileName)
                harlocs.append(harrisFeatures)

        if config.endFrame[indexVideo] == -1:
            assert (len(harlocs) + config.initFrame[indexVideo]) == numFrames
            #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs
        else:
            assert (len(harlocs) + config.initFrame[indexVideo]
                    ) == config.endFrame[indexVideo] + 1
            #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs

        return harlocs

    if config.USE_MULTITHREADING == True:
        global g
        g.captureQ = None
        # We need to reopen the capture device in each process, separately
        g.captureR = None
        # We need to reopen the capture device in each process, separately
        #g.capture = capture;

        g.harlocsFolder = harlocsFolder
        g.fileNamePrefix = fileNamePrefix
        g.fileNameExtension = fileNameExtension
        g.indexVideo = indexVideo

        if config.OCV_OLD_PY_BINDINGS:
            frameCount = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
        else:
            frameCount = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
        #listParams = range(frameCount);
        listParams = range(config.initFrame[indexVideo], frameCount,
                           counterStep)

        common.DebugPrint("ComputeHarlocs(): frameCount = %d" % frameCount)

        print("ComputeHarlocs(): Spawning a pool of %d workers" % \
                                config.numProcesses)
        """
        # DEBUG purposes ONLY - since when we use Pool() and call function, if
        #   we have an error in the function the exception reported is very
        #   vague...
        for i in listParams:
            IterationStandaloneMSH(i);
        #import time
        #time.sleep(1000);
        """
        """
        Start worker processes to use on multi-core processor (circumvent
          also the GIL issue).
        """
        pool = multiprocessing.Pool(processes=config.numProcesses)
        print("ComputeHarlocs(): Spawned a pool of %d workers" % \
                                config.numProcesses)

        #res = pool.map(IterationStandaloneMSH, listParams);
        # See https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool
        res = pool.map(func=IterationStandaloneMSH, iterable=listParams, \
                        chunksize=1)
        print("Pool.map returns %s" % str(res))
        """
        From https://medium.com/building-things-on-the-internet/40e9b2b36148
         close the pool and wait for the work to finish
        """
        pool.close()
        pool.join()

        #!!!!TODO: do more efficient - don't load the results from the CSV files
        return ComputeHarlocs(capture, counterStep, folderName, \
                              fileNamePrefix, fileNameExtension, indexVideo)
        #return [];

    #indexHarloc = 0;
    while True:
        if config.OCV_OLD_PY_BINDINGS:
            framePos = capture.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
        else:
            """
            From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get:
                <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>>
            """
            framePos = capture.get(cv2.CAP_PROP_POS_FRAMES)
        common.DebugPrint("ComputeHarlocs(): framePos = %d" % framePos)

        counter = int(framePos)
        #0
        common.DebugPrint("ComputeHarlocs(): counter = %d" % counter)

        ret, img = capture.read()

        if common.MY_DEBUG_STDOUT:
            common.DebugPrint("ComputeHarlocs(): img = %s" % str(img))

        if False and config.SAVE_FRAMES:
            fileName = config.IMAGES_FOLDER + "/img_%05d.png" % counter
            if not os.path.exists(fileName):
                #print "dir(img) = %s"% str(dir(img))
                """
                imgCV = cv.fromarray(img)
                cv2.imwrite(fileName, imgCV)
                """
                cv2.imwrite(fileName, img)

        #if ret == False: #MatchFrames.counterQ == 3:
        if (ret == False) or ((counter > numFrames) or \
                             (config.endFrame[indexVideo] != -1 and \
                              counter > config.endFrame[indexVideo])):
            break

        if config.VIDEO_FRAME_RESIZE_SCALING_FACTOR != 1:
            img = Matlab.imresize(img, \
                            scale=config.VIDEO_FRAME_RESIZE_SCALING_FACTOR)

        im = img
        pp = multi_scale_harris.multi_scale_harris(im, nos, disp=0)
        # n=0:nos-1

        #harlocs = pp
        harlocs.append(pp)

        multi_scale_harris.StoreMultiScaleHarrisFeatures( \
                harlocsFolder + "/" + fileNamePrefix + "%05d%s" % \
                                    (counter, fileNameExtension),
                pp)

        counter += counterStep
        # If we try to seek to a frame out-of-bounds frame it gets to the last one
        if config.OCV_OLD_PY_BINDINGS:
            capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, counter)
        else:
            capture.set(cv2.CAP_PROP_POS_FRAMES, counter)

        #indexHarloc += 1;

    t2 = float(cv2.getTickCount())
    myTime = (t2 - t1) / cv2.getTickFrequency()
    common.DebugPrint("ComputeHarlocs(): computing the multiscale harlocs " \
                        "took %.6f [sec]" % myTime)

    #common.DebugPrint("ComputeHarlocs(): len(harlocs) = %s" % str(len(harlocs)));

    if False:
        for i, h in enumerate(harlocs):
            #common.DebugPrint("ComputeHarlocs(): len(harlocs[%d]) = %d" % \
            #                                                        (i, len(h)));
            multi_scale_harris.StoreMultiScaleHarrisFeatures(
                harlocsFolder + "/" + fileNamePrefix + "%05d.txt" % i, h)

    #if False:
    if common.MY_DEBUG_STDOUT:
        common.DebugPrint("ComputeHarlocs(): harlocs = %s" % str(harlocs))

    return harlocs
Пример #4
0
def ComputeHarlocs(capture, counterStep, folderName, fileNamePrefix,
                    fileNameExtension=".csv", indexVideo=-1):
    print( \
            "Entered ComputeHarlocs(capture=%s, counterStep=%d, folderName=%s, " \
                                     "indexVideo=%d)" % \
                            (str(capture), counterStep, folderName, indexVideo));

    harlocsFolder = config.VIDEOS_FOLDER + "/" + folderName;

    t1 = float(cv2.getTickCount());

    harlocs = [];

    if config.OCV_OLD_PY_BINDINGS:
        numFrames = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT));
    else:
        numFrames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT));
    common.DebugPrint("ComputeHarlocs(): numFrames = %d" % numFrames);

    if not os.path.exists(harlocsFolder):
        os.makedirs(harlocsFolder);
    else:
        #!!!!TODO: check that the loaded Harlocs are complete - same frame numbers as in the videos
        # Folder with precomputed Harris features exists
        folderContent = os.listdir(harlocsFolder);
        sortedFolderContent = sorted(folderContent);

        for fileName in sortedFolderContent:
            pathFileName = harlocsFolder + "/" + fileName;
            """
            common.DebugPrint("ComputeHarlocs(): pathFileName = %s" % pathFileName);
            common.DebugPrint("ComputeHarlocs(): fileName = %s" % fileName);
            """
            if os.path.isfile(pathFileName) and \
                            fileName.startswith(fileNamePrefix) and \
                            pathFileName.endswith(fileNameExtension):
                common.DebugPrint("ComputeHarlocs(): Loading %s" % pathFileName);
                harrisFeatures = multi_scale_harris.LoadMultiScaleHarrisFeatures(pathFileName);
                harlocs.append(harrisFeatures);

        if config.endFrame[indexVideo] == -1:
            assert (len(harlocs) + config.initFrame[indexVideo]) == numFrames; #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs
        else:
            assert (len(harlocs) + config.initFrame[indexVideo]) == config.endFrame[indexVideo] + 1; #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs

        return harlocs;


    if config.USE_MULTITHREADING == True:
        global g;
        g.captureQ = None; # We need to reopen the capture device in each process, separately
        g.captureR = None; # We need to reopen the capture device in each process, separately
        #g.capture = capture;

        g.harlocsFolder = harlocsFolder;
        g.fileNamePrefix = fileNamePrefix;
        g.fileNameExtension = fileNameExtension;
        g.indexVideo = indexVideo;

        if config.OCV_OLD_PY_BINDINGS:
            frameCount = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT));
        else:
            frameCount = int(capture.get(cv2.CAP_PROP_FRAME_COUNT));
        #listParams = range(frameCount);
        listParams = range(config.initFrame[indexVideo], frameCount, counterStep);

        common.DebugPrint("ComputeHarlocs(): frameCount = %d" % frameCount);

        print("ComputeHarlocs(): Spawning a pool of %d workers" % \
                                config.numProcesses);

        """
        # DEBUG purposes ONLY - since when we use Pool() and call function, if
        #   we have an error in the function the exception reported is very
        #   vague...
        for i in listParams:
            IterationStandaloneMSH(i);
        #import time
        #time.sleep(1000);
        """

        """
        Start worker processes to use on multi-core processor (circumvent
          also the GIL issue).
        """
        pool = multiprocessing.Pool(processes=config.numProcesses);
        print("ComputeHarlocs(): Spawned a pool of %d workers" % \
                                config.numProcesses);

        #res = pool.map(IterationStandaloneMSH, listParams);
        # See https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool
        res = pool.map(func=IterationStandaloneMSH, iterable=listParams, \
                        chunksize=1);
        print("Pool.map returns %s" % str(res));

        """
        From https://medium.com/building-things-on-the-internet/40e9b2b36148
         close the pool and wait for the work to finish
        """
        pool.close();
        pool.join();

        #!!!!TODO: do more efficient - don't load the results from the CSV files
        return ComputeHarlocs(capture, counterStep, folderName, \
                              fileNamePrefix, fileNameExtension, indexVideo);
        #return [];

    #indexHarloc = 0;
    while True:
        if config.OCV_OLD_PY_BINDINGS:
            framePos = capture.get(cv2.cv.CV_CAP_PROP_POS_FRAMES);
        else:
            """
            From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get:
                <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>>
            """
            framePos = capture.get(cv2.CAP_PROP_POS_FRAMES);
        common.DebugPrint("ComputeHarlocs(): framePos = %d" % framePos);

        counter = int(framePos); #0
        common.DebugPrint("ComputeHarlocs(): counter = %d" % counter);

        ret, img = capture.read();

        if common.MY_DEBUG_STDOUT:
            common.DebugPrint("ComputeHarlocs(): img = %s" % str(img));

        if False and config.SAVE_FRAMES:
            fileName = config.IMAGES_FOLDER + "/img_%05d.png" % counter;
            if not os.path.exists(fileName):
                #print "dir(img) = %s"% str(dir(img))
                """
                imgCV = cv.fromarray(img)
                cv2.imwrite(fileName, imgCV)
                """
                cv2.imwrite(fileName, img);

        #if ret == False: #MatchFrames.counterQ == 3:
        if (ret == False) or ((counter > numFrames) or \
                             (config.endFrame[indexVideo] != -1 and \
                              counter > config.endFrame[indexVideo])):
            break;

        if config.VIDEO_FRAME_RESIZE_SCALING_FACTOR != 1:
            img = Matlab.imresize(img, \
                            scale=config.VIDEO_FRAME_RESIZE_SCALING_FACTOR);

        im = img;
        pp = multi_scale_harris.multi_scale_harris(im, nos, disp=0); # n=0:nos-1

        #harlocs = pp
        harlocs.append(pp);

        multi_scale_harris.StoreMultiScaleHarrisFeatures( \
                harlocsFolder + "/" + fileNamePrefix + "%05d%s" % \
                                    (counter, fileNameExtension),
                pp);

        counter += counterStep;
        # If we try to seek to a frame out-of-bounds frame it gets to the last one
        if config.OCV_OLD_PY_BINDINGS:
            capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, counter);
        else:
            capture.set(cv2.CAP_PROP_POS_FRAMES, counter);

        #indexHarloc += 1;

    t2 = float(cv2.getTickCount());
    myTime = (t2 - t1) / cv2.getTickFrequency();
    common.DebugPrint("ComputeHarlocs(): computing the multiscale harlocs " \
                        "took %.6f [sec]" % myTime);

    #common.DebugPrint("ComputeHarlocs(): len(harlocs) = %s" % str(len(harlocs)));

    if False:
        for i, h in enumerate(harlocs):
            #common.DebugPrint("ComputeHarlocs(): len(harlocs[%d]) = %d" % \
            #                                                        (i, len(h)));
            multi_scale_harris.StoreMultiScaleHarrisFeatures(
                    harlocsFolder + "/" + fileNamePrefix + "%05d.txt" % i, h);

    #if False:
    if common.MY_DEBUG_STDOUT:
        common.DebugPrint("ComputeHarlocs(): harlocs = %s" % str(harlocs));

    return harlocs;