szili2011 commited on
Commit
d8c37f0
·
verified ·
1 Parent(s): 8ff870f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -43
app.py CHANGED
@@ -1,60 +1,58 @@
1
- import cv2
2
  import gradio as gr
 
3
  import numpy as np
4
- from keras.models import load_model
5
 
6
- # Load your trained model
7
- model = load_model('model.h5')
8
 
 
 
 
 
 
9
  def process_frame(frame):
10
- img = cv2.resize(frame, (64, 64)) # Adjust size based on your model input
11
- img = img.astype('float32') / 255.0
 
 
 
12
  img = np.expand_dims(img, axis=0)
 
13
 
14
  prediction = model.predict(img)
15
  return prediction[0][1] # Assuming category 1 is jumpscare
16
 
 
17
  def convert_video_to_dumpscare(video_path, sensitivity):
18
- try:
19
- cap = cv2.VideoCapture(video_path)
20
- fps = cap.get(cv2.CAP_PROP_FPS)
21
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
22
 
23
- output_path = 'output_dumpscare.mp4'
24
- out = cv2.VideoWriter(output_path, fourcc, fps, (int(cap.get(3)), int(cap.get(4))))
 
 
 
 
 
25
 
26
- while cap.isOpened():
27
- ret, frame = cap.read()
28
- if not ret:
29
- break
30
 
31
- if process_frame(frame) > sensitivity:
32
- out.write(frame)
33
 
34
- cap.release()
35
- out.release()
36
- return output_path
 
37
 
38
- except Exception as e:
39
- return f"Error processing video: {str(e)}"
 
 
 
 
40
 
41
- # Gradio interface function
42
- def gradio_interface(video, sensitivity):
43
- if video is None:
44
- return "Please upload a video."
45
- return convert_video_to_dumpscare(video, sensitivity) # Use video directly
46
-
47
- # Set up Gradio app
48
- iface = gr.Interface(
49
- fn=gradio_interface,
50
- inputs=[
51
- gr.Video(label="Import Video"), # Removed type argument
52
- gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Sensitivity"),
53
- ],
54
- outputs=gr.Video(label="Output Dumpscare Video"),
55
- title="Dumpscare Video Converter",
56
- description="Upload a video, set sensitivity, and click 'Cut' to process the video.",
57
- )
58
-
59
- # Launch the interface
60
- iface.launch()
 
 
1
  import gradio as gr
2
+ import cv2
3
  import numpy as np
4
+ import tensorflow as tf
5
 
6
+ # Load your model
7
+ model = tf.keras.models.load_model('model.h5') # No path needed if it's in the same directory
8
 
9
+ # Function to resize frames
10
+ def resize_frame(frame, size=(64, 64)):
11
+ return cv2.resize(frame, size)
12
+
13
+ # Function to process each frame
14
  def process_frame(frame):
15
+ # Resize the frame
16
+ resized_frame = resize_frame(frame)
17
+
18
+ # Normalize and prepare the frame for the model
19
+ img = resized_frame.astype('float32') / 255.0
20
  img = np.expand_dims(img, axis=0)
21
+ img = img.reshape(1, -1) # Flatten to match the input shape
22
 
23
  prediction = model.predict(img)
24
  return prediction[0][1] # Assuming category 1 is jumpscare
25
 
26
+ # Function to convert video to dumpscare
27
  def convert_video_to_dumpscare(video_path, sensitivity):
28
+ cap = cv2.VideoCapture(video_path)
29
+ jumpscare_frames = []
 
 
30
 
31
+ while cap.isOpened():
32
+ ret, frame = cap.read()
33
+ if not ret:
34
+ break
35
+ prediction = process_frame(frame)
36
+ if prediction > sensitivity: # Adjust this threshold as needed
37
+ jumpscare_frames.append(frame)
38
 
39
+ cap.release()
 
 
 
40
 
41
+ # Here you can save jumpscare frames or create a new video
42
+ return "Dumpscare video created successfully!" # Change this as needed
43
 
44
+ # Gradio interface
45
+ def gradio_interface(video, sensitivity):
46
+ result = convert_video_to_dumpscare(video, sensitivity)
47
+ return result
48
 
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("## Video Dumpscare Generator")
51
+ video_input = gr.Video(label="Upload Video")
52
+ sensitivity_input = gr.Slider(minimum=0, maximum=1, label="Sensitivity", value=0.5)
53
+ submit_btn = gr.Button("Cut Video")
54
+ output_text = gr.Textbox(label="Output")
55
 
56
+ submit_btn.click(gradio_interface, inputs=[video_input, sensitivity_input], outputs=output_text)
57
+
58
+ demo.launch()