Spaces:
Sleeping
Sleeping
Commit
·
2f73dc1
1
Parent(s):
bf67113
update syntax connection
Browse files- clip_component.py +9 -5
clip_component.py
CHANGED
|
@@ -6,7 +6,7 @@ import clip
|
|
| 6 |
|
| 7 |
similarity_threshold = 22.00
|
| 8 |
def get_token_from_clip(image):
|
| 9 |
-
text_inputs = ["apple", "banana", "
|
| 10 |
text_tokens = clip.tokenize(text_inputs)
|
| 11 |
|
| 12 |
device = "cpu"
|
|
@@ -28,18 +28,22 @@ def get_token_from_clip(image):
|
|
| 28 |
similarity = text_features.cpu().numpy() @ image_feature.cpu().numpy().T
|
| 29 |
|
| 30 |
results = []
|
| 31 |
-
|
| 32 |
|
| 33 |
for i in range(similarity.shape[0]):
|
| 34 |
similarity_num = (100.0 * similarity[i][0])
|
| 35 |
text_input = text_inputs[i]
|
| 36 |
results.append({"text_input": text_input, "similarity": similarity_num})
|
| 37 |
-
if similarity_num >= similarity_threshold:
|
| 38 |
-
detect_food += " " + text_input + " ."
|
| 39 |
# print(similarity_num)
|
| 40 |
-
|
| 41 |
results.sort(key=lambda x: x["similarity"], reverse=True)
|
| 42 |
# Print the caption for each text input along with their similarity scores
|
|
|
|
|
|
|
| 43 |
for result in results:
|
| 44 |
print(f"Text input: {result['text_input']}, Similarity: {result['similarity']:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return detect_food_list
|
|
|
|
| 6 |
|
| 7 |
similarity_threshold = 22.00
|
| 8 |
def get_token_from_clip(image):
|
| 9 |
+
text_inputs = ["apple", "banana", "cereal", "milk", "lemon", "orange", "salad", "juice", "chicken", "bread"]
|
| 10 |
text_tokens = clip.tokenize(text_inputs)
|
| 11 |
|
| 12 |
device = "cpu"
|
|
|
|
| 28 |
similarity = text_features.cpu().numpy() @ image_feature.cpu().numpy().T
|
| 29 |
|
| 30 |
results = []
|
| 31 |
+
|
| 32 |
|
| 33 |
for i in range(similarity.shape[0]):
|
| 34 |
similarity_num = (100.0 * similarity[i][0])
|
| 35 |
text_input = text_inputs[i]
|
| 36 |
results.append({"text_input": text_input, "similarity": similarity_num})
|
|
|
|
|
|
|
| 37 |
# print(similarity_num)
|
| 38 |
+
|
| 39 |
results.sort(key=lambda x: x["similarity"], reverse=True)
|
| 40 |
# Print the caption for each text input along with their similarity scores
|
| 41 |
+
|
| 42 |
+
detect_food = ""
|
| 43 |
for result in results:
|
| 44 |
print(f"Text input: {result['text_input']}, Similarity: {result['similarity']:.2f}")
|
| 45 |
+
if result['similarity'] >= similarity_threshold:
|
| 46 |
+
detect_food += " " + text_input + " ."
|
| 47 |
+
|
| 48 |
+
detect_food_list = detect_food[1:]
|
| 49 |
return detect_food_list
|