Sanjayraju30 commited on
Commit
4fd2f10
Β·
verified Β·
1 Parent(s): b0756ce

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +33 -34
src/streamlit_app.py CHANGED
@@ -1,41 +1,40 @@
1
  import streamlit as st
2
- import numpy as np
3
- import cv2
4
  from paddleocr import PaddleOCR
5
  from PIL import Image
 
6
  import re
7
 
 
8
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
9
 
10
- def preprocess_image(image):
11
- img = np.array(image.convert("RGB"))
12
- gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
13
- _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
14
- return Image.fromarray(thresh)
15
-
16
- def extract_weight_text(image):
17
- results = ocr.ocr(np.array(image), cls=True)
18
- for line in results[0]:
19
- text = line[1][0]
20
- match = re.search(r"\d+\.\d+", text)
21
- if match:
22
- return match.group()
23
- return None
24
-
25
- st.title("πŸ“¦ Auto Weight Logger")
26
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
27
- camera_image = st.camera_input("Or Capture Image")
28
-
29
- input_image = uploaded_file or camera_image
30
-
31
- if input_image:
32
- image = Image.open(input_image)
33
- st.image(image, caption="Original Image", use_column_width=True)
34
-
35
- processed_image = preprocess_image(image)
36
- weight = extract_weight_text(processed_image)
37
-
38
- if weight:
39
- st.success(f"βœ… Weight Detected: {weight} kg")
40
- else:
41
- st.error("❌ Weight not detected.")
 
1
  import streamlit as st
 
 
2
  from paddleocr import PaddleOCR
3
  from PIL import Image
4
+ import numpy as np
5
  import re
6
 
7
+ # Initialize OCR
8
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
9
 
10
+ st.set_page_config(page_title="Auto Weight Logger", layout="centered")
11
+ st.title("πŸ“· Auto Weight Logger")
12
+
13
+ # Upload image
14
+ uploaded_file = st.file_uploader("Upload an image of the weight display", type=["jpg", "jpeg", "png"])
15
+
16
+ if uploaded_file is not None:
17
+ # Load image using PIL (no OpenCV)
18
+ image = Image.open(uploaded_file).convert("RGB")
19
+ st.image(image, caption="Uploaded Image", use_column_width=True)
20
+
21
+ # Convert PIL image to numpy array
22
+ img_array = np.array(image)
23
+
24
+ # Run OCR
25
+ with st.spinner("Detecting weight..."):
26
+ result = ocr.ocr(img_array, cls=True)
27
+
28
+ weight = "Not detected"
29
+ confidence = 0.0
30
+
31
+ for line in result[0]:
32
+ for word_info in line:
33
+ text, conf = word_info[1]
34
+ match = re.search(r'\d+\.\d+|\d+', text)
35
+ if match:
36
+ weight = match.group()
37
+ confidence = conf
38
+ break
39
+
40
+ st.markdown(f"### βœ… Weight: **{weight} kg** (Confidence: {confidence:.2%})")