Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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()
|