commit1
Browse files- FoodNoFood.py +16 -0
FoodNoFood.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 5 |
+
|
| 6 |
+
def food_not_food(input_image):
|
| 7 |
+
model = CLIPModel.from_pretrained("flax-community/clip-rsicd-v2")
|
| 8 |
+
processor = CLIPProcessor.from_pretrained("flax-community/clip-rsicd-v2")
|
| 9 |
+
|
| 10 |
+
labels = ["food", "not food"]
|
| 11 |
+
inputs = processor(text=[f"a photo of a {l}" for l in labels], images=input_image, return_tensors="pt", padding=True)
|
| 12 |
+
|
| 13 |
+
outputs = model(**inputs)
|
| 14 |
+
logits_per_image = outputs.logits_per_image
|
| 15 |
+
prob = logits_per_image.softmax(dim=1).detach().cpu().numpy().argmax(axis=1)
|
| 16 |
+
return labels[prob[0]]
|