File size: 6,386 Bytes
7acf2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4049dd8
 
 
 
 
 
 
 
 
 
 
 
 
 
7acf2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4049dd8
1caccfc
7acf2e3
1caccfc
 
7acf2e3
 
 
 
 
 
 
 
 
 
 
 
 
29f484d
7acf2e3
 
 
 
 
 
 
 
4049dd8
9d54e16
7acf2e3
 
 
 
 
 
4049dd8
7acf2e3
 
 
 
 
4049dd8
7acf2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4049dd8
 
 
 
 
7acf2e3
 
 
 
 
4049dd8
7acf2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import json
import requests
import streamlit as st
import pandas as pd
import numpy as np
from urllib.request import urlretrieve ,urlopen
import os
import torch
from distutils.dir_util import copy_tree
import tempfile
import zipfile
import subprocess
import mmcv
from mmcv import Config
import sys
from shapely.geometry import Polygon 


import shutil
import ssl

import urllib.request
from pathlib import Path

import cv2
from PIL import Image
##########
@st.cache(allow_output_mutation=True)
def loading_resources():
  dl_url = os.environ['dl_url']
  if 'utils2.py' not in os.listdir('./'):
    urlretrieve(dl_url, 'appdata.zip')
    with zipfile.ZipFile("appdata.zip","r") as zip_ref:
      zip_ref.extractall("./")
loading_resources()

import utilss
from utils2 import *
if 'model_inference' not in sys.modules:
  from mmocr.apis import init_detector ,model_inference
else:
  model_inference = sys.modules['model_inference']
  init_detector = sys.modules['init_detector']

########## utils
def draw_conts(im,conts,fcolor,stroke):
  opacity = .3
  im =im.copy()
  # fcolor =fcolor if fcolor else get_random_color()
  frontmm = np.full_like(im,fcolor,np.uint8)
  mask = np.zeros_like(im)
  for cont in conts:
    cv2.drawContours(mask, [cont], -1, color=[int(opacity*255)]*3, thickness=cv2.FILLED)
  mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
  rgba = np.dstack((frontmm, mask))
  im = utilss.overlay_image_alpha(im,rgba)
  # drawline
  cv2.drawContours(im, conts, -1, color=fcolor, thickness=stroke)
  return im
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]
    if width is None and height is None:
        return image

    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))
    resized = cv2.resize(image, dim, interpolation = inter)
    return resized
def download_file(url, local_filename):
    with requests.get(url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

    return local_filename
def load_and_preprocess_img(img_path, bbox=None):
  img = Image.open(img_path).convert('RGB')
  img = np.array(img).astype(np.uint8)
  return img


# @st.cache()
def predictim(im , model):
  if 'model_inference' not in sys.modules:
    # if not model_inference:
    from mmocr.apis import model_inference
  result = model_inference(model,im )
  # result[]
  return result
  # return result
# minfer=None
# minit=None
@st.cache(allow_output_mutation=True)
def loading_model():
  # global minfer
  # global minit
  
  from mmdet.apis import set_random_seed
  set_random_seed(0, deterministic=False)
  cfg = Config.fromfile('./cfgsn.py')
  # checkpoint = "./200s.pth"
  checkpoint = "./100s.pth"
  model1 = init_detector(cfg, checkpoint, device="cpu")
  # if model1.cfg.data.test['type'] == 'ConcatDataset':
  #   model1.cfg.data.test.pipeline = model1.cfg.data.test['datasets'][0].pipeline
  return model1
model = loading_model()



def main():
  st.sidebar.info('Images are deleted instantly')
  form0 =st.sidebar.form("my_form0")

  f = form0.file_uploader("Upload an Image", type=['png', 'jpg', 'jpeg', 'tiff', 'gif'])
  btnpredicrupload = form0.form_submit_button("PREDICT")


  # st.sidebar.write(" ------ ")
  form =st.sidebar.form("my_form")
  photos = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg',]
  option = form.selectbox('Or choose a sample image', photos)
  submitted = form.form_submit_button("PREDICT")

  st.sidebar.write(" ------ ")
  st.sidebar.info('Options')
  extra_postprocess = st.sidebar.checkbox('perform extra postprocessing',value=True)
  fcolor = st.sidebar.selectbox('boundary color', ['red','green','blue'],index =0)
  fstroke = st.sidebar.selectbox('boundary thickness', ['1','2','3'] ,index=0 )
  # do_thresh_box = st.sidebar.checkbox('adaptive threshold')

  if btnpredicrupload and f is not None:
    tfile = tempfile.NamedTemporaryFile(delete=True)
    tfile.write(f.read())
    imgpath=tfile.name
    run_app(imgpath ,fcolor,extra_post=extra_postprocess,fstroke=fstroke)
  if submitted:
    st.empty()
    directory ='./imgs/'
    pic = os.path.join(directory, option)
    imgpath=directory+option
    run_app(imgpath ,fcolor,extra_post=extra_postprocess,fstroke=fstroke)
  # inp = st.text_input('t2','')
  # if st.button('run'):
  #   subprocess.run(inp.split(' '))


# def run_app(imgpath,do_thresh,fcolor):
def run_app(imgpath,fcolor,**kwargs):
  d0 =dict(red=[255,0,0] ,green=[0,255,0] ,blue=[0,0,255]  )
  fcolor = d0[fcolor]
  # st.sidebar.write(imgpath)
  # r = np.random.randint(1e3,1e7)
  tfile = tempfile.NamedTemporaryFile(delete=True,suffix='.jpg')
  dst = tfile.name
  # tfile = open()
  img = load_and_preprocess_img(imgpath)
  # img = utilss.resize_with_pad(img,800,800)
  # if do_thresh:
  #   gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  #   threshim = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 51, 20)
  #   img= cv2.cvtColor(threshim, cv2.COLOR_GRAY2RGB)
  
  cv2.imwrite(dst,cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  # imgo = image_resize(img , width = 640)
  st.write('input:')
  st.image(img, caption = "Selected Input" , width =640)

  # if st.button('predict2'):
  
  # try:
  # except: print('errr--------------')
  ''
  ##################
  preds,img_metas,downsample_ratio =  model_inference(model,dst )
  ff= fixer(img,preds,img_metas,downsample_ratio)
  polys= ff.polysfin3
  if kwargs['extra_post']:
    try:
      polys =find_polys4(ff) 
    except:
      print('error polys4')
      polys= ff.polysfin3
  

  # with open('./0.json','r') as f:
  #   res = json.load(f)
  ################
  mm = draw_conts(img,[poly2cont(p) for p in polys],fcolor,stroke=int(kwargs['fstroke']))
  ## mm=image_resize(mm, width = 640)
  #######
  # mm=preds[1]*preds[0]
  # mm=cv2.cvtColor(mm, cv2.COLOR_GRAY2RGB)
  st.image(mm,caption='output image' , width = 640)
  # del polys
  del ff
  del mm 
  del preds
  del polys
  del img
  del img_metas
  # os.remove(dst)
  # x = predictim(img,model)
  # st.write(x)

main()