Update run.py
Browse files
run.py
CHANGED
@@ -1,8 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
from time import sleep
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
Flips the image periodically with the given interval.
|
8 |
|
@@ -13,13 +22,18 @@ def flip_periodically(im, interval_ms=100):
|
|
13 |
Returns:
|
14 |
The flipped image.
|
15 |
"""
|
|
|
|
|
|
|
16 |
sleep(interval_ms / 1000) # Convert milliseconds to seconds
|
17 |
-
return np.flipud(im)
|
18 |
|
19 |
with gr.Blocks() as demo:
|
20 |
inp = gr.Image(sources=["webcam"], streaming=True)
|
21 |
out = gr.Image()
|
22 |
inp.stream(flip_periodically, inputs=inp, outputs=out)
|
23 |
|
|
|
24 |
if __name__ == "__main__":
|
|
|
25 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
from time import sleep
|
4 |
+
import torch
|
5 |
+
from transformers import SegformerImageProcessor
|
6 |
|
7 |
+
image_processor = SegformerImageProcessor(reduce_labels=True)
|
8 |
+
|
9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
model.load_state_dict(torch.load(weights2load, weights_only=True, map_location=device))
|
11 |
+
model.to(device).eval()
|
12 |
+
|
13 |
+
|
14 |
+
def flip_periodically(im, interval_ms=0):
|
15 |
"""
|
16 |
Flips the image periodically with the given interval.
|
17 |
|
|
|
22 |
Returns:
|
23 |
The flipped image.
|
24 |
"""
|
25 |
+
pixel_values = image_processor(image, return_tensors="pt").pixel_values.to(device)
|
26 |
+
outputs = model(pixel_values=pixel_values)
|
27 |
+
logits = outputs.logits.cpu()
|
28 |
sleep(interval_ms / 1000) # Convert milliseconds to seconds
|
29 |
+
return logits[0, 0] #np.flipud(im)
|
30 |
|
31 |
with gr.Blocks() as demo:
|
32 |
inp = gr.Image(sources=["webcam"], streaming=True)
|
33 |
out = gr.Image()
|
34 |
inp.stream(flip_periodically, inputs=inp, outputs=out)
|
35 |
|
36 |
+
|
37 |
if __name__ == "__main__":
|
38 |
+
|
39 |
demo.launch()
|