Pierre Chapuis
commited on
resize image before upload
Browse filesthis significantly speeds up the space when using e.g. mobile photos
- src/app.py +21 -6
src/app.py
CHANGED
|
@@ -19,6 +19,14 @@ with env.prefixed("ERASER_"):
|
|
| 19 |
auth = None if API_KEY is None else httpx.BasicAuth("hf", API_KEY)
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def process_bbox(
|
| 23 |
prompts: dict[str, Any],
|
| 24 |
request: gr.Request | None,
|
|
@@ -27,16 +35,21 @@ def process_bbox(
|
|
| 27 |
assert isinstance(boxes := prompts["boxes"], list)
|
| 28 |
assert len(boxes) == 1
|
| 29 |
assert isinstance(box := boxes[0], dict)
|
| 30 |
-
data = {"bbox": ",".join([str(box[k]) for k in ["xmin", "ymin", "xmax", "ymax"]])}
|
| 31 |
headers = {}
|
| 32 |
if request: # avoid DOS - can be None despite type hint!
|
| 33 |
client_ip = request.headers.get("x-forwarded-for") or request.client.host
|
| 34 |
headers = {"X-HF-Client-IP": client_ip}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with io.BytesIO() as f:
|
| 36 |
-
|
| 37 |
r = httpx.post(
|
| 38 |
API_URL,
|
| 39 |
-
data=
|
| 40 |
files={"file": f},
|
| 41 |
verify=CA_BUNDLE or True,
|
| 42 |
timeout=30.0,
|
|
@@ -58,16 +71,18 @@ def process_prompt(
|
|
| 58 |
prompt: str,
|
| 59 |
request: gr.Request | None,
|
| 60 |
) -> tuple[Image.Image, Image.Image]:
|
| 61 |
-
data = {"prompt": prompt}
|
| 62 |
headers = {}
|
| 63 |
if request: # avoid DOS - can be None despite type hint!
|
| 64 |
client_ip = request.headers.get("x-forwarded-for") or request.client.host
|
| 65 |
headers = {"X-HF-Client-IP": client_ip}
|
|
|
|
|
|
|
|
|
|
| 66 |
with io.BytesIO() as f:
|
| 67 |
-
|
| 68 |
r = httpx.post(
|
| 69 |
API_URL,
|
| 70 |
-
data=
|
| 71 |
files={"file": f},
|
| 72 |
verify=CA_BUNDLE or True,
|
| 73 |
timeout=30.0,
|
|
|
|
| 19 |
auth = None if API_KEY is None else httpx.BasicAuth("hf", API_KEY)
|
| 20 |
|
| 21 |
|
| 22 |
+
def resize(image: Image.Image, shortest_side: int = 768) -> Image.Image:
|
| 23 |
+
if image.width <= shortest_side and image.height <= shortest_side:
|
| 24 |
+
return image
|
| 25 |
+
if image.width < image.height:
|
| 26 |
+
return image.resize(size=(shortest_side, int(shortest_side * image.height / image.width)))
|
| 27 |
+
return image.resize(size=(int(shortest_side * image.width / image.height), shortest_side))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
def process_bbox(
|
| 31 |
prompts: dict[str, Any],
|
| 32 |
request: gr.Request | None,
|
|
|
|
| 35 |
assert isinstance(boxes := prompts["boxes"], list)
|
| 36 |
assert len(boxes) == 1
|
| 37 |
assert isinstance(box := boxes[0], dict)
|
|
|
|
| 38 |
headers = {}
|
| 39 |
if request: # avoid DOS - can be None despite type hint!
|
| 40 |
client_ip = request.headers.get("x-forwarded-for") or request.client.host
|
| 41 |
headers = {"X-HF-Client-IP": client_ip}
|
| 42 |
+
|
| 43 |
+
resized_img = resize(img)
|
| 44 |
+
bbox = [box[k] for k in ["xmin", "ymin", "xmax", "ymax"]]
|
| 45 |
+
if resized_img.width != img.width:
|
| 46 |
+
bbox = [int(v * resized_img.width / img.width) for v in bbox]
|
| 47 |
+
|
| 48 |
with io.BytesIO() as f:
|
| 49 |
+
resized_img.save(f, format="JPEG")
|
| 50 |
r = httpx.post(
|
| 51 |
API_URL,
|
| 52 |
+
data={"bbox": ",".join([str(v) for v in bbox])},
|
| 53 |
files={"file": f},
|
| 54 |
verify=CA_BUNDLE or True,
|
| 55 |
timeout=30.0,
|
|
|
|
| 71 |
prompt: str,
|
| 72 |
request: gr.Request | None,
|
| 73 |
) -> tuple[Image.Image, Image.Image]:
|
|
|
|
| 74 |
headers = {}
|
| 75 |
if request: # avoid DOS - can be None despite type hint!
|
| 76 |
client_ip = request.headers.get("x-forwarded-for") or request.client.host
|
| 77 |
headers = {"X-HF-Client-IP": client_ip}
|
| 78 |
+
|
| 79 |
+
resized_img = resize(img)
|
| 80 |
+
|
| 81 |
with io.BytesIO() as f:
|
| 82 |
+
resized_img.save(f, format="JPEG")
|
| 83 |
r = httpx.post(
|
| 84 |
API_URL,
|
| 85 |
+
data={"prompt": prompt},
|
| 86 |
files={"file": f},
|
| 87 |
verify=CA_BUNDLE or True,
|
| 88 |
timeout=30.0,
|