primerz commited on
Commit
5f6ad01
·
verified ·
1 Parent(s): 5dafe2c

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +5 -56
utils.py CHANGED
@@ -1,20 +1,15 @@
1
  from PIL import Image
2
- from transformers import BlipProcessor, BlipForConditionalGeneration
3
- import torch
4
  from config import Config
5
  import cv2
6
  import numpy as np
7
  import math
8
 
9
- # Simple global caching for the captioner
10
- captioner_processor = None
11
- captioner_model = None
12
 
13
  def resize_image_to_1mp(image):
14
  """Resizes image to approx 1MP (e.g., 1024x1024) preserving aspect ratio."""
15
  image = image.convert("RGB")
16
  w, h = image.size
17
- target_pixels = 1024 * 1024
18
  aspect_ratio = w / h
19
 
20
  # Calculate new dimensions
@@ -22,56 +17,10 @@ def resize_image_to_1mp(image):
22
  new_w = int(new_h * aspect_ratio)
23
 
24
  # Ensure divisibility by 48 for efficiency
25
- new_w = (new_w // 48) * 48
26
- new_h = (new_h // 48) * 48
27
 
28
  if new_w == 0 or new_h == 0:
29
- new_w, new_h = 1024, 1024 # Fallback
30
 
31
- return image.resize((new_w, new_h), Image.LANCZOS)
32
-
33
- def get_caption(image):
34
- """Generates a caption for the image if one isn't provided."""
35
- global captioner_processor, captioner_model
36
-
37
- if captioner_model is None:
38
- print("Loading Captioner (BLIP)...")
39
- captioner_processor = BlipProcessor.from_pretrained(Config.CAPTIONER_REPO)
40
- captioner_model = BlipForConditionalGeneration.from_pretrained(Config.CAPTIONER_REPO).to(Config.DEVICE)
41
-
42
- inputs = captioner_processor(image, return_tensors="pt").to(Config.DEVICE)
43
- out = captioner_model.generate(**inputs)
44
- caption = captioner_processor.decode(out[0], skip_special_tokens=True)
45
- return caption
46
-
47
- # --- ADDED: Function from your provided file ---
48
- def draw_kps(image_pil, kps, color_list=[(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]):
49
- stickwidth = 4
50
- limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]])
51
- kps = np.array(kps)
52
-
53
- w, h = image_pil.size
54
- out_img = np.zeros([h, w, 3])
55
-
56
- for i in range(len(limbSeq)):
57
- index = limbSeq[i]
58
- color = color_list[index[0]]
59
-
60
- x = kps[index][:, 0]
61
- y = kps[index][:, 1]
62
- length = ((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2) ** 0.5
63
- angle = math.degrees(math.atan2(y[0] - y[1], x[0] - x[1]))
64
- polygon = cv2.ellipse2Poly(
65
- (int(np.mean(x)), int(np.mean(y))), (int(length / 2), stickwidth), int(angle), 0, 360, 1
66
- )
67
- out_img = cv2.fillConvexPoly(out_img.copy(), polygon, color)
68
- out_img = (out_img * 0.6).astype(np.uint8)
69
-
70
- for idx_kp, kp in enumerate(kps):
71
- color = color_list[idx_kp]
72
- x, y = kp
73
- out_img = cv2.circle(out_img.copy(), (int(x), int(y)), 10, color, -1)
74
-
75
- out_img_pil = Image.fromarray(out_img.astype(np.uint8))
76
- return out_img_pil
77
- # --- END ADDED ---
 
1
  from PIL import Image
 
 
2
  from config import Config
3
  import cv2
4
  import numpy as np
5
  import math
6
 
 
 
 
7
 
8
  def resize_image_to_1mp(image):
9
  """Resizes image to approx 1MP (e.g., 1024x1024) preserving aspect ratio."""
10
  image = image.convert("RGB")
11
  w, h = image.size
12
+ target_pixels = 768 * 768
13
  aspect_ratio = w / h
14
 
15
  # Calculate new dimensions
 
17
  new_w = int(new_h * aspect_ratio)
18
 
19
  # Ensure divisibility by 48 for efficiency
20
+ new_w = (new_w // 64) * 64
21
+ new_h = (new_h // 64) * 64
22
 
23
  if new_w == 0 or new_h == 0:
24
+ new_w, new_h = 768, 768 # Fallback
25
 
26
+ return image.resize((new_w, new_h), Image.LANCZOS)