-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (68 loc) · 2.22 KB
/
Copy pathmain.py
File metadata and controls
87 lines (68 loc) · 2.22 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
from flask import Flask, render_template, jsonify, request
from flask_socketio import SocketIO, emit
import cv2
from PIL import Image
import io
import numpy as np
import imutils
import base64
from find_phone import find_phone
from eye_track import eyes_are_closed
import matplotlib.pyplot as plt
import seaborn as sns
app = Flask(__name__)
app.static_folder = 'static'
socketio = SocketIO(app,cors_allowed_origins='*')
global imageCount
imageCount = 1
@socketio.on('image')
def image(data_image):
sbuf = io.StringIO()
sbuf.write(data_image)
# decode and convert into image
b = io.BytesIO(base64.b64decode(data_image))
pimg = Image.open(b)
stream = io.BytesIO()
pimg.save(stream, format='PNG') # convert PIL Image to Bytes
bin_img = stream.getvalue()
#Detect Phone Usage
phone_status = find_phone(bin_img)
#Detect Eyes
eye_closed = eyes_are_closed(bin_img)
# emit the frame back
emit('response_back', [phone_status, eye_closed])
@socketio.on('get_data')
def get_data(dict_data):
global imageCount
sns.set_theme()
# Data
min_length = min(min(len(dict_data['tabData']), len(dict_data['phoneData'])), len(dict_data['eyeData']))
y=[[x*100/3 for x in dict_data['tabData'][:min_length]], [x*100/3 for x in dict_data['phoneData'][:min_length]], [x*100/3 for x in dict_data['eyeData'][:min_length]]]
x=list(range(0,len(y[0])))
print(x,y)
# Plot
plt.stackplot(x,y[0],y[1],y[2], labels=['Tab Focus','Phone','Sleep'])
plt.legend(loc='upper left')
plt.title('Total Overview of Productivity')
plt.xlabel('Time')
plt.ylabel('Stacked Percentage of Undistracted Time')
imageCount += 1
plt.savefig(f'static/test{imageCount}.jpg')
plt.close()
emit('redirect', f'/summary?count={imageCount}')
@app.route('/', methods=['POST', 'GET'])
def index():
return render_template('index.html')
@app.route("/camera", methods=['POST', 'GET'])
def camera():
return render_template("camera.html")
@app.route("/summary", methods=['POST', 'GET'])
def summary():
count = request.args['count']
return render_template("summary.html", count=count)
if __name__ == '__main__':
# app.run( # Starts the site
# host='0.0.0.0',
# port=2000
# )
socketio.run(app, host="0.0.0.0")