wahab12 commited on
Commit
a942a5f
1 Parent(s): d47b315

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.yaml +0 -0
  2. main.py +136 -0
  3. requirements.txt +5 -0
app.yaml ADDED
File without changes
main.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import os
3
+ import tensorflow as tf
4
+ import tensorflow_hub as hub
5
+ import numpy as np
6
+ import cv2
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Define constants or parameters
11
+ min_kick_angle = 30 # Minimum angle for the leg to be considered a kick
12
+ frame_window = 10 # Number of frames to consider for action recognition
13
+ kick_counter = 0
14
+ highest_kick_frame = -1 # Initialize the frame number of the highest kick
15
+ highest_kick_knee = None # Initialize coordinates of the knee for the highest kick
16
+ highest_kick_hip = None # Initialize coordinates of the hip for the highest kick
17
+
18
+ # Initialize variables for action recognition
19
+ frame_buffer = []
20
+
21
+ # Load the MoveNet model for pose estimation from TensorFlow Hub
22
+ model = hub.load("https://tfhub.dev/google/movenet/singlepose/thunder/4")
23
+ pose_net = model.signatures['serving_default']
24
+
25
+ # Define upload folder for video files
26
+ UPLOAD_FOLDER = 'uploads'
27
+ ALLOWED_EXTENSIONS = {'mp4'}
28
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
29
+
30
+ # Function to detect front kick based on keypoints
31
+ def detect_front_kick_func(keypoints, frame_number):
32
+ keypoints_array = keypoints[0] # Get the NumPy array from the list
33
+
34
+ right_hip = keypoints_array[0, 0, 8, :] # Right hip is at index 8
35
+ right_knee = keypoints_array[0, 0, 9, :] # Right knee is at index 9
36
+
37
+ # print(right_hip, ' ', right_knee)
38
+ if right_knee[2] < 0.4 and right_hip[2] < 0.4:
39
+ return False, -1, None, None
40
+
41
+ angle = np.arctan2(right_knee[1] - right_hip[1], right_knee[0] - right_hip[0]) * 180 / np.pi
42
+
43
+ if angle > min_kick_angle:
44
+ return True, frame_number, right_knee, right_hip
45
+ else:
46
+ return False, -1, None, None
47
+
48
+ def allowed_file(filename):
49
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
50
+
51
+
52
+ @app.route('/detect_front_kick', methods=['POST'])
53
+ def detect_front_kick():
54
+ try:
55
+ # Check if the 'video' field is in the request
56
+ if 'video' not in request.files:
57
+ return jsonify({'error': 'No video file provided'})
58
+
59
+ video_file = request.files['video']
60
+
61
+ # Check if the file has the allowed extension
62
+ if not allowed_file(video_file.filename):
63
+ return jsonify({'error': 'Invalid file format. Only MP4 videos are allowed.'})
64
+
65
+ # Save the video file to the upload folder with a secure name
66
+ video_filename = (video_file.filename)
67
+ video_filepath = os.path.join(app.config['UPLOAD_FOLDER'], video_filename)
68
+ video_file.save(video_filepath)
69
+
70
+ # Open the video file for processing
71
+ cap = cv2.VideoCapture(video_filepath)
72
+
73
+ # Check if the video file was opened successfully
74
+ if not cap.isOpened():
75
+ return jsonify({'error': 'Failed to open video file.'})
76
+
77
+ frame_number = 0 # Initialize frame number
78
+
79
+ while True:
80
+ ret, frame = cap.read()
81
+
82
+ if not ret:
83
+ break
84
+
85
+ # Preprocess the frame (resize, normalize, denoise, etc.)
86
+
87
+ # Perform pose estimation using MoveNet
88
+ resized_frame = cv2.resize(frame, (256, 256))
89
+ image = tf.constant(resized_frame, dtype=tf.int32)
90
+ image = tf.expand_dims(image, axis=0)
91
+
92
+ # Run model inference
93
+ outputs = pose_net(image)
94
+ keypoints = outputs['output_0'].numpy()
95
+
96
+ # Append the keypoints to the frame buffer
97
+ frame_buffer.append(keypoints)
98
+
99
+ # Maintain a sliding window of frames for action recognition
100
+ if len(frame_buffer) > frame_window:
101
+ frame_buffer.pop(0)
102
+
103
+ # Perform action recognition using the frame buffer
104
+ if len(frame_buffer) == frame_window:
105
+ is_kick, frame_with_kick, knee, hip = detect_front_kick_func(frame_buffer, frame_number)
106
+
107
+ if is_kick:
108
+ kick_counter += 1
109
+ if frame_with_kick > highest_kick_frame:
110
+ highest_kick_frame = frame_with_kick
111
+ highest_kick_knee = knee
112
+ highest_kick_hip = hip
113
+
114
+ frame_number += 1
115
+
116
+ cap.release()
117
+
118
+ response_data = {
119
+ 'kick_counter': kick_counter,
120
+ 'highest_kick_frame': highest_kick_frame,
121
+ 'highest_kick_knee': highest_kick_knee.tolist() if highest_kick_knee is not None else None,
122
+ 'highest_kick_hip': highest_kick_hip.tolist() if highest_kick_hip is not None else None,
123
+ }
124
+
125
+ return jsonify(response_data)
126
+
127
+ except Exception as e:
128
+ return jsonify({'error': str(e)})
129
+
130
+ @app.route('/home', methods=['GET'])
131
+ def homie():
132
+ return jsonify({"message":"none"})
133
+
134
+
135
+ if __name__ == '__main__':
136
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Flask==2.1.1
2
+ numpy==1.21.2
3
+ opencv-python==4.5.3.56
4
+ tensorflow==2.5.1
5
+ tensorflow-hub==0.12.0