-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (79 loc) · 4.38 KB
/
main.py
File metadata and controls
101 lines (79 loc) · 4.38 KB
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
import streamlit as st
import os
from ocr.document_ai import DocumentOCR
from ocr.preprocess import preprocess_image
from ocr.extract import extract_nutritional_info
from ocr.barcode_scanner import BarcodeScanner
from ocr.barcode_extract import extract_nutritional_info_from_barcode
PROJECT_ID = os.getenv("PROJECT_ID")
PROCESSOR_ID = os.getenv("PROCESSOR_ID")
st.title("🥗 Food Label OCR & Barcode Scanner")
# Add a tab interface to switch between OCR and barcode scanning
tab_selection = st.radio("Select Input Method:", ["OCR Label Scanner", "Barcode Scanner"])
if tab_selection == "OCR Label Scanner":
st.header("OCR Label Scanner")
uploaded_file = st.file_uploader("Upload a food label image", type=["png", "jpg", "jpeg"])
if uploaded_file:
image_path = "uploaded_image.png"
with open(image_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.image(image_path, caption="Uploaded Image", use_column_width=True)
# Process button
if st.button("Extract Nutrition Information"):
with st.spinner("Processing image..."):
# Preprocess image
processed_image_path = preprocess_image(image_path)
# Perform OCR
ocr = DocumentOCR(PROJECT_ID, PROCESSOR_ID)
extracted_data = ocr.process_document(processed_image_path)
# Extract nutritional information
nutrition_info = extract_nutritional_info(extracted_data)
# Display extracted information
st.subheader("Extracted Nutritional Information")
for key, value in nutrition_info.items():
st.write(f"**{key}:** {value if value else 'Not Found'}")
else: # Barcode Scanner
st.header("Barcode Scanner")
st.write("Upload an image containing a barcode or enter a barcode manually to fetch nutrition information")
barcode_method = st.radio("Select barcode input method:", ["Upload Image", "Enter Manually"])
barcode_value = None
barcode_scanner = BarcodeScanner()
if barcode_method == "Upload Image":
barcode_file = st.file_uploader("Upload barcode image", type=["png", "jpg", "jpeg"])
if barcode_file:
# Save the uploaded file
barcode_image_path = "barcode_image.png"
with open(barcode_image_path, "wb") as f:
f.write(barcode_file.getbuffer())
st.image(barcode_image_path, caption="Uploaded Barcode Image", width=300)
# Process button for barcode scanning
if st.button("Scan Barcode"):
with st.spinner("Scanning barcode..."):
try:
barcode_value = barcode_scanner.scan_barcode_from_image(barcode_image_path)
if barcode_value:
st.success(f"Barcode detected: {barcode_value}")
else:
st.error("No barcode detected in the image")
except Exception as e:
st.error(f"Error scanning barcode: {str(e)}")
else: # Manual entry
barcode_value = st.text_input("Enter barcode number")
# Fetch product information if we have a barcode
if barcode_value and st.button("Fetch Product Information"):
with st.spinner("Fetching product information..."):
product_info = barcode_scanner.get_product_info(barcode_value)
if product_info["success"]:
nutrition_info, additional_info = extract_nutritional_info_from_barcode(product_info)
# Display product details
st.subheader(additional_info.get("Product Name", "Product Information"))
st.write(f"**Brand:** {additional_info.get('Brand', 'Unknown')}")
# Display product image if available
if additional_info.get("Image URL"):
st.image(additional_info["Image URL"], caption="Product Image", width=300)
# Display nutritional information
st.subheader("Nutritional Information")
for key, value in nutrition_info.items():
st.write(f"**{key}:** {value}")
else:
st.error(f"Error: {product_info.get('error', 'Failed to fetch product information')}")