using global counter
Browse files
run.py
CHANGED
@@ -5,6 +5,8 @@ import torch
|
|
5 |
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
6 |
# from torchvision import transforms
|
7 |
|
|
|
|
|
8 |
class Count:
|
9 |
def __init__(self):
|
10 |
self.n = 0
|
@@ -13,7 +15,8 @@ class Count:
|
|
13 |
def step(self):
|
14 |
self.n += 1
|
15 |
|
16 |
-
|
|
|
17 |
weights2load = 'segformer_ep15_loss0.00.pth'
|
18 |
id2label = {0: 'seal', 255: 'bck'}
|
19 |
label2id = {'seal': 0, 'bck': 255}
|
@@ -31,23 +34,15 @@ model.to(device).eval()
|
|
31 |
counter = Count()
|
32 |
|
33 |
def flip_periodically(im, interval_s=2):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
im: The input image.
|
39 |
-
interval_ms: The interval in milliseconds between flips.
|
40 |
-
|
41 |
-
Returns:
|
42 |
-
The flipped image.
|
43 |
-
"""
|
44 |
-
counter.step()
|
45 |
-
if (counter.imout.sum() == 0) or ((counter.n % 2) == 0):
|
46 |
pixel_values = image_processor(im, return_tensors="pt").pixel_values.to(device)
|
47 |
outputs = model(pixel_values=pixel_values)
|
48 |
logits = outputs.logits.cpu().detach().numpy() ** 2
|
49 |
counter.imout = (logits[0, 0] - logits[0, 0].min()) / (logits[0, 0].max() - logits[0, 0].min())
|
50 |
-
return counter.imout,
|
51 |
|
52 |
with gr.Blocks() as demo:
|
53 |
inp = gr.Image(sources=["webcam"], streaming=True)
|
|
|
5 |
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
6 |
# from torchvision import transforms
|
7 |
|
8 |
+
|
9 |
+
|
10 |
class Count:
|
11 |
def __init__(self):
|
12 |
self.n = 0
|
|
|
15 |
def step(self):
|
16 |
self.n += 1
|
17 |
|
18 |
+
|
19 |
+
cnt = 0
|
20 |
weights2load = 'segformer_ep15_loss0.00.pth'
|
21 |
id2label = {0: 'seal', 255: 'bck'}
|
22 |
label2id = {'seal': 0, 'bck': 255}
|
|
|
34 |
counter = Count()
|
35 |
|
36 |
def flip_periodically(im, interval_s=2):
|
37 |
+
global cnt # Access the global counter variable
|
38 |
+
cnt += 1
|
39 |
+
# counter.step()
|
40 |
+
if (counter.imout.sum() == 0) or ((cnt % 100) == 0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
pixel_values = image_processor(im, return_tensors="pt").pixel_values.to(device)
|
42 |
outputs = model(pixel_values=pixel_values)
|
43 |
logits = outputs.logits.cpu().detach().numpy() ** 2
|
44 |
counter.imout = (logits[0, 0] - logits[0, 0].min()) / (logits[0, 0].max() - logits[0, 0].min())
|
45 |
+
return counter.imout, cnt #np.flipud(im)
|
46 |
|
47 |
with gr.Blocks() as demo:
|
48 |
inp = gr.Image(sources=["webcam"], streaming=True)
|