-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
202 lines (151 loc) · 4.93 KB
/
app.py
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
import os
import pathlib
import threading
import time
import uuid
import ffmpeg
from PIL import Image
from flask import Flask, render_template, request, jsonify, send_file
from setting import mode_dev as mode
is_pi = mode['pi']
is_playing = mode['movie_playing']
pathlib.Path(mode['image_path']).mkdir(exist_ok=True)
pathlib.Path(mode['video_path']).mkdir(exist_ok=True)
if is_pi:
from waveshare_epd import epd7in5_V2
epd = epd7in5_V2.EPD()
epd.init()
app = Flask(__name__)
def read_frame_as_jpeg(in_file, frame_num):
width = 800
height = 480
(
ffmpeg.input(in_file)
.filter('scale', width, height, force_original_aspect_ratio=1)
.filter('pad', width, height, -1, -1)
.filter('select', 'gte(n,{})'.format(frame_num))
.output(mode['image_path'] + '/temp.jpg', vframes=1)
.overwrite_output()
.run(capture_stdout=True)
)
def play_video(video_path):
global is_playing
is_playing = True
total_frames = int(ffmpeg.probe(video_path)['streams'][0]['nb_frames'])
count = 1
while count < total_frames:
read_frame_as_jpeg(video_path, count)
img = Image.open(mode['image_path'] + '/temp.jpg')
show(img, 3)
time_count = 0
while time_count < 60:
if not is_playing:
return
time.sleep(1)
time_count += 1
count += 1
if count == total_frames - 5:
count = 1
def show(img, transpose):
_transpose = int(transpose)
if _transpose != 0:
img = img.transpose(_transpose)
img.convert(mode='1', dither=Image.FLOYDSTEINBERG)
if is_pi:
img = img.resize((epd.width, epd.height))
epd.display(epd.getbuffer(img))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/image')
def image():
return render_template('image.html')
@app.route('/api/image', methods=['POST'])
def api_image():
upload_file = request.files['image']
transpose = request.form.get('transpose')
filename = os.path.join(mode['image_path'], str(uuid.uuid4()) + '.jpg')
upload_file.save(filename)
img = Image.open(upload_file.stream)
show(img, transpose)
return jsonify({
'msg': '上传成功'
})
@app.route('/api/image/show', methods=['POST'])
def api_image_show():
global is_playing
is_playing = False
# 等待1.5秒, 等待上个播放线程结束
time.sleep(1.5)
img = Image.open(mode['image_path'] + '/' + request.form.get('name'))
show(img, request.form.get('transpose'))
return jsonify({
'msg': '发布成功'
})
@app.route('/api/video/show', methods=['POST'])
def api_video_show():
video_path = mode['video_path'] + '/' + request.form.get('name')
global is_playing
is_playing = False
# 等待1.5秒, 等待上个播放线程结束
time.sleep(1.5)
t = threading.Thread(target=play_video, args=(video_path,))
t.start()
t.join()
return jsonify({
'msg': '发布成功'
})
@app.route('/api/image/upload', methods=['POST'])
def api_image_upload():
upload_file = request.files['image']
filename = os.path.join(mode['image_path'], str(uuid.uuid4()) + '.jpg')
upload_file.save(filename)
return jsonify({
'msg': '上传成功'
})
@app.route('/api/video/upload', methods=['POST'])
def api_video_upload():
upload_file = request.files['video']
filename = os.path.join(mode['video_path'], str(uuid.uuid4()) + '.mp4')
upload_file.save(filename)
return jsonify({
'msg': '上传成功'
})
def list_images(path):
files = os.listdir(path)
return [{'name': file,
'path': path + '/' + file,
'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat(path + '/' + file).st_mtime)),
'size': round(os.path.getsize(path + '/' + file) / float(1024 * 1024), 2)} for file in files]
@app.route('/images/list')
def images_list():
return jsonify(list_images(path=mode['image_path']))
@app.route('/videos/list')
def videos_list():
return jsonify(list_images(path=mode['video_path']))
@app.route('/videos')
def videos():
return render_template('videos.html')
@app.route('/images')
def images():
return render_template('images.html')
@app.route('/del/image', methods=['POST'])
def del_image():
os.remove(mode['image_path'] + '/' + request.form.get('name'))
return jsonify({
'msg': '删除成功'
})
@app.route('/del/video', methods=['POST'])
def del_video():
os.remove(mode['video_path'] + '/' + request.form.get('name'))
return jsonify({
'msg': '删除成功'
})
@app.route('/get/image')
def get_image():
return send_file(mode['image_path'] + '/' + request.args.get('name'))
@app.route('/get/video')
def get_video():
return send_file(mode['video_path'] + '/' + request.args.get('name'))
if __name__ == '__main__':
app.run(host='0.0.0.0')