BilalSardar commited on
Commit
05dda97
Β·
1 Parent(s): 48f5ff0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -8
app.py CHANGED
@@ -7,17 +7,40 @@ from tensorflow.keras.models import Sequential
7
  from tensorflow.keras.optimizers import legacy
8
  from tensorflow.keras.layers import Conv3D, LSTM, Dense, Dropout, Bidirectional, MaxPool3D, Activation, Reshape, SpatialDropout3D, BatchNormalization, TimeDistributed, Flatten
9
  from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
10
- from moviepy.editor import VideoFileClip
11
 
12
 
13
 
14
- def convert_mp4_to_mpg(input_file):
15
- if not os.path.exists(input_file):
16
- raise FileNotFoundError(f"Input file '{input_file}' not found.")
17
 
18
- video_clip = VideoFileClip(input_file)
19
- video_clip.write_videofile("output.mpg", codec='mpeg2video')
20
- video_clip.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def load_video(video_path):
23
 
@@ -84,7 +107,7 @@ model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['ac
84
  model.load_weights('model/checkpoint')
85
 
86
  def Predict(Video):
87
- convert_mp4_to_mpg(Video)
88
  sample = load_data(tf.convert_to_tensor("output.mpg"))
89
  yhat = model.predict(tf.expand_dims(sample, axis=0))
90
  decoded = tf.keras.backend.ctc_decode(yhat, input_length=[75], greedy=True)[0][0].numpy()
 
7
  from tensorflow.keras.optimizers import legacy
8
  from tensorflow.keras.layers import Conv3D, LSTM, Dense, Dropout, Bidirectional, MaxPool3D, Activation, Reshape, SpatialDropout3D, BatchNormalization, TimeDistributed, Flatten
9
  from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
 
10
 
11
 
12
 
13
+ def convert_mp4_to_mpg(input_file, output_file):
14
+ """
15
+ Convert an MP4 video file to an MPG video file using OpenCV.
16
 
17
+ Args:
18
+ input_file (str): Path to the input MP4 file.
19
+ output_file (str): Path to the output MPG file.
20
+
21
+ Returns:
22
+ None
23
+ """
24
+ cap = cv2.VideoCapture(input_file)
25
+
26
+ # Check if the video file was opened successfully
27
+ if not cap.isOpened():
28
+ raise Exception(f"Could not open video file: {input_file}")
29
+
30
+ # Define the codec and create a VideoWriter object
31
+ fourcc = cv2.VideoWriter_fourcc(*'mpg2') # Use 'mpg2' codec for MPG files
32
+ out = cv2.VideoWriter(output_file, fourcc, 30.0, (int(cap.get(3)), int(cap.get(4))))
33
+
34
+ while cap.isOpened():
35
+ ret, frame = cap.read()
36
+ if not ret:
37
+ break
38
+
39
+ out.write(frame)
40
+
41
+ # Release the video objects
42
+ cap.release()
43
+ out.release()
44
 
45
  def load_video(video_path):
46
 
 
107
  model.load_weights('model/checkpoint')
108
 
109
  def Predict(Video):
110
+ convert_mp4_to_mpg(Video,'output.mpg')
111
  sample = load_data(tf.convert_to_tensor("output.mpg"))
112
  yhat = model.predict(tf.expand_dims(sample, axis=0))
113
  decoded = tf.keras.backend.ctc_decode(yhat, input_length=[75], greedy=True)[0][0].numpy()