Spaces:
Build error
Build error
LuisV commited on
Commit ·
cdbe626
1
Parent(s): fc1f075
creating funciton that extracts all information from image (colors, objects, emotion)
Browse files
imageprocessing/imageprocessingtools.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys, os
|
| 2 |
+
sys.path.append(os.path.join(os.curdir, "artemis"))
|
| 3 |
+
|
| 4 |
+
import artemis
|
| 5 |
+
from color_detection import get_colors
|
| 6 |
+
from clip_object_recognition import get_objects_in_image
|
| 7 |
+
from emotion_detection import get_all_emotions_in_image
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
import clip
|
| 13 |
+
|
| 14 |
+
clip_device = "cpu"#"cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
clip_model, clip_preprocess = clip.load('ViT-B/32', clip_device)
|
| 16 |
+
|
| 17 |
+
emo_device = "cpu"
|
| 18 |
+
img2emo_model = torch.load(
|
| 19 |
+
"img2emo.pt",
|
| 20 |
+
map_location=emo_device
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def extract_all_information_from_image(
|
| 25 |
+
image_filepath : os.PathLike
|
| 26 |
+
)-> dict:
|
| 27 |
+
"""Extracts objects (and probabilities), colors, and emotion from the image.
|
| 28 |
+
|
| 29 |
+
Parameters
|
| 30 |
+
----------
|
| 31 |
+
image_filepath : os.PathLike
|
| 32 |
+
Path to the image
|
| 33 |
+
|
| 34 |
+
Returns
|
| 35 |
+
-------
|
| 36 |
+
dict
|
| 37 |
+
Dictionary with the objects, colors, and emotion from the image
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
colors = get_colors(image_filepath)
|
| 41 |
+
|
| 42 |
+
objects_and_probs = get_objects_in_image(
|
| 43 |
+
image_filepath = image_filepath,
|
| 44 |
+
model = clip_model,
|
| 45 |
+
preprocess = clip_preprocess,
|
| 46 |
+
device = clip_device
|
| 47 |
+
)
|
| 48 |
+
emotion, _ = get_all_emotions_in_image(
|
| 49 |
+
filepath = image_filepath,
|
| 50 |
+
model = img2emo_model,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
result = {
|
| 54 |
+
"colors": colors,
|
| 55 |
+
"objects_and_probs" : objects_and_probs,
|
| 56 |
+
"emotion": emotion
|
| 57 |
+
}
|
| 58 |
+
return result
|