hrishikesh commited on
Commit
b92347d
·
1 Parent(s): 056546a

Create imageclassifier.py

Browse files
Files changed (1) hide show
  1. imageclassifier.py +32 -0
imageclassifier.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """imageClassifier.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1S-yO7cqOfeKz8Iu1h-DwhTgkkGml0tKb
8
+ """
9
+
10
+ pip install git+https://github.com/huggingface/transformers.git
11
+
12
+ from transformers import ViTFeatureExtractor, ViTForImageClassification
13
+ from PIL import Image
14
+ import requests
15
+
16
+ url = 'https://www.livechennai.com/businesslistings/News_photo/dosa11218.jpg'
17
+ image = Image.open(requests.get(url, stream=True).raw)
18
+
19
+ display(image)
20
+
21
+ feature_extractor = ViTFeatureExtractor.from_pretrained("Amrrs/south-indian-foods")
22
+ model = ViTForImageClassification.from_pretrained("Amrrs/south-indian-foods")
23
+
24
+ inputs = feature_extractor(images=image, return_tensors="pt")
25
+
26
+ outputs = model(**inputs)
27
+
28
+ logits = outputs.logits
29
+
30
+ predicted_class_idx = logits.argmax(-1).item()
31
+ print("Predicted class:", model.config.id2label[predicted_class_idx])
32
+