selectmixer commited on
Commit
1515f1d
·
1 Parent(s): a05778d

first commit

Browse files
Files changed (2) hide show
  1. handler.py +118 -0
  2. requirements.txt +3 -0
handler.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # handler.py
2
+
3
+ import os
4
+ import io
5
+ import tempfile
6
+ import numpy as np
7
+ from PIL import Image
8
+ from DepthFlow import DepthScene
9
+ from DepthFlow.Motion import Presets
10
+
11
+ class Handler:
12
+ def __init__(self):
13
+ """
14
+ Initialize the handler and load necessary resources.
15
+ This method is called once when the service starts.
16
+ """
17
+ # Initialize DepthFlow once for efficiency
18
+ self.depthflow = DepthScene()
19
+
20
+ def preprocess(self, inputs):
21
+ """
22
+ Preprocess the input data.
23
+
24
+ Args:
25
+ inputs (dict): The input payload containing the image data.
26
+
27
+ Returns:
28
+ str: Path to the preprocessed image file.
29
+ """
30
+ if 'image' not in inputs:
31
+ raise ValueError("Missing 'image' in inputs")
32
+
33
+ image_bytes = inputs['image'].read()
34
+ image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
35
+
36
+ # Save image to a temporary file
37
+ temp_image_file = tempfile.NamedTemporaryFile(suffix='.jpg', delete=False)
38
+ image.save(temp_image_file.name)
39
+
40
+ return temp_image_file.name
41
+
42
+ def inference(self, image_path):
43
+ """
44
+ Perform the main inference logic.
45
+
46
+ Args:
47
+ image_path (str): Path to the preprocessed image file.
48
+
49
+ Returns:
50
+ str: Path to the output video file.
51
+ """
52
+ # Load the image into DepthFlow
53
+ self.depthflow.input(image=image_path)
54
+
55
+ # Set custom parameters (modify as needed)
56
+ self.depthflow.state.height = 1
57
+ self.depthflow.state.zoom = 1.1
58
+ self.depthflow.state.dolly = 1
59
+ self.depthflow.state.dof_enable = True
60
+ self.depthflow.state.dof_intensity = 1.2
61
+ self.depthflow.state.vignette_intensity = 40
62
+
63
+ # Apply the animation preset
64
+ self.depthflow.add_animation(Presets.Dolly())
65
+
66
+ # Generate the output video
67
+ temp_video_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
68
+ self.depthflow.update()
69
+ self.depthflow.main(output=temp_video_file.name)
70
+
71
+ return temp_video_file.name
72
+
73
+ def postprocess(self, video_path):
74
+ """
75
+ Postprocess the output and prepare the response.
76
+
77
+ Args:
78
+ video_path (str): Path to the generated video file.
79
+
80
+ Returns:
81
+ dict: Response containing the video file.
82
+ """
83
+ with open(video_path, 'rb') as f:
84
+ video_bytes = f.read()
85
+
86
+ # Clean up temporary files
87
+ os.remove(video_path)
88
+
89
+ return {
90
+ 'video': video_bytes
91
+ }
92
+
93
+ def __call__(self, inputs):
94
+ """
95
+ Handle the incoming request.
96
+
97
+ Args:
98
+ inputs (dict): The input payload.
99
+
100
+ Returns:
101
+ dict: The response payload.
102
+ """
103
+ try:
104
+ # Preprocess
105
+ image_path = self.preprocess(inputs)
106
+
107
+ # Inference
108
+ video_path = self.inference(image_path)
109
+
110
+ # Postprocess
111
+ result = self.postprocess(video_path)
112
+
113
+ # Clean up image file
114
+ os.remove(image_path)
115
+
116
+ return result
117
+ except Exception as e:
118
+ return {'error': str(e)}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ DepthFlow==0.6.0
2
+ Pillow==10.4.0
3
+ numpy==1.26.4