bnsapa commited on
Commit
5ee4460
1 Parent(s): d908411

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -55
app.py CHANGED
@@ -1,55 +1,60 @@
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()
 
 
 
 
 
 
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 maskDetection(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=maskDetection, inputs="image", outputs="text", title="Mask Detection")
55
+ if __name__ == "__main__":
56
+ if share:
57
+ server = "0.0.0.0"
58
+ else:
59
+ server = "127.0.0.1"
60
+ iface.launch(server_name = server)