File size: 1,927 Bytes
22801e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ed07d4
22801e0
 
 
 
 
 
 
5cbf245
22801e0
 
 
 
5cbf245
 
22801e0
 
 
5cbf245
 
 
22801e0
5cbf245
 
22801e0
5cbf245
22801e0
 
 
 
 
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
import cv2
import numpy as np
import math
from collections import deque
import tensorflow as tf
from tensorflow import keras
from collections import OrderedDict


from keras.models import load_model

LCRN_model = load_model('LRCN_model.h5')

class Predict:

    def __init__(self) :
        pass

       
    def prediction(self,input_path, output_path,frames_needed):
        video_reader=cv2.VideoCapture(input_path)

        original_video_width=int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
        original_video_hight=int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))

        video_writer=cv2.VideoWriter(output_path,cv2.VideoWriter_fourcc('V', 'P', '9', '0'),video_reader.get(cv2.CAP_PROP_FPS),(original_video_width,original_video_hight))
        frames_deque=deque(maxlen=frames_needed)

        predicted_class_name=""
        IMAGE_HEIGHT , IMAGE_WIDTH = 64, 64
        classes_to_be_used=["HorseRace","BenchPress","PullUps","PushUps","HorseRiding","HighJump","Swing"]

        while video_reader.isOpened():
            ok, frame = video_reader.read()

            if not ok:
                break

            resized_frame = cv2.resize(frame, (IMAGE_HEIGHT, IMAGE_WIDTH))
            normalized_frame = resized_frame / 255

            frames_deque.append(normalized_frame)

            if len(frames_deque) == frames_needed:
                input_sequence = np.expand_dims(frames_deque, axis=0)  # Add batch dimension
                predicted_labels_probabilities = LCRN_model.predict(input_sequence)[0]  # Get rid of outer batch dimension

                predicted_label = np.argmax(predicted_labels_probabilities)
                predicted_class_name = classes_to_be_used[predicted_label]

                cv2.putText(frame, predicted_class_name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

                video_writer.write(frame)

        video_reader.release()
        video_writer.release()