prthm11 commited on
Commit
4e5cbf0
·
verified ·
1 Parent(s): 24eb3f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -202
app.py CHANGED
@@ -1843,110 +1843,6 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
1843
  matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
1844
  print("matched_indices------------------>",matched_indices)
1845
 
1846
- # from pathlib import Path
1847
-
1848
- # # normalize & resolve once at top of function
1849
- # sprite_base_p = Path(sprite_base_path).resolve()
1850
- # backdrop_base_p = Path(backdrop_base_path).resolve()
1851
- # project_folder_p = Path(project_folder).resolve()
1852
- # project_folder_p.mkdir(parents=True, exist_ok=True) # ensure dest exists
1853
-
1854
- # for matched_idx in matched_indices:
1855
- # matched_image_path = paths_list[matched_idx]
1856
- # print("matched_image_path ---1-->",matched_image_path)
1857
- # matched_folder = Path(matched_image_path).parent
1858
- # print("matched_folder ---2-->",matched_folder)
1859
- # matched_filename = Path(matched_image_path).name
1860
- # print("matched_filename ---3-->",matched_filename)
1861
-
1862
-
1863
- # # resolve matched_folder (safe even if it's already absolute)
1864
- # try:
1865
- # matched_folder_p = matched_folder.resolve()
1866
- # except Exception:
1867
- # # fallback to absolute path join if resolve fails
1868
- # matched_folder_p = (Path.cwd() / matched_folder).resolve()
1869
-
1870
- # # print(f"Processing matched image: {matched_image_path}")
1871
- # # print(f" matched_folder_p = {matched_folder_p}")
1872
- # # print(f" sprite_base_p = {sprite_base_p}")
1873
- # # print(f" backdrop_base_p = {backdrop_base_p}")
1874
-
1875
- # # Check if matched_folder is under sprite_base_p
1876
- # is_sprite = False
1877
- # try:
1878
- # matched_folder_p.relative_to(sprite_base_p)
1879
- # is_sprite = True
1880
- # print("matched_folder_p--------->",matched_folder_p)
1881
- # print("is_sprite---------->",is_sprite)
1882
- # except Exception:
1883
- # is_sprite = False
1884
- # print("is_sprite---------->",is_sprite)
1885
-
1886
- # if is_sprite and str(matched_folder) not in copied_sprite_folders:
1887
- # print(f"Processing SPRITE folder: {matched_folder}")
1888
- # copied_sprite_folders.add(str(matched_folder))
1889
- # sprite_json_path = matched_folder / "sprite.json"
1890
- # # ... (rest of your logic, but use Path objects)
1891
- # sprite_files = list(matched_folder.iterdir())
1892
- # print(f" Files in sprite folder: {[p.name for p in sprite_files]}")
1893
- # for p in sprite_files:
1894
- # fname = p.name
1895
- # if fname in (matched_filename, "sprite.json"):
1896
- # print(f" Skipping {fname} (matched image or sprite.json)")
1897
- # continue
1898
- # if p.is_file():
1899
- # dst = project_folder_p / fname
1900
- # try:
1901
- # shutil.copy2(str(p), str(dst))
1902
- # print(f" ✓ Copied sprite asset: {p} -> {dst}")
1903
- # except Exception as e:
1904
- # print(f" ✗ Failed to copy sprite asset {p}: {e}")
1905
- # else:
1906
- # print(f" Skipping {fname} (not a file)")
1907
-
1908
- # # Check if matched_folder is under backdrop_base_p
1909
- # is_backdrop = False
1910
- # try:
1911
- # matched_folder.relative_to(backdrop_base_p)
1912
- # print("matched_folder_p--------->",matched_folder_p)
1913
- # is_backdrop = True
1914
- # print("is_backdrop---------->",is_backdrop)
1915
- # except Exception:
1916
- # is_backdrop = False
1917
- # print("is_backdrop---------->",is_backdrop)
1918
-
1919
- # if is_backdrop and str(matched_folder) not in copied_backdrop_folders:
1920
- # print(f"Processing BACKDROP folder: {matched_folder}")
1921
- # copied_backdrop_folders.add(str(matched_folder))
1922
- # # copy matched backdrop image
1923
- # backdrop_dst = project_folder_p / matched_filename
1924
- # try:
1925
- # shutil.copy2(str(matched_folder / matched_filename), str(backdrop_dst))
1926
- # print(f" ✓ Copied matched backdrop image: {matched_folder / matched_filename} -> {backdrop_dst}")
1927
- # except Exception as e:
1928
- # print(f" ✗ Failed to copy matched backdrop image {matched_folder / matched_filename}: {e}")
1929
-
1930
- # # copy other files similarly using Path.iterdir()
1931
- # # read project.json at matched_folder / "project.json"
1932
- # pj = matched_folder / "project.json"
1933
- # if pj.exists():
1934
- # try:
1935
- # with pj.open("r", encoding="utf-8") as f:
1936
- # bd_json = json.load(f)
1937
- # stage_count = 0
1938
- # for tgt in bd_json.get("targets", []):
1939
- # if tgt.get("isStage"):
1940
- # backdrop_data.append(tgt)
1941
- # stage_count += 1
1942
- # print(f" ✓ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
1943
- # except Exception as e:
1944
- # print(f" ✗ Failed to read project.json in {matched_folder}: {e}")
1945
- # else:
1946
- # print(f" ⚠ No project.json in {matched_folder}")
1947
-
1948
- # print("---")
1949
- # add at top of file
1950
  import shutil
1951
  import json
1952
  import os
@@ -2109,105 +2005,7 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
2109
  print(f" ⚠ No project.json in {matched_folder_display}")
2110
 
2111
  print("---")
2112
- # for matched_idx in matched_indices:
2113
- # matched_image_path = paths_list[matched_idx]
2114
- # matched_folder = os.path.dirname(matched_image_path)
2115
- # matched_filename = os.path.basename(matched_image_path)
2116
-
2117
- # print(f"Processing matched image: {matched_image_path}")
2118
- # print(f" - Folder: {matched_folder}")
2119
- # print(f" - Sprite path: {sprite_base_path}")
2120
- # print(f" - Backdrop path: {backdrop_base_path}")
2121
- # print(f" - Filename: {matched_filename}")
2122
-
2123
- # # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
2124
- # if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
2125
- # print(f"Processing SPRITE folder: {matched_folder}")
2126
- # copied_sprite_folders.add(matched_folder)
2127
- # sprite_json_path = os.path.join(matched_folder, "sprite.json")
2128
- # print("sprite_json_path----------------------->",sprite_json_path)
2129
- # print("copied sprite folder----------------------->",copied_sprite_folders)
2130
- # if os.path.exists(sprite_json_path):
2131
- # try:
2132
- # with open(sprite_json_path, "r", encoding="utf-8") as f:
2133
- # sprite_info = json.load(f)
2134
- # project_data.append(sprite_info)
2135
- # print(f" ✓ Successfully read sprite.json from {matched_folder}")
2136
- # except Exception as e:
2137
- # print(f" ✗ Failed to read sprite.json in {matched_folder}: {e}")
2138
- # else:
2139
- # print(f" ⚠ No sprite.json in {matched_folder}")
2140
-
2141
- # # copy non-matching files from the sprite folder (except the matched image and sprite.json)
2142
- # sprite_files = os.listdir(matched_folder)
2143
- # print(f" Files in sprite folder: {sprite_files}")
2144
- # for fname in sprite_files:
2145
- # if fname in (matched_filename, "sprite.json"):
2146
- # print(f" Skipping {fname} (matched image or sprite.json)")
2147
- # continue
2148
- # src = os.path.join(matched_folder, fname)
2149
- # dst = os.path.join(project_folder, fname)
2150
- # if os.path.isfile(src):
2151
- # try:
2152
- # shutil.copy2(src, dst)
2153
- # print(f" ✓ Copied sprite asset: {src} -> {dst}")
2154
- # except Exception as e:
2155
- # print(f" ✗ Failed to copy sprite asset {src}: {e}")
2156
- # else:
2157
- # print(f" Skipping {fname} (not a file)")
2158
-
2159
- # # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
2160
- # if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
2161
- # print(f"Processing BACKDROP folder: {matched_folder}")
2162
- # copied_backdrop_folders.add(matched_folder)
2163
- # print("backdrop_base_path----------------------->",backdrop_base_path)
2164
- # print("copied backdrop folder----------------------->",copied_backdrop_folders)
2165
- # # copy matched backdrop image
2166
- # backdrop_dst = os.path.join(project_folder, matched_filename)
2167
- # try:
2168
- # shutil.copy2(matched_image_path, backdrop_dst)
2169
- # print(f" ✓ Copied matched backdrop image: {matched_image_path} -> {backdrop_dst}")
2170
- # except Exception as e:
2171
- # print(f" ✗ Failed to copy matched backdrop image {matched_image_path}: {e}")
2172
-
2173
- # # copy other files from folder (skip project.json and matched image)
2174
- # backdrop_files = os.listdir(matched_folder)
2175
- # print(f" Files in backdrop folder: {backdrop_files}")
2176
- # for fname in backdrop_files:
2177
- # if fname in (matched_filename, "project.json"):
2178
- # print(f" Skipping {fname} (matched image or project.json)")
2179
- # continue
2180
- # src = os.path.join(matched_folder, fname)
2181
- # dst = os.path.join(project_folder, fname)
2182
- # if os.path.isfile(src):
2183
- # try:
2184
- # shutil.copy2(src, dst)
2185
- # print(f" ✓ Copied backdrop asset: {src} -> {dst}")
2186
- # except Exception as e:
2187
- # print(f" ✗ Failed to copy backdrop asset {src}: {e}")
2188
- # else:
2189
- # print(f" Skipping {fname} (not a file)")
2190
-
2191
- # # read project.json to extract Stage/targets
2192
- # pj = os.path.join(matched_folder, "project.json")
2193
- # if os.path.exists(pj):
2194
- # try:
2195
- # with open(pj, "r", encoding="utf-8") as f:
2196
- # bd_json = json.load(f)
2197
- # stage_count = 0
2198
- # for tgt in bd_json.get("targets", []):
2199
- # if tgt.get("isStage"):
2200
- # backdrop_data.append(tgt)
2201
- # stage_count += 1
2202
- # print(f" ✓ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
2203
- # except Exception as e:
2204
- # print(f" ✗ Failed to read project.json in {matched_folder}: {e}")
2205
- # else:
2206
- # print(f" ⚠ No project.json in {matched_folder}")
2207
 
2208
- # print("---")
2209
-
2210
- # --- Merge into final Scratch project.json (identical logic to before)
2211
  final_project = {
2212
  "targets": [], "monitors": [], "extensions": [],
2213
  "meta": {
@@ -2217,6 +2015,12 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
2217
  }
2218
  }
2219
 
 
 
 
 
 
 
2220
  # then backdrop as the Stage
2221
  if backdrop_data:
2222
  all_costumes, sounds = [], []
 
1843
  matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
1844
  print("matched_indices------------------>",matched_indices)
1845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1846
  import shutil
1847
  import json
1848
  import os
 
2005
  print(f" ⚠ No project.json in {matched_folder_display}")
2006
 
2007
  print("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2008
 
 
 
 
2009
  final_project = {
2010
  "targets": [], "monitors": [], "extensions": [],
2011
  "meta": {
 
2015
  }
2016
  }
2017
 
2018
+
2019
+ # Add sprite targets (non-stage)
2020
+ for spr in project_data:
2021
+ if not spr.get("isStage", False):
2022
+ final_project["targets"].append(spr)
2023
+
2024
  # then backdrop as the Stage
2025
  if backdrop_data:
2026
  all_costumes, sounds = [], []