-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
143 lines (118 loc) · 4.42 KB
/
main.py
File metadata and controls
143 lines (118 loc) · 4.42 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
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
# http://127.0.0.1:5000/
# https://flask.palletsprojects.com/en/2.0.x/patterns/fileuploads/
import os
from flask import Flask, render_template, flash, request, redirect, url_for, jsonify
from werkzeug.utils import secure_filename
from DeepFakeDetector.extract_landmarks import ExtractLandmarks
from DeepFakeDetector.classify import Classify
UPLOAD_FOLDER = 'D:\\arnav\\Python\\githubStuff\\deepfake-detector\\DeepFakeDetector\\input'
LANDMARKS_FOLDER = 'D:\\arnav\\Python\\githubStuff\\deepfake-detector\\DeepFakeDetector\\landmarks'
ALLOWED_EXTENSIONS = {'mp4'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['LANDMARKS_FOLDER'] = LANDMARKS_FOLDER
app.secret_key = "sdhwiuhdeiuwh"
# @app.route('/')
# def home():
# return render_template("index.html")
# @app.route("/post/<int:blog_id>")
# def get_post(blog_id):
# return render_template("post.html")
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return "<h1>Failed!</h1>"
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return "<h1>Select file!</h1>"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
video_filename = os.path.join(
app.config['UPLOAD_FOLDER'], filename)
landmark_filename = os.path.join(
app.config['LANDMARKS_FOLDER'], f"{filename}.txt")
file.save(video_filename)
e = ExtractLandmarks()
c = Classify()
os.remove(video_filename)
os.remove(landmark_filename)
return f"<h1>{c.label}</h1>"
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
#
# curl -X POST "http://127.0.0.1:5000/api" -F file=aagfhgtpmv.mp4
# curl -F file=@aagfhgtpmv.mp4 "http://127.0.0.1:5000/api"
@app.route('/api', methods=['POST'])
def upload_file_api():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return jsonify(
success=False,
message="No .mp4 video file sent."
)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return "<h1>Select file!</h1>"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
video_filename = os.path.join(
app.config['UPLOAD_FOLDER'], filename)
landmark_filename = os.path.join(
app.config['LANDMARKS_FOLDER'], f"{filename}.txt")
file.save(video_filename)
e = ExtractLandmarks()
c = Classify()
os.remove(video_filename)
os.remove(landmark_filename)
return jsonify(
success=True,
message=c.label
)
return jsonify(
success=False,
message="Unkown error, please send a video file as a post request."
)
# def shutdown_server():
# func = request.environ.get('werkzeug.server.shutdown')
# if func is None:
# raise RuntimeError('Not running with the Werkzeug Server')
# func()
# @app.get('/shutdown')
# def shutdown():
# shutdown_server()
# return 'Server shutting down...'
if __name__ == "__main__":
app.run(debug=True)
# # start flask
# app = Flask(__name__)
# # render default webpage
# @app.route('/')
# def home():
# return render_template('home.html')
# # when the post method detect, then redirect to success function
# @app.route('/', methods=['POST', 'GET'])
# def get_data():
# if request.method == 'POST':
# user = request.form['search']
# return redirect(url_for('success', name=user))
# # get the data for the requested query
# @app.route('/success/<name>')
# def success(name):
# return "<xmp>" + str(requestResults(name)) + " </xmp> "