Ejemplo n.º 1
0
def parseImageWithMask(img, mask, show, name):
    target = img.copy()

    image_utils.applyMask(target, mask)
    target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)

    colorThreshold = 40

    notBlack = numpy.sum(target > 0)
    countAbove = numpy.sum(target > colorThreshold)
    countBelow = notBlack - countAbove

    #for rows in target:
    #    for pixel in rows:
    #        if pixel != 0:
    #            if pixel > colorThreshold:
    #                countAbove += 1
    #            else:
    #                countBelow += 1

    stau = countBelow > countAbove

    info = {}
    info["jam"] = stau
    info["vehicles"] = 0

    return (info, target)
Ejemplo n.º 2
0
def validateSingle(image, mask, info, name):
    img = image.copy()

    image_utils.applyMask(img, mask)

    text = "Kein Stau"
    color = (0, 255, 0)
    if info["jam"]:
        text = "Stau"
        color = (0, 0, 255)
        stau = True

    cv2.putText(img, text, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 1,
                cv2.LINE_AA)

    cv2.imshow(name, img)

    key = cv2.waitKey(0) & 0xFF
    cv2.destroyAllWindows()

    if key == 13:  # Enter - Stau
        print("Stau")
        return True

    print("Kein Stau")
    return False
Ejemplo n.º 3
0
def parseImageWithMask(img, mask, show, name):
    target = img.copy()

    med = cv2.GaussianBlur(target, (5, 5), 1.2)
    image_utils.applyMask(med, mask)
    med = cv2.cvtColor(med, cv2.COLOR_BGR2GRAY)

    edges = cv2.Canny(med, 100, 200)

    #cv2.imshow("hi1", edges)
    #cv2.waitKey(0)

    fgthres = cv2.threshold(edges.copy(), 200, 255, cv2.THRESH_BINARY)[1]

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
    closing = cv2.morphologyEx(fgthres, cv2.MORPH_CLOSE, kernel)
    opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
    dilation = cv2.dilate(opening, kernel, iterations=2)

    img_dilation = dilation

    vehicles = image_utils.findVehicles(img_dilation)

    stau = len(vehicles) > 10

    info = {}
    info["jam"] = stau
    info["vehicles"] = 0

    return (info, target)
Ejemplo n.º 4
0
def parseImageWithMask(img, mask, show, name, classifier):
    target = img.copy()

    image_utils.applyMask(target, mask)
    target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
    cars = classifier.detectMultiScale(target, 1.1, 1)
	
    stau = len(cars) > 10

    info = {}
    info["jam"] = stau
    info["vehicles"] = 0

    return (info, target)
Ejemplo n.º 5
0
def parseImageWithMask(img, bgs, mask, show, name):
    target = img.copy()

    med = cv2.GaussianBlur(target, (5, 5), 1.2)
    image_utils.applyMask(med, mask)
    image_utils.applyMask(target, mask)

    image = bgs.apply(med)

    fgthres = cv2.threshold(image.copy(), 200, 255, cv2.THRESH_BINARY)[1]

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
    closing = cv2.morphologyEx(fgthres, cv2.MORPH_CLOSE, kernel)
    opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
    dilation = cv2.dilate(opening, kernel, iterations=2)

    img_dilation = dilation

    vehicles = image_utils.findVehicles(img_dilation)

    for vehicle in vehicles:
        cv2.rectangle(target, (vehicle[0], vehicle[1]), (vehicle[0] +
                                                         vehicle[2], vehicle[1] + vehicle[3]), (0, 255, 0))

    text = "Kein Stau"
    color = (0, 255, 0)
    stau = False
    if len(vehicles) >= 10:
        text = "Stau"
        color = (0,0,255)
        stau = True
    cv2.putText(target,text,(10,100), cv2.FONT_HERSHEY_SIMPLEX, 2,color,2,cv2.LINE_AA)

    if show:
        cv2.imshow(name, target)

    info = {}
    info["jam"] = stau
    info["vehicles"] = len(vehicles)

    return (info, target)
Ejemplo n.º 6
0
def train_image(pic1, pic2, alpha):
    pf1 = PointFinder(pic1)
    pf2 = PointFinder(pic2)

    tri1, landmarks, image, convex = getTriangulation(pf1)
    tri2, landmarks2, image1, convex = getTriangulation(pf2)

    for point in convex:
        print(point)

    b, g, r = cv2.split(image1)

    outputImage = np.zeros((image.shape[0], image.shape[1], 4), np.uint8)
    outputImage[:, :, 0] = b
    outputImage[:, :, 1] = g
    outputImage[:, :, 2] = r
    outputImage[:, :, 3] = 255

    count = 0

    landmarks3 = []

    for i in range(0, len(landmarks)):
        x = (1 - alpha) * landmarks2[i][0] + alpha * landmarks2[i][0]
        y = (1 - alpha) * landmarks2[i][1] + alpha * landmarks2[i][1]
        landmarks3.append((x, y))

    for tri in tri1:
        first = np.float32(
            [landmarks[tri[0]], landmarks[tri[1]], landmarks[tri[2]]])
        second = np.float32(
            [landmarks3[tri[0]], landmarks3[tri[1]], landmarks3[tri[2]]])

        r1 = cv2.boundingRect(first)
        r2 = cv2.boundingRect(second)

        (x, y, w, h) = r1
        (x1, y1, w1, h1) = r2

        firstCropped = np.array([[first[0][0] - x, first[0][1] - y],
                                 [first[1][0] - x, first[1][1] - y],
                                 [first[2][0] - x, first[2][1] - y]], np.int32)
        secondCropped = np.array([[second[0][0] - x1, second[0][1] - y1],
                                  [second[1][0] - x1, second[1][1] - y1],
                                  [second[2][0] - x1, second[2][1] - y1]],
                                 np.int32)

        try:
            affine = cv2.getAffineTransform(np.float32(firstCropped),
                                            np.float32(secondCropped))
        except TypeError as t:
            print("errored on")
            print(type(first))
            print(type(second))

        # warp triangle from first image to shape of second
        cropped = image[y:y + h, x:x + w]
        mask = np.zeros_like(cropped)

        cv2.fillConvexPoly(mask, firstCropped, 255)

        masked = applyMask(cropped, mask)

        warped = cv2.warpAffine(masked, affine, (w1, h1))

        #add triangle to full image
        area = outputImage[y1:y1 + h1, x1:x1 + w1]

        overlay(area, warped)

        outputImage[y1:y1 + h1, x1:x1 + w1] = area

        count += 1

    return outputImage