mrbui1990 commited on
Commit
a15b0f3
·
verified ·
1 Parent(s): cd515f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -355,12 +355,36 @@ def infer(
355
 
356
  # --- NEW: Load default image if no input ---
357
  if not pil_images:
358
- default_path = os.path.join(os.path.dirname(__file__), "1.jpg")
359
- if os.path.exists(default_path):
360
- pil_images = [Image.open(default_path).convert("RGB")]
361
- print("Loaded default image: 1.jpg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  else:
363
- raise gr.Error("No input images and '1.jpg' not found in app directory.")
 
364
 
365
  if height==256 and width==256:
366
  height, width = None, None
 
355
 
356
  # --- NEW: Load default image if no input ---
357
  if not pil_images:
358
+ # 1. Định nghĩa đường dẫn đến thư mục /Face/
359
+ # os.path.dirname(__file__) lấy thư mục chứa file hiện tại (app.py)
360
+ face_dir = os.path.join(os.path.dirname(__file__), "Face")
361
+
362
+ if os.path.isdir(face_dir):
363
+ # 2. Lấy danh sách tất cả các file trong thư mục /Face/
364
+ all_files = os.listdir(face_dir)
365
+
366
+ # 3. Lọc ra các file ảnh (bạn có thể tùy chỉnh các phần mở rộng)
367
+ image_extensions = ('.jpg', '.jpeg', '.png', '.webp')
368
+ image_files = [f for f in all_files if f.lower().endswith(image_extensions)]
369
+
370
+ if image_files:
371
+ # 4. Chọn ngẫu nhiên một file ảnh
372
+ random_image_name = random.choice(image_files)
373
+ random_image_path = os.path.join(face_dir, random_image_name)
374
+
375
+ # 5. Tải ảnh và thêm vào pil_images
376
+ try:
377
+ pil_images = [Image.open(random_image_path).convert("RGB")]
378
+ print(f"Loaded random default image: {random_image_name}")
379
+ except Exception as e:
380
+ # Xử lý nếu file được chọn không phải là ảnh hợp lệ hoặc lỗi tải
381
+ raise gr.Error(f"Error loading random image '{random_image_name}': {e}")
382
+ else:
383
+ # Lỗi nếu thư mục /Face/ rỗng hoặc không có ảnh
384
+ raise gr.Error(f"No input images provided and no image files found in '{face_dir}'.")
385
  else:
386
+ # Lỗi nếu thư mục /Face/ không tồn tại
387
+ raise gr.Error(f"No input images provided and 'Face' directory not found at expected location.")
388
 
389
  if height==256 and width==256:
390
  height, width = None, None