ammariii08 commited on
Commit
5fd20fc
·
verified ·
1 Parent(s): 9eca874

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -35
app.py CHANGED
@@ -71,18 +71,18 @@ if not os.path.exists(reference_model_path):
71
  reference_detector_global = YOLO(reference_model_path)
72
  print("YOLO reference model loaded in {:.2f} seconds".format(time.time() - start_time))
73
 
74
- # print("Loading U²-Net model for reference background removal (U2NETP)...")
75
- # start_time = time.time()
76
- # u2net_model_path = os.path.join(CACHE_DIR, "u2netp.pth")
77
- # if not os.path.exists(u2net_model_path):
78
- # print("Caching U²-Net model to", u2net_model_path)
79
- # shutil.copy("u2netp.pth", u2net_model_path)
80
- # u2net_global = U2NETP(3, 1)
81
- # u2net_global.load_state_dict(torch.load(u2net_model_path, map_location="cpu"))
82
  device = "cpu"
83
- # u2net_global.to(device)
84
- # u2net_global.eval()
85
- # print("U²-Net model loaded in {:.2f} seconds".format(time.time() - start_time))
86
 
87
  print("Loading BiRefNet model...")
88
  start_time = time.time()
@@ -121,14 +121,14 @@ def unload_and_reload_models():
121
  )
122
  new_birefnet.to(device)
123
  new_birefnet.eval()
124
- # new_u2net = U2NETP(3, 1)
125
- # new_u2net.load_state_dict(torch.load(os.path.join(CACHE_DIR, "u2netp.pth"), map_location="cpu"))
126
- # new_u2net.to(device)
127
- # new_u2net.eval()
128
  drawer_detector_global = new_drawer_detector
129
  reference_detector_global = new_reference_detector
130
  birefnet_global = new_birefnet
131
- u2net_global = new_birefnet
132
  print("Models reloaded in {:.2f} seconds".format(time.time() - start_time))
133
 
134
  # ---------------------
@@ -160,24 +160,24 @@ def detect_reference_square(img: np.ndarray):
160
  )
161
 
162
  # Use U2NETP for reference background removal.
163
- # def remove_bg_u2netp(image: np.ndarray) -> np.ndarray:
164
- # t = time.time()
165
- # image_pil = Image.fromarray(image)
166
- # transform_u2netp = transforms.Compose([
167
- # transforms.Resize((320, 320)),
168
- # transforms.ToTensor(),
169
- # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
170
- # ])
171
- # input_tensor = transform_u2netp(image_pil).unsqueeze(0).to("cpu")
172
- # with torch.no_grad():
173
- # outputs = u2net_global(input_tensor)
174
- # pred = outputs[0]
175
- # pred = (pred - pred.min()) / (pred.max() - pred.min() + 1e-8)
176
- # pred_np = pred.squeeze().cpu().numpy()
177
- # pred_np = cv2.resize(pred_np, (image_pil.width, image_pil.height))
178
- # pred_np = (pred_np * 255).astype(np.uint8)
179
- # print("U2NETP background removal completed in {:.2f} seconds".format(time.time() - t))
180
- # return pred_np
181
 
182
  # Use BiRefNet for main object background removal.
183
  def remove_bg(image: np.ndarray) -> np.ndarray:
@@ -492,7 +492,7 @@ def predict(
492
  # ---------------------
493
  t = time.time()
494
  reference_obj_img = make_square(reference_obj_img)
495
- reference_square_mask = remove_bg(reference_obj_img)
496
  print("Reference image processing completed in {:.2f} seconds".format(time.time() - t))
497
 
498
  t = time.time()
 
71
  reference_detector_global = YOLO(reference_model_path)
72
  print("YOLO reference model loaded in {:.2f} seconds".format(time.time() - start_time))
73
 
74
+ print("Loading U²-Net model for reference background removal (U2NETP)...")
75
+ start_time = time.time()
76
+ u2net_model_path = os.path.join(CACHE_DIR, "u2netp.pth")
77
+ if not os.path.exists(u2net_model_path):
78
+ print("Caching U²-Net model to", u2net_model_path)
79
+ shutil.copy("u2netp.pth", u2net_model_path)
80
+ u2net_global = U2NETP(3, 1)
81
+ u2net_global.load_state_dict(torch.load(u2net_model_path, map_location="cpu"))
82
  device = "cpu"
83
+ u2net_global.to(device)
84
+ u2net_global.eval()
85
+ print("U²-Net model loaded in {:.2f} seconds".format(time.time() - start_time))
86
 
87
  print("Loading BiRefNet model...")
88
  start_time = time.time()
 
121
  )
122
  new_birefnet.to(device)
123
  new_birefnet.eval()
124
+ new_u2net = U2NETP(3, 1)
125
+ new_u2net.load_state_dict(torch.load(os.path.join(CACHE_DIR, "u2netp.pth"), map_location="cpu"))
126
+ new_u2net.to(device)
127
+ new_u2net.eval()
128
  drawer_detector_global = new_drawer_detector
129
  reference_detector_global = new_reference_detector
130
  birefnet_global = new_birefnet
131
+ u2net_global = new_u2net
132
  print("Models reloaded in {:.2f} seconds".format(time.time() - start_time))
133
 
134
  # ---------------------
 
160
  )
161
 
162
  # Use U2NETP for reference background removal.
163
+ def remove_bg_u2netp(image: np.ndarray) -> np.ndarray:
164
+ t = time.time()
165
+ image_pil = Image.fromarray(image)
166
+ transform_u2netp = transforms.Compose([
167
+ transforms.Resize((320, 320)),
168
+ transforms.ToTensor(),
169
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
170
+ ])
171
+ input_tensor = transform_u2netp(image_pil).unsqueeze(0).to("cpu")
172
+ with torch.no_grad():
173
+ outputs = u2net_global(input_tensor)
174
+ pred = outputs[0]
175
+ pred = (pred - pred.min()) / (pred.max() - pred.min() + 1e-8)
176
+ pred_np = pred.squeeze().cpu().numpy()
177
+ pred_np = cv2.resize(pred_np, (image_pil.width, image_pil.height))
178
+ pred_np = (pred_np * 255).astype(np.uint8)
179
+ print("U2NETP background removal completed in {:.2f} seconds".format(time.time() - t))
180
+ return pred_np
181
 
182
  # Use BiRefNet for main object background removal.
183
  def remove_bg(image: np.ndarray) -> np.ndarray:
 
492
  # ---------------------
493
  t = time.time()
494
  reference_obj_img = make_square(reference_obj_img)
495
+ reference_square_mask = remove_bg_u2netp(reference_obj_img)
496
  print("Reference image processing completed in {:.2f} seconds".format(time.time() - t))
497
 
498
  t = time.time()