Spaces:
Runtime error
Runtime error
| 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 | |
| ########## | |
| 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 | |
| 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() | |