Kingoteam commited on
Commit
4a0c367
·
verified ·
1 Parent(s): 13452b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -23
app.py CHANGED
@@ -1,23 +1,25 @@
 
 
 
 
1
  import gradio as gr
 
2
  from gfpgan import GFPGANer
3
  from huggingface_hub import hf_hub_download
4
- import cv2
5
- import numpy as np
6
- import torchvision.transforms.functional as F
7
- import sys
8
- import types
9
 
10
- # Patch برای حل مشکل 'functional_tensor' در basicsr
11
- sys.modules['torchvision.transforms.functional_tensor'] = types.SimpleNamespace(
12
- rgb_to_grayscale=F.rgb_to_grayscale
13
- )
 
 
14
 
15
- # کش مدل‌ها
16
  loaded_models = {}
17
 
18
  def load_model(version):
19
- if version == "v1.4 (original)":
20
- if version not in loaded_models:
21
  model_path = hf_hub_download("leonelhs/gfpgan", "GFPGANv1.4.pth")
22
  loaded_models[version] = GFPGANer(
23
  model_path=model_path,
@@ -26,10 +28,7 @@ def load_model(version):
26
  channel_multiplier=2,
27
  bg_upsampler=None
28
  )
29
- return loaded_models[version]
30
-
31
- elif version == "v1.3 (clean)":
32
- if version not in loaded_models:
33
  model_path = hf_hub_download("leonelhs/gfpgan", "GFPGANv1.3.pth")
34
  loaded_models[version] = GFPGANer(
35
  model_path=model_path,
@@ -38,26 +37,25 @@ def load_model(version):
38
  channel_multiplier=2,
39
  bg_upsampler=None
40
  )
41
- return loaded_models[version]
42
-
43
 
 
44
  def enhance_face(image, version):
45
  restorer = load_model(version)
46
 
47
- # تبدیل تصویر PIL/numpy به BGR
48
  if isinstance(image, np.ndarray):
49
  img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
50
  else:
51
  raise ValueError("Invalid image format")
52
 
53
- _, _, restored_img = restorer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
 
 
54
 
55
- # تبدیل دوباره به RGB برای نمایش
56
  restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
57
-
58
  return restored_img
59
 
60
-
61
  iface = gr.Interface(
62
  fn=enhance_face,
63
  inputs=[
 
1
+ import os
2
+ import sys
3
+ import types
4
+ import cv2
5
  import gradio as gr
6
+ import numpy as np
7
  from gfpgan import GFPGANer
8
  from huggingface_hub import hf_hub_download
 
 
 
 
 
9
 
10
+ # -------- Patch برای جلوگیری از ارور torchvision.transforms.functional_tensor --------
11
+ try:
12
+ import torchvision.transforms.functional_tensor
13
+ except ModuleNotFoundError:
14
+ # ساخت یک ماژول خالی به جای functional_tensor
15
+ sys.modules['torchvision.transforms.functional_tensor'] = types.ModuleType('functional_tensor')
16
 
17
+ # -------- کش مدل‌ها --------
18
  loaded_models = {}
19
 
20
  def load_model(version):
21
+ if version not in loaded_models:
22
+ if version == "v1.4 (original)":
23
  model_path = hf_hub_download("leonelhs/gfpgan", "GFPGANv1.4.pth")
24
  loaded_models[version] = GFPGANer(
25
  model_path=model_path,
 
28
  channel_multiplier=2,
29
  bg_upsampler=None
30
  )
31
+ elif version == "v1.3 (clean)":
 
 
 
32
  model_path = hf_hub_download("leonelhs/gfpgan", "GFPGANv1.3.pth")
33
  loaded_models[version] = GFPGANer(
34
  model_path=model_path,
 
37
  channel_multiplier=2,
38
  bg_upsampler=None
39
  )
40
+ return loaded_models[version]
 
41
 
42
+ # -------- تابع اصلی پردازش تصویر --------
43
  def enhance_face(image, version):
44
  restorer = load_model(version)
45
 
 
46
  if isinstance(image, np.ndarray):
47
  img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
48
  else:
49
  raise ValueError("Invalid image format")
50
 
51
+ _, _, restored_img = restorer.enhance(
52
+ img, has_aligned=False, only_center_face=False, paste_back=True
53
+ )
54
 
 
55
  restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
 
56
  return restored_img
57
 
58
+ # -------- رابط کاربری Gradio --------
59
  iface = gr.Interface(
60
  fn=enhance_face,
61
  inputs=[