srivatsavdamaraju commited on
Commit
53f5f46
1 Parent(s): cb2d9db

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, jsonify
2
+ import cv2
3
+ import numpy as np
4
+ import torch
5
+ from torchvision import transforms
6
+ import base64
7
+ from io import BytesIO
8
+ from PIL import Image
9
+ import threading
10
+ import queue
11
+
12
+ # Load the MiDaS model from PyTorch Hub
13
+ model = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
14
+ model.eval()
15
+
16
+ # Image transformation function
17
+ transform = transforms.Compose([
18
+ transforms.ToPILImage(),
19
+ transforms.Resize((256, 256)),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
22
+ ])
23
+
24
+ # Create Flask app
25
+ app = Flask(__name__)
26
+
27
+ # Function to estimate depth from a frame and apply color mapping
28
+ def estimate_depth(frame):
29
+ input_batch = transform(frame).unsqueeze(0)
30
+ with torch.no_grad():
31
+ prediction = model(input_batch)
32
+ depth_map = prediction.squeeze().cpu().numpy()
33
+
34
+ # Normalize and apply a colormap
35
+ depth_map = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX)
36
+ depth_map = depth_map.astype(np.uint8)
37
+ colored_depth_map = cv2.applyColorMap(depth_map, cv2.COLORMAP_JET)
38
+ return colored_depth_map
39
+
40
+ # Function to process the video frame in a separate thread
41
+ def process_frame_thread(data, response_queue):
42
+ image_data = base64.b64decode(data.split(',')[1])
43
+ image = Image.open(BytesIO(image_data))
44
+ frame = np.array(image)
45
+
46
+ # Convert RGB to BGR format (as OpenCV expects BGR)
47
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
48
+ depth_map = estimate_depth(frame)
49
+
50
+ # Encode depth map as a base64 image to send back
51
+ _, buffer = cv2.imencode('.jpg', depth_map)
52
+ depth_map_base64 = base64.b64encode(buffer).decode('utf-8')
53
+
54
+ # Add the result to the response queue
55
+ response_queue.put(f"data:image/jpeg;base64,{depth_map_base64}")
56
+
57
+ # Route to serve the HTML template
58
+ @app.route('/')
59
+ def index():
60
+ return render_template('index.html')
61
+
62
+ # Route to process video frames and return depth map
63
+ @app.route('/process_frame', methods=['POST'])
64
+ def process_frame():
65
+ data = request.json['image']
66
+
67
+ # Create a queue to hold the response from the background thread
68
+ response_queue = queue.Queue()
69
+
70
+ # Start the processing thread
71
+ thread = threading.Thread(target=process_frame_thread, args=(data, response_queue))
72
+ thread.start()
73
+
74
+ # Wait for the thread to complete and get the result from the queue
75
+ thread.join()
76
+ depth_map_base64 = response_queue.get()
77
+
78
+ return jsonify({'depth_map': depth_map_base64})
79
+
80
+ if __name__ == "__main__":
81
+ app.run(debug=True)