示例#1
0
def localization(image, min_size_components, similitary_contour_with_circle, model, count, current_sign_type, file_bin_path):
    original_image = image.copy()
    binary_image = preprocess_image(image)

    binary_image = removeSmallComponents(binary_image, min_size_components)

    binary_image = cv2.bitwise_and(binary_image,binary_image, mask=remove_other_color(image))

    cv2.imwrite(os.path.join(file_bin_path, str(count)+'_'+'binary_image'+'.png'), binary_image)

    #cv2.imshow('BINARY IMAGE', binary_image)
    contours = findContour(binary_image)
    #signs, coordinates = findSigns(image, contours, similitary_contour_with_circle, 15)
    sign, coordinate = findLargestSign(original_image, contours, similitary_contour_with_circle, 15)
    
    text = ""
    sign_type = -1
    i = 0

    if sign is not None:
        sign_type = getLabel(model, sign)
        sign_type = sign_type if sign_type <= 8 else 8
        text = SIGNS[sign_type]

    if sign_type > 0 and sign_type != current_sign_type:        
        cv2.rectangle(original_image, coordinate[0],coordinate[1], (0, 255, 0), 1)
        font = cv2.FONT_HERSHEY_PLAIN
        cv2.putText(original_image,text,(coordinate[0][0], coordinate[0][1] -15), font, 1,(0,0,255),2,cv2.LINE_4)
    return coordinate, original_image, sign_type, text
def detectImageFromVideo(clf):
    font = cv2.FONT_HERSHEY_PLAIN
    count = 0
    cam = cv2.VideoCapture("MVI_1049.avi")
    while (1):
        _, img = cam.read()

        if not _:
            print("FINISHED")
            break
        else:
            original_image = img
            img = preprocess_image(original_image)
            imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(imgHSV, lowerBound, upperBound)
            mask_tmp = mask.copy()
            mask = cv2.dilate(mask, kernelOpen)
            img_binarization = binarization(img)
            mask = MorpRemoveNoise(mask)
            img_binarization = cv2.bitwise_and(mask_tmp, mask)
            mask = removeSmallComponents(img_binarization, 300)
            _, contours, h = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                              cv2.CHAIN_APPROX_SIMPLE)
            sign, coordinate = cropContour(img_binarization, contours, 0.65,
                                           15)
            text = ""

            sign_type = None
            if sign is not None:
                sign_type = classification.getLabel(clf, sign)
                sign_type = sign_type if sign_type > 0 else 0
                text = signName[sign_type]
            if sign_type > 0:
                cv2.rectangle(original_image, coordinate[0], coordinate[1],
                              (0, 255, 0), 1)
                cv2.putText(original_image, text,
                            (coordinate[0][0], coordinate[0][1] - 15), font, 1,
                            (0, 0, 255), 2, cv2.LINE_4)
            cv2.imshow("Result", original_image)

            if cv2.waitKey(10) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
示例#3
0
文件: main.py 项目: tannd1904/XuLiAnh
def localization(image, min_size_components, similitary_contour_with_circle,
                 model, count, current_sign_type):
    original_image = image.copy()
    binary_image = preprocess_image(image)

    binary_image = removeSmallComponents(binary_image, min_size_components)

    binary_image = cv2.bitwise_and(binary_image,
                                   binary_image,
                                   mask=remove_other_color(image))

    #binary_image = remove_line(binary_image)

    cv2.imshow('BINARY IMAGE', binary_image)
    contours = findContour(binary_image)

    sign, coordinate = findLargestSign(original_image, contours,
                                       similitary_contour_with_circle, 15)

    text = ""
    sign_type = -1
    i = 0
    # nhận dạng loại biển báo
    if sign is not None:
        #cắt hình là biển báo sau đó lưu vào file dạng số_tên.png
        sign_type = getLabel(model, sign)
        sign_type = sign_type if sign_type <= 11 else 11
        text = SIGNS[sign_type]
        cv2.imwrite(str(count) + '_' + text + '.png', sign)

    if sign_type > 0 and sign_type != current_sign_type:  #vẽ hình chữ nhật bao bên ngoài và puttext(ghi tên biển báo)
        cv2.rectangle(original_image, coordinate[0], coordinate[1],
                      (0, 255, 0), 1)
        font = cv2.FONT_HERSHEY_PLAIN
        cv2.putText(original_image, text,
                    (coordinate[0][0], coordinate[0][1] - 15), font, 1,
                    (0, 0, 255), 2, cv2.LINE_4)
    return coordinate, original_image, sign_type, text
while True:
    if not video_cap.isOpened():
        print("error: video capture is not opened")
        break
    ret, frame = video_cap.read()

    if not ret:
        print("error: failed to read frame")
        break

    frame = cv2.resize(frame, (640, 480))

    ret = extract_roi(frame)

    if ret is None:
        print("error: failed to extract roi")
        continue

    roi, x, y, r = ret

    label = textLables[getLabel(model, roi)]

    print(label)

    cv2.rectangle(frame, (x - r, y - r), (x + r, y + r), (0, 255, 255), 3)

    cv2.imshow("", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'): break

video_cap.release()