sdfswefrwerwe commited on
Commit
e1eecef
1 Parent(s): 383fa96
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -1,27 +1,31 @@
1
  import gradio as gr
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
- import requests
4
  from PIL import Image
5
-
6
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-printed")
7
  model = VisionEncoderDecoderModel.from_pretrained("pENrknSoysneed/8kun-captcha-ocr-meme")
8
 
9
- # load image examples
10
- urls = [
11
- 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nfcb5.png',
12
- 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/p57fn.png',
13
- 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w2yp7.png',
14
- 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/pme86.png',
15
- 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w4nfx.png',
16
- 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nf8b8.png'
17
- ]
18
- for idx, url in enumerate(urls):
19
- image = Image.open(requests.get(url, stream=True).raw)
20
- image.save(f"image_{idx}.png")
21
 
22
  def process_image(image):
23
  # prepare image
24
- pixel_values = processor(image, return_tensors="pt").pixel_values
 
 
 
 
25
 
26
  # generate (no beam search)
27
  generated_ids = model.generate(pixel_values)
@@ -39,8 +43,10 @@ description = "Due to events. in 8chan staff moderation. I am attacking it. The
39
  #css = """.output_image, .input_image {height: 600px !important}"""
40
 
41
  iface = gr.Interface(fn=process_image,
42
- inputs=gr.inputs.Image(type="pil"),
43
- outputs=gr.outputs.Textbox(),
 
 
44
  title=title,
45
  description=description,
46
  # article=article,
 
1
  import gradio as gr
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ import requests, re, base64
4
  from PIL import Image
5
+ from io import BytesIO
6
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-printed")
7
  model = VisionEncoderDecoderModel.from_pretrained("pENrknSoysneed/8kun-captcha-ocr-meme")
8
 
9
+ # # load image examples
10
+ # urls = [
11
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nfcb5.png',
12
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/p57fn.png',
13
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w2yp7.png',
14
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/pme86.png',
15
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w4nfx.png',
16
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nf8b8.png'
17
+ # ]
18
+ # for idx, url in enumerate(urls):
19
+ # image = Image.open(requests.get(url, stream=True).raw)
20
+ # image.save(f"image_{idx}.png")
21
 
22
  def process_image(image):
23
  # prepare image
24
+ image_data = re.sub('^data:image/.+;base64,', '', image)
25
+ im = Image.open(BytesIO(base64.b64decode(image_data)))
26
+
27
+ #Take's the picture
28
+ pixel_values = processor(im, return_tensors="pt").pixel_values
29
 
30
  # generate (no beam search)
31
  generated_ids = model.generate(pixel_values)
 
43
  #css = """.output_image, .input_image {height: 600px !important}"""
44
 
45
  iface = gr.Interface(fn=process_image,
46
+ # inputs=gr.inputs.Image(type="pil"),
47
+ inputs=gr.Textbox(placeholder="base64 string (right-click => copy-link) ...")
48
+ outputs=gr.outputs.Textbox(),
49
+
50
  title=title,
51
  description=description,
52
  # article=article,