def stopDetect(image, hsv):
    stop_detected = False

    #Stop detection
    # lowerR = np.array(boundaryR[0][0], dtype='uint8')
    # upperR = np.array(boundaryR[0][1], dtype='uint8')

    kernel = np.ones((3, 3), np.uint8)
    maskR = cv2.inRange(hsv, lowerR, upperR)
    maskR = cv2.morphologyEx(maskR, cv2.MORPH_CLOSE, kernel)
    # maskR = cv2.morphologyEx(maskR, cv2.MORPH_OPEN, kernel)

    maskR_cnt, maskR_max_cnt, maskR_approx = geom.find_main_contour_approx(
        maskR)
    try:
        shapeR, thresh = detect(maskR_max_cnt, maskR)
        if shapeR == 'octagon' and cv2.countNonZero(maskR) > 1000:
            cv2.putText(image, 'STOP',
                        (image.shape[1] - 100, image.shape[0] - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            stop_detected = True
    except:
        pass

    return image, stop_detected
def detect_stop_sign(image, boundaryR):
    lowerR = np.array(boundaryR[0][0], dtype='uint8')
    upperR = np.array(boundaryR[0][1], dtype='uint8')

    img = edge_enhancement(image)

    alpha = 1.5
    beta = 50
    res = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)

    hsv = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
    H, S, V = cv2.split(hsv)
    S = S + 50
    hsvR = cv2.merge((H, S, V))

    kernel = np.ones((3, 3), np.uint8)
    maskR = cv2.inRange(hsvR, lowerR, upperR)
    maskR = cv2.morphologyEx(maskR, cv2.MORPH_CLOSE, kernel)
    maskR = cv2.morphologyEx(maskR, cv2.MORPH_OPEN, kernel)
    cntR, max_cntR, approx_cntR = geom.find_main_contour_approx(maskR)
    try:
        shapeR, thresh = detect(max_cntR, maskR)
        if shapeR == 'octagon' and cv2.countNonZero(maskR) > 1000:
            cv2.putText(image, 'STOP',
                        (image.shape[1] - 100, image.shape[0] - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    except:
        pass

    return image
def check_green_size(maskG):
    maskG_cnt, maskG_max_cnt, maskG_approx = geom.find_main_contour_approx(maskG)
    W = 0
    H = 0
    if (maskG_approx is not None) and len(maskG_approx) == 4:
        maskG_cnt = max(maskG_cnt, key=cv2.contourArea)
        maskG_rect = cv2.minAreaRect(maskG_cnt)
        W = int(maskG_rect[1][0])
        H = int(maskG_rect[1][1])
    maskG_size = W*H
    return maskG_approx, maskG_size
def check_STOP(image, maskR):
    cntR, max_cntR, approx_cntR = geom.find_main_contour_approx(maskR)
    if max_cntR is not None:
    # try:
        shapeR, thresh = detect(max_cntR, maskR)
        if shapeR == 'octagon' and cv2.countNonZero(maskR) > 1000:
            cv2.putText(image, 'STOP', (image.shape[1] - 100, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    # except:
    else:
        pass
    return image
def addressDetect(img, maskG):

    address = None

    maskG_cnt, _, maskG_approx = geom.find_main_contour_approx(maskG)
    W = 0
    H = 0
    if maskG_approx is not None and len(maskG_approx) == 4:
        maskG_cnt = max(maskG_cnt, key=cv2.contourArea)
        maskG_rect = cv2.minAreaRect(maskG_cnt)
        maskG_box = np.int0(cv2.boxPoints(maskG_rect))
        W = int(maskG_rect[1][0])
        H = int(maskG_rect[1][1])
    maskG_size = W * H

    if maskG_size >= 1000 and maskG_size < 3000:
        peri = cv2.arcLength(maskG_approx, True)
        approx = cv2.approxPolyDP(maskG_approx, 0.02 * peri, True)
        warp_img = warp(approx, img)

        thresh = make_thresh(warp_img)

        thresh_cut = []
        if thresh.shape[1] >= 45:
            for i in range(3):
                cut_size = thresh.shape[1] // 3
                if i == 2:
                    cut_img = thresh[:, i * cut_size:]
                else:
                    cut_img = thresh[:, i * cut_size:(i + 1) * cut_size]
                thresh_cut.append(cut_img)

            digits = []
            for i in range(3):
                cut_img = thresh_cut[i]
                each_cnts, max_cnt, box = geom.find_main_contour_box(cut_img.copy())
                (x, y, w, h) = cv2.boundingRect(max_cnt)
                # (x, y, w, h) = box
                if i == 0:
                    if w < 8:
                        digits.append(1)
                    else:
                        digits.append(2)
                elif i == 1:
                    digits.append(0)
                else:
                    if w < 8 and h >= 10:
                        digits.append(1)
                    elif h >= 10:
                        roi = cut_img[y:y + h, x:x + w]
                        (roiH, roiW) = roi.shape
                        # (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))
                        (dH, dW) = (int(roiH * 0.15), int(roiW * 0.2))
                        dHC = int(roiH * 0.1)
                        segments = [
                            ((0, 0), (roiW, dH)),  # top
                            ((0, dH), (dW, roiH // 2)),  # top-left
                            ((roiW - dW, dH), (roiW, roiH // 2)),  # top-right
                            ((0, (roiH // 2) - dHC), (roiW, (roiH // 2) + dHC)),  # center
                            ((0, roiH // 2), (dW, roiH)),  # bottom-left
                            ((roiW - dW, roiH // 2), (roiW, roiH)),  # bottom-right
                            ((0, roiH - dH), (roiW, roiH))  # bottom
                        ]
                        on = [0] * len(segments)
                        for (segi, ((xA, yA), (xB, yB))) in enumerate(segments):
                            segROI = roi[yA:yB, xA:xB]
                            total = cv2.countNonZero(segROI)
                            area = (xB - xA) * (yB - yA)
                            if total / float(area) > 0.55:
                                on[segi] = 1
                        try:
                            digit = DIGITS_LOOKUP[tuple(on)]
                            digits.append(digit)
                        except:
                            pass
            if len(digits) == 3:
                address = '{}{}{}'.format(*digits[:3])
                cv2.drawContours(img, [maskG_box], -1, (0, 255, 0), 2)
                cv2.putText(img, '{}{}{}'.format(*digits[:3]), (img.shape[1]-100, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

    return img, address
def greenaddressDetect(img, maskG):

    addressFlag = False

    maskG_cnt, _, maskG_approx = geom.find_main_contour_approx(maskG)
    W = 0
    H = 0
    if maskG_approx is not None and len(maskG_approx) == 4:
        maskG_cnt = max(maskG_cnt, key=cv2.contourArea)
        maskG_rect = cv2.minAreaRect(maskG_cnt)
        maskG_box = np.int0(cv2.boxPoints(maskG_rect))
        W = int(maskG_rect[1][0])
        H = int(maskG_rect[1][1])
    maskG_size = W * H

    if maskG_size >= 1000 and maskG_size < 3000:    #FIXME
        addressFlag = True
        cv2.drawContours(img, [maskG_box], -1, (0, 255, 0), 2)
        # peri = cv2.arcLength(maskG_approx, True)
        # approx = cv2.approxPolyDP(maskG_approx, 0.02 * peri, True)
        # warp_img = warp(approx, img)
        #
        # thresh = make_thresh(warp_img)
        #
        # thresh_cut = []
        # if thresh.shape[1] >= 45:
        #     for i in range(3):
        #         cut_size = thresh.shape[1] // 3
        #         if i == 2:
        #             cut_img = thresh[:, i * cut_size:]
        #         else:
        #             cut_img = thresh[:, i * cut_size:(i + 1) * cut_size]
        #         thresh_cut.append(cut_img)
        #
        #     digits = []
        #     for i in range(3):
        #         cut_img = thresh_cut[i]
        #         each_cnts, max_cnt, box = geom.find_main_contour_box(cut_img.copy())
        #         (x, y, w, h) = cv2.boundingRect(max_cnt)
        #         # (x, y, w, h) = box
        #         if i == 0:
        #             if w < 8:
        #                 digits.append(1)
        #             else:
        #                 digits.append(2)
        #         elif i == 1:
        #             digits.append(0)
        #         else:
        #             if w < 8 and h >= 10:
        #                 digits.append(1)
        #             elif h >= 10:
        #                 roi = cut_img[y:y + h, x:x + w]
        #                 (roiH, roiW) = roi.shape
        #                 # (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))
        #                 (dH, dW) = (int(roiH * 0.15), int(roiW * 0.2))
        #                 dHC = int(roiH * 0.1)
        #                 segments = [
        #                     ((0, 0), (roiW, dH)),  # top
        #                     ((0, dH), (dW, roiH // 2)),  # top-left
        #                     ((roiW - dW, dH), (roiW, roiH // 2)),  # top-right
        #                     ((0, (roiH // 2) - dHC), (roiW, (roiH // 2) + dHC)),  # center
        #                     ((0, roiH // 2), (dW, roiH)),  # bottom-left
        #                     ((roiW - dW, roiH // 2), (roiW, roiH)),  # bottom-right
        #                     ((0, roiH - dH), (roiW, roiH))  # bottom
        #                 ]
        #                 on = [0] * len(segments)
        #                 for (segi, ((xA, yA), (xB, yB))) in enumerate(segments):
        #                     segROI = roi[yA:yB, xA:xB]
        #                     total = cv2.countNonZero(segROI)
        #                     area = (xB - xA) * (yB - yA)
        #                     if total / float(area) > 0.55:
        #                         on[segi] = 1
        #                 try:
        #                     digit = DIGITS_LOOKUP[tuple(on)]
        #                     digits.append(digit)
        #                 except:
        #                     pass
        #     if len(digits) == 3:
        #         address = '{}{}{}'.format(*digits[:3])
        #         cv2.drawContours(img, [maskG_box], -1, (0, 255, 0), 2)
        #         cv2.putText(img, '{}{}{}'.format(*digits[:3]), (img.shape[1]-100, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

    return img, addressFlag
# cv2.waitKey(0)
# boundaries = [([60, 90, 80], [90, 255, 110])]  #GREEN
# boundaries = [([50, 80, 0], [90, 150, 20])] # sterling_demo 201
boundaries = [([40, 80, 0], [70, 255, 80])]  # sterling_demo2 102

lowerG = np.array(boundaries[0][0], dtype='uint8')
upperG = np.array(boundaries[0][1], dtype='uint8')

kernel = np.ones((3, 3), np.uint8)
mask = cv2.inRange(hsv, lowerG, upperG)
cv2.imshow('mask', mask)
# cv2.waitKey(0)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

img_copy = img.copy()
cnts, max_cont, approx_cnt = geom.find_main_contour_approx(mask)
cv2.drawContours(img_copy, max_cont, -1, (255, 0, 0), 2)
cv2.imshow('first contour', img_copy)
cv2.drawContours(img_copy, [approx_cnt], -1, (0, 0, 255), 2)
cv2.imshow('approx contour', img_copy)

peri = cv2.arcLength(approx_cnt, True)
approx = cv2.approxPolyDP(approx_cnt, 0.02 * peri, True)
if len(approx) == 4:
    our_cnt = approx
#
warp = warp(our_cnt, img)
cv2.imshow('warp image', warp)

warp_gray = cv2.cvtColor(warp, cv2.COLOR_BGR2GRAY)
cv2.imshow('warp gray', warp_gray)
    cv2.imshow('hsvG', hsvG)
    # cv2.waitKey(0)
    # make mask for Green and Red
    maskG, maskR = make_mask(hsvG, hsvR)
    cv2.imshow('Green Mask', maskG)
    # cv2.waitKey(0)

    maskG_equ = cv2.bitwise_and(equ, equ, mask = maskG)
    cv2.imshow('equ and mask', maskG_equ)
    cv2.waitKey(0)
    # check STOP sign is in frame
    stop_img_ = check_STOP(img_, maskR)

    # check Green mask size in frame
    # maskG_approx, maskG_size = check_green_size(maskG)
    maskG_cnt, maskG_max_cnt, maskG_approx = geom.find_main_contour_approx(maskG_equ)
    # cv2.drawContours(img_, maskG_cnt, -1, (0, 255, 0), 2)
    # cv2.imshow('mask Green contour', img_)
    # cv2.waitKey(0)
    W = 0
    H = 0
    if (maskG_approx is not None) and len(maskG_approx) == 4:
        maskG_cnt = max(maskG_cnt, key=cv2.contourArea)
        maskG_rect = cv2.minAreaRect(maskG_cnt)
        W = int(maskG_rect[1][0])
        H = int(maskG_rect[1][1])
    maskG_size = W*H
    # cv2.drawContours(img, maskG_cnt, -1, (0, 0, 255), 1)
    # cv2.imshow('Green contour', img)
    # cv2.waitKey(0)
hsvG = cv2.merge((H, S, V))
cv2.imshow('green hsv', hsvG)

kernel = np.ones((3, 3), np.uint8)
maskG = cv2.inRange(hsvG, lowerG, upperG)
maskG = cv2.morphologyEx(maskG, cv2.MORPH_CLOSE, kernel)
# maskG = cv2.morphologyEx(maskG, cv2.MORPH_OPEN, kernel)
cv2.imshow('green mask', maskG)

maskR = cv2.inRange(hsvR, lowerR, upperR)
maskR = cv2.morphologyEx(maskR, cv2.MORPH_CLOSE, kernel)
maskR = cv2.morphologyEx(maskR, cv2.MORPH_OPEN, kernel)
cv2.imshow('red mask', maskR)
# cv2.waitKey(0)

cntR, max_cntR, approx_cntR = geom.find_main_contour_approx(maskR)
cv2.drawContours(img, max_cntR, -1, (255, 0, 0), 2)
cv2.drawContours(img, approx_cntR, -1, (0, 255, 0), 2)
cv2.imshow('Red contours', img)
cv2.waitKey(0)
try:
    # maskR = cv2.morphologyEx(maskR, cv2.MORPH_CLOSE, kernel)
    shapeR, thresh = detect(max_cntR, maskR)
    if shapeR == 'octagon' and cv2.countNonZero(maskR) > 1000:
        cv2.putText(img_, 'STOP', (img_.shape[1]-100, img_.shape[0]-20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
except:
    pass

maskG_cnt, _, maskG_approx = geom.find_main_contour_approx(maskG)
# try:
#     maskG_cnt = max(maskG_cnt, key=cv2.contourArea)
Exemple #10
0
    cv2.imshow('B space', B)
    greenB = B.copy()
    greenB[B < 140] = 0
    greenB[B > 160] = 0
    maskG = cv2.bitwise_and(greenA, greenA, mask=greenB)

    maskG = cv2.morphologyEx(maskG, cv2.MORPH_OPEN, kernel)
    maskG = cv2.morphologyEx(maskG, cv2.MORPH_CLOSE, kernel)
    maskG = cv2.morphologyEx(maskG, cv2.MORPH_DILATE, kernel)
    cv2.imshow('after B space', greenB)
    cv2.imshow('mask for green', maskG)
    cv2.waitKey(0)

    start_time = time.time()

    maskR_cnt, maskR_max_cnt, maskR_approx = geom.find_main_contour_approx(
        maskR)
    try:
        shapeR, thresh = detect(maskR_max_cnt, maskR)
        if shapeR == 'octagon' and cv2.countNonZero(maskR) > 1000:
            cv2.putText(img_, 'STOP',
                        (img_.shape[1] - 100, img_.shape[0] - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    except:
        pass

    maskG_cnt, maskG_max_cnt, maskG_approx = geom.find_main_contour_approx(
        maskG)
    cv2.drawContours(img_, maskG_cnt, -1, (0, 255, 0), 2)
    cv2.imshow('mask Green contour', img_)
    cv2.waitKey(0)
    W = 0