|
1 | | -from flask import Flask, request, Response, send_file |
2 | | -from app.services.storage import save_file_locally |
3 | | -from app.models.session import Session |
4 | | -#from app.services import database as db |
5 | | -from app.services import gaze_tracker |
| 1 | +# Necesary imports |
| 2 | +import os |
| 3 | +import re |
6 | 4 | import time |
7 | 5 | import json |
8 | 6 | import csv |
| 7 | + |
9 | 8 | from pathlib import Path |
10 | | -import os |
11 | | -import re |
| 9 | +from flask import Flask, request, Response, send_file |
| 10 | + |
| 11 | +# Local imports from app |
| 12 | +from app.services.storage import save_file_locally |
| 13 | +from app.models.session import Session |
| 14 | + |
| 15 | +# from app.services import database as db |
| 16 | +from app.services import gaze_tracker |
12 | 17 |
|
13 | | -ALLOWED_EXTENSIONS = {'txt', 'webm'} |
14 | | -COLLECTION_NAME = u'session' |
15 | 18 |
|
| 19 | +# Constants |
| 20 | +ALLOWED_EXTENSIONS = {"txt", "webm"} |
| 21 | +COLLECTION_NAME = "session" |
| 22 | + |
| 23 | +# Initialize Flask app |
16 | 24 | app = Flask(__name__) |
17 | 25 |
|
18 | 26 |
|
|
139 | 147 |
|
140 | 148 |
|
141 | 149 | def calib_results(): |
142 | | - file_name = json.loads(request.form['file_name']) |
143 | | - fixed_points = json.loads(request.form['fixed_circle_iris_points']) |
144 | | - calib_points = json.loads(request.form['calib_circle_iris_points']) |
145 | | - screen_height = json.loads(request.form['screen_height']) |
146 | | - screen_width = json.loads(request.form['screen_width']) |
147 | | - k = json.loads(request.form['k']) |
| 150 | + """ |
| 151 | + Generate calibration results. |
| 152 | +
|
| 153 | + This function generates calibration results based on the provided form data. |
| 154 | + It saves the calibration points to a CSV file. Then, it uses the gaze_tracker module to predict the calibration results. |
| 155 | +
|
| 156 | + Returns: |
| 157 | + Response: A JSON response containing the calibration results. |
| 158 | +
|
| 159 | + Raises: |
| 160 | + IOError: If there is an error while writing to the CSV files. |
| 161 | + """ |
| 162 | + # Get form data from request |
| 163 | + file_name = json.loads(request.form["file_name"]) |
| 164 | + fixed_points = json.loads(request.form["fixed_circle_iris_points"]) |
| 165 | + calib_points = json.loads(request.form["calib_circle_iris_points"]) |
| 166 | + screen_height = json.loads(request.form["screen_height"]) |
| 167 | + screen_width = json.loads(request.form["screen_width"]) |
| 168 | + k = json.loads(request.form["k"]) |
| 169 | + model = json.loads(request.form["model"]) |
148 | 170 |
|
149 | 171 | # Generate csv dataset of calibration points |
150 | 172 | os.makedirs( |
151 | | - f'{Path().absolute()}/app/services/calib_validation/csv/data/', exist_ok=True) |
152 | | - calib_csv_file = f'{Path().absolute()}/app/services/calib_validation/csv/data/{file_name}_fixed_train_data.csv' |
153 | | - csv_columns = ['left_iris_x', 'left_iris_y', |
154 | | - 'right_iris_x', 'right_iris_y', 'point_x', 'point_y', 'screen_height', 'screen_width'] |
| 173 | + f"{Path().absolute()}/app/services/calib_validation/csv/data/", exist_ok=True |
| 174 | + ) |
| 175 | + |
| 176 | + # Generate csv of calibration points with following columns |
| 177 | + calib_csv_file = f"{Path().absolute()}/app/services/calib_validation/csv/data/{file_name}_fixed_train_data.csv" |
| 178 | + csv_columns = [ |
| 179 | + "left_iris_x", |
| 180 | + "left_iris_y", |
| 181 | + "right_iris_x", |
| 182 | + "right_iris_y", |
| 183 | + "point_x", |
| 184 | + "point_y", |
| 185 | + "screen_height", |
| 186 | + "screen_width", |
| 187 | + ] |
| 188 | + |
| 189 | + # Save calibration points to CSV file |
155 | 190 | try: |
156 | | - with open(calib_csv_file, 'w') as csvfile: |
| 191 | + # Open CSV file |
| 192 | + with open(calib_csv_file, "w") as csvfile: |
157 | 193 | writer = csv.DictWriter(csvfile, fieldnames=csv_columns) |
158 | 194 | writer.writeheader() |
| 195 | + |
| 196 | + # Write calibration points to CSV file |
159 | 197 | for data in fixed_points: |
160 | | - data['screen_height'] = screen_height |
161 | | - data['screen_width'] = screen_width |
| 198 | + data["screen_height"] = screen_height |
| 199 | + data["screen_width"] = screen_width |
162 | 200 | writer.writerow(data) |
| 201 | + |
| 202 | + # Handle I/O error |
163 | 203 | except IOError: |
164 | 204 | print("I/O error") |
165 | 205 |
|
166 | 206 | # Generate csv of iris points of session |
167 | 207 | os.makedirs( |
168 | | - f'{Path().absolute()}/app/services/calib_validation/csv/data/', exist_ok=True) |
169 | | - predict_csv_file = f'{Path().absolute()}/app/services/calib_validation/csv/data/{file_name}_predict_train_data.csv' |
170 | | - csv_columns = ['left_iris_x', 'left_iris_y', |
171 | | - 'right_iris_x', 'right_iris_y'] |
| 208 | + f"{Path().absolute()}/app/services/calib_validation/csv/data/", exist_ok=True |
| 209 | + ) |
| 210 | + predict_csv_file = f"{Path().absolute()}/app/services/calib_validation/csv/data/{file_name}_predict_train_data.csv" |
| 211 | + csv_columns = ["left_iris_x", "left_iris_y", "right_iris_x", "right_iris_y"] |
172 | 212 | try: |
173 | | - with open(predict_csv_file, 'w') as csvfile: |
| 213 | + with open(predict_csv_file, "w") as csvfile: |
174 | 214 | writer = csv.DictWriter(csvfile, fieldnames=csv_columns) |
175 | 215 | writer.writeheader() |
176 | 216 | for data in calib_points: |
| 217 | + # print(data) |
177 | 218 | writer.writerow(data) |
178 | 219 | except IOError: |
179 | 220 | print("I/O error") |
180 | 221 |
|
181 | 222 | # data = gaze_tracker.train_to_validate_calib(calib_csv_file, predict_csv_file) |
182 | | - data = gaze_tracker.predict(calib_csv_file, calib_csv_file, k) |
183 | 223 |
|
184 | | - return Response(json.dumps(data), status=200, mimetype='application/json') |
| 224 | + # Predict calibration results |
| 225 | + data = gaze_tracker.predict(calib_csv_file, k, model_X=model, model_Y=model) |
| 226 | + |
| 227 | + # Return calibration results |
| 228 | + return Response(json.dumps(data), status=200, mimetype="application/json") |
185 | 229 |
|
186 | 230 |
|
187 | 231 | # def session_results(): |
|
0 commit comments