musfiqdehan commited on
Commit
6ab21ce
·
verified ·
1 Parent(s): 1dc5be5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ import gradio as gr
5
+ import os
6
+ import shutil
7
+ import spaces
8
+
9
+ class PanoramaGenerator:
10
+ def __init__(self, video_path):
11
+ self.video_path = video_path
12
+ self.frames = []
13
+
14
+ def extract_frames(self, frame_interval=10):
15
+ cap = cv2.VideoCapture(self.video_path)
16
+
17
+ if not cap.isOpened():
18
+ raise ValueError("Error opening video file")
19
+
20
+ frame_count = 0
21
+ while True:
22
+ ret, frame = cap.read()
23
+
24
+ if not ret:
25
+ break
26
+
27
+ # Extract frame
28
+ if frame_count % frame_interval == 0:
29
+ # Resize frame
30
+ resized_frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
31
+ self.frames.append(resized_frame)
32
+
33
+ frame_count += 1
34
+
35
+ cap.release()
36
+ return self.frames
37
+
38
+ def create_panorama(self):
39
+ if len(self.frames) < 2:
40
+ raise ValueError("Not enough frames to create panorama")
41
+
42
+ stitcher = cv2.Stitcher_create(mode=cv2.Stitcher_PANORAMA)
43
+
44
+ # Attempt to stitch frames
45
+ status, panorama = stitcher.stitch(self.frames)
46
+
47
+ # Check if stitching was successful
48
+ if status != cv2.Stitcher_OK:
49
+ raise RuntimeError(f"Stitching failed with error code {status}")
50
+
51
+ return panorama
52
+
53
+ def save_panorama(self, output_path="panorama.jpg"):
54
+ panorama = self.create_panorama()
55
+ cv2.imwrite(output_path, panorama)
56
+ return output_path
57
+
58
+
59
+
60
+
61
+ def process_video(video_file):
62
+ # Save the uploaded video to a temporary file
63
+ temp_video_path = "temp_video.mp4"
64
+ shutil.copy(video_file.name, temp_video_path)
65
+
66
+ # Generate the panorama
67
+ panorama_gen = PanoramaGenerator(temp_video_path)
68
+ panorama_gen.extract_frames()
69
+ panorama_path = panorama_gen.save_panorama()
70
+
71
+ # Clean up the temporary video file
72
+ os.remove(temp_video_path)
73
+
74
+ return panorama_path
75
+
76
+ @spaces.GPU
77
+ def generate_and_display(video_file):
78
+ panorama_path = process_video(video_file)
79
+ return panorama_path, panorama_path
80
+
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("# Video to Panorama Generator")
83
+
84
+ with gr.Row():
85
+ with gr.Column():
86
+ video_input = gr.File(label="Upload Video", file_types=["video"])
87
+ generate_button = gr.Button("Generate Panorama")
88
+ with gr.Column():
89
+ panorama_output = gr.Image(label="Generated Panorama")
90
+ download_button = gr.DownloadButton("Download Panorama", file_name="panorama.jpg")
91
+
92
+ generate_button.click(
93
+ fn=generate_and_display,
94
+ inputs=video_input,
95
+ outputs=[panorama_output, download_button]
96
+ )
97
+
98
+ demo.launch()