File size: 3,702 Bytes
48e4ba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97cee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36845d6
97cee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb4ee89
48e4ba2
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import cv2
import requests
import time
import os
import gradio as gr

def textdetect(img):
    if img is not None:
      cv2.imwrite("input.jpg",img)
      #if flag == True:
      with open('input.jpg', 'rb') as f:
        data = f.read()
      r = requests.post(os.environ["endpoint"],data=data,headers={"Ocp-Apim-Subscription-Key":os.environ["key"],"Content-Type": "application/octet-stream"})
      time.sleep(3)
      try:
        r2 = requests.get(r.headers['Operation-Location'],headers={"Ocp-Apim-Subscription-Key":os.environ["key"]})
        my_dict = {}
        print(r2.json())
        lines=[]
        for line in r2.json()['analyzeResult']['readResults'][0]['lines']:
          lines.append(line['text'])
          #st.markdown(f"<h1 style='text-align:center;font-family:cursive;'>{line['text']}</h1>",unsafe_allow_html=True)
          for word in line['words']:
            my_dict[word['text']] = word['confidence']
        #for key in my_dict:
            #st.metric(label='',value=f"{key}", delta=f"{my_dict[key]*100} %")
            #st.progress(my_dict[key])
        print(lines)
        s = '\n'.join(lines)
        #s = '<h1>' +s+ '</h1>'
        return (s,my_dict)
      except:
        return 'Something went wrong, try refreshing',None
    else:
      return None,None

css = """
footer {display:none !important}
.output-markdown{display:none !important}
.gr-button-primary {
    z-index: 14;
    height: 43px;
    left: 0px;
    top: 0px;
    padding: 0px;
    cursor: pointer !important; 
    background: none rgb(17, 20, 45) !important;
    border: none !important;
    text-align: center !important;
    font-family: Poppins !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    color: rgb(255, 255, 255) !important;
    line-height: 1 !important;
    border-radius: 12px !important;
    transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
    box-shadow: none !important;
}
.gr-button-primary:hover{
    z-index: 14;
    height: 43px;
    left: 0px;
    top: 0px;
    padding: 0px;
    cursor: pointer !important;
    background: none rgb(66, 133, 244) !important;
    border: none !important;
    text-align: center !important;
    font-family: Poppins !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    color: rgb(255, 255, 255) !important;
    line-height: 1 !important;
    border-radius: 12px !important;
    transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
    box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
}
.hover\:bg-orange-50:hover {
    --tw-bg-opacity: 1 !important;
    background-color: rgb(229,225,255) !important;
}
.to-orange-200 {
    --tw-gradient-to: rgb(37 56 133 / 37%) !important;
}
.from-orange-400 {
    --tw-gradient-from: rgb(17, 20, 45) !important;
    --tw-gradient-to: rgb(255 150 51 / 0);
    --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to) !important;
}
.group-hover\:from-orange-500{
    --tw-gradient-from:rgb(17, 20, 45) !important; 
    --tw-gradient-to: rgb(37 56 133 / 37%);
    --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to) !important;
}
.group:hover .group-hover\:text-orange-500{
    --tw-text-opacity: 1 !important;
    color:rgb(37 56 133 / var(--tw-text-opacity)) !important;
}
"""
with gr.Blocks(title="Sketchpad Text Detection | Data Science Dojo", css=css) as demo:
  with gr.Row():
    inp = gr.Paint()
  but = gr.Button('Submit',variant='primary')
  with gr.Row():
    with gr.Column():
      out1 = gr.Text(label='Lines')
    with gr.Column():
      out2 = gr.Label(label='Words')

  but.click(textdetect,inputs =[inp],outputs = [out1,out2])
demo.launch(debug=True)