-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (60 loc) · 2.47 KB
/
app.py
File metadata and controls
74 lines (60 loc) · 2.47 KB
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
from flask import Flask, render_template, request, send_file, url_for, jsonify
import os
from shoplifting_model import ShopliftingPrediction
app = Flask(__name__)
# Configure folders with absolute paths
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
UPLOAD_FOLDER = os.path.join(BASE_DIR, 'static', 'uploads')
OUTPUT_FOLDER = os.path.join(BASE_DIR, 'static', 'outputs')
MODEL_PATH = os.path.join(BASE_DIR, 'models', 'lrcn_160S_90_90Q.h5')
DATA_FOLDER = os.path.join(BASE_DIR, 'data', 'input')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
app.config['DATA_FOLDER'] = DATA_FOLDER
# Ensure the directories exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_video():
if 'video' not in request.files:
return jsonify({'error': 'No video file uploaded'}), 400
video = request.files['video']
if video.filename == '':
return jsonify({'error': 'No selected file'}), 400
try:
# Save the uploaded video
input_path = os.path.join(app.config['UPLOAD_FOLDER'], video.filename)
video.save(input_path)
# Process the video
output_filename = f'output_{video.filename}'
output_path = os.path.join(app.config['OUTPUT_FOLDER'], output_filename)
# Initialize and run the model
model = ShopliftingPrediction(
model_path=MODEL_PATH,
frame_width=90,
frame_height=90,
sequence_length=160
)
model.load_model()
model.Predict_Video(input_path, output_path)
# Verify the output file exists
if not os.path.exists(output_path):
raise Exception("Output video file was not created")
# Get file size for debugging
file_size = os.path.getsize(output_path)
print(f"Output video created at: {output_path}")
print(f"File size: {file_size} bytes")
# Return the video URL for download
video_url = url_for('static', filename=f'outputs/{output_filename}', _external=True)
return jsonify({
'video_url': video_url,
'file_size': file_size
})
except Exception as e:
print(f"Error processing video: {str(e)}")
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)