Esempio n. 1
0
def getDecisionFunctionsForWindows(checkerFile,
                                   imageDirs,
                                   classifier,
                                   thresh,
                                   hog=True,
                                   subwindow=False):
    checker = detection_checker.Checker(checkerFile)
    fileLists = checker.getFileList()
    metadata = utils.parseMetadata(*imageDirs)

    trueClasses = []
    scores = numpy.array([])

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    index = 0
    for imgPath in fileLists[:10]:
        print index
        index += 1
        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        boundingRect = None
        if subwindow:
            boundingRect = optical_flow.optical_flow(
                imgPath, utils.get_prev_img(imgPath))

        if hog:
            wf = tester_hog.getWindowsAndDescriptors(imgPath,
                                                     imgScales,
                                                     subwindow=boundingRect)
        else:
            wf = cascade_tester.get_images(classifier,
                                           imgPath,
                                           imgScales,
                                           subwindow=boundingRect)

        tc = checker.getWindowsClasses(imgPath, [w[0] for w in wf])

        df = computeClassifierDecisions(wf, classifier, thresh, hog=hog)

        print df

        trueClasses = trueClasses + tc
        scores = numpy.concatenate((scores, df))

    return trueClasses, scores
Esempio n. 2
0
def test_cascade(classifier_file, icf_result_dir, checker, resultsFile):

    cascade_detector = classifier.loadCascadeClassifier(classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    resultsIcf = open(resultsFile + "cascade.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        before = time.time()
        pos_windows = test_img(cascade_detector,
                               imgPath,
                               imgScales,
                               subwindow=boundingRect)
        after = time.time()

        print "Sample", sample, "time elapsed=", after - before

        if pos_windows != None and pos_windows != []:
            utils.draw_detections(img, pos_windows)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(
            imgPath, pos_windows)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" +
                         str(falsePositives) + " fn=" + str(falseNegative) +
                         "\n")

        sample += 1

    resultsIcf.close()
Esempio n. 3
0
def test_cascade(
        classifier_file,
        icf_result_dir,
        checker,
        resultsFile):

    cascade_detector = classifier.loadCascadeClassifier(classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    resultsIcf = open(resultsFile + "cascade.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        before = time.time()
        pos_windows = test_img(cascade_detector, imgPath, imgScales, subwindow=boundingRect)
        after = time.time()

        print "Sample", sample, "time elapsed=", after-before

        if pos_windows != None and pos_windows != []:
            utils.draw_detections(img, pos_windows)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(imgPath, pos_windows)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        sample += 1

    resultsIcf.close()
Esempio n. 4
0
def test_multiscale_checker(hog_classifier_file, icf_classifier_file,
                            hog_result_dir, icf_result_dir, checker,
                            resultsFile):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    # icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    resultsHog = open(resultsFile + "hog.txt", "w")
    # resultsIcf = open(resultsFile + "icf.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        posWindowsHog = tester_hog.test_img(hog_classifier,
                                            imgPath,
                                            imgScales,
                                            subwindow=boundingRect)
        if posWindowsHog != None and posWindowsHog != []:
            utils.draw_detections(img, posWindowsHog)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(
            imgPath, posWindowsHog)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsHog.write(imgPath + " tp=" + str(truePositives) + " fp=" +
                         str(falsePositives) + " fn=" + str(falseNegative) +
                         "\n")

        # beforeIcf = time.time()
        # windowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect)
        # afterIcf = time.time()
        #
        # print "Sample", sample, "time elapsed=", afterIcf-beforeIcf
        #
        # if windowsIcf != None and windowsIcf != []:
        #     scale = windowsIcf[0][4]
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        #     utils.draw_detections(img_icf, windowsIcf)
        # else:
        #     scale = 0.5
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        # cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
        #
        # # Check detections
        # detections, truePositive = checker.checkDetections(imgPath, windowsIcf)
        # c = Counter(detections)
        # truePositives = c[True]
        # falsePositives = c[False]
        # falseNegative = 0 if truePositive else 1
        # resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        sample += 1
Esempio n. 5
0
def test_multiscale(hog_classifier_file, icf_classifier_file, hog_result_dir,
                    icf_result_dir, no_samples):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*filepaths)

    metadata = utils.parseMetadata(*filepaths)

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    for sample in range(0, no_samples):
        print "### Sample " + str(sample) + " ###"
        imgPath = random.choice(testImages)

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        prev_img_path = utils.get_prev_img(imgPath)
        prev_img = cv2.imread(prev_img_path)

        flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)

        height, width, _ = img.shape

        bestWindowsHog = tester_hog.test_img(hog_classifier,
                                             imgPath,
                                             imgScales,
                                             allPositive=True,
                                             flow_rgb=flow_rgb,
                                             subwindow=boundingRect)
        if bestWindowsHog != None and bestWindowsHog != []:
            scale = bestWindowsHog[0][4]
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_hog.shape[1],
                                                      img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

            utils.draw_detections(img_hog, bestWindowsHog)
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_hog.shape[1],
                                                      img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg",
                    img_hog)

        bestWindowsIcf = tester_icf.test_img(icf_classifier,
                                             imgPath,
                                             imgScales,
                                             allPositive=True,
                                             subwindow=boundingRect)
        if bestWindowsIcf != None and bestWindowsIcf != []:
            scale = bestWindowsIcf[0][4]
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_icf.shape[1],
                                                      img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

            utils.draw_detections(img_icf, bestWindowsIcf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_icf.shape[1],
                                                      img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg",
                    img_icf)
def main(data):
    ## Loop through languages
    trPath = os.path.join(
        data["definitions"]["runtime"]["cwd"],
        data["config"]["Filesystem"]["SourcePathTranslations"],
    )
    trPath = os.path.abspath(trPath)
    languages = sorted(next(os.walk(trPath))[1])
    for languageKey in languages:
        data["translations"][languageKey] = {}
        trLanguagePath = os.path.join(
            trPath,
            languageKey,
        )
        ## Loop through artist groups
        groups = sorted(next(os.walk(trLanguagePath))[1])
        for groupKey in groups:
            data["translations"][languageKey][groupKey] = {}
            trLanguageGroupPath = os.path.join(
                trLanguagePath,
                groupKey,
            )
            ## Loop through artists start begin with
            artists = sorted(next(os.walk(trLanguageGroupPath))[1],
                             key=str.lower)
            for artistKey in artists:
                trLanguageGroupArtistPath = os.path.join(
                    trLanguageGroupPath,
                    artistKey,
                )
                data["translations"][languageKey][groupKey][artistKey] = {
                    "printable_name": artistKey,
                    "releases": [],
                }
                ## Loop through artist’s releases
                releases = sorted(next(os.walk(trLanguageGroupArtistPath))[1],
                                  key=str.lower)
                for releaseKey in releases:
                    trLanguageGroupArtistReleasePath = os.path.join(
                        trLanguageGroupArtistPath,
                        releaseKey,
                    )
                    data["translations"][languageKey][groupKey][artistKey][
                        "releases"].append({
                            "name": releaseKey,
                            "printable_name": releaseKey,
                            "recordings": [[]],
                        })
                    ## Loop through release’s recordings
                    recordings = sorted(next(
                        os.walk(trLanguageGroupArtistReleasePath))[2],
                                        key=str.lower)
                    for recordingKey in recordings:
                        trLanguageGroupArtistReleaseRecordingPath = os.path.join(
                            trLanguageGroupArtistReleasePath,
                            recordingKey,
                        )
                        rawContents = open(
                            trLanguageGroupArtistReleaseRecordingPath,
                            "r").read().strip()
                        rawText = utils.getText(rawContents)
                        rawMetadata = utils.getMetadata(rawContents)
                        data["translations"][languageKey][groupKey][artistKey][
                            "releases"][-1]["recordings"][0].append({
                                "name":
                                recordingKey,
                                "printable_name":
                                recordingKey,
                                "text":
                                rawText,
                                "metadata":
                                utils.parseMetadata(rawMetadata),
                            })
Esempio n. 7
0
def test_multiscale(
        hog_classifier_file,
        icf_classifier_file,
        hog_result_dir,
        icf_result_dir,
        no_samples):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*filepaths)

    metadata = utils.parseMetadata(*filepaths)

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    for sample in range(0, no_samples):
        print "### Sample " + str(sample) + " ###"
        imgPath = random.choice(testImages)

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        prev_img_path = utils.get_prev_img(imgPath)
        prev_img = cv2.imread(prev_img_path)

        flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)

        height, width, _ = img.shape

        bestWindowsHog = tester_hog.test_img(hog_classifier, imgPath, imgScales, allPositive=True, flow_rgb=flow_rgb, subwindow=boundingRect)
        if bestWindowsHog != None and bestWindowsHog != []:
            scale = bestWindowsHog[0][4]
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

            utils.draw_detections(img_hog, bestWindowsHog)
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img_hog)

        bestWindowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect)
        if bestWindowsIcf != None and bestWindowsIcf != []:
            scale = bestWindowsIcf[0][4]
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

            utils.draw_detections(img_icf, bestWindowsIcf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
Esempio n. 8
0
def test_multiscale_checker(
        hog_classifier_file,
        icf_classifier_file,
        hog_result_dir,
        icf_result_dir,
        checker,
        resultsFile):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    # icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    resultsHog = open(resultsFile + "hog.txt", "w")
    # resultsIcf = open(resultsFile + "icf.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        posWindowsHog = tester_hog.test_img(hog_classifier, imgPath, imgScales, subwindow=boundingRect)
        if posWindowsHog != None and posWindowsHog != []:
            utils.draw_detections(img, posWindowsHog)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(imgPath, posWindowsHog)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsHog.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        # beforeIcf = time.time()
        # windowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect)
        # afterIcf = time.time()
        #
        # print "Sample", sample, "time elapsed=", afterIcf-beforeIcf
        #
        # if windowsIcf != None and windowsIcf != []:
        #     scale = windowsIcf[0][4]
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        #     utils.draw_detections(img_icf, windowsIcf)
        # else:
        #     scale = 0.5
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        # cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
        #
        # # Check detections
        # detections, truePositive = checker.checkDetections(imgPath, windowsIcf)
        # c = Counter(detections)
        # truePositives = c[True]
        # falsePositives = c[False]
        # falseNegative = 0 if truePositive else 1
        # resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        sample += 1
Esempio n. 9
0
def test_multiscale(hog_classifier_file, icf_classifier_file, imgpaths,
                    kinect_detections, hog_result_dir, icf_result_dir):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*imgpaths)
    metadata = utils.parseMetadata(*imgpaths)

    kinect_ts = read_kinect_metadata(kinect_detections)

    print metadata
    print kinect_ts

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    for kinectts in kinect_ts:
        sel = min(metadata.items(),
                  key=lambda mt: abs(kinectts - mt[1]['time']))
        print str(kinectts) + " " + str(sel[1]['time'])

        imgPath = sel[0]
        imgTs = sel[1]['time']

        print "### Sample " + str(imgPath) + " ###"

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])

        print tilt

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        bestWindowHog = tester_hog.test_img(hog_classifier, imgPath, imgScales)
        if bestWindowHog != None:
            scale = bestWindowHog[4]
            print "best scale hog = " + str(scale)
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_hog, [bestWindowHog[0:4]])
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        cv2.imwrite(hog_result_dir + "/" + str(imgTs) + ".jpg", img_hog)

        bestWindowIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales)
        if bestWindowIcf != None:
            scale = bestWindowIcf[4]
            print "best scale icf = " + str(scale)
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_icf, [bestWindowIcf[0:4]])
            cv2.imshow("icf", img_icf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            cv2.imshow("icf", img_icf)
        cv2.imwrite(icf_result_dir + "/" + str(imgTs) + ".jpg", img_icf)
Esempio n. 10
0
def test_multiscale(
        hog_classifier_file,
        icf_classifier_file,
        imgpaths,
        kinect_detections,
        hog_result_dir,
        icf_result_dir):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*imgpaths)
    metadata = utils.parseMetadata(*imgpaths)

    kinect_ts = read_kinect_metadata(kinect_detections)

    print metadata
    print kinect_ts

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    for kinectts in kinect_ts:
        sel = min(metadata.items(), key=lambda mt: abs(kinectts - mt[1]['time']))
        print str(kinectts) + " " + str(sel[1]['time'])

        imgPath = sel[0]
        imgTs = sel[1]['time']

        print "### Sample " + str(imgPath) + " ###"

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])

        print tilt

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        bestWindowHog = tester_hog.test_img(hog_classifier, imgPath, imgScales)
        if bestWindowHog != None:
            scale = bestWindowHog[4]
            print "best scale hog = " + str(scale)
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_hog, [bestWindowHog[0:4]])
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        cv2.imwrite(hog_result_dir + "/" + str(imgTs) + ".jpg", img_hog)

        bestWindowIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales)
        if bestWindowIcf != None:
            scale = bestWindowIcf[4]
            print "best scale icf = " + str(scale)
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_icf, [bestWindowIcf[0:4]])
            cv2.imshow("icf", img_icf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            cv2.imshow("icf", img_icf)
        cv2.imwrite(icf_result_dir + "/" + str(imgTs) + ".jpg", img_icf)