Beispiel #1
0
def processDatabase(dataset,
                    names,
                    deg=0,
                    scale=1.0,
                    min_score_thresh=0.7,
                    showImg=True):
    """run face detection for named dataset as names.
    dataset:
    names:
    deg: angle (anti-clockwise)
    """
    if dataset == "headPose":
        import readheadPose
        d = readheadPose.getTruePosition()

    log = open("log_%s_%d_%f.csv" % (dataset, deg, scale), "wt")
    log.write("name,num,truePositives,falsePositives,meanSize\n")

    tDetector = TensoflowFaceDector()
    category_index = getGategoryIndex()

    windowNotSet = True

    for p in names:
        dstDir = "result"
        dstname = os.path.join(dstDir, p)
        dirname = os.path.dirname(dstname)
        if not os.path.isdir(dirname):
            os.makedirs(dirname)

        frame = cv.imread(p)
        if deg != 0:
            frame = helper.rotate(frame, deg)

        [h, w] = frame.shape[:2]
        scaledImg = helper.scaledImage(frame, scale)
        frame = scaledImg

        cols = frame.shape[1]
        rows = frame.shape[0]
        [h, w] = frame.shape[:2]
        imgCenter = [cols / 2, rows / 2]

        (boxes, scores, classes, num_detections) = tDetector.run(frame)

        trueDetection = {True: 0, False: 0}

        if dataset in ("lfw", ):
            center = imgCenter
            center = (int(scale * center[0]), int(scale * center[1]))

        elif dataset == "headPose":
            v = d[p]
            center = (v[0], v[1])
            center = readheadPose.getRotatedPoint(center, deg, imgCenter)
            # ここで縮小したことによる画像の点の扱いを修正すること
            center = (int(scale * center[0]), int(scale * center[1]))

            r = int(50 * scale)

            cv.circle(frame, center, r, (0, 255, 0))
        else:
            center = imgCenter
            center = (int(scale * center[0]), int(scale * center[1]))

        trueSizes = []
        boxes_shape = boxes.shape
        for i in range(boxes_shape[0]):
            ymin, xmin, ymax, xmax = boxes[i, 0], boxes[i,
                                                        1], boxes[i,
                                                                  2], boxes[i,
                                                                            3]
            yLeftTop, xLeftTop, yRightBottom, xRightBottom = ymin * h, xmin * w, ymax * h, xmax * w
            yLeftTop, xLeftTop, yRightBottom, xRightBottom = int(
                yLeftTop), int(xLeftTop), int(yRightBottom), int(xRightBottom)
            width = xRightBottom - xLeftTop

            if scores[i] <= min_score_thresh:
                continue

            isPositive = helper.isInside(center, (xLeftTop, yLeftTop),
                                         (xRightBottom, yRightBottom))

            trueDetection[isPositive] += 1
            trueSizes.append(width)

            cv.circle(frame, (xLeftTop, yLeftTop), 5, (0, 255, 0))
            cv.circle(frame, (xRightBottom, yRightBottom), 5, (0, 255, 0))

            color = {True: (0, 255, 0), False: (0, 0, 255)}[isPositive]
            cv.rectangle(frame, (xLeftTop, yLeftTop),
                         (xRightBottom, yRightBottom), color, 5)

        found = trueDetection[True] + trueDetection[False]
        log.write("%s, %d, %d, %d, %s\n" %
                  (p, found, trueDetection[True], trueDetection[False], `
                   np.mean(trueSizes) `))

        if windowNotSet is True:
            cv.namedWindow("tensorflow based (%d, %d)" % (w, h),
                           cv.WINDOW_NORMAL)
            windowNotSet = False

        if showImg:
            cv.imshow("tensorflow based (%d, %d)" % (w, h), frame)
            k = cv.waitKey(1) & 0xff
            if k == ord('q') or k == 27:
                break

    log.close()
    cv.destroyAllWindows()
def processDatabase(dataset,
                    names,
                    deg=0,
                    scale=1.0,
                    confThreshold=0.5,
                    showImg=True):
    """run face detection for named dataset as names.
    dataset:
    names:
    deg: angle (anti-clockwise)
    """
    if dataset == "headPose":
        import readheadPose
        d = readheadPose.getTruePosition()

    log = open("log_%s_%d_%f.csv" % (dataset, deg, scale), "wt")
    log.write("name,num,truePositives,falsePositives,meanSize\n")

    detector = resnetFaceDetector.ResnetFaceDetector()

    for p in names:
        dstDir = "result"
        dstname = os.path.join(dstDir, p)
        dirname = os.path.dirname(dstname)
        if not os.path.isdir(dirname):
            os.makedirs(dirname)

        frame = cv.imread(p)
        if deg != 0:
            frame = helper.rotate(frame, deg)

        [h, w] = frame.shape[:2]
        scaledImg = helper.scaledImage(frame, scale)
        frame = scaledImg

        cols = frame.shape[1]
        rows = frame.shape[0]
        [h, w] = frame.shape[:2]
        imgCenter = [cols / 2, rows / 2]

        dets, confidences, perf_stats = detector.run(frame, confThreshold)

        trueDetection = {True: 0, False: 0}

        if dataset in ("lfw", ):
            center = imgCenter
            center = (int(scale * center[0]), int(scale * center[1]))

        elif dataset == "headPose":
            v = d[p]
            center = (v[0], v[1])
            center = readheadPose.getRotatedPoint(center, deg, imgCenter)
            # ここで縮小したことによる画像の点の扱いを修正すること
            center = (int(scale * center[0]), int(scale * center[1]))

            r = int(50 * scale)

            cv.circle(frame, center, r, (0, 255, 0))
        else:
            center = imgCenter
            center = (int(scale * center[0]), int(scale * center[1]))

        trueSizes = []

        for i, det in enumerate(dets):
            confidence = confidences[i]
            xLeftTop, yLeftTop, width, height = det
            xRightBottom = xLeftTop + width
            yRightBottom = yLeftTop + height

            width = xRightBottom - xLeftTop

            isPositive = helper.isInside(center, (xLeftTop, yLeftTop),
                                         (xRightBottom, yRightBottom))
            trueDetection[isPositive] += 1
            trueSizes.append(width)

            cv.circle(frame, (xLeftTop, yLeftTop), 5, (0, 255, 0))
            cv.circle(frame, (xRightBottom, yRightBottom), 5, (0, 255, 0))

            color = {True: (0, 255, 0), False: (0, 0, 128)}[isPositive]
            cv.rectangle(frame, (xLeftTop, yLeftTop),
                         (xRightBottom, yRightBottom), color)

            label = "face: %.4f" % confidence
            labelSize, baseLine = cv.getTextSize(label,
                                                 cv.FONT_HERSHEY_SIMPLEX, 0.5,
                                                 1)

            cv.rectangle(frame, (xLeftTop, yLeftTop - labelSize[1]),
                         (xLeftTop + labelSize[0], yLeftTop + baseLine),
                         (255, 255, 255), cv.FILLED)
            cv.putText(frame, label, (xLeftTop, yLeftTop),
                       cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))

        found = trueDetection[True] + trueDetection[False]
        log.write("%s, %d, %d, %d, %s\n" %
                  (p, found, trueDetection[True], trueDetection[False], `
                   np.mean(trueSizes) `))
        cv.imwrite(dstname, frame)

        if showImg:
            cv.imshow("resnet based (%d, %d)" % (w, h), frame)
            k = cv.waitKey(1) & 0xff
            if k == ord('q') or k == 27:
                break
    log.close()
    cv.destroyAllWindows()