AItool commited on
Commit
63062e5
Β·
verified Β·
1 Parent(s): 69e8bc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -40
app.py CHANGED
@@ -7,14 +7,13 @@ import streamlit as st
7
  import numpy as np
8
  import requests
9
  from io import BytesIO
10
- import easyocr as ocr
11
 
12
  def local_css(file_name):
13
  with open(file_name) as f:
14
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
15
 
16
  st.set_page_config(
17
- page_title="Streamlit iCodeIdoia - OCR an IMAGE - Extract text from an image",
18
  page_icon="images/ilpicon1.png",layout="wide",initial_sidebar_state="expanded"
19
  )
20
 
@@ -23,62 +22,81 @@ st.image("images/banner.jpg")
23
  # ---- LOAD
24
  local_css("styles/style.css")
25
 
26
- @st.cache_resource
27
- def load_model():
28
- reader = ocr.Reader(['en'],model_storage_directory='.')
29
- return reader
30
 
31
- reader = load_model() #load model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # ---- TABS
34
  tab1, tab2 = st.tabs(["Demo","Application"])
35
 
36
  with tab1:
37
  # Handle first image
38
-
39
- url = "https://https://raw.githubusercontent.com/webdevserv/images_video/main/ocr_sample.jpg"
 
40
 
41
- st.subheader("OCR an image demo")
42
- img_description = st.text('Image text will extracted using OCR.')
43
 
44
- if st.button('OCR Demo'):
45
  response = requests.get(url)
46
  img = Image.open(BytesIO(response.content))
47
- st.image(input_image) #display image
48
  img.load()
49
- st.image(img) #display image
50
 
51
- with st.spinner("πŸ”„ OCR in process."):
52
- result = reader.readtext(np.array(img))
53
- result_text = [] #empty list
54
- for text in result:
55
- result_text.append(text[1])
56
 
57
- st.write(result_text)
58
- st.balloons()
59
- else:
60
- st.write("Upload an image to extract text using OCR.")
61
 
 
 
62
 
63
  with tab2:
64
- st.subheader("OCR an image app")
65
- img_description = st.text('Image text will be extracted using OCR.')
66
- uploaded_file = st.file_uploader("Upload a image to OCR.", type=['jpg'])
67
 
68
  if uploaded_file is not None:
69
  img = Image.open(uploaded_file)
70
  img.load()
71
- st.image(img) #display image
72
-
73
- with st.spinner("πŸ”„ OCR in process."):
74
- result = reader.readtext(np.array(img))
75
-
76
- result_text = [] #empty list for results
77
-
78
- for text in result:
79
- result_text.append(text[1])
80
-
81
- st.write(result_text)
82
- st.balloons()
83
- else:
84
- st.write("Upload an image to extract text using OCR.")
 
7
  import numpy as np
8
  import requests
9
  from io import BytesIO
 
10
 
11
  def local_css(file_name):
12
  with open(file_name) as f:
13
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
14
 
15
  st.set_page_config(
16
+ page_title="Streamlit iCodeIdoia",
17
  page_icon="images/ilpicon1.png",layout="wide",initial_sidebar_state="expanded"
18
  )
19
 
 
22
  # ---- LOAD
23
  local_css("styles/style.css")
24
 
25
+ def fill_square_cropper(img):
26
+ imgsz = [img.height, img.width]
 
 
27
 
28
+ original_size = imgsz
29
+
30
+ smallestsize = min(imgsz)
31
+ biggestsize = max(imgsz)
32
+
33
+ #get vertical color filler
34
+ avg_color_per_row = np.average(img, axis=0)
35
+ avg_color = np.average(avg_color_per_row, axis=0)
36
+
37
+ if img.height > img.width:
38
+ #height is bigger than width
39
+ area = (0, 0, img.width + (img.height - img.width),img.height)
40
+ cropped_img = img.crop(area)
41
+ newimgsz = [cropped_img.height, cropped_img.width]
42
+
43
+ newimg = Image.new('RGB', ([newimgsz[1],newimgsz[0]]), (round(avg_color[0]), round(avg_color[1]), round(avg_color[2])))
44
+
45
+ newpos = (img.height-img.width)
46
+ newpos = newpos/2
47
+ newimg.paste(img,(int(newpos),0))
48
+
49
+ return newimg
50
+
51
+ #vertically add color
52
+ if img.width > img.height:
53
+ area = (0, 0, img.width, img.height+ (img.width - img.height))
54
+ cropped_img = img.crop(area)
55
+ newimgsz = [cropped_img.height, cropped_img.width]
56
+
57
+ newimg = Image.new('RGB', ([newimgsz[1],newimgsz[0]]), (round(avg_color[0]), round(avg_color[1]), round(avg_color[2])))
58
+
59
+ newpos = (img.width-img.height)
60
+ newpos = newpos/2
61
+ newimg.paste(img,(0,(int(newpos))))
62
+
63
+ return newimg
64
 
65
  # ---- TABS
66
  tab1, tab2 = st.tabs(["Demo","Application"])
67
 
68
  with tab1:
69
  # Handle first image
70
+ url = "https://raw.githubusercontent.com/webdevserv/images_video/main/cowportrait.jpg"
71
+ # Handle second image
72
+ url2 = "https://raw.githubusercontent.com/webdevserv/images_video/main/cowlandscape.jpg"
73
 
74
+ st.subheader("Square an image demo")
75
+ img_description = st.text('Image will be squared with color filler where applicable.')
76
 
77
+ if st.button('Square and Fill Demo'):
78
  response = requests.get(url)
79
  img = Image.open(BytesIO(response.content))
 
80
  img.load()
 
81
 
82
+ generated_img = fill_square_cropper(img)
83
+ st.image(generated_img)
 
 
 
84
 
85
+ response = requests.get(url2)
86
+ img = Image.open(BytesIO(response.content))
87
+ img.load()
 
88
 
89
+ generated_img = fill_square_cropper(img)
90
+ st.image(generated_img)
91
 
92
  with tab2:
93
+ st.subheader("Square an image app")
94
+ img_description = st.text('Image will be squared with color filler where applicable.')
95
+ uploaded_file = st.file_uploader("Upload a JPG image to square and fill with color.", type=['jpg'])
96
 
97
  if uploaded_file is not None:
98
  img = Image.open(uploaded_file)
99
  img.load()
100
+ generated_img = fill_square_cropper(img)
101
+ st.image(generated_img)
102
+ st.balloons()