bipinkrish commited on
Commit
7dfa51c
·
verified ·
1 Parent(s): fa813d8

Upload 13 files

Browse files
Files changed (2) hide show
  1. sign2img.py +32 -6
  2. text2gloss.py +18 -0
sign2img.py CHANGED
@@ -2,20 +2,46 @@ from io import BytesIO
2
  from signwriting.visualizer.visualize import signwriting_to_image
3
  from PIL import Image
4
 
5
-
6
  def to_bytes_io(image: Image) -> bytes:
7
  imgByte = BytesIO()
8
  image.save(imgByte, "png")
9
  imgByte.seek(0)
10
  return imgByte.getvalue()
11
 
12
-
13
- def convert(fsw: str, line_color=None, fill_color=None) -> bytes:
14
  if not line_color or len(line_color) != 4:
15
  line_color = (0, 0, 0, 255)
16
 
17
  if not fill_color or len(fill_color) != 4:
18
  fill_color = (0, 0, 0, 0)
19
-
20
- img: Image = signwriting_to_image(fsw, line_color=line_color, fill_color=fill_color)
21
- return to_bytes_io(img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from signwriting.visualizer.visualize import signwriting_to_image
3
  from PIL import Image
4
 
 
5
  def to_bytes_io(image: Image) -> bytes:
6
  imgByte = BytesIO()
7
  image.save(imgByte, "png")
8
  imgByte.seek(0)
9
  return imgByte.getvalue()
10
 
11
+ def convert(fsw: str, line_color=None, fill_color=None, arrangement="row") -> bytes:
 
12
  if not line_color or len(line_color) != 4:
13
  line_color = (0, 0, 0, 255)
14
 
15
  if not fill_color or len(fill_color) != 4:
16
  fill_color = (0, 0, 0, 0)
17
+
18
+ fsw_list = fsw.split()
19
+
20
+ # Convert each FSW string into an image and store them in a list
21
+ images = []
22
+ for fsw_string in fsw_list:
23
+ img = signwriting_to_image(fsw_string, line_color=line_color, fill_color=fill_color)
24
+ images.append(img)
25
+
26
+ # Determine the maximum height and total width of all images
27
+ max_height = max(img.height for img in images)
28
+ total_width = sum(img.width for img in images)
29
+
30
+ # Create a new image to hold all images either row-wise or column-wise
31
+ if arrangement == "row":
32
+ final_image = Image.new("RGBA", (total_width, max_height), (255, 255, 255, 0))
33
+ elif arrangement == "column":
34
+ final_image = Image.new("RGBA", (max_height, total_width), (255, 255, 255, 0))
35
+
36
+ # Paste each image onto the final image either row-wise or column-wise
37
+ x_offset = 0
38
+ y_offset = 0
39
+ for img in images:
40
+ if arrangement == "row":
41
+ final_image.paste(img, (x_offset, 0))
42
+ x_offset += img.width
43
+ elif arrangement == "column":
44
+ final_image.paste(img, (0, y_offset))
45
+ y_offset += img.height
46
+
47
+ return to_bytes_io(final_image)
text2gloss.py CHANGED
@@ -61,16 +61,34 @@ TRANSLATOR = Translator(
61
 
62
  def translate(text: str) -> str:
63
  spoken = text.lower().strip().split()
 
 
 
 
64
  spoken.append(".")
65
 
66
  src_seq = [SRC.vocab.stoi.get(word, unk_idx) for word in spoken]
67
  pred_seq = TRANSLATOR.translate_sentence(
68
  torch.LongTensor([src_seq]).to(device))
 
69
  pred_line = ' '.join(TRG.vocab.itos[idx] for idx in pred_seq)
70
  pred_line = pred_line.replace(Constants.BOS_WORD, '').replace(
71
  Constants.EOS_WORD, '').replace(Constants.PAD_WORD, '').replace(Constants.UNK_WORD, '')
72
 
73
  final = str(pred_line.strip())
 
 
 
 
74
  if final.endswith(" ."):
75
  final = final[:-2]
 
 
 
 
76
  return final
 
 
 
 
 
 
61
 
62
  def translate(text: str) -> str:
63
  spoken = text.lower().strip().split()
64
+
65
+ if all(c.isdigit() for c in spoken):
66
+ return text
67
+
68
  spoken.append(".")
69
 
70
  src_seq = [SRC.vocab.stoi.get(word, unk_idx) for word in spoken]
71
  pred_seq = TRANSLATOR.translate_sentence(
72
  torch.LongTensor([src_seq]).to(device))
73
+ pred_seq = set(pred_seq)
74
  pred_line = ' '.join(TRG.vocab.itos[idx] for idx in pred_seq)
75
  pred_line = pred_line.replace(Constants.BOS_WORD, '').replace(
76
  Constants.EOS_WORD, '').replace(Constants.PAD_WORD, '').replace(Constants.UNK_WORD, '')
77
 
78
  final = str(pred_line.strip())
79
+
80
+ if not contains_alpha_or_digits(final):
81
+ return text.lower().strip()
82
+
83
  if final.endswith(" ."):
84
  final = final[:-2]
85
+
86
+ if "i" in spoken and "me" not in final.split():
87
+ final = "me " + final
88
+
89
  return final
90
+
91
+ def contains_alpha_or_digits(s: str) -> bool:
92
+ contains_alpha = any(c.isalpha() for c in s)
93
+ contains_digits = any(c.isdigit() for c in s)
94
+ return any([contains_alpha, contains_digits])