Spaces:
Runtime error
Runtime error
junyangwang0410
commited on
Commit
•
b4af017
1
Parent(s):
43c0e1c
Update MobileAgent/crop.py
Browse files- MobileAgent/crop.py +0 -52
MobileAgent/crop.py
CHANGED
@@ -2,8 +2,6 @@ import math
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
-
import clip
|
6 |
-
import torch
|
7 |
|
8 |
|
9 |
def crop_image(img, position):
|
@@ -89,53 +87,3 @@ def in_box(box, target):
|
|
89 |
return True
|
90 |
else:
|
91 |
return False
|
92 |
-
|
93 |
-
|
94 |
-
def crop_for_clip(image, box, i, position):
|
95 |
-
image = Image.open(image)
|
96 |
-
w, h = image.size
|
97 |
-
if position == "left":
|
98 |
-
bound = [0, 0, w/2, h]
|
99 |
-
elif position == "right":
|
100 |
-
bound = [w/2, 0, w, h]
|
101 |
-
elif position == "top":
|
102 |
-
bound = [0, 0, w, h/2]
|
103 |
-
elif position == "bottom":
|
104 |
-
bound = [0, h/2, w, h]
|
105 |
-
elif position == "top left":
|
106 |
-
bound = [0, 0, w/2, h/2]
|
107 |
-
elif position == "top right":
|
108 |
-
bound = [w/2, 0, w, h/2]
|
109 |
-
elif position == "bottom left":
|
110 |
-
bound = [0, h/2, w/2, h]
|
111 |
-
elif position == "bottom right":
|
112 |
-
bound = [w/2, h/2, w, h]
|
113 |
-
else:
|
114 |
-
bound = [0, 0, w, h]
|
115 |
-
|
116 |
-
if in_box(box, bound):
|
117 |
-
cropped_image = image.crop(box)
|
118 |
-
cropped_image.save(f"./temp/{i}.jpg")
|
119 |
-
return True
|
120 |
-
else:
|
121 |
-
return False
|
122 |
-
|
123 |
-
|
124 |
-
def clip_for_icon(clip_model, clip_preprocess, images, prompt):
|
125 |
-
image_features = []
|
126 |
-
for image_file in images:
|
127 |
-
image = clip_preprocess(Image.open(image_file)).unsqueeze(0).to(next(clip_model.parameters()).device)
|
128 |
-
image_feature = clip_model.encode_image(image)
|
129 |
-
image_features.append(image_feature)
|
130 |
-
image_features = torch.cat(image_features)
|
131 |
-
|
132 |
-
text = clip.tokenize([prompt]).to(next(clip_model.parameters()).device)
|
133 |
-
text_features = clip_model.encode_text(text)
|
134 |
-
|
135 |
-
image_features /= image_features.norm(dim=-1, keepdim=True)
|
136 |
-
text_features /= text_features.norm(dim=-1, keepdim=True)
|
137 |
-
similarity = (100.0 * image_features @ text_features.T).softmax(dim=0).squeeze(0)
|
138 |
-
_, max_pos = torch.max(similarity, dim=0)
|
139 |
-
pos = max_pos.item()
|
140 |
-
|
141 |
-
return pos
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
5 |
|
6 |
|
7 |
def crop_image(img, position):
|
|
|
87 |
return True
|
88 |
else:
|
89 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|