Spaces:
Sleeping
Sleeping
add code to app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from torchvision import models
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch
|
6 |
+
import os
|
7 |
+
from PIL import Image
|
8 |
+
from torchvision.transforms import transforms
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
load_dotenv()
|
11 |
|
12 |
+
share = os.getenv("SHARE", False)
|
13 |
+
pretrained_model = models.vgg19(pretrained=True)
|
14 |
+
class NeuralNet(nn.Module):
|
15 |
+
def __init__(self):
|
16 |
+
super().__init__()
|
17 |
+
self.model = nn.Sequential(
|
18 |
+
pretrained_model,
|
19 |
+
nn.Flatten(),
|
20 |
+
nn.Linear(1000, 1),
|
21 |
+
nn.Sigmoid()
|
22 |
+
)
|
23 |
+
|
24 |
+
def forward(self, x):
|
25 |
+
return self.model(x)
|
26 |
+
|
27 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
28 |
+
|
29 |
+
model = NeuralNet()
|
30 |
+
|
31 |
+
model.load_state_dict(torch.load("mask_detection.pth", map_location=device))
|
32 |
+
|
33 |
+
model = model.to(device)
|
34 |
+
|
35 |
+
transform=transforms.Compose([
|
36 |
+
transforms.Resize((150,150)),
|
37 |
+
transforms.RandomHorizontalFlip(),
|
38 |
+
transforms.ToTensor(),
|
39 |
+
transforms.Normalize([0.5, 0.5, 0.5],[0.5, 0.5, 0.5])
|
40 |
+
])
|
41 |
+
|
42 |
+
def greet(image):
|
43 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
44 |
+
image.save("input.png")
|
45 |
+
image = Image.open("input.png")
|
46 |
+
input = transform(image).unsqueeze(0)
|
47 |
+
output = model(input.to(device))
|
48 |
+
probability = output.item()
|
49 |
+
if probability < 0.5:
|
50 |
+
return "Person in the pic has mask"
|
51 |
+
else:
|
52 |
+
return "Person in the pic does not have mask"
|
53 |
+
|
54 |
+
iface = gr.Interface(fn=greet, inputs="image", outputs="text")
|
55 |
iface.launch()
|