prthm11 commited on
Commit
96dc1d5
·
verified ·
1 Parent(s): d3605fc

Update app_main.py

Browse files
Files changed (1) hide show
  1. app_main.py +100 -28
app_main.py CHANGED
@@ -317,12 +317,12 @@ def similarity_matching(input_json_path: str) -> str:
317
  # # ========================================= #
318
  # # Walk folders to collect all image paths #
319
  # # ========================================= #
320
- # folder_image_paths = []
321
- # for image_dir in image_dirs:
322
- # for root, _, files in os.walk(image_dir):
323
- # for fname in files:
324
- # if fname.lower().endswith((".png", ".jpg", ".jpeg")):
325
- # folder_image_paths.append(os.path.join(root, fname))
326
 
327
  # # ============================== #
328
  # # EMBED FOLDER IMAGES (REF) #
@@ -377,43 +377,115 @@ def similarity_matching(input_json_path: str) -> str:
377
  most_similar_indices = np.argmax(similarity, axis=1)
378
 
379
  # ============= Match and copy ================
380
- project_data, backdrop_data = [], []
381
  copied_folders = set()
382
- start_time = time.perf_counter()
 
 
 
 
 
383
  for sprite_idx, matched_idx in enumerate(most_similar_indices):
384
- matched_entry = embedding_json[matched_idx]
385
- # matched_image_path = os.path.normpath(folder_image_paths[matched_idx])
386
- matched_image_path = os.path.normpath(matched_entry["file-path"])
387
  matched_folder = os.path.dirname(matched_image_path)
 
 
388
  if matched_folder in copied_folders:
389
  continue
390
  copied_folders.add(matched_folder)
 
391
 
392
- # Sprite
393
  sprite_json_path = os.path.join(matched_folder, 'sprite.json')
394
- if os.path.exists(sprite_json_path):
395
- with open(sprite_json_path, 'r') as f:
396
- sprite_data = json.load(f)
397
- project_data.append(sprite_data)
398
 
399
- for fname in os.listdir(matched_folder):
400
- if fname not in {os.path.basename(matched_image_path), 'sprite.json'}:
401
- shutil.copy2(os.path.join(
402
- matched_folder, fname), project_folder)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
 
404
- # Backdrop
405
  if matched_image_path.startswith(os.path.normpath(backdrop_images_path)):
 
 
 
 
 
 
 
 
 
 
 
 
 
406
  backdrop_json_path = os.path.join(matched_folder, 'project.json')
407
  if os.path.exists(backdrop_json_path):
408
  with open(backdrop_json_path, 'r') as f:
409
  backdrop_json_data = json.load(f)
410
- for target in backdrop_json_data.get("targets", []):
411
- if target.get("isStage"):
412
- backdrop_data.append(target)
413
- for fname in os.listdir(matched_folder):
414
- if fname not in {os.path.basename(matched_image_path), 'project.json'}:
415
- shutil.copy2(os.path.join(
416
- matched_folder, fname), project_folder)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
  # Merge JSON structure
419
  final_project = {
 
317
  # # ========================================= #
318
  # # Walk folders to collect all image paths #
319
  # # ========================================= #
320
+ folder_image_paths = []
321
+ for image_dir in image_dirs:
322
+ for root, _, files in os.walk(image_dir):
323
+ for fname in files:
324
+ if fname.lower().endswith((".png", ".jpg", ".jpeg")):
325
+ folder_image_paths.append(os.path.join(root, fname))
326
 
327
  # # ============================== #
328
  # # EMBED FOLDER IMAGES (REF) #
 
377
  most_similar_indices = np.argmax(similarity, axis=1)
378
 
379
  # ============= Match and copy ================
380
+ project_data = []
381
  copied_folders = set()
382
+
383
+ # =============================================================== #
384
+ # Loop through most similar images from Sprites folder #
385
+ # → Copy sprite assets (excluding matched image + sprite.json) #
386
+ # → Load sprite.json and append its data to project_data #
387
+ # =============================================================== #
388
  for sprite_idx, matched_idx in enumerate(most_similar_indices):
389
+ matched_image_path = folder_image_paths[matched_idx]
390
+ matched_image_path = os.path.normpath(matched_image_path)
391
+
392
  matched_folder = os.path.dirname(matched_image_path)
393
+ folder_name = os.path.basename(matched_folder)
394
+
395
  if matched_folder in copied_folders:
396
  continue
397
  copied_folders.add(matched_folder)
398
+ logger.info(f"Matched image path: {matched_image_path}")
399
 
 
400
  sprite_json_path = os.path.join(matched_folder, 'sprite.json')
401
+ if not os.path.exists(sprite_json_path):
402
+ logger.warning(f"sprite.json not found in: {matched_folder}")
403
+ continue
 
404
 
405
+ with open(sprite_json_path, 'r') as f:
406
+ sprite_data = json.load(f)
407
+ print(f"SPRITE DATA: \n{sprite_data}")
408
+ # Copy only non-matched files
409
+ for fname in os.listdir(matched_folder):
410
+ fpath = os.path.join(matched_folder, fname)
411
+ if os.path.isfile(fpath) and fname not in {os.path.basename(matched_image_path), 'sprite.json'}:
412
+ shutil.copy2(fpath, os.path.join(project_folder, fname))
413
+ logger.info(f"Copied Sprite asset: {fname}")
414
+ project_data.append(sprite_data)
415
+
416
+ # ================================================================== #
417
+ # Loop through most similar images from Backdrops folder #
418
+ # → Copy Backdrop assets (excluding matched image + project.json) #
419
+ # → Load project.json and append its data to project_data #
420
+ # ================================================================== #
421
+ backdrop_data = [] # for backdrop-related entries
422
+
423
+ for backdrop_idx, matched_idx in enumerate(most_similar_indices):
424
+ matched_image_path = os.path.normpath(folder_image_paths[matched_idx])
425
 
426
+ # Check if the match is from the Backdrops folder
427
  if matched_image_path.startswith(os.path.normpath(backdrop_images_path)):
428
+ matched_folder = os.path.dirname(matched_image_path)
429
+ folder_name = os.path.basename(matched_folder)
430
+
431
+ logger.info(f"Backdrop matched image: {matched_image_path}")
432
+
433
+ # Copy only non-matched files
434
+ for fname in os.listdir(matched_folder):
435
+ fpath = os.path.join(matched_folder, fname)
436
+ if os.path.isfile(fpath) and fname not in {os.path.basename(matched_image_path), 'project.json'}:
437
+ shutil.copy2(fpath, os.path.join(project_folder, fname))
438
+ logger.info(f"Copied Backdrop asset: {fname}")
439
+
440
+ # Append backdrop's project.json
441
  backdrop_json_path = os.path.join(matched_folder, 'project.json')
442
  if os.path.exists(backdrop_json_path):
443
  with open(backdrop_json_path, 'r') as f:
444
  backdrop_json_data = json.load(f)
445
+ print(f"SPRITE DATA: \n{backdrop_json_data}")
446
+ if "targets" in backdrop_json_data:
447
+ for target in backdrop_json_data["targets"]:
448
+ if target.get("isStage") == True:
449
+ backdrop_data.append(target)
450
+ else:
451
+ logger.warning(f"project.json not found in: {matched_folder}")
452
+ # project_data, backdrop_data = [], []
453
+ # copied_folders = set()
454
+ # start_time = time.perf_counter()
455
+ # for sprite_idx, matched_idx in enumerate(most_similar_indices):
456
+ # matched_entry = embedding_json[matched_idx]
457
+ # # matched_image_path = os.path.normpath(folder_image_paths[matched_idx])
458
+ # matched_image_path = os.path.normpath(matched_entry["file-path"])
459
+ # matched_folder = os.path.dirname(matched_image_path)
460
+ # if matched_folder in copied_folders:
461
+ # continue
462
+ # copied_folders.add(matched_folder)
463
+
464
+ # # Sprite
465
+ # sprite_json_path = os.path.join(matched_folder, 'sprite.json')
466
+ # if os.path.exists(sprite_json_path):
467
+ # with open(sprite_json_path, 'r') as f:
468
+ # sprite_data = json.load(f)
469
+ # project_data.append(sprite_data)
470
+
471
+ # for fname in os.listdir(matched_folder):
472
+ # if fname not in {os.path.basename(matched_image_path), 'sprite.json'}:
473
+ # shutil.copy2(os.path.join(
474
+ # matched_folder, fname), project_folder)
475
+
476
+ # # Backdrop
477
+ # if matched_image_path.startswith(os.path.normpath(backdrop_images_path)):
478
+ # backdrop_json_path = os.path.join(matched_folder, 'project.json')
479
+ # if os.path.exists(backdrop_json_path):
480
+ # with open(backdrop_json_path, 'r') as f:
481
+ # backdrop_json_data = json.load(f)
482
+ # for target in backdrop_json_data.get("targets", []):
483
+ # if target.get("isStage"):
484
+ # backdrop_data.append(target)
485
+ # for fname in os.listdir(matched_folder):
486
+ # if fname not in {os.path.basename(matched_image_path), 'project.json'}:
487
+ # shutil.copy2(os.path.join(
488
+ # matched_folder, fname), project_folder)
489
 
490
  # Merge JSON structure
491
  final_project = {