How to establish if text is upside-down?

I need to establish if text on my image is upside down.Examples of my images:

1

2

3

4

5

I do that via comparison of confidence scores of primal image and 180 degree rotated image, but sometimes this approach gives wrong result so i am looking for alternative way to do one more independant check.

I tried to calculate number of black pixels above and below the middle line, but this approach doesnt work sometimes, even for the perfect image without any deffects.

Perfect image

Is it possible to fix this approach ?
Could you, please, advice me alternative approach of establishing if text is upside down ?

Here is my code for establishing if text is upsdie-down via calculation of black pixels above and below middle line:

`import cv2
import numpy as np

def process_image_and_draw_lines(image_path):
def count_black_pixels_from_bottom(binary_img, N):
height, width = binary_img.shape
if N >= height:
raise ValueError(“N is out of range”)
return np.sum(binary_img[height – 1 – N, :] == 0)

def count_black_pixels_from_top(binary_img, K):
    height, width = binary_img.shape
    if K >= height:
        raise ValueError("K is out of range")
    return np.sum(binary_img[K, :] == 0)

def draw_line_on_row(img, row_number, color=(0, 0, 255), thickness=1):
    img[row_number:row_number+thickness, :] = color
    return img

def find_highest_difference_row(binary_img, from_bottom=True):
    height = binary_img.shape[0]
    max_diff = 0
    max_diff_index = 0

    if from_bottom:
        for N in range(height - 1):
            black_pixels_current = count_black_pixels_from_bottom(binary_img, N)
            black_pixels_next = count_black_pixels_from_bottom(binary_img, N + 1)
            difference = black_pixels_next - black_pixels_current
            if difference > max_diff:
                max_diff = difference
                max_diff_index = N + 1
        return height - 1 - max_diff_index
    else:
        for K in range(height - 1):
            black_pixels_current = count_black_pixels_from_top(binary_img, K)
            black_pixels_next = count_black_pixels_from_top(binary_img, K + 1)
            difference = black_pixels_next - black_pixels_current
            if difference > max_diff:
                max_diff = difference
                max_diff_index = K + 1
        return max_diff_index

def process_image(image_path):
    img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    if img is None:
        print(f"Error loading image {image_path}")
        return
    
    _, binary_img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)
    top_line = find_highest_difference_row(binary_img, from_bottom=False)
    bottom_line = find_highest_difference_row(binary_img, from_bottom=True)

    if bottom_line > top_line:
        top_line, bottom_line = bottom_line, top_line

    middle_line = (top_line + bottom_line) // 2

    img_copy = cv2.cvtColor(binary_img, cv2.COLOR_GRAY2BGR)
    img_copy = draw_line_on_row(img_copy, top_line, color=(0, 0, 255), thickness=1)
    img_copy = draw_line_on_row(img_copy, bottom_line, color=(0, 0, 255), thickness=1)
    img_copy = draw_line_on_row(img_copy, middle_line, color=(0, 255, 0), thickness=1)

    above_middle = np.sum(binary_img[:middle_line, :] == 0)
    below_middle = np.sum(binary_img[middle_line:, :] == 0)

    if above_middle > below_middle:
        print("above_middle")
        print(above_middle)
        print("below_middle")
        print(below_middle)
        print("Image is not upside-down.")
    elif below_middle > above_middle:
        print("Image is upside-down.")
        print("above_middle")
        print(above_middle)
        print("below_middle")
        print(below_middle)
    else:
        print("Number of black pixels above and below the middle line are equal.")

    cv2.imshow('Image with Lines', img_copy)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

process_image(image_path)

process_image_and_draw_lines('image_5.png')

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật