imgar commited on
Commit
ca774c4
·
1 Parent(s): 7ac123e

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +58 -28
backend.py CHANGED
@@ -5,11 +5,20 @@ from flask_cors import CORS
5
  from PIL import Image
6
  import numpy as np
7
  import potrace
 
 
 
 
 
 
8
 
9
  app = Flask(__name__)
10
  CORS(app)
11
 
12
- FRAMES = 5258
 
 
 
13
 
14
  def png_to_np_array(filename):
15
  img = Image.open(filename)
@@ -30,35 +39,56 @@ def png_to_svg(filename):
30
 
31
  frame_coords = []
32
 
33
- for i in range(FRAMES):
34
-
35
- latex = []
36
-
37
- path = png_to_svg('frames/frame%d.png' % (i+1))
38
-
39
- for curve in path.curves:
40
- segments = curve.segments
41
- start = curve.start_point
42
- for segment in segments:
43
- x0, y0 = start
44
- if segment.is_corner:
45
- x1, y1 = segment.c
46
- x2, y2 = segment.end_point
47
- latex.append('((1-t)%f+t%f,(1-t)%f+t%f)' % (x0, x1, y0, y1))
48
- latex.append('((1-t)%f+t%f,(1-t)%f+t%f)' % (x1, x2, y1, y2))
49
- else:
50
- x1, y1 = segment.c1
51
- x2, y2 = segment.c2
52
- x3, y3 = segment.end_point
53
- latex.append('((1-t)((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f))+t((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f)),\
54
- (1-t)((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f))+t((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f)))' % \
55
- (x0, x1, x1, x2, x1, x2, x2, x3, y0, y1, y1, y2, y1, y2, y2, y3))
56
- start = segment.end_point
57
-
58
- frame_coords.append(latex)
 
59
 
60
  @app.route('/')
61
  def index():
62
  return json.dumps(frame_coords)
63
 
64
- app.run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from PIL import Image
6
  import numpy as np
7
  import potrace
8
+ import cv2
9
+ import os
10
+ import sys
11
+ import traceback
12
+ import multiprocessing
13
+
14
 
15
  app = Flask(__name__)
16
  CORS(app)
17
 
18
+ #FRAMES = 5258
19
+ FRAME_DIR = 'frames'
20
+ frame_latex = 0
21
+
22
 
23
  def png_to_np_array(filename):
24
  img = Image.open(filename)
 
39
 
40
  frame_coords = []
41
 
42
+ def gefd(frame_latexs):
43
+ for i in range(frame_latexs):
44
+
45
+ latex = []
46
+
47
+ path = png_to_svg('frames/frame%d.png' % (i+1))
48
+
49
+ for curve in path.curves:
50
+ segments = curve.segments
51
+ start = curve.start_point
52
+ for segment in segments:
53
+ x0, y0 = start
54
+ if segment.is_corner:
55
+ x1, y1 = segment.c
56
+ x2, y2 = segment.end_point
57
+ latex.append('((1-t)%f+t%f,(1-t)%f+t%f)' % (x0, x1, y0, y1))
58
+ latex.append('((1-t)%f+t%f,(1-t)%f+t%f)' % (x1, x2, y1, y2))
59
+ else:
60
+ x1, y1 = segment.c1
61
+ x2, y2 = segment.c2
62
+ x3, y3 = segment.end_point
63
+ latex.append('((1-t)((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f))+t((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f)),\
64
+ (1-t)((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f))+t((1-t)((1-t)%f+t%f)+t((1-t)%f+t%f)))' % \
65
+ (x0, x1, x1, x2, x1, x2, x2, x3, y0, y1, y1, y2, y1, y2, y2, y3))
66
+ start = segment.end_point
67
+
68
+ frame_coords.append(latex)
69
 
70
  @app.route('/')
71
  def index():
72
  return json.dumps(frame_coords)
73
 
74
+ @app.route('/app')
75
+ def app_():
76
+ return render_template('index.html')
77
+
78
+ if __name__ == '__main__':
79
+ frame_latex = range(len(os.listdir(FRAME_DIR)))
80
+ with multiprocessing.Pool(processes = multiprocessing.cpu_count()) as pool:
81
+ print('Processing %d frames... Please wait for processing to finish before running on frontend\n' % len(os.listdir(FRAME_DIR)))
82
+
83
+ try:
84
+ frame_latex = pool.map(gefd, frame_latex)
85
+ except cv2.error as e:
86
+ print('[ERROR] Unable to process one or more files. Remember image files should be named <DIRECTORY>/frame<INDEX>.<EXTENSION> where INDEX represents the frame number starting from 1 and DIRECTORY and EXTENSION are defined by command line arguments (e.g. frames/frame1.png). Please check if:\n\tThe files exist\n\tThe files are all valid image files\n\tThe name of the files given is correct as per command line arguments\n\tThe program has the necessary permissions to read the file.\n\nUse backend.py -h for further documentation\n')
87
+
88
+ print('-----------------------------')
89
+
90
+ print('Full error traceback:\n')
91
+ traceback.print_exc()
92
+ sys.exit(2)
93
+
94
+ app.run()