File size: 2,762 Bytes
c5b5437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from Rotate.predict_cls import *
from Rotate.predict_det import *
from Rotate.cut import *
import cv2
import matplotlib.pyplot as plt

def is_90():
    file_path="Rotate/output/det_results.txt"
    with open(file_path, 'r') as file:
        data = file.read().splitlines()

    count_width_greater = 0
    count_height_greater = 0

    for line in data:
        _, boxes = line.split('\t', 1)
        boxes = eval(boxes)
        for coordinates in boxes:
            width = ((coordinates[1][0] - coordinates[0][0])**2 + (coordinates[1][1] - coordinates[0][1])**2)**0.5
            height = ((coordinates[2][0] - coordinates[1][0])**2 + (coordinates[2][1] - coordinates[1][1])**2)**0.5

            if width > height:
                count_width_greater += 1
            else:
                count_height_greater += 1

    return count_width_greater < count_height_greater

def is_180(img):
    crop(img)
    run_text_classifier(
        image_dir="Rotate/output/cut",
        use_gpu=False,
        cls_model_dir="Rotate/ch_ppocr_mobile_v2.0_cls_infer/",
        draw_img_save_dir="Rotate/output/"
    )
    file_path = "Rotate/output/rotate.txt"
    # Khởi tạo biến đếm
    count_180 = 0
    count_0 = 0
    # Đọc từ file và đếm
    with open(file_path, "r") as file:
        for line in file:
            # Chuyển đổi dòng thành số nguyên
            number = int(line.strip())
            # Đếm số lượng
            if number == 180:
                count_180 += 1
            elif number == 0:
                count_0 += 1
    return count_180>count_0

def rotate_180(img):
    height, width = img.shape[:2]

    # Tính toán ma trận biến đổi để xoay hình ảnh 180 độ theo chiều kim đồng hồ
    # Trung tâm xoay là tâm của hình ảnh
    rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), 180, 1)

    # Thực hiện việc xoay hình ảnh
    rotated_image = cv2.warpAffine(img, rotation_matrix, (width, height))
    return rotated_image
def rotate_90(img):
    rotated_image = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
    return rotated_image
def rotate(img):
    run_text_detector(
    img=img,
    use_gpu=False,
    # det_algorithm="DB",
    det_model_dir="Rotate/ch_PP-OCRv4_det_infer/",
    draw_img_save_dir='Rotate/output/'
)
    if is_90() == True : 
        img=rotate_90(img)
        run_text_detector(
        img=img,
        use_gpu=False,
        # det_algorithm="DB",
        det_model_dir="Rotate/ch_PP-OCRv4_det_infer/",
        draw_img_save_dir='Rotate/output/'
    )
    if is_180(img)==True:
        result=rotate_180(img)
    else:
        result=img
    return result
# img=cv2.imread("0.png")
# result=rotate(img)
# plt.imshow(result)
# plt.show()