huntrezz commited on
Commit
c376cbb
·
verified ·
1 Parent(s): 813ce65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -8
app.py CHANGED
@@ -5,6 +5,8 @@ from transformers import DPTForDepthEstimation, DPTImageProcessor
5
  import gradio as gr
6
  import torch.quantization
7
  import torch.nn.utils.prune as prune
 
 
8
 
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
 
@@ -18,7 +20,7 @@ parameters_to_prune = [
18
  prune.global_unstructured(
19
  parameters_to_prune,
20
  pruning_method=prune.L1Unstructured,
21
- amount=0.8, # Prune 80% of weights
22
  )
23
 
24
  for module, _ in parameters_to_prune:
@@ -35,6 +37,9 @@ color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFER
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, 72), interpolation=cv2.INTER_AREA).transpose(2, 0, 1).astype(np.float32) / 255.0
40
 
@@ -55,11 +60,47 @@ def process_frame(image):
55
 
56
  return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
57
 
58
- interface = gr.Interface(
59
- fn=process_frame,
60
- inputs=gr.Image(sources="webcam", streaming=True),
61
- outputs="image",
62
- live=True
63
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- interface.launch()
 
 
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
  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:
 
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
 
 
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())