Пример #1
0
def detect_plates_in_scene(img_original_scene):
    list_of_possible_plates = []  # this will be the return value

    height, width, num_of_channels = img_original_scene.shape

    img_contours = np.zeros((height, width, 3), np.uint8)

    cv2.destroyAllWindows()

    img_grayscale_scene, img_thresh_scene = Preprocess.preprocess(
        img_original_scene)

    list_of_possible_chars_in_scene = find_possible_chars_in_scene(
        img_thresh_scene)

    list_of_lists_of_matching_chars = DetectChars.find_list_of_lists_of_matching_chars(
        list_of_possible_chars_in_scene)

    for list_of_matching_chars in list_of_lists_of_matching_chars:
        random_blue = random.randint(0, 255)
        random_green = random.randint(0, 255)
        random_red = random.randint(0, 255)

        contours = []

        for matching_char in list_of_matching_chars:
            contours.append(matching_char.contour)

        cv2.drawContours(img_contours, contours, -1,
                         (random_blue, random_green, random_red))

    for list_of_matching_chars in list_of_lists_of_matching_chars:
        possible_plate = extract_plate(img_original_scene,
                                       list_of_matching_chars)

        if possible_plate.imgPlate is not None:  # if plate was found
            list_of_possible_plates.append(possible_plate)
        # end if
    # end for

    print("\n" + str(len(list_of_possible_plates)) + " possible plates found")

    for i in range(0, len(list_of_possible_plates)):
        rect_points = cv2.boxPoints(
            list_of_possible_plates[i].rrLocationOfPlateInScene)

        cv2.line(img_contours, tuple(rect_points[0]), tuple(rect_points[1]),
                 PlateRecognition.SCALAR_RED, 2)
        cv2.line(img_contours, tuple(rect_points[1]), tuple(rect_points[2]),
                 PlateRecognition.SCALAR_RED, 2)
        cv2.line(img_contours, tuple(rect_points[2]), tuple(rect_points[3]),
                 PlateRecognition.SCALAR_RED, 2)
        cv2.line(img_contours, tuple(rect_points[3]), tuple(rect_points[0]),
                 PlateRecognition.SCALAR_RED, 2)

        cv2.imshow("4b", list_of_possible_plates[i].imgPlate)
        cv2.waitKey(0)

    return list_of_possible_plates
Пример #2
0
def detect_plates_in_scene(img_original_scene) -> [PossiblePlate]:
    if img_original_scene is None:
        print("\n### Error: image not found ### \n\n")
        os.system("pause")
        return

    list_of_possible_plates: [PossiblePlate] = []

    height, width, num_channels = img_original_scene.shape

    # Creo las matrices vacias del tamaño de la imagen
    img_grayscale_scene = np.zeros((height, width, 1), np.uint8)
    img_thresh_scene = np.zeros((height, width, 1), np.uint8)
    img_contours = np.zeros((height, width, 3), np.uint8)

    # Proceso el INPUT y obtengo un grayScale y un Threshold.
    img_grayscale_scene, img_thresh_scene = Preprocess.preprocess(img_original_scene)

    # Guardo como jpg el grayScale y Threshold.
    if SAVE_IMAGE:
        cv2.imwrite("./output/showImage/grayscale.jpg", img_grayscale_scene)
        cv2.imwrite("./output/showImage/thresh.jpg", img_thresh_scene)

    # Muestro el grayScale y Threshold.
    if SHOW_IMAGE:
        cv2.imshow("GrayScaleImage", img_grayscale_scene)
        cv2.imshow("AdaptativeThresholdImage", img_thresh_scene)

    # Busco todos los posibles chars
    # Primero buscamos todos los contornos, y despues los filtramos por los que pueden ser chars
    # Sin comparar contra otros
    if SHOW_TIME:
        start_time_2 = time.time()
        list_of_possible_chars_in_scene = find_possible_chars_in_scene(img_thresh_scene)
        finish_time_2 = time.time() - start_time_2
        print("--- Find possible chars: %s seconds ---" % finish_time_2)
    else:
        list_of_possible_chars_in_scene: [PossibleChar] = find_possible_chars_in_scene(img_thresh_scene)

    # Busco grupos de posibles chars
    list_of_lists_of_matching_chars_in_scene = DetectChars.find_list_of_lists_of_matching_chars(
        list_of_possible_chars_in_scene)

    # Dibujo los contornos a los posibles chars
    if not SUPER_SPEED_MODE:
        for list_of_matching_chars in list_of_lists_of_matching_chars_in_scene:
            int_random_blue = random.randint(0, 255)
            int_random_green = random.randint(0, 255)
            int_random_red = random.randint(0, 255)

            contours = []

            for matching_char in list_of_matching_chars:
                contours.append(matching_char.contour)

            cv2.drawContours(img_contours, contours, -1, (int_random_blue, int_random_green, int_random_red))

            if SHOW_IMAGE:
                cv2.imshow("5- Posibles contornos", img_contours)

    for list_of_matching_chars in list_of_lists_of_matching_chars_in_scene:
        possible_plate = extract_plate(img_original_scene, list_of_matching_chars)

        if possible_plate.imgPlate is not None:
            list_of_possible_plates.append(possible_plate)

    if NO_ERROR_PRINT_ENABLED:
        print("\n" + str(len(list_of_possible_plates)) + " possible plates found" + "\n")

    for i in range(0, len(list_of_possible_plates)):
        p2f_rect_points = cv2.boxPoints(list_of_possible_plates[i].rrLocationOfPlateInScene)

        cv2.line(img_contours, tuple(p2f_rect_points[0]), tuple(p2f_rect_points[1]), SCALAR_RED, 2)
        cv2.line(img_contours, tuple(p2f_rect_points[1]), tuple(p2f_rect_points[2]), SCALAR_RED, 2)
        cv2.line(img_contours, tuple(p2f_rect_points[2]), tuple(p2f_rect_points[3]), SCALAR_RED, 2)
        cv2.line(img_contours, tuple(p2f_rect_points[3]), tuple(p2f_rect_points[0]), SCALAR_RED, 2)

        if SHOW_IMAGE:
            cv2.imshow("Posibles patentes " + str(i), img_contours)

        if SAVE_IMAGE:
            cv2.imwrite("./output/showImage/PosiblesPatentes.jpg", img_contours)

        if NO_ERROR_PRINT_ENABLED:
            print("possible plate " + str(i))
            print("\n### Plate detection complete ###\n")

    return list_of_possible_plates