sooooner commited on
Commit
dcbff67
Β·
1 Parent(s): 4ed8c02

multiple inputs

Browse files
Files changed (2) hide show
  1. app.py +3 -0
  2. utils.py +43 -1
app.py CHANGED
@@ -9,6 +9,9 @@ from utils import Image2Text
9
  @spaces.GPU(duration=15)
10
  def greet(input_img):
11
  global image_to_text
 
 
 
12
  contents = image_to_text.get_text(input_img[0], num_beams=4)
13
  return '\n'.join(contents)
14
 
 
9
  @spaces.GPU(duration=15)
10
  def greet(input_img):
11
  global image_to_text
12
+ print('-----------')
13
+ print(input_img[0])
14
+ print('-----------')
15
  contents = image_to_text.get_text(input_img[0], num_beams=4)
16
  return '\n'.join(contents)
17
 
utils.py CHANGED
@@ -1,3 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import re
2
  import torch
3
  import numpy as np
@@ -52,7 +93,8 @@ class Image2Text:
52
  return model, processor
53
 
54
  def load_img(self, inputs, width=480, height=480):
55
- image = Image.fromarray(inputs)
 
56
  image = aspect_ratio_preserving_resize_and_crop(image, target_width=width, target_height=height).convert("RGB")
57
  img = self.processor(image , return_tensors="pt", size=(width, height)).pixel_values
58
  pixel_values = img.to(self.device)
 
1
+ import os
2
+ from typing import Union
3
+
4
+ import PIL.Image
5
+ import PIL.ImageOps
6
+ import requests
7
+
8
+
9
+ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image:
10
+ """
11
+ Loads `image` to a PIL Image.
12
+
13
+ Args:
14
+ image (`str` or `PIL.Image.Image`):
15
+ The image to convert to the PIL Image format.
16
+ Returns:
17
+ `PIL.Image.Image`:
18
+ A PIL Image.
19
+ """
20
+ if isinstance(image, str):
21
+ if image.startswith("http://") or image.startswith("https://"):
22
+ image = PIL.Image.open(requests.get(image, stream=True).raw)
23
+ elif os.path.isfile(image):
24
+ image = PIL.Image.open(image)
25
+ else:
26
+ raise ValueError(
27
+ f"Incorrect path or url, URLs must start with `http://` or `https://`, and {image} is not a valid path"
28
+ )
29
+ elif isinstance(image, PIL.Image.Image):
30
+ image = image
31
+ else:
32
+ raise ValueError(
33
+ "Incorrect format used for image. Should be an url linking to an image, a local path, or a PIL image."
34
+ )
35
+ image = PIL.ImageOps.exif_transpose(image)
36
+ image = image.convert("RGB")
37
+ return image
38
+
39
+
40
+
41
+
42
  import re
43
  import torch
44
  import numpy as np
 
93
  return model, processor
94
 
95
  def load_img(self, inputs, width=480, height=480):
96
+ # image = Image.fromarray(inputs)
97
+ image = load_image(inputs)
98
  image = aspect_ratio_preserving_resize_and_crop(image, target_width=width, target_height=height).convert("RGB")
99
  img = self.processor(image , return_tensors="pt", size=(width, height)).pixel_values
100
  pixel_values = img.to(self.device)