Spaces:
Sleeping
Sleeping
create prototype for is someone is there
Browse files- charles_actor.py +11 -2
- clip_transform.py +7 -0
- prototypes.py +71 -0
- prototypes/no_person-001.jpg +3 -0
- prototypes/no_person-002.jpg +3 -0
- prototypes/no_person-003.jpg +3 -0
- prototypes/no_person-004.jpg +3 -0
- prototypes/no_person-005.jpg +3 -0
- prototypes/no_person-006.jpg +3 -0
- prototypes/no_person-007.jpg +3 -0
- prototypes/no_person-008.jpg +3 -0
- prototypes/no_person-009.jpg +3 -0
- prototypes/no_person-010.jpg +3 -0
- prototypes/no_person-011.jpg +3 -0
- prototypes/no_person-012.jpg +3 -0
- prototypes/no_person-013.jpg +3 -0
- prototypes/no_person-014.jpg +3 -0
- prototypes/no_person-015.jpg +3 -0
- prototypes/no_person-016.jpg +3 -0
- prototypes/no_person-017.jpg +3 -0
- prototypes/no_person-018.jpg +3 -0
- prototypes/no_person-019.jpg +3 -0
- prototypes/no_person-020.jpg +3 -0
- prototypes/person-001.jpg +3 -0
- prototypes/person-002.jpg +3 -0
- prototypes/person-003.jpg +3 -0
- prototypes/person-004.jpg +3 -0
- prototypes/person-005.jpg +3 -0
- prototypes/person-006.jpg +3 -0
- prototypes/person-007.jpg +3 -0
- prototypes/person-008.jpg +3 -0
- prototypes/person-009.jpg +3 -0
- prototypes/person-010.jpg +3 -0
- prototypes/person-011.jpg +3 -0
- prototypes/person-012.jpg +3 -0
- prototypes/person-013.jpg +3 -0
- prototypes/person-014.jpg +3 -0
- prototypes/person-015.jpg +3 -0
- prototypes/person-016.jpg +3 -0
- prototypes/person-017.jpg +3 -0
- prototypes/person-018.jpg +3 -0
- prototypes/person-019.jpg +3 -0
- prototypes/person-020.jpg +3 -0
charles_actor.py
CHANGED
@@ -44,6 +44,11 @@ class CharlesActor:
|
|
44 |
# "hello, how are you today?",
|
45 |
# "hmm, interesting, tell me more about that.",
|
46 |
]
|
|
|
|
|
|
|
|
|
|
|
47 |
print("010")
|
48 |
self._needs_init = True
|
49 |
self._state = "Initialized"
|
@@ -99,8 +104,12 @@ class CharlesActor:
|
|
99 |
total_video_frames += 1
|
100 |
skipped_video_frames += (len(video_frames) -1)
|
101 |
image_as_array = video_frames[-1]
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
104 |
|
105 |
await asyncio.sleep(0.01)
|
106 |
loops+=1
|
|
|
44 |
# "hello, how are you today?",
|
45 |
# "hmm, interesting, tell me more about that.",
|
46 |
]
|
47 |
+
|
48 |
+
print("004")
|
49 |
+
print("creating prototypes")
|
50 |
+
from prototypes import Prototypes
|
51 |
+
self._prototypes = Prototypes()
|
52 |
print("010")
|
53 |
self._needs_init = True
|
54 |
self._state = "Initialized"
|
|
|
104 |
total_video_frames += 1
|
105 |
skipped_video_frames += (len(video_frames) -1)
|
106 |
image_as_array = video_frames[-1]
|
107 |
+
image_vector = self._clip_transform.image_to_embeddings(image_as_array)
|
108 |
+
# image_vector = image_vector.unsqueeze(0)
|
109 |
+
image_vector = image_vector[0]
|
110 |
+
print(f"image_vector.shape: {image_vector.shape}")
|
111 |
+
distances, closest_item_key, distance_debug_str = self._prototypes.get_distances(image_vector)
|
112 |
+
vector_debug = f"{closest_item_key} {distance_debug_str}"
|
113 |
|
114 |
await asyncio.sleep(0.01)
|
115 |
loops+=1
|
clip_transform.py
CHANGED
@@ -48,6 +48,13 @@ class CLIPTransform:
|
|
48 |
image_embeddings = self.model.encode_image(prepro)
|
49 |
image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
|
50 |
return(image_embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def preprocessed_image_to_emdeddings(self, prepro):
|
53 |
with torch.no_grad():
|
|
|
48 |
image_embeddings = self.model.encode_image(prepro)
|
49 |
image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
|
50 |
return(image_embeddings)
|
51 |
+
|
52 |
+
def pil_image_to_embeddings(self, input_im):
|
53 |
+
prepro = self.preprocess(input_im).unsqueeze(0).to(self.device)
|
54 |
+
with torch.no_grad():
|
55 |
+
image_embeddings = self.model.encode_image(prepro)
|
56 |
+
image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
|
57 |
+
return(image_embeddings)
|
58 |
|
59 |
def preprocessed_image_to_emdeddings(self, prepro):
|
60 |
with torch.no_grad():
|
prototypes.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from clip_transform import CLIPTransform
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
from torch.nn import functional as F
|
9 |
+
|
10 |
+
class Prototypes:
|
11 |
+
def __init__(self):
|
12 |
+
self._clip_transform = CLIPTransform()
|
13 |
+
self._prepare_prototypes()
|
14 |
+
|
15 |
+
|
16 |
+
def _prepare_prototypes(self):
|
17 |
+
image_embeddings = self.load_images_from_folder('prototypes')
|
18 |
+
assert image_embeddings is not None, "no image embeddings found"
|
19 |
+
assert len(image_embeddings) > 0, "no image embeddings found"
|
20 |
+
person_keys = [key for key in image_embeddings.keys() if key.startswith('person-')]
|
21 |
+
no_person_keys = [key for key in image_embeddings.keys() if key.startswith('no_person-')]
|
22 |
+
person_keys.sort()
|
23 |
+
no_person_keys.sort()
|
24 |
+
# create pytorch vector of person embeddings
|
25 |
+
person_embeddings = torch.cat([image_embeddings[key] for key in person_keys])
|
26 |
+
# create pytorch vector of no_person embeddings
|
27 |
+
no_person_embeddings = torch.cat([image_embeddings[key] for key in no_person_keys])
|
28 |
+
person_embedding = person_embeddings.mean(dim=0)
|
29 |
+
person_embedding /= person_embedding.norm(dim=-1, keepdim=True)
|
30 |
+
no_person_embedding = no_person_embeddings.mean(dim=0)
|
31 |
+
no_person_embedding /= no_person_embedding.norm(dim=-1, keepdim=True)
|
32 |
+
|
33 |
+
self.prototype_keys = ["person", "no_person"]
|
34 |
+
self.prototypes = torch.stack([person_embedding, no_person_embedding])
|
35 |
+
|
36 |
+
|
37 |
+
def load_images_from_folder(self, folder):
|
38 |
+
image_embeddings = {}
|
39 |
+
supported_filetypes = ['.jpg','.png','.jpeg']
|
40 |
+
for filename in os.listdir(folder):
|
41 |
+
if not any([filename.endswith(ft) for ft in supported_filetypes]):
|
42 |
+
continue
|
43 |
+
image = Image.open(os.path.join(folder,filename))
|
44 |
+
embeddings = self._clip_transform.pil_image_to_embeddings(image)
|
45 |
+
image_embeddings[filename] = embeddings
|
46 |
+
return image_embeddings
|
47 |
+
|
48 |
+
def get_distances(self, embeddings):
|
49 |
+
# case not normalized
|
50 |
+
# distances = F.cosine_similarity(embeddings, self.prototypes)
|
51 |
+
# case normalized
|
52 |
+
distances = embeddings @ self.prototypes.T
|
53 |
+
closest_item_idex = distances.argmax().item()
|
54 |
+
closest_item_key = self.prototype_keys[closest_item_idex]
|
55 |
+
debug_str = ""
|
56 |
+
for key, value in zip(self.prototype_keys, distances):
|
57 |
+
debug_str += f"{key}: {value.item():.2f}, "
|
58 |
+
return distances, closest_item_key, debug_str
|
59 |
+
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
prototypes = Prototypes()
|
63 |
+
print ("prototypes:")
|
64 |
+
for key, value in zip(prototypes.prototype_keys, prototypes.prototypes):
|
65 |
+
print (f"{key}: {len(value)}")
|
66 |
+
|
67 |
+
embeddings = prototypes.prototypes[0]
|
68 |
+
distances, closest_item_key, debug_str = prototypes.get_distances(embeddings)
|
69 |
+
print (f"closest_item_key: {closest_item_key}")
|
70 |
+
print (f"distances: {debug_str}")
|
71 |
+
print ("done")
|
prototypes/no_person-001.jpg
ADDED
Git LFS Details
|
prototypes/no_person-002.jpg
ADDED
Git LFS Details
|
prototypes/no_person-003.jpg
ADDED
Git LFS Details
|
prototypes/no_person-004.jpg
ADDED
Git LFS Details
|
prototypes/no_person-005.jpg
ADDED
Git LFS Details
|
prototypes/no_person-006.jpg
ADDED
Git LFS Details
|
prototypes/no_person-007.jpg
ADDED
Git LFS Details
|
prototypes/no_person-008.jpg
ADDED
Git LFS Details
|
prototypes/no_person-009.jpg
ADDED
Git LFS Details
|
prototypes/no_person-010.jpg
ADDED
Git LFS Details
|
prototypes/no_person-011.jpg
ADDED
Git LFS Details
|
prototypes/no_person-012.jpg
ADDED
Git LFS Details
|
prototypes/no_person-013.jpg
ADDED
Git LFS Details
|
prototypes/no_person-014.jpg
ADDED
Git LFS Details
|
prototypes/no_person-015.jpg
ADDED
Git LFS Details
|
prototypes/no_person-016.jpg
ADDED
Git LFS Details
|
prototypes/no_person-017.jpg
ADDED
Git LFS Details
|
prototypes/no_person-018.jpg
ADDED
Git LFS Details
|
prototypes/no_person-019.jpg
ADDED
Git LFS Details
|
prototypes/no_person-020.jpg
ADDED
Git LFS Details
|
prototypes/person-001.jpg
ADDED
Git LFS Details
|
prototypes/person-002.jpg
ADDED
Git LFS Details
|
prototypes/person-003.jpg
ADDED
Git LFS Details
|
prototypes/person-004.jpg
ADDED
Git LFS Details
|
prototypes/person-005.jpg
ADDED
Git LFS Details
|
prototypes/person-006.jpg
ADDED
Git LFS Details
|
prototypes/person-007.jpg
ADDED
Git LFS Details
|
prototypes/person-008.jpg
ADDED
Git LFS Details
|
prototypes/person-009.jpg
ADDED
Git LFS Details
|
prototypes/person-010.jpg
ADDED
Git LFS Details
|
prototypes/person-011.jpg
ADDED
Git LFS Details
|
prototypes/person-012.jpg
ADDED
Git LFS Details
|
prototypes/person-013.jpg
ADDED
Git LFS Details
|
prototypes/person-014.jpg
ADDED
Git LFS Details
|
prototypes/person-015.jpg
ADDED
Git LFS Details
|
prototypes/person-016.jpg
ADDED
Git LFS Details
|
prototypes/person-017.jpg
ADDED
Git LFS Details
|
prototypes/person-018.jpg
ADDED
Git LFS Details
|
prototypes/person-019.jpg
ADDED
Git LFS Details
|
prototypes/person-020.jpg
ADDED
Git LFS Details
|