prthm11 commited on
Commit
6bfa9fd
Β·
verified Β·
1 Parent(s): fb78a72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -169
app.py CHANGED
@@ -1829,194 +1829,96 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
1829
  matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
1830
  print("matched_indices------------------>",matched_indices)
1831
 
1832
- from pathlib import Path
1833
 
1834
- # normalize & resolve once at top of function
1835
- sprite_base_p = Path(sprite_base_path).resolve()
1836
- backdrop_base_p = Path(backdrop_base_path).resolve()
1837
- project_folder_p = Path(project_folder).resolve()
1838
- project_folder_p.mkdir(parents=True, exist_ok=True) # ensure dest exists
1839
 
1840
- for matched_idx in matched_indices:
1841
- matched_image_path = paths_list[matched_idx]
1842
- print("matched_image_path ---1-->",matched_image_path)
1843
- matched_folder = Path(matched_image_path).parent
1844
- print("matched_folder ---2-->",matched_folder)
1845
- matched_filename = Path(matched_image_path).name
1846
- print("matched_filename ---3-->",matched_filename)
1847
 
1848
 
1849
- # resolve matched_folder (safe even if it's already absolute)
1850
- try:
1851
- matched_folder_p = matched_folder.resolve()
1852
- except Exception:
1853
- # fallback to absolute path join if resolve fails
1854
- matched_folder_p = (Path.cwd() / matched_folder).resolve()
1855
 
1856
- # print(f"Processing matched image: {matched_image_path}")
1857
- # print(f" matched_folder_p = {matched_folder_p}")
1858
- # print(f" sprite_base_p = {sprite_base_p}")
1859
- # print(f" backdrop_base_p = {backdrop_base_p}")
1860
 
1861
- # Check if matched_folder is under sprite_base_p
1862
- is_sprite = False
1863
- try:
1864
- matched_folder_p.relative_to(sprite_base_p)
1865
- is_sprite = True
1866
- print("matched_folder_p--------->",matched_folder_p)
1867
- print("is_sprite---------->",is_sprite)
1868
- except Exception:
1869
- is_sprite = False
1870
- print("is_sprite---------->",is_sprite)
1871
 
1872
- if is_sprite and str(matched_folder) not in copied_sprite_folders:
1873
- print(f"Processing SPRITE folder: {matched_folder}")
1874
- copied_sprite_folders.add(str(matched_folder))
1875
- sprite_json_path = matched_folder / "sprite.json"
1876
- # ... (rest of your logic, but use Path objects)
1877
- sprite_files = list(matched_folder.iterdir())
1878
- print(f" Files in sprite folder: {[p.name for p in sprite_files]}")
1879
- for p in sprite_files:
1880
- fname = p.name
1881
- if fname in (matched_filename, "sprite.json"):
1882
- print(f" Skipping {fname} (matched image or sprite.json)")
1883
- continue
1884
- if p.is_file():
1885
- dst = project_folder_p / fname
1886
- try:
1887
- shutil.copy2(str(p), str(dst))
1888
- print(f" βœ“ Copied sprite asset: {p} -> {dst}")
1889
- except Exception as e:
1890
- print(f" βœ— Failed to copy sprite asset {p}: {e}")
1891
- else:
1892
- print(f" Skipping {fname} (not a file)")
1893
-
1894
- # Check if matched_folder is under backdrop_base_p
1895
- is_backdrop = False
1896
- try:
1897
- matched_folder.relative_to(backdrop_base_p)
1898
- print("matched_folder_p--------->",matched_folder_p)
1899
- is_backdrop = True
1900
- print("is_backdrop---------->",is_backdrop)
1901
- except Exception:
1902
- is_backdrop = False
1903
- print("is_backdrop---------->",is_backdrop)
1904
-
1905
- if is_backdrop and str(matched_folder) not in copied_backdrop_folders:
1906
- print(f"Processing BACKDROP folder: {matched_folder}")
1907
- copied_backdrop_folders.add(str(matched_folder))
1908
- # copy matched backdrop image
1909
- backdrop_dst = project_folder_p / matched_filename
1910
- try:
1911
- shutil.copy2(str(matched_folder / matched_filename), str(backdrop_dst))
1912
- print(f" βœ“ Copied matched backdrop image: {matched_folder / matched_filename} -> {backdrop_dst}")
1913
- except Exception as e:
1914
- print(f" βœ— Failed to copy matched backdrop image {matched_folder / matched_filename}: {e}")
1915
-
1916
- # copy other files similarly using Path.iterdir()
1917
- # read project.json at matched_folder / "project.json"
1918
- pj = matched_folder / "project.json"
1919
- if pj.exists():
1920
- try:
1921
- with pj.open("r", encoding="utf-8") as f:
1922
- bd_json = json.load(f)
1923
- stage_count = 0
1924
- for tgt in bd_json.get("targets", []):
1925
- if tgt.get("isStage"):
1926
- backdrop_data.append(tgt)
1927
- stage_count += 1
1928
- print(f" βœ“ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
1929
- except Exception as e:
1930
- print(f" βœ— Failed to read project.json in {matched_folder}: {e}")
1931
- else:
1932
- print(f" ⚠ No project.json in {matched_folder}")
1933
-
1934
- print("---")
1935
-
1936
- # for matched_idx in matched_indices:
1937
- # matched_image_path = paths_list[matched_idx]
1938
- # matched_folder = os.path.dirname(matched_image_path)
1939
- # matched_filename = os.path.basename(matched_image_path)
1940
-
1941
- # print(f"Processing matched image: {matched_image_path}")
1942
- # print(f" - Folder: {matched_folder}")
1943
- # print(f" - Sprite path: {sprite_base_path}")
1944
- # print(f" - Backdrop path: {backdrop_base_path}")
1945
- # print(f" - Filename: {matched_filename}")
1946
-
1947
- # # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
1948
- # if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
1949
  # print(f"Processing SPRITE folder: {matched_folder}")
1950
- # copied_sprite_folders.add(matched_folder)
1951
- # sprite_json_path = os.path.join(matched_folder, "sprite.json")
1952
- # print("sprite_json_path----------------------->",sprite_json_path)
1953
- # print("copied sprite folder----------------------->",copied_sprite_folders)
1954
- # if os.path.exists(sprite_json_path):
1955
- # try:
1956
- # with open(sprite_json_path, "r", encoding="utf-8") as f:
1957
- # sprite_info = json.load(f)
1958
- # project_data.append(sprite_info)
1959
- # print(f" βœ“ Successfully read sprite.json from {matched_folder}")
1960
- # except Exception as e:
1961
- # print(f" βœ— Failed to read sprite.json in {matched_folder}: {e}")
1962
- # else:
1963
- # print(f" ⚠ No sprite.json in {matched_folder}")
1964
-
1965
- # # copy non-matching files from the sprite folder (except the matched image and sprite.json)
1966
- # sprite_files = os.listdir(matched_folder)
1967
- # print(f" Files in sprite folder: {sprite_files}")
1968
- # for fname in sprite_files:
1969
  # if fname in (matched_filename, "sprite.json"):
1970
  # print(f" Skipping {fname} (matched image or sprite.json)")
1971
  # continue
1972
- # src = os.path.join(matched_folder, fname)
1973
- # dst = os.path.join(project_folder, fname)
1974
- # if os.path.isfile(src):
1975
  # try:
1976
- # shutil.copy2(src, dst)
1977
- # print(f" βœ“ Copied sprite asset: {src} -> {dst}")
1978
  # except Exception as e:
1979
- # print(f" βœ— Failed to copy sprite asset {src}: {e}")
1980
  # else:
1981
  # print(f" Skipping {fname} (not a file)")
1982
-
1983
- # # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
1984
- # if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
 
 
 
 
 
 
 
 
 
 
1985
  # print(f"Processing BACKDROP folder: {matched_folder}")
1986
- # copied_backdrop_folders.add(matched_folder)
1987
- # print("backdrop_base_path----------------------->",backdrop_base_path)
1988
- # print("copied backdrop folder----------------------->",copied_backdrop_folders)
1989
  # # copy matched backdrop image
1990
- # backdrop_dst = os.path.join(project_folder, matched_filename)
1991
  # try:
1992
- # shutil.copy2(matched_image_path, backdrop_dst)
1993
- # print(f" βœ“ Copied matched backdrop image: {matched_image_path} -> {backdrop_dst}")
1994
  # except Exception as e:
1995
- # print(f" βœ— Failed to copy matched backdrop image {matched_image_path}: {e}")
1996
-
1997
- # # copy other files from folder (skip project.json and matched image)
1998
- # backdrop_files = os.listdir(matched_folder)
1999
- # print(f" Files in backdrop folder: {backdrop_files}")
2000
- # for fname in backdrop_files:
2001
- # if fname in (matched_filename, "project.json"):
2002
- # print(f" Skipping {fname} (matched image or project.json)")
2003
- # continue
2004
- # src = os.path.join(matched_folder, fname)
2005
- # dst = os.path.join(project_folder, fname)
2006
- # if os.path.isfile(src):
2007
- # try:
2008
- # shutil.copy2(src, dst)
2009
- # print(f" βœ“ Copied backdrop asset: {src} -> {dst}")
2010
- # except Exception as e:
2011
- # print(f" βœ— Failed to copy backdrop asset {src}: {e}")
2012
- # else:
2013
- # print(f" Skipping {fname} (not a file)")
2014
-
2015
- # # read project.json to extract Stage/targets
2016
- # pj = os.path.join(matched_folder, "project.json")
2017
- # if os.path.exists(pj):
2018
  # try:
2019
- # with open(pj, "r", encoding="utf-8") as f:
2020
  # bd_json = json.load(f)
2021
  # stage_count = 0
2022
  # for tgt in bd_json.get("targets", []):
@@ -2028,8 +1930,106 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
2028
  # print(f" βœ— Failed to read project.json in {matched_folder}: {e}")
2029
  # else:
2030
  # print(f" ⚠ No project.json in {matched_folder}")
2031
-
2032
  # print("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2033
 
2034
  # --- Merge into final Scratch project.json (identical logic to before)
2035
  final_project = {
 
1829
  matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
1830
  print("matched_indices------------------>",matched_indices)
1831
 
1832
+ # from pathlib import Path
1833
 
1834
+ # # normalize & resolve once at top of function
1835
+ # sprite_base_p = Path(sprite_base_path).resolve()
1836
+ # backdrop_base_p = Path(backdrop_base_path).resolve()
1837
+ # project_folder_p = Path(project_folder).resolve()
1838
+ # project_folder_p.mkdir(parents=True, exist_ok=True) # ensure dest exists
1839
 
1840
+ # for matched_idx in matched_indices:
1841
+ # matched_image_path = paths_list[matched_idx]
1842
+ # print("matched_image_path ---1-->",matched_image_path)
1843
+ # matched_folder = Path(matched_image_path).parent
1844
+ # print("matched_folder ---2-->",matched_folder)
1845
+ # matched_filename = Path(matched_image_path).name
1846
+ # print("matched_filename ---3-->",matched_filename)
1847
 
1848
 
1849
+ # # resolve matched_folder (safe even if it's already absolute)
1850
+ # try:
1851
+ # matched_folder_p = matched_folder.resolve()
1852
+ # except Exception:
1853
+ # # fallback to absolute path join if resolve fails
1854
+ # matched_folder_p = (Path.cwd() / matched_folder).resolve()
1855
 
1856
+ # # print(f"Processing matched image: {matched_image_path}")
1857
+ # # print(f" matched_folder_p = {matched_folder_p}")
1858
+ # # print(f" sprite_base_p = {sprite_base_p}")
1859
+ # # print(f" backdrop_base_p = {backdrop_base_p}")
1860
 
1861
+ # # Check if matched_folder is under sprite_base_p
1862
+ # is_sprite = False
1863
+ # try:
1864
+ # matched_folder_p.relative_to(sprite_base_p)
1865
+ # is_sprite = True
1866
+ # print("matched_folder_p--------->",matched_folder_p)
1867
+ # print("is_sprite---------->",is_sprite)
1868
+ # except Exception:
1869
+ # is_sprite = False
1870
+ # print("is_sprite---------->",is_sprite)
1871
 
1872
+ # if is_sprite and str(matched_folder) not in copied_sprite_folders:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1873
  # print(f"Processing SPRITE folder: {matched_folder}")
1874
+ # copied_sprite_folders.add(str(matched_folder))
1875
+ # sprite_json_path = matched_folder / "sprite.json"
1876
+ # # ... (rest of your logic, but use Path objects)
1877
+ # sprite_files = list(matched_folder.iterdir())
1878
+ # print(f" Files in sprite folder: {[p.name for p in sprite_files]}")
1879
+ # for p in sprite_files:
1880
+ # fname = p.name
 
 
 
 
 
 
 
 
 
 
 
 
1881
  # if fname in (matched_filename, "sprite.json"):
1882
  # print(f" Skipping {fname} (matched image or sprite.json)")
1883
  # continue
1884
+ # if p.is_file():
1885
+ # dst = project_folder_p / fname
 
1886
  # try:
1887
+ # shutil.copy2(str(p), str(dst))
1888
+ # print(f" βœ“ Copied sprite asset: {p} -> {dst}")
1889
  # except Exception as e:
1890
+ # print(f" βœ— Failed to copy sprite asset {p}: {e}")
1891
  # else:
1892
  # print(f" Skipping {fname} (not a file)")
1893
+
1894
+ # # Check if matched_folder is under backdrop_base_p
1895
+ # is_backdrop = False
1896
+ # try:
1897
+ # matched_folder.relative_to(backdrop_base_p)
1898
+ # print("matched_folder_p--------->",matched_folder_p)
1899
+ # is_backdrop = True
1900
+ # print("is_backdrop---------->",is_backdrop)
1901
+ # except Exception:
1902
+ # is_backdrop = False
1903
+ # print("is_backdrop---------->",is_backdrop)
1904
+
1905
+ # if is_backdrop and str(matched_folder) not in copied_backdrop_folders:
1906
  # print(f"Processing BACKDROP folder: {matched_folder}")
1907
+ # copied_backdrop_folders.add(str(matched_folder))
 
 
1908
  # # copy matched backdrop image
1909
+ # backdrop_dst = project_folder_p / matched_filename
1910
  # try:
1911
+ # shutil.copy2(str(matched_folder / matched_filename), str(backdrop_dst))
1912
+ # print(f" βœ“ Copied matched backdrop image: {matched_folder / matched_filename} -> {backdrop_dst}")
1913
  # except Exception as e:
1914
+ # print(f" βœ— Failed to copy matched backdrop image {matched_folder / matched_filename}: {e}")
1915
+
1916
+ # # copy other files similarly using Path.iterdir()
1917
+ # # read project.json at matched_folder / "project.json"
1918
+ # pj = matched_folder / "project.json"
1919
+ # if pj.exists():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1920
  # try:
1921
+ # with pj.open("r", encoding="utf-8") as f:
1922
  # bd_json = json.load(f)
1923
  # stage_count = 0
1924
  # for tgt in bd_json.get("targets", []):
 
1930
  # print(f" βœ— Failed to read project.json in {matched_folder}: {e}")
1931
  # else:
1932
  # print(f" ⚠ No project.json in {matched_folder}")
1933
+
1934
  # print("---")
1935
+
1936
+ for matched_idx in matched_indices:
1937
+ matched_image_path = paths_list[matched_idx]
1938
+ matched_folder = os.path.dirname(matched_image_path)
1939
+ matched_filename = os.path.basename(matched_image_path)
1940
+
1941
+ print(f"Processing matched image: {matched_image_path}")
1942
+ print(f" - Folder: {matched_folder}")
1943
+ print(f" - Sprite path: {sprite_base_path}")
1944
+ print(f" - Backdrop path: {backdrop_base_path}")
1945
+ print(f" - Filename: {matched_filename}")
1946
+
1947
+ # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
1948
+ if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
1949
+ print(f"Processing SPRITE folder: {matched_folder}")
1950
+ copied_sprite_folders.add(matched_folder)
1951
+ sprite_json_path = os.path.join(matched_folder, "sprite.json")
1952
+ print("sprite_json_path----------------------->",sprite_json_path)
1953
+ print("copied sprite folder----------------------->",copied_sprite_folders)
1954
+ if os.path.exists(sprite_json_path):
1955
+ try:
1956
+ with open(sprite_json_path, "r", encoding="utf-8") as f:
1957
+ sprite_info = json.load(f)
1958
+ project_data.append(sprite_info)
1959
+ print(f" βœ“ Successfully read sprite.json from {matched_folder}")
1960
+ except Exception as e:
1961
+ print(f" βœ— Failed to read sprite.json in {matched_folder}: {e}")
1962
+ else:
1963
+ print(f" ⚠ No sprite.json in {matched_folder}")
1964
+
1965
+ # copy non-matching files from the sprite folder (except the matched image and sprite.json)
1966
+ sprite_files = os.listdir(matched_folder)
1967
+ print(f" Files in sprite folder: {sprite_files}")
1968
+ for fname in sprite_files:
1969
+ if fname in (matched_filename, "sprite.json"):
1970
+ print(f" Skipping {fname} (matched image or sprite.json)")
1971
+ continue
1972
+ src = os.path.join(matched_folder, fname)
1973
+ dst = os.path.join(project_folder, fname)
1974
+ if os.path.isfile(src):
1975
+ try:
1976
+ shutil.copy2(src, dst)
1977
+ print(f" βœ“ Copied sprite asset: {src} -> {dst}")
1978
+ except Exception as e:
1979
+ print(f" βœ— Failed to copy sprite asset {src}: {e}")
1980
+ else:
1981
+ print(f" Skipping {fname} (not a file)")
1982
+
1983
+ # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
1984
+ if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
1985
+ print(f"Processing BACKDROP folder: {matched_folder}")
1986
+ copied_backdrop_folders.add(matched_folder)
1987
+ print("backdrop_base_path----------------------->",backdrop_base_path)
1988
+ print("copied backdrop folder----------------------->",copied_backdrop_folders)
1989
+ # copy matched backdrop image
1990
+ backdrop_dst = os.path.join(project_folder, matched_filename)
1991
+ try:
1992
+ shutil.copy2(matched_image_path, backdrop_dst)
1993
+ print(f" βœ“ Copied matched backdrop image: {matched_image_path} -> {backdrop_dst}")
1994
+ except Exception as e:
1995
+ print(f" βœ— Failed to copy matched backdrop image {matched_image_path}: {e}")
1996
+
1997
+ # copy other files from folder (skip project.json and matched image)
1998
+ backdrop_files = os.listdir(matched_folder)
1999
+ print(f" Files in backdrop folder: {backdrop_files}")
2000
+ for fname in backdrop_files:
2001
+ if fname in (matched_filename, "project.json"):
2002
+ print(f" Skipping {fname} (matched image or project.json)")
2003
+ continue
2004
+ src = os.path.join(matched_folder, fname)
2005
+ dst = os.path.join(project_folder, fname)
2006
+ if os.path.isfile(src):
2007
+ try:
2008
+ shutil.copy2(src, dst)
2009
+ print(f" βœ“ Copied backdrop asset: {src} -> {dst}")
2010
+ except Exception as e:
2011
+ print(f" βœ— Failed to copy backdrop asset {src}: {e}")
2012
+ else:
2013
+ print(f" Skipping {fname} (not a file)")
2014
+
2015
+ # read project.json to extract Stage/targets
2016
+ pj = os.path.join(matched_folder, "project.json")
2017
+ if os.path.exists(pj):
2018
+ try:
2019
+ with open(pj, "r", encoding="utf-8") as f:
2020
+ bd_json = json.load(f)
2021
+ stage_count = 0
2022
+ for tgt in bd_json.get("targets", []):
2023
+ if tgt.get("isStage"):
2024
+ backdrop_data.append(tgt)
2025
+ stage_count += 1
2026
+ print(f" βœ“ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
2027
+ except Exception as e:
2028
+ print(f" βœ— Failed to read project.json in {matched_folder}: {e}")
2029
+ else:
2030
+ print(f" ⚠ No project.json in {matched_folder}")
2031
+
2032
+ print("---")
2033
 
2034
  # --- Merge into final Scratch project.json (identical logic to before)
2035
  final_project = {