File size: 8,194 Bytes
83e35a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import random
import copy
from backend.class_def import panel


template_specs = {
    "1" : {
        "span" : 1,
        "direction": "row"
    },
    "2" : {
        "span" : 2,
        "direction": "row"
    },
    "3" : {
        "span" : 3,
        "direction": "column"
    },
     "4" : {
        "span" : 2,
        "direction": "column"
    },
    # High-accuracy templates with fewer, larger panels
    "5" : {
        "span" : 4,
        "direction": "row"
    },
    "6" : {
        "span" : 2,
        "direction": "row"
    },
    "7" : {
        "span" : 3,
        "direction": "row"
    },
    "8" : {
        "span" : 4,
        "direction": "column"
    }
}

input = '433343333343343333443333443334333343344443433'



def hammingDist(str1, str2): 
    i = 0
    count = 0
  
    while(i < len(str1)): 
        if(str1[i] != str2[i]): 
            count += 1
        i += 1
    return count

def get_files_in_folder(folder_path):
    file_dicts = []
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            rank = random.randint(1, 3) 

            file_dicts.append({"name": file , 'rank' :  rank})
    return file_dicts

templates = ['14124114','312341' , '4432111' , '21411241' , '3241141' , '13411141' , '12411131' ,'1321113', '131423' , 
'142344' , '234241','2411413','3141214','42111131']

# High-accuracy mode: when HIGH_ACCURACY is set, use 2x2 grid layout
HIGH_ACCURACY = os.getenv('HIGH_ACCURACY', '0')
if HIGH_ACCURACY in ('1', 'true', 'True', 'YES', 'yes'):
    # Use templates with 2x2 grid layout (4 equal squares per page)
    templates = ['333333333333']  # 12 panels in 3x4 grid  # Always 2x2 grid
    print("Using HIGH_ACCURACY mode with 2x2 grid layout (4 equal squares per page)")
else:
    # Optional grid layout for efficiency: when GRID_LAYOUT is set, prefer uniform grids
    GRID_LAYOUT = os.getenv('GRID_LAYOUT', '0')
    if GRID_LAYOUT in ('1', 'true', 'True', 'YES', 'yes'):
        # Use simple repetitive templates that create grid-like pages
        templates = ['6666', '4488', '44446', '666', '67']

# Adjust minimum length based on accuracy mode
if HIGH_ACCURACY in ('1', 'true', 'True', 'YES', 'yes'):
    min_length = 4  # Allow 4-image pages in high accuracy mode
else:
    min_length = 6
folder_path = 'frames/final' # Specify the folder path



def get_templates(input):
    page_templates = []
    start = 0

    while(start<len(input)):
        # print(f"start: {start}")
        result = []
        print(input)
        for template in templates:

            temp = input[start:start + len(template)]
            print(f"start: {start} len:{len(template)} temp:{temp}" )
            result.append(hammingDist(temp,template))            

       
        page_templates.append(templates[result.index(min(result))])

        start = start + len(templates[result.index(min(result))]) 



    if(len(temp) < min_length):
        if(len(temp) ==1):
          temp="5"
        elif(len(temp) ==2):
          temp="67"
        elif(len(temp) ==3):
          temp="666"
        elif(len(temp) ==4):
          temp="4488"
        elif(len(temp) ==5):
          temp="44446"

        page_templates[len(page_templates)-1] = temp
        # print("****************")

    return page_templates


def last_page(panels,count_images, length):
    count = 1
    
    # For high-accuracy mode, always use 2x2 grid
    if HIGH_ACCURACY in ('1', 'true', 'True', 'YES', 'yes'):
        if length == 1:
            # Single image takes full page
            new_panel = panel(f'frame{count_images:03d}', 4, 4)
            panels.append(new_panel)
        elif length == 2:
            # Two images: each takes half page
            new_panel = panel(f'frame{count_images:03d}', 2, 4)  # Top half
            panels.append(new_panel)
            count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 2, 4)  # Bottom half
            panels.append(new_panel)
        elif length == 3:
            # Three images: 2x2 grid with one panel empty
            new_panel = panel(f'frame{count_images:03d}', 2, 2)  # Top-left
            panels.append(new_panel)
            count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 2, 2)  # Top-right
            panels.append(new_panel)
            count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 2, 2)  # Bottom-left
            panels.append(new_panel)
        elif length == 4:
            # Perfect 2x2 grid
            for i in range(0, 4):
                new_panel = panel(f'frame{count_images:03d}', 2, 2)
                panels.append(new_panel)
                count_images += 1
        elif length == 5:
            # 2x2 grid plus one panel (will go to next page)
            for i in range(0, 4):
                new_panel = panel(f'frame{count_images:03d}', 2, 2)
                panels.append(new_panel)
                count_images += 1
    else:
        # Original logic for non-high-accuracy mode
        if length == 1:
            new_panel = panel(f'frame{count_images:03d}', 4, 4)  # Full page for single image
            panels.append(new_panel)
        elif length == 2:
            new_panel = panel(f'frame{count_images:03d}', 2, 4)  # Half page each
            panels.append(new_panel)
            count += 1
            count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 2, 4)
            panels.append(new_panel)
        elif length == 3:
            # 2x2 grid with one full-width panel
            new_panel = panel(f'frame{count_images:03d}', 2, 2)  # Top-left
            panels.append(new_panel)
            count += 1
            count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 2, 2)  # Top-right
            panels.append(new_panel)
            count += 1
            count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 4, 2)  # Bottom full-width
            panels.append(new_panel)
            count += 1
            count_images += 1
        elif length == 4:
            # Perfect 2x2 grid
            for i in range(0, 4):
                new_panel = panel(f'frame{count_images:03d}', 2, 2)
                panels.append(new_panel)
                count += 1
                count_images += 1
        elif length == 5:
            # 2x2 grid plus one full-width panel
            for i in range(0, 4):
                new_panel = panel(f'frame{count_images:03d}', 2, 2)
                panels.append(new_panel)
                count += 1
                count_images += 1
            new_panel = panel(f'frame{count_images:03d}', 4, 2)  # Full-width bottom
            panels.append(new_panel)
            count += 1
            count_images += 1

    return panels



def panel_create(page_templates):

    panels = []

    images = get_files_in_folder(folder_path)
    print(images)
    count_images = 1

    for page_template in page_templates:


        if(len(page_template)<min_length): #To handle last page 
            panels = last_page(panels,count_images,len(page_template))
            break


        count = 1
        
        # For high-accuracy mode, always create 2x2 grid
        if HIGH_ACCURACY in ('1', 'true', 'True', 'YES', 'yes'):
            # Create perfect 2x2 grid: each panel is 2x2
            for i in range(4):  # Always 4 panels per page
                new = panel(f'frame{count_images:03d}', 2, 2)  # 2 columns, 2 rows
                panels.append(new)
                count_images += 1
        else:
            # Original logic for non-high-accuracy mode
            for i in page_template:
                if(template_specs[i]['direction'] == 'row'):
                    new = panel(f'frame{count_images:03d}',template_specs[i]['span'] , 1)
                else:
                    new = panel(f'frame{count_images:03d}', 1 ,template_specs[i]['span'])
                panels.append(new)
                count = count+1
                count_images+=1

        
    
    return(panels)


# v = get_templates(input)
# print(v)
# new = panel_create(v)


# for i in new:
#     print(i.__dict__)