forked from vedant-rgb/People_Counter_Tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (80 loc) · 2.84 KB
/
main.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
from flask import Flask, render_template, jsonify, request , send_file
import os
import threading
# from flask import redirect, url_for
from roboflow import Roboflow
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
allowed_extensions = {'mp4'}
filename=""
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in allowed_extensions
@app.route('/')
def server():
return render_template('home.html')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/model')
def model():
return render_template('upload_form.html')
@app.route('/imag/<int:img_id>')
def img(img_id):
imgname = f'images/img{img_id}.jpg'
return send_file(imgname , mimetype = 'image/jpg')
@app.route('/gif')
def Gif():
gif_name = 'images/preloader.gif'
return send_file(gif_name , mimetype = 'image/gif')
@app.route('/api/upload', methods=['POST'])
def upload_video():
global filename
if 'file' not in request.files:
return jsonify({"error": "No file part in the request"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
filename = file.filename
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
return jsonify({"message": "File uploaded successfully", "filename": filename}), 200
else:
return jsonify({"error": "File type not permitted"}), 400
@app.route('/api/predict')
def predict():
global filename
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if not os.path.exists(file_path):
return jsonify({"error": f"File not found: {filename}"})
try:
rf = Roboflow(api_key="2dbsmAHtO3IDId2Qnbj5")
project = rf.workspace().project("person_detection-b9e6g")
model = project.version("7").model
job_id, signed_url, expire_time = model.predict_video(
file_path, # Use uploaded file path
fps=5,
prediction_type="batch-video",
)
results = model.poll_until_video_results(job_id)
up = 0
down = 0
n = len(results)
for i in range(n):
m = len(results['person_detection-b9e6g'][i]['predictions'])
for j in range(m):
cls = results['person_detection-b9e6g'][i]['predictions'][j]['class']
if cls == 'Up':
up += 1
else:
down += 1
data={"dataup":up,"datadown":down}
return render_template("index.html",data=data)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)