Spaces:
Sleeping
Sleeping
Download app.py from StarParty1972/object-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/StarParty1972/object-classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/StarParty1972/object-classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/StarParty1972/object-classifier/resolve/main/app.py
1.49 kB
| import gradio as gr | |
| import torch | |
| import torchvision.transforms as transforms | |
| from torchvision.models import mobilenet_v2, MobileNet_V2_Weights | |
| from PIL import Image | |
| weights = MobileNet_V2_Weights.DEFAULT | |
| model = mobilenet_v2(weights=weights) | |
| model.eval() | |
| categories = weights.meta["categories"] | |
| transform = transforms.Compose([ | |
| transforms.Resize(256), | |
| transforms.CenterCrop(224), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | |
| ]) | |
| def predict(image): | |
| if image is None: | |
| return "์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํด์ฃผ์ธ์." | |
| img = Image.fromarray(image.astype('uint8'), 'RGB') | |
| img_t = transform(img) | |
| batch_t = torch.unsqueeze(img_t, 0) | |
| with torch.no_grad(): | |
| output = model(batch_t) | |
| probabilities = torch.nn.functional.softmax(output[0], dim=0) | |
| top3_prob, top3_catid = torch.topk(probabilities, 3) | |
| result = {} | |
| for i in range(top3_prob.size(0)): | |
| label = categories[top3_catid[i].item()] | |
| score = top3_prob[i].item() | |
| result[label] = score | |
| return result | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(), | |
| outputs=gr.Label(num_top_classes=3), | |
| title="๐ AI ์ค์๊ฐ ์ฌ๋ฌผ ์ธ์๊ธฐ (Object Classifier)", | |
| description="์ฌ์ง์ ์ ๋ก๋ํ๋ฉด AI๊ฐ ์ต๋ํ ์ ํํ๊ฒ ๋ง์ถฐ๋๋ฆฌ์ฃ " | |
| ) | |
| # ํ๋ก๊ทธ๋จ ์คํ | |
| if __name__ == "__main__": | |
| demo.launch() |