StarParty1972 commited on
Commit
9850cd4
ยท
verified ยท
1 Parent(s): 25bbdcc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchvision.transforms as transforms
4
+ from torchvision.models import mobilenet_v2, MobileNet_V2_Weights
5
+ from PIL import Image
6
+
7
+ weights = MobileNet_V2_Weights.DEFAULT
8
+ model = mobilenet_v2(weights=weights)
9
+ model.eval()
10
+
11
+ categories = weights.meta["categories"]
12
+
13
+ transform = transforms.Compose([
14
+ transforms.Resize(256),
15
+ transforms.CenterCrop(224),
16
+ transforms.ToTensor(),
17
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
18
+ ])
19
+
20
+ def predict(image):
21
+ if image is None:
22
+ return "์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”."
23
+
24
+ img = Image.fromarray(image.astype('uint8'), 'RGB')
25
+ img_t = transform(img)
26
+ batch_t = torch.unsqueeze(img_t, 0)
27
+
28
+ with torch.no_grad():
29
+ output = model(batch_t)
30
+
31
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
32
+
33
+ top3_prob, top3_catid = torch.topk(probabilities, 3)
34
+
35
+ result = {}
36
+ for i in range(top3_prob.size(0)):
37
+ label = categories[top3_catid[i].item()]
38
+ score = top3_prob[i].item()
39
+ result[label] = score
40
+
41
+ return result
42
+
43
+ demo = gr.Interface(
44
+ fn=predict,
45
+ inputs=gr.Image(),
46
+ outputs=gr.Label(num_top_classes=3),
47
+ title="๐Ÿ›’ AI ์‹ค์‹œ๊ฐ„ ์‚ฌ๋ฌผ ์ธ์‹๊ธฐ (Object Classifier)",
48
+ description="์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•˜๋ฉด AI๊ฐ€ ์ตœ๋Œ€ํ•œ ์ •ํ™•ํ•˜๊ฒŒ ๋งž์ถฐ๋“œ๋ฆฌ์ฃ "
49
+ )
50
+
51
+ # ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰
52
+ if __name__ == "__main__":
53
+ demo.launch()