prthm11 commited on
Commit
3a28f7c
·
verified ·
1 Parent(s): 92a55ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -9
app.py CHANGED
@@ -855,23 +855,51 @@ def similarity_matching(input_json_path: str, project_folder:str) -> str:
855
  # sprite_image_paths.append(temp_path)
856
  # print(f"\n\n\nSPRITE IMAGE PATHS: \n{sprite_image_paths}")
857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
858
  # ============================== #
859
  # EMBED SPRITE IMAGES #
860
  # ============================== #
861
- sprite_features = clip_embd.embed_image(sprite_base64)
 
 
 
 
 
 
 
 
 
 
 
862
 
863
  # ============================== #
864
  # COMPUTE SIMILARITIES #
865
  # ============================== #
866
- with open(f"{BLOCKS_DIR}/embeddings.json", "r") as f:
867
- embedding_json = json.load(f)
868
- # print(f"\n\n EMBEDDING JSON: {embedding_json}")
869
-
870
- img_matrix = np.array([img["embeddings"] for img in embedding_json])
871
- sprite_matrix = np.array(sprite_features)
872
-
873
  similarity = np.matmul(sprite_matrix, img_matrix.T)
874
-
875
  most_similar_indices = np.argmax(similarity, axis=1)
876
  print(f"")
877
  # ============= Match and copy ===============
 
855
  # sprite_image_paths.append(temp_path)
856
  # print(f"\n\n\nSPRITE IMAGE PATHS: \n{sprite_image_paths}")
857
 
858
+ # # ============================== #
859
+ # # EMBED SPRITE IMAGES #
860
+ # # ============================== #
861
+ # sprite_features = clip_embd.embed_image(sprite_base64)
862
+
863
+ # # ============================== #
864
+ # # COMPUTE SIMILARITIES #
865
+ # # ============================== #
866
+ # with open(f"{BLOCKS_DIR}/embeddings.json", "r") as f:
867
+ # embedding_json = json.load(f)
868
+ # # print(f"\n\n EMBEDDING JSON: {embedding_json}")
869
+
870
+ # img_matrix = np.array([img["embeddings"] for img in embedding_json])
871
+ # sprite_matrix = np.array(sprite_features)
872
+
873
+ # similarity = np.matmul(sprite_matrix, img_matrix.T)
874
+
875
+ # most_similar_indices = np.argmax(similarity, axis=1)
876
+ # ========================================= #
877
+ # Load reference embeddings from JSON file #
878
+ # ========================================= #
879
+ with open(f"{BLOCKS_DIR}/embeddings.json", "r") as f:
880
+ embedding_json = json.load(f)
881
+ img_matrix = np.array([img["embeddings"] for img in embedding_json])
882
+
883
  # ============================== #
884
  # EMBED SPRITE IMAGES #
885
  # ============================== #
886
+ sprite_features = []
887
+ for b64 in sprite_base64:
888
+ # strip data URI if present
889
+ if "," in b64:
890
+ b64 = b64.split(",", 1)[1]
891
+ img_bytes = base64.b64decode(b64)
892
+ pil_img = Image.open(BytesIO(img_bytes)).convert("RGB")
893
+ feats = clip_embd.embed_image(pil_img)
894
+ sprite_features.append(feats)
895
+
896
+ # stack into (n_sprites, dim)
897
+ sprite_matrix = np.vstack(sprite_features)
898
 
899
  # ============================== #
900
  # COMPUTE SIMILARITIES #
901
  # ============================== #
 
 
 
 
 
 
 
902
  similarity = np.matmul(sprite_matrix, img_matrix.T)
 
903
  most_similar_indices = np.argmax(similarity, axis=1)
904
  print(f"")
905
  # ============= Match and copy ===============