huntrezz commited on
Commit
4f1fd81
·
verified ·
1 Parent(s): c376cbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -56
app.py CHANGED
@@ -3,10 +3,7 @@ import torch
3
  import numpy as np
4
  from transformers import DPTForDepthEstimation, DPTImageProcessor
5
  import gradio as gr
6
- import torch.quantization
7
  import torch.nn.utils.prune as prune
8
- import asyncio
9
- import queue
10
 
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
 
@@ -20,16 +17,17 @@ parameters_to_prune = [
20
  prune.global_unstructured(
21
  parameters_to_prune,
22
  pruning_method=prune.L1Unstructured,
23
- amount=0.3, # Prune 30% of weights
24
  )
25
 
26
  for module, _ in parameters_to_prune:
27
  prune.remove(module, "weight")
28
 
29
- # Apply quantization after pruning
30
  model = torch.quantization.quantize_dynamic(
31
  model, {torch.nn.Linear, torch.nn.Conv2d}, dtype=torch.qint8
32
- ).to(device)
 
 
33
 
34
  processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
35
 
@@ -37,11 +35,8 @@ color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFER
37
 
38
  input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float32, device=device)
39
 
40
- frame_queue = queue.Queue(maxsize=1)
41
- result_queue = queue.Queue(maxsize=1)
42
-
43
  def preprocess_image(image):
44
- return cv2.resize(image, (128, 72), interpolation=cv2.INTER_AREA).transpose(2, 0, 1).astype(np.float32) / 255.0
45
 
46
  @torch.inference_mode()
47
  def process_frame(image):
@@ -52,55 +47,17 @@ def process_frame(image):
52
 
53
  predicted_depth = model(input_tensor).predicted_depth
54
  depth_map = predicted_depth.squeeze().cpu().numpy()
55
- num_bins = 1000
56
- depth_map = np.digitize(depth_map, bins=np.linspace(depth_map.min(), depth_map.max(), num_bins)) - 1
57
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
58
  depth_map = (depth_map * 255).astype(np.uint8)
59
  depth_map_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
60
 
61
  return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
 
 
 
 
 
 
 
62
 
63
- async def capture_frames(webcam):
64
- frame_count = 0
65
- while True:
66
- ret, frame = webcam.read()
67
- if not ret:
68
- break
69
- frame_count += 1
70
- if frame_count % 5 == 0: # Process every 5th frame
71
- if frame_queue.full():
72
- frame_queue.get() # Remove old frame if queue is full
73
- frame_queue.put(frame)
74
- await asyncio.sleep(0.01) # Small delay to prevent blocking
75
-
76
- async def process_frames():
77
- while True:
78
- if not frame_queue.empty():
79
- frame = frame_queue.get()
80
- result = process_frame(frame)
81
- if result_queue.full():
82
- result_queue.get() # Remove old result if queue is full
83
- result_queue.put(result)
84
- await asyncio.sleep(0.01) # Small delay to prevent blocking
85
-
86
- def get_latest_frame():
87
- if result_queue.empty():
88
- return None
89
- return result_queue.get()
90
-
91
- async def main():
92
- webcam = cv2.VideoCapture(0)
93
- asyncio.create_task(capture_frames(webcam))
94
- asyncio.create_task(process_frames())
95
-
96
- interface = gr.Interface(
97
- fn=get_latest_frame,
98
- inputs=None,
99
- outputs="image",
100
- live=True
101
- )
102
-
103
- await interface.launch()
104
-
105
- if __name__ == "__main__":
106
- asyncio.run(main())
 
3
  import numpy as np
4
  from transformers import DPTForDepthEstimation, DPTImageProcessor
5
  import gradio as gr
 
6
  import torch.nn.utils.prune as prune
 
 
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
 
 
17
  prune.global_unstructured(
18
  parameters_to_prune,
19
  pruning_method=prune.L1Unstructured,
20
+ amount=0.4, # Prune 40% of weights
21
  )
22
 
23
  for module, _ in parameters_to_prune:
24
  prune.remove(module, "weight")
25
 
 
26
  model = torch.quantization.quantize_dynamic(
27
  model, {torch.nn.Linear, torch.nn.Conv2d}, dtype=torch.qint8
28
+ )
29
+
30
+ model = model.to(device)
31
 
32
  processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
33
 
 
35
 
36
  input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float32, device=device)
37
 
 
 
 
38
  def preprocess_image(image):
39
+ return cv2.resize(image, (128, 128), interpolation=cv2.INTER_AREA).transpose(2, 0, 1).astype(np.float32) / 255.0
40
 
41
  @torch.inference_mode()
42
  def process_frame(image):
 
47
 
48
  predicted_depth = model(input_tensor).predicted_depth
49
  depth_map = predicted_depth.squeeze().cpu().numpy()
 
 
50
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
51
  depth_map = (depth_map * 255).astype(np.uint8)
52
  depth_map_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
53
 
54
  return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
55
+
56
+ interface = gr.Interface(
57
+ fn=process_frame,
58
+ inputs=gr.Image(source="webcam", streaming=True),
59
+ outputs="image",
60
+ live=True
61
+ )
62
 
63
+ interface.launch()