szili2011 commited on
Commit
bea3a5d
·
verified ·
1 Parent(s): 0374df8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +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') # Ensure this matches the model filename in your Space
8
+
9
+ def process_frame(frame):
10
+ # Preprocess the frame for the model
11
+ img = cv2.resize(frame, (64, 64)) # Adjust size based on your model input
12
+ img = img.astype('float32') / 255.0 # Normalize
13
+ img = np.expand_dims(img, axis=0) # Add batch dimension
14
+
15
+ # Predict using the model
16
+ prediction = model.predict(img)
17
+ return prediction[0][1] # Assuming category 1 is jumpscare
18
+
19
+ def convert_video_to_dumpscare(video_path, sensitivity):
20
+ cap = cv2.VideoCapture(video_path)
21
+ fps = cap.get(cv2.CAP_PROP_FPS)
22
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
23
+
24
+ output_path = 'output_dumpscare.mp4'
25
+ out = cv2.VideoWriter(output_path, fourcc, fps, (int(cap.get(3)), int(cap.get(4))))
26
+
27
+ while cap.isOpened():
28
+ ret, frame = cap.read()
29
+ if not ret:
30
+ break
31
+
32
+ # Check if the frame is a jumpscare
33
+ if process_frame(frame) > sensitivity: # Use sensitivity slider
34
+ out.write(frame) # Write frame if it's a jumpscare
35
+
36
+ cap.release()
37
+ out.release()
38
+ return output_path
39
+
40
+ # Gradio interface
41
+ def gradio_interface(video, sensitivity):
42
+ output_video = convert_video_to_dumpscare(video.name, sensitivity)
43
+ return output_video
44
+
45
+ # Set up Gradio app
46
+ iface = gr.Interface(
47
+ fn=gradio_interface,
48
+ inputs=[
49
+ gr.Video(label="Import Video"),
50
+ gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Sensitivity"),
51
+ ],
52
+ outputs=gr.Video(label="Output Dumpscare Video"),
53
+ title="Dumpscare Video Converter",
54
+ description="Upload a video, set sensitivity, and click 'Cut' to process the video.",
55
+ live=True
56
+ )
57
+
58
+ iface.launch()