Esempio n. 1
0
 def load(self, data_filename=None):
     if None in [data_filename]:
         raise ValueError('data is empty')
     with open(data_filename) as fp:
         ctx = json.load(fp)
         fp.close()
         for key in ctx.keys():
             record, prop = ctx[key], {}
             image = cv2.imread(record['filename'])
             if image is None:
                 continue
             # resize image to be continue
             num = record['object_num']
             labels = [float(val) for val in record['object_labels']]
             labels = utility.resize(int(num), labels, cfg.max_predictions)
             bboxes = [float(val) for val in record['object_bboxes']]
             bboxes = utility.resize(int(num), bboxes, cfg.max_predictions)
             puts_debug(
                 '{}\nval: \n{}, shape: {}\nval: \n{}, shape: {}'.format(
                     num, labels, np.shape(labels), bboxes,
                     np.shape(bboxes)))
             prop['raw_data'], prop['num'], prop['labels'], prop[
                 'bboxes'] = image / 255., int(num), labels, bboxes
             self._provider.append(prop)
             prop = np.concatenate([prop['labels'], prop['bboxes']], axis=1)
             puts_debug('prop shape: {}'.format(np.shape(prop)))
             self._groundtruth.append(prop)
             self._size += 1
Esempio n. 2
0
def convert(num):
    counter = 0
    print 'Thread: ', num
    for folder in folders:
        if ".DS_Store" not in folder:
            if os.path.exists(outputDir + folder) == False:
                os.mkdir(outputDir + folder)
            # 	continue
            # else:
            files = os.listdir(inputDir + folder)
            print len(files)
            for file in files:
                if ".DS_Store" not in file:
                    # print 'Thread: ', num, inputDir + folder + "/" +file
                    if os.path.exists(outputDir + folder + "/" +
                                      file) == False:
                        img = cv2.imread(inputDir + folder + "/" + file, 1)
                        if img != None:
                            # image = cv2.resize(img, (224, 224))
                            img = ut.resize(img)
                            newImg, x, y = ut.scale(img, [], [], imSize=28)

                            cv2.imwrite(outputDir + folder + "/" + file,
                                        newImg)
                            # print 'Thread: ', num, "write to : ", outputDir + folder + "/" +file
                    else:
                        pass
                        # print "skip: " + outputDir + folder + "/" +file
            counter += 1
            print counter
Esempio n. 3
0
def find_components(edges, max_components=10):

    count = max_components + 1
    n = 1
    components = None
    while count > max_components:
        n += 1
        dilated_image = cv2.convertScaleAbs(
            cv2.dilate(edges / 255, np.ones((3, 3)), iterations=n))
        components = cv2.connectedComponentsWithStats(dilated_image)
        count = components[0]
    if imshow:
        cv2.imshow('Edged', utility.resize(edges, height=650))
        cv2.imshow('Edged dilated',
                   utility.resize(255 * dilated_image, height=650))
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    return components
Esempio n. 4
0
    def getDerivatives(self, originalImg, X, Y):
        # img = np.asarray(originalImg)
        imgs = []
        Xs, Ys = [], []

        img = originalImg.copy()
        mirImage, newX, newY = ut.mirrorImage(img, X, Y)
        mirImage = mirImage.copy()
        imgs.append(mirImage)
        Xs.append(newX)
        Ys.append(newY)

        if self.debug:
            self.plotLandmarks(mirImage, newX, newY, "mirror")

        for i in range(self.derivativeNum):
            img = originalImg.copy()
            scaleImage, newX, newY = ut.resize(img, X, Y, random=True)
            imgs.append(scaleImage)
            Xs.append(newX)
            Ys.append(newY)

            if self.debug:
                self.plotLandmarks(scaleImage, newX, newY, "scale")

        for i in range(self.derivativeNum):
            img = originalImg.copy()
            rotateImage, newX, newY = ut.rotate(img, X, Y)
            if rotateImage != None:
                imgs.append(rotateImage)
                Xs.append(newX)
                Ys.append(newY)
                if self.debug:
                    self.plotLandmarks(rotateImage, newX, newY, "rotate")

        for i in range(self.derivativeNum):
            img = originalImg.copy()
            cbImage, newX, newY = ut.contrastBrightess(img, X, Y)
            imgs.append(cbImage)
            Xs.append(newX)
            Ys.append(newY)
            if self.debug:
                self.plotLandmarks(cbImage, newX, newY, "contrastBrightness")

        for i in range(self.derivativeNum):
            img = originalImg.copy()
            transImage, newX, newY = ut.translateImage(img, X, Y)
            if transImage != None:
                imgs.append(transImage)
                Xs.append(newX)
                Ys.append(newY)
                if self.debug:
                    self.plotLandmarks(transImage, newX, newY, "trans")

        return imgs, Xs, Ys
Esempio n. 5
0
    def getOrigin(self, img, X, Y):
        croppedImage, X, Y = self.crop(img, X, Y)

        if self.debug:
            self.plotLandmarks(croppedImage, X, Y, "cropped")

        resizedImage, X, Y = ut.resize(croppedImage, X, Y)

        if self.debug:
            self.plotLandmarks(resizedImage, X, Y, "resized")
        return resizedImage, X, Y
Esempio n. 6
0
    def getDataByFiles(self):
        counter = 0
        files = os.listdir(self.rawDataDir)
        print "len(files): ", len(files)
        for file in files:
            # if file != ".DS_Store" in file:
            if ".pts39" in file:
                fileHeader = file.split(".")[0]

                print "fileHeader: ", fileHeader
                img = cv2.imread(self.rawDataDir + fileHeader + ".jpg")
                print "img.shape: ", img.shape
                print "file name: ", file
                pts = np.loadtxt(self.rawDataDir + file)
                print "type(pts): ", type(pts)
                print "pts.shape: ", pts.shape
                x, y = self.unpackLandmarks(pts)

                if self.debug:
                    cv2.imshow("originalImg", img)

                img, x, y = ut.resize(img,
                                      x,
                                      y,
                                      random=False,
                                      size=(self.imSize, self.imSize))
                # img, x, y = ut.scale(img, x, y, self.imSize)

                print "after resize img.shape: ", img.shape
                # if self.debug:
                # img = ut.plotLandmarks(img, x, y, imSize = self.imSize, ifReturn = True, circleSize = 2)

                if self.debug:
                    cv2.imshow("resizedImg", img)
                    cv2.waitKey(0)

                cv2.imwrite(self.outputDir + fileHeader + '.jpg', img)

                info = ""
                info += (self.outputDir + fileHeader + '.jpg')
                for index in range(len(x)):
                    info += (" " + str(x[index]))
                    info += (" " + str(y[index]))

                if os.path.exists(self.outputDir +
                                  'menpo39Data.txt') and self.init == False:
                    f = open(self.outputDir + 'menpo39Data.txt', 'a')
                else:
                    f = open(self.outputDir + 'menpo39Data.txt', 'w')
                    self.init = False

                f.write(info)
                f.write('\n')
                f.close()
# cv2.imwrite("original.jpg",img)
# raise "debug"

# print imgName
# print labelsPTS.shape
counter = 0
while True:

    # imgName = "testFiles/image_100_05.png"
    # img = cv2.imread(imgName)
    # print img.shape
    # (w, h, _) = img.shape
    # resizeImg = None
    x, y = ut.unpackLandmarks(labelsPTS, 256)

    resizeImg, x, y = ut.resize(img, x, y, random=True)

    print type(resizeImg)
    print resizeImg.shape

    resizeImg = ut.plotLandmarks(resizeImg,
                                 x,
                                 y,
                                 name=None,
                                 ifRescale=False,
                                 ifReturn=True)

    # cv2.imshow("resize"+ str(counter), resizeImg)

    # cv2.imshow("original",img)
def augmentation(imgPath, name):
    # generateFunc = ["original", "mirror", "rotate", "translate", "brightnessAndContrast"]
    # generateFunc = ["mirror", "rotate", "translate", "brightnessAndContrast", "blur"]
    generateFunc = ["mirror", "brightnessAndContrast", "blur"]

    if debug:
        print "imgPath + name: ", imgPath + name

    img = cv2.imread(imgPath + name)
    x, y = [], []

    img = ut.resize(img)
    newImg, x, y = ut.scale(img, [], [], imSize=224)
    print "name: ", name
    print "type(img): ", type(img)
    print "img.shape: ", img.shape
    if img != None:
        if debug:
            print "FIND image: ", imgPath + name
            print "img.shape: ", img.shape
        derivateNum = len(generateFunc)
        for index in range(derivateNum):

            (w, h, _) = img.shape
            # method = random.choice(generateFunc)
            method = generateFunc[index]

            # if index == 0:
            # 	method = "original"

            # if method == "resize":
            #     newImg, newX, newY = ut.resize(img, x, y, xMaxBound = w, yMaxBound = h, random = True)
            if method == "rotate":
                newImg, newX, newY = ut.rotate(img, x, y, w=w, h=h)
            elif method == "mirror":
                newImg, newX, newY = ut.mirror(img, x, y, w=w, h=h)
            elif method == "translate":
                newImg, newX, newY = ut.translate(img, x, y, w=w, h=h)
            elif method == "brightnessAndContrast":
                newImg, newX, newY = ut.contrastBrightess(img, x, y)
            elif method == "original":
                newImg, newX, newY = img, x, y
            elif method == "blur":
                newImg, newX, newY = blur = cv2.blur(img, (5, 5)), x, y
                # newImg, newX, newY = ut.scale(img, x, y, imSize = imSize)
            # elif method == "scale":
            # 	newImg, newX, newY = ut.scale(img, x, y)
            else:
                raise "not existing function"

            if debug:
                print "name: ", name
                print "index: ", index
                print "newImg.shape: ", newImg.shape
                print "method: ", method
                cv2.imshow("img", img)
                cv2.imshow("newImg", newImg)
                cv2.waitKey(0)

            cv2.imwrite(
                path + name.replace(".jpg", "") + "_" + str(index) + ".jpg",
                newImg)
            print "saving to ............"
            print path + name.replace(".jpg", "") + "_" + str(index) + ".jpg"
Esempio n. 9
0
def scan(im_path, show=True):
    im = cv2.imread(im_path)
    orig = im.copy()

    downscaled_height = 700.0
    im, scale = utility.downscale(im, downscaled_height)

    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

    kern_size = 5
    gray_blurred = cv2.medianBlur(gray, kern_size)

    threshold_lower = 40
    threshold_upper = 150
    edged = cv2.Canny(gray_blurred, threshold_lower, threshold_upper)

    edged_copy = edged.copy()
    edged_copy = cv2.GaussianBlur(edged_copy, (3, 3), 0)

    cv2.imwrite('edged.jpg', edged)
    if show:
        cv2.imshow('Edged', edged)
        cv2.imshow('Edged blurred', edged_copy)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    (_, cnts, _) = cv2.findContours(edged_copy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:4]

    screenCnt = []

    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.015 * peri, True)
        debugging = False
        if debugging:
            cv2.drawContours(im, [approx], -1, (0, 255, 0), 2)
            cv2.imshow('Outline', im)
            cv2.waitKey(0)
        if len(approx) == 4:
            screenCnt = approx
            break
    if screenCnt.__len__() != 0:
        if show:
            cv2.drawContours(im, [screenCnt], -1, (0, 255, 0), 2)
            cv2.imwrite('outlined.jpg', im)
            cv2.imshow('Outline', im)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
        warped = four_point_transform(orig, screenCnt.reshape(4, 2) * scale)
    else:
        warped = orig

    warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
    warped = warped > threshold_local(warped, 251, offset=10)
    warped = warped.astype('uint8') * 255

    if show:
        cv2.imshow('Original', utility.resize(orig, height=650))
        cv2.imshow('Scanned', utility.resize(warped, height=650))
        cv2.waitKey(0)
    cv2.imwrite('deskewed.jpg', warped)
Esempio n. 10
0
    def DataGenBB(self, DataStrs, train_start, train_end):
        generateFunc = [
            "original", "scale", "rotate", "translate", "scaleAndTranslate",
            "brightnessAndContrast"
        ]
        # generateFunc = ["original", "scale", "rotate", "translate", "scaleAndTranslate"]

        InputData = np.zeros(
            [self.batch_size * len(generateFunc), self.imSize, self.imSize, 3],
            dtype=np.float32)
        # InputLabel = np.zeros([self.batch_size * len(generateFunc), 7], dtype = np.float32)
        InputLabel = np.zeros([self.batch_size * len(generateFunc), 3],
                              dtype=np.float32)

        InputNames = []
        count = 0
        for i in range(train_start, train_end):
            strLine = DataStrs[i]
            strCells = strLine.rstrip(' \n').split(' ')
            imgName = strCells[0]

            labels = np.array(strCells[1:]).astype(np.float)

            if len(labels) == 78:
                # print "switch to menpo39"
                labelsPTS = labels[:136].reshape([39, 2])
                self.ifMenpo39Data = True
            else:
                # print "not menpo39"
                labelsPTS = labels[:136].reshape([68, 2])
                self.ifMenpo39Data = False

            # if self.debug:
            #     print "imgName: ", imgName
            img = cv2.imread(imgName)

            if img != None:
                # print "find image: ", imgName
                # print "img.shape: ", img.shape

                img = cv2.resize(img, (self.imSize, self.imSize))
                # print "img.shape: ", img.shape
                if self.ifMenpo39Data:
                    x, y = self.unpackLandmarks(labelsPTS)
                else:
                    x, y = ut.unpackLandmarks(labelsPTS, self.imSize)

                # newImg, newX, newY = img, x, y

                for index in range(len(generateFunc)):
                    method = generateFunc[index]
                    (w, h, _) = img.shape
                    # tag = random.choice(generateFunc)
                    if method == "resize":
                        newImg, newX, newY = ut.resize(img,
                                                       x,
                                                       y,
                                                       xMaxBound=w,
                                                       yMaxBound=h,
                                                       random=True)
                    elif method == "rotate":
                        newImg, newX, newY = ut.rotate(img, x, y, w=w, h=h)
                    elif method == "mirror":
                        newImg, newX, newY = ut.mirror(img, x, y, w=w, h=h)
                    elif method == "translate" or method == "scaleAndTranslate":
                        newImg, newX, newY = ut.translate(img, x, y, w=w, h=h)
                    elif method == "brightnessAndContrast":
                        newImg, newX, newY = ut.contrastBrightess(img, x, y)
                    elif method == "original":
                        newImg, newX, newY = img, x, y
                    elif method == "scale":
                        newImg, newX, newY = img, x, y
                    else:
                        raise "not existing function"

                    # if self.debug:
                    #     plotOriginal = ut.plotLandmarks(img, x, y, self.imSize, ifReturn = True)
                    #     plotNew = ut.plotLandmarks(newImg, newX, newY, self.imSize, ifReturn = True)

                    #     cv2.imwrite(self.outputDir + 'testOriginal' + str(count) + '.jpg', img)
                    #     cv2.imwrite(self.outputDir + 'testNew' + str(count) + '.jpg', newImg)
                    #     cv2.imwrite(self.outputDir + 'plotOriginal' + str(count) + '.jpg', plotOriginal)
                    #     cv2.imwrite(self.outputDir + 'plotNew' + str(count) + '.jpg', plotNew)

                    # print "before normalize: ", newX

                    # normX = ut.normalize(newX)
                    # normY = ut.normalize(newY)

                    # print "after normalize: ", newX
                    # print "after denormalize again: ", ut.deNormalize(newX)

                    # normXMin = min(normX)
                    # normYMin = min(normY)
                    # normXMax = max(normX)
                    # normYMax = max(normY)
                    # normXMean = (normXMax + normXMin)/2.0
                    # normYMean = (normYMax + normYMin)/2.0
                    # normEdge = max(normYMax - normYMin, normXMax - normXMin)
                    newXMin = min(newX)
                    newYMin = min(newY)
                    newXMax = max(newX)
                    newYMax = max(newY)
                    newXMean = (newXMax + newXMin) / 2.0
                    newYMean = (newYMax + newYMin) / 2.0
                    edge = max(newYMax - newYMin, newXMax - newXMin)

                    # if method == "scale":
                    #     cv2.imshow("originalImg", newImg)
                    #     cv2.waitKey(0)

                    if method == "scale" or method == "scaleAndTranslate":
                        newEdge = np.random.uniform(0.7, 0.9) * edge
                        newXMin = int(newXMean - newEdge / 2.0)
                        newXMax = int(newXMean + newEdge / 2.0)
                        newYMin = int(newYMean - newEdge / 2.0)
                        newYMax = int(newYMean + newEdge / 2.0)

                        newXMean = newXMean - newXMin
                        newYMean = newYMean - newYMin

                        # print "newXMin, newYMin, newXMax, newYMax: ", newXMin, newYMin, newXMax, newYMax

                        newImg = Image.fromarray(newImg.astype(np.uint8))
                        cropImg = newImg.crop(
                            (newXMin, newYMin, newXMax, newYMax))
                        newImg = np.array(cropImg)

                        # cv2.imshow("processing", newImg)
                        # cv2.waitKey(0)

                        w, h, _ = newImg.shape
                        edge = edge * self.imSize / w
                        newXMean = newXMean * self.imSize / w
                        newYMean = newYMean * self.imSize / h
                        newImg = cv2.resize(newImg, (self.imSize, self.imSize))

                    # print "newXMin: ", newXMin
                    # print "newYMin: ", newYMin
                    # print "newXMax: ", newXMax
                    # print "newYMax: ", newYMax
                    # print "newXMean: ", newXMean
                    # print "newYMean: ", newYMean
                    # print "newEdge: ", newEdge

                    # if method == "scale":
                    #     newImg = ut.plotTarget(newImg, [newXMean, newYMean, edge], ifSquareOnly = True, ifGreen = True)
                    #     cv2.imshow("newImg", newImg)
                    #     cv2.waitKey(0)

                    # if self.ifMenpo39Data == False:
                    print "imgName: ", imgName.split("/")[-2] + imgName.split(
                        "/")[-1].split(".")[0]
                    print "inputCheck/" + imgName.split(
                        "/")[-2] + imgName.split("/")[-1].split(".")[0] + str(
                            method) + str(count) + '.jpg'
                    cv2.imwrite(
                        "inputCheck/" + imgName.split("/")[-2] +
                        imgName.split("/")[-1].split(".")[0] + str(method) +
                        str(count) + '.jpg', newImg)

                    normX = ut.normalize(newX, self.imSize)
                    normY = ut.normalize(newY, self.imSize)
                    # normPTS = np.asarray(ut.packLandmarks(normX, normY))
                    normXMean, normYMean, normEdge = ut.normalize(
                        newXMean, self.imSize), ut.normalize(
                            newYMean,
                            self.imSize), ut.normalize(edge, self.imSize)
                    # print "newPTS: ", newPTS.shape

                    # print "ut.deNormalize(normXMin): ", ut.deNormalize(normXMin)
                    # print "ut.deNormalize(normYMin): ", ut.deNormalize(normYMin)
                    # print "ut.deNormalize(normXMax): ", ut.deNormalize(normXMax)
                    # print "ut.deNormalize(normYMax): ", ut.deNormalize(normYMax)
                    # print "ut.deNormalize(normXMean): ",ut.deNormalize(normXMean)
                    # print "ut.deNormalize(normYMean): ",ut.deNormalize(normYMean)
                    # print "ut.deNormalize(normEdge): ", ut.deNormalize(normEdge)

                    # print "method: ", method
                    # print "newImg.shape: ", newImg.shape

                    # print "len(InputData): ", len(InputData)
                    InputData[count, ...] = newImg
                    # labels = np.array([normPTS[27][0], normPTS[27][1], normPTS[8][0],
                    #     normPTS[8][1], normXMean, normYMean, normEdge])
                    labels = np.array([normXMean, normYMean, normEdge])
                    InputLabel[count, ...] = labels
                    InputNames.append(imgName)

                    # print "count: ", count
                    count += 1

            else:
                print "cannot : ", imgName

        return InputData, InputLabel, np.asarray(InputNames)
import utility
import imutils

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(
    '/home/rahul/Programming/Github-Content/Chrome-Dinosaur-Pythonified/Face-detection/shape_predictor_68_face_landmarks.dat'
)

cap = cv2.VideoCapture(0)

while True:
    _, orig = cap.read()
    _, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray, ratio = utility.resize(gray, width=120)

    rects = detector(gray, 1)
    for (i, rect) in enumerate(rects):
        shape = predictor(gray, rect)
        shape = utility.shape_to_np(shape)

        (x, y, w, h) = utility.rect_to_bb(rect)
        #cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0),2)
        cv2.rectangle(frame, (int(x / ratio), int(y / ratio)),
                      (int(w / ratio), int(h / ratio)), (255, 0, 0), 5)

        for (x, y) in shape:
            cv2.circle(frame, (int(x / ratio), int(y / ratio)), 2, (0, 255, 0),
                       2)
    cv2.imshow('frame', frame)