-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
160 lines (126 loc) · 5.61 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import cv2
import numpy as np
from flask_cors import CORS
from flask import Flask, request, Response, jsonify
from camera import VideoCamera
from scrape import scrape_signing_savvy
from wordDetectionLoading.model import callModel
import mediapipe as mp
import os
from RAG import answer_query_with_rag
app = Flask(__name__)
CORS(app)
video_camera = None
def ensure_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
@app.route('/video_feed')
def video_feed():
global video_camera
video_camera = VideoCamera()
return Response(video_camera.get_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/stop_feed', methods=['POST'])
def stop_feed():
global video_camera
if video_camera:
video_camera.stop_stream()
return jsonify({'message': 'Stopping the video feed'})
def extract_frames(video_path):
# Initialize MediaPipe Hands and Drawing
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
# Create a Hands object with the desired parameters
hands = mp_hands.Hands(static_image_mode=True, max_num_hands=2, min_detection_confidence=0.8)
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Could not open video {video_path}")
return []
frame_count = 0
all_hand_landmarks = []
while True:
ret, frame = cap.read()
if not ret:
break
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
frame_hand_landmarks = []
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
landmarks = []
for landmark in hand_landmarks.landmark:
if landmark.x is None or landmark.y is None:
landmarks.extend([0.0, 0.0])
else:
landmarks.extend([landmark.x, landmark.y])
if len(landmarks) < 84:
remaining = 84 - len(landmarks)
landmarks.extend([0.0] * remaining)
elif len(landmarks) > 84:
landmarks = landmarks[:84]
frame_hand_landmarks.extend(landmarks)
if len(frame_hand_landmarks) < 84:
frame_hand_landmarks.extend([0.0] * (84 - len(frame_hand_landmarks)))
elif len(frame_hand_landmarks) > 84:
frame_hand_landmarks = frame_hand_landmarks[:84]
else:
frame_hand_landmarks = [0.0] * 84
all_hand_landmarks.append(frame_hand_landmarks)
frame_count += 1
# Pad with empty frames if fewer than 200 frames
while len(all_hand_landmarks) < 200:
all_hand_landmarks.append([0.0] * 84)
# Truncate if more than 200 frames
all_hand_landmarks = all_hand_landmarks[:200]
cap.release()
final_landmarks = np.array(all_hand_landmarks)
print(final_landmarks.shape) # Should print (200, 84)
return final_landmarks
@app.route('/scrape', methods=['GET'])
def scrape():
word = request.args.get('word', default='', type=str)
if not word:
return jsonify({'error': 'No word provided'}), 400
video_url = scrape_signing_savvy(word)
if video_url == "No .mp4 link found" or video_url == "Failed to retrieve the webpage":
return jsonify({'error': video_url}), 404
return jsonify({'mp4_url': video_url})
@app.route("/uploader", methods=['POST'])
def upload_file():
process_video_dir = 'processVideoFile'
ensure_directory(process_video_dir)
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
file_path = os.path.join(process_video_dir, file.filename)
file.save(file_path)
hand_pose_data = extract_frames(file_path)
prediction = callModel(hand_pose_data)
return jsonify({
'message': 'File successfully uploaded and frames extracted',
'file_path': file_path,
'predicted_word': prediction
}), 200
return jsonify({'error': 'Unexpected error occurred'}), 500
@app.route('/callRAG', methods=['GET'])
def callRAG():
user_query = request.args.get('query')
if not user_query:
return jsonify({"error": "Query parameter is required"}), 400
system_instructions = "Please provide a detailed and comprehensive answer with actionable advice for improving ASL skills based on this user's profile: Include tips for improving letter, word, and sentence hand gesture formation, and emphasize the importance of facial expressions and body language."
response = answer_query_with_rag(user_query, system_instructions)
return jsonify({"response": response.content})
# @app.route('/calllearningPlanRAG', methods=['GET'])
# def calllearningPlanRAG():
# user_query = request.args.get('query')
# if not user_query:
# return jsonify({"error": "Query parameter is required"}), 400
# system_instructions = "You are an american sign language assistant that provides structured learning plans for learning letters,words, and sentences"
# response = answer_query_with_rag("generate me a beginner friendly american sign language learning roadmap", system_instructions)
# return jsonify({"response": response.content})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, threaded=True, use_reloader=False)