File size: 1,260 Bytes
049c619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
from models.blip_vqa import blip_vqa
import torch
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
image_size = 480

transform = transforms.Compose([
    transforms.Resize((image_size,image_size),interpolation=InterpolationMode.BICUBIC),
    transforms.ToTensor(),
    transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
        ])

model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth'
model = blip_vqa(pretrained=model_url, image_size=image_size, vit='base')
model.eval()
model = model.to(device)


def pool_alarm(raw_image):
    question = 'there is someone in the pool?'
    image = transform(raw_image).unsqueeze(0).to(device)
    with torch.no_grad():
        answer = model(image, question, train=False, inference='generate')

    return 'answer: ' + answer[0]


input = gr.inputs.Image(type='pil')
output = gr.outputs.Textbox()
examples = ['alarm.jpeg', 'alarm1.jpeg', 'walk.jpeg']
title = ""
description = ""
intf = gr.Interface(fn=pool_alarm, inputs=input, outputs=output, examples=examples).launch()