piyush3 commited on
Commit
07de1be
·
verified ·
1 Parent(s): c57e035

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import cv2
3
+ import mediapipe as mp
4
+ import numpy as np
5
+ import statistics
6
+ import pyrealsense2 as rs
7
+ from sklearn.linear_model import LinearRegression
8
+
9
+ app = Flask(__name__)
10
+
11
+ # Initialize MediaPipe Face Mesh
12
+ mp_face_mesh = mp.solutions.face_mesh
13
+ face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.7)
14
+
15
+ def initialize_realsense():
16
+ pipeline = rs.pipeline()
17
+ config = rs.config()
18
+ config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
19
+ config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
20
+ pipeline.start(config)
21
+ return pipeline
22
+
23
+ def process_frame(color_image, depth_frame):
24
+ rgb_frame = cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB)
25
+ results = face_mesh.process(rgb_frame)
26
+
27
+ if results.multi_face_landmarks:
28
+ for face_landmarks in results.multi_face_landmarks:
29
+ upper_lip = face_landmarks.landmark[13]
30
+ lower_lip = face_landmarks.landmark[14]
31
+
32
+ h, w, _ = color_image.shape
33
+ upper_lip_coords = (int(upper_lip.x * w), int(upper_lip.y * h))
34
+ lower_lip_coords = (int(lower_lip.x * w), int(lower_lip.y * h))
35
+
36
+ return upper_lip_coords, lower_lip_coords
37
+ return None, None
38
+
39
+ @app.route('/')
40
+ def home():
41
+ return render_template('index.html')
42
+
43
+ @app.route('/analyze', methods=['POST'])
44
+ def analyze():
45
+ pipeline = initialize_realsense()
46
+ try:
47
+ frames = pipeline.wait_for_frames()
48
+ color_frame = frames.get_color_frame()
49
+ depth_frame = frames.get_depth_frame()
50
+
51
+ color_image = np.asanyarray(color_frame.get_data())
52
+ upper_lip_coords, lower_lip_coords = process_frame(color_image, depth_frame)
53
+
54
+ if upper_lip_coords and lower_lip_coords:
55
+ # Process measurements and calculate distance
56
+ distance = calculate_distance(upper_lip_coords, lower_lip_coords, depth_frame)
57
+ stage = determine_stage(distance)
58
+
59
+ return jsonify({
60
+ 'distance': float(distance),
61
+ 'stage': stage,
62
+ 'status': 'success'
63
+ })
64
+
65
+ return jsonify({'status': 'error', 'message': 'No face detected'})
66
+
67
+ finally:
68
+ pipeline.stop()
69
+
70
+ def calculate_distance(upper_lip_coords, lower_lip_coords, depth_frame):
71
+ # Your distance calculation logic here
72
+ pass
73
+
74
+ def determine_stage(distance):
75
+ if distance >= 45:
76
+ return "Stage I"
77
+ elif 20 <= distance < 45:
78
+ return "Stage II"
79
+ else:
80
+ return "Stage III"
81
+
82
+ if __name__ == '__main__':
83
+ app.run(host='0.0.0.0', port=7860)