Explorar el Código

Split extract_crossword into extract_crossword_grid and grid_to_png
functions.

Andrew Klopper hace 6 años
padre
commit
6fdfd76066
Se han modificado 3 ficheros con 25 adiciones y 11 borrados
  1. 8 3
      home/management/commands/extract.py
  2. 3 2
      home/views.py
  3. 14 6
      home/xword.py

+ 8 - 3
home/management/commands/extract.py

@@ -1,5 +1,5 @@
1 1
 from django.core.management.base import BaseCommand
2
-from ...xword import extract_crossword
2
+from ...xword import extract_crossword_grid, grid_to_png
3 3
 
4 4
 
5 5
 class Command(BaseCommand):
@@ -37,7 +37,7 @@ class Command(BaseCommand):
37 37
         group.add_argument('--html')
38 38
 
39 39
     def handle(self, *args, **options):
40
-        image, warnings = extract_crossword(
40
+        warnings, grid, num_rows, num_cols, block_img = extract_crossword_grid(
41 41
             options['input_file_name'],
42 42
             gaussian_blur_size=options['gaussian_blur_size'],
43 43
             adaptive_threshold_block_size=options['adaptive_threshold_block_size'],
@@ -49,7 +49,12 @@ class Command(BaseCommand):
49 49
             line_detector_element_size=options['line_detector_element_size'],
50 50
             sampling_block_size_ratio=options['sampling_block_size_ratio'],
51 51
             sampling_threshold_quantile=options['sampling_threshold_quantile'],
52
-            sampling_threshold=options['sampling_threshold'],
52
+            sampling_threshold=options['sampling_threshold']
53
+        )
54
+        image = grid_to_png(
55
+            grid,
56
+            num_rows,
57
+            num_cols,
53 58
             grid_line_thickness=options['grid_line_thickness'],
54 59
             grid_square_size=options['grid_square_size'],
55 60
             grid_border_size=options['grid_border_size']

+ 3 - 2
home/views.py

@@ -4,7 +4,7 @@ from django.http import HttpResponse, HttpResponseBadRequest
4 4
 from django.shortcuts import render
5 5
 from django.views import View
6 6
 from .forms import CrosswordForm
7
-from .xword import extract_crossword
7
+from .xword import extract_crossword_grid, grid_to_png
8 8
 
9 9
 
10 10
 class HomeView(View):
@@ -15,10 +15,11 @@ class HomeView(View):
15 15
         form = CrosswordForm(request.POST, request.FILES)
16 16
         if not form.is_valid():
17 17
             return HttpResponseBadRequest('Invalid form data')
18
-        image, warnings = extract_crossword(
18
+        warnings, grid, num_rows, num_cols, block_img = extract_crossword_grid(
19 19
             form.cleaned_data['file'].temporary_file_path(),
20 20
             filter_colours=False
21 21
         )
22
+        image = grid_to_png(grid, num_rows, num_cols)
22 23
         return render(request, 'home/output.html', {
23 24
             'warnings': warnings,
24 25
             'image_file_name': 'xword_{}.png'.format(datetime.now().strftime('%Y%m%d_%H%M%S')),

+ 14 - 6
home/xword.py

@@ -196,7 +196,7 @@ def show_image(image):
196 196
     cv2.destroyAllWindows()
197 197
 
198 198
 
199
-def extract_crossword(
199
+def extract_crossword_grid(
200 200
     file_name,
201 201
     filter_colours=False,
202 202
     colour_filter_threshold=48,
@@ -210,10 +210,7 @@ def extract_crossword(
210 210
     line_detector_element_size=51,
211 211
     sampling_block_size_ratio=0.25,
212 212
     sampling_threshold_quantile=0.3,
213
-    sampling_threshold=None,
214
-    grid_line_thickness=4,
215
-    grid_square_size=64,
216
-    grid_border_size=20,
213
+    sampling_threshold=None
217 214
 ):
218 215
     warnings = []
219 216
 
@@ -245,6 +242,17 @@ def extract_crossword(
245 242
     if warning:
246 243
         warnings.append("Some blocks may be the wrong colour")
247 244
 
245
+    return warnings, grid, num_rows, num_cols, block_img
246
+
247
+
248
+def grid_to_png(
249
+    grid,
250
+    num_rows,
251
+    num_cols,
252
+    grid_line_thickness=4,
253
+    grid_square_size=64,
254
+    grid_border_size=20
255
+):
248 256
     step = grid_square_size + grid_line_thickness
249 257
     grid_height = num_rows * step + grid_line_thickness
250 258
     grid_width = num_cols * step + grid_line_thickness
@@ -258,4 +266,4 @@ def extract_crossword(
258 266
                 cv2.rectangle(output, (x, y), (x + grid_square_size - 1, y + grid_square_size - 1), 255, -1)
259 267
 
260 268
     _, png = cv2.imencode('.png', output)
261
-    return png.tobytes(), warnings
269
+    return png.tobytes()