Spaces:
Sleeping
Sleeping
Commit
·
449f4e3
1
Parent(s):
99fa310
Initial Commit
Browse files- DenseNet20_model.h5 +3 -0
- app.py +142 -0
- b.pt +3 -0
- requirements.txt +9 -0
- yolov5s.pt +3 -0
DenseNet20_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d7a7deb953774a8f8ef09fe617c93889be9dcc4003f1a9775f0a9a71376e0294
|
3 |
+
size 87387592
|
app.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from paddleocr import PaddleOCR
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
7 |
+
import numpy as np
|
8 |
+
from datetime import datetime, timedelta
|
9 |
+
import re
|
10 |
+
from ultralytics import YOLO
|
11 |
+
import pandas as pd
|
12 |
+
|
13 |
+
# Initialize models
|
14 |
+
ocr = PaddleOCR(lang='en')
|
15 |
+
fruit_model = load_model('DenseNet20_model.h5')
|
16 |
+
brand_model = YOLO('b.pt')
|
17 |
+
|
18 |
+
# Class names for fruit freshness classification
|
19 |
+
class_names = {
|
20 |
+
0: 'Banana_Bad',
|
21 |
+
1: 'Banana_Good',
|
22 |
+
2: 'Fresh',
|
23 |
+
3: 'FreshCarrot',
|
24 |
+
4: 'FreshCucumber',
|
25 |
+
5: 'FreshMango',
|
26 |
+
6: 'FreshTomato',
|
27 |
+
7: 'Guava_Bad',
|
28 |
+
8: 'Guava_Good',
|
29 |
+
9: 'Lime_Bad',
|
30 |
+
10: 'Lime_Good',
|
31 |
+
11: 'Rotten',
|
32 |
+
12: 'RottenCarrot',
|
33 |
+
13: 'RottenCucumber',
|
34 |
+
14: 'RottenMango',
|
35 |
+
15: 'RottenTomato',
|
36 |
+
16: 'freshBread',
|
37 |
+
17: 'rottenBread'
|
38 |
+
}
|
39 |
+
|
40 |
+
# Helper functions
|
41 |
+
def preprocess_image(image_path):
|
42 |
+
img = load_img(image_path, target_size=(128, 128))
|
43 |
+
img_array = img_to_array(img)
|
44 |
+
img_array = np.expand_dims(img_array, axis=0)
|
45 |
+
img_array = img_array / 255.0
|
46 |
+
return img_array
|
47 |
+
|
48 |
+
def extract_expiry_dates(text):
|
49 |
+
expiry_date_patterns = [
|
50 |
+
r'USE BY (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})',
|
51 |
+
r'BEST BEFORE (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})',
|
52 |
+
r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})',
|
53 |
+
r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2})',
|
54 |
+
r'(\d{1,2}\s*[A-Za-z]{3,}\s*\d{4})',
|
55 |
+
r'(\d{1,2}\s*[A-Za-z]{3,}\s*\d{2})',
|
56 |
+
r'(\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2})',
|
57 |
+
r'(\d{4}[A-Za-z]{3,}\d{1,2})',
|
58 |
+
r'(\d{1,2}[A-Za-z]{3,}\d{4})',
|
59 |
+
r'Best before (\d+) months'
|
60 |
+
]
|
61 |
+
dates = []
|
62 |
+
for pattern in expiry_date_patterns:
|
63 |
+
matches = re.findall(pattern, text, re.IGNORECASE)
|
64 |
+
dates.extend(matches)
|
65 |
+
return dates
|
66 |
+
|
67 |
+
# Streamlit app
|
68 |
+
st.title("Image Processing Application")
|
69 |
+
|
70 |
+
# User choice for processing
|
71 |
+
task_choice = st.radio("Choose a task", ("Text and Brand Detection", "Freshness Detection"))
|
72 |
+
|
73 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
74 |
+
|
75 |
+
if uploaded_file is not None:
|
76 |
+
# Ensure the uploads directory exists
|
77 |
+
if not os.path.exists("uploads"):
|
78 |
+
os.makedirs("uploads")
|
79 |
+
|
80 |
+
# Save the uploaded file
|
81 |
+
image_path = os.path.join("uploads", uploaded_file.name)
|
82 |
+
with open(image_path, "wb") as f:
|
83 |
+
f.write(uploaded_file.getbuffer())
|
84 |
+
|
85 |
+
# Display the uploaded image
|
86 |
+
st.image(image_path, caption='Uploaded Image', use_container_width=True)
|
87 |
+
|
88 |
+
if task_choice == "Text and Brand Detection":
|
89 |
+
# Text Extraction
|
90 |
+
st.header("Text Extraction")
|
91 |
+
result = ocr.ocr(image_path)
|
92 |
+
text = ' '.join([line[1][0] for line in result[0]])
|
93 |
+
st.write("Extracted Text:")
|
94 |
+
st.text(text)
|
95 |
+
|
96 |
+
# Expiry Date Extraction
|
97 |
+
st.header("Expiry Date Extraction")
|
98 |
+
expiry_dates = extract_expiry_dates(text)
|
99 |
+
if expiry_dates:
|
100 |
+
st.write("Expiry Dates Found:")
|
101 |
+
for date in expiry_dates:
|
102 |
+
st.text(date)
|
103 |
+
else:
|
104 |
+
st.write("No expiry dates found.")
|
105 |
+
|
106 |
+
# Brand Prediction
|
107 |
+
st.header("Brand Prediction")
|
108 |
+
results = brand_model(image_path)
|
109 |
+
detected_brands = []
|
110 |
+
for result in results:
|
111 |
+
boxes = result.boxes.data.cpu().numpy()
|
112 |
+
for box in boxes:
|
113 |
+
class_id = int(box[5])
|
114 |
+
confidence = box[4] # Assuming the confidence score is at index 4
|
115 |
+
detected_brands.append((result.names[class_id], confidence))
|
116 |
+
|
117 |
+
# Count occurrences and average confidence of each brand
|
118 |
+
brand_data = {}
|
119 |
+
for brand, confidence in detected_brands:
|
120 |
+
if brand in brand_data:
|
121 |
+
brand_data[brand]['count'] += 1
|
122 |
+
brand_data[brand]['total_confidence'] += confidence
|
123 |
+
else:
|
124 |
+
brand_data[brand] = {'count': 1, 'total_confidence': confidence}
|
125 |
+
|
126 |
+
# Prepare data for display
|
127 |
+
brand_display_data = [
|
128 |
+
{'Object': brand, 'Count': data['count'], 'Average Confidence': data['total_confidence'] / data['count']}
|
129 |
+
for brand, data in brand_data.items()
|
130 |
+
]
|
131 |
+
|
132 |
+
# Display detected brands in a table with column names
|
133 |
+
st.write("Detected Brands and Counts:")
|
134 |
+
st.table(pd.DataFrame(brand_display_data))
|
135 |
+
|
136 |
+
elif task_choice == "Freshness Detection":
|
137 |
+
# Freshness Prediction
|
138 |
+
st.header("Freshness Prediction")
|
139 |
+
img_array = preprocess_image(image_path)
|
140 |
+
predictions = fruit_model.predict(img_array)
|
141 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
142 |
+
st.write("Predicted Freshness:", class_names[predicted_class])
|
b.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d4bfbf90d2489df68ac124e67bc911aab931bb8ff7b7f224c40a30faee2000c2
|
3 |
+
size 22467811
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
paddleocr
|
4 |
+
tensorflow
|
5 |
+
numpy
|
6 |
+
Pillow
|
7 |
+
opencv-python-headless
|
8 |
+
paddlepaddle
|
9 |
+
ultralytics
|
yolov5s.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b3b748c1e592ddd8868022e8732fde20025197328490623cc16c6f24d0782ee
|
3 |
+
size 14808437
|