Raykarr's picture
Update app.py
0d554e5 verified
import streamlit as st
import numpy as np
import cv2
import matplotlib.pyplot as plt
# -----------------------------------------------------------------
# Utility functions
# -----------------------------------------------------------------
def generate_colorful_image(height=256, width=256, p_blue=0.5):
"""
Generates a synthetic image (height x width) with:
- 'p_blue' fraction of blueish pixels
- (1 - p_blue) fraction of near-white/grey
Includes near-blue shades for more realistic challenge.
"""
img = np.zeros((height, width, 3), dtype=np.uint8)
for i in range(height):
for j in range(width):
if np.random.rand() < p_blue:
# Random shade of blue
b = np.random.randint(100, 256)
g = np.random.randint(0, 121)
r = np.random.randint(0, 121)
if np.random.rand() < 0.3: # shift to near-blue
g += np.random.randint(0, 30)
r += np.random.randint(0, 30)
b = min(b, 255)
g = min(g, 255)
r = min(r, 255)
img[i, j] = [b, g, r]
else:
# White/grey region
base = np.random.randint(180, 256)
diff_r = np.random.randint(-20, 20)
diff_g = np.random.randint(-20, 20)
diff_b = np.random.randint(-20, 20)
b = np.clip(base + diff_b, 0, 255)
g = np.clip(base + diff_g, 0, 255)
r = np.clip(base + diff_r, 0, 255)
img[i, j] = [b, g, r]
return img
def simple_threshold_blue(image_bgr):
"""
Simple approach:
1) Convert to HSV
2) Single broad threshold for blue
3) Count ratio of blue pixels
"""
hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)
# Broad range for 'blue'
lower_blue = np.array([90, 50, 50], dtype=np.uint8)
upper_blue = np.array([130, 255, 255], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_blue, upper_blue)
blue_pixels = cv2.countNonZero(mask)
total_pixels = image_bgr.shape[0] * image_bgr.shape[1]
perc_blue = (blue_pixels / total_pixels) * 100
return perc_blue, mask
def plot_color_histogram(image_bgr):
"""
Creates a matplotlib figure of B, G, and R channel histograms.
"""
color = ('b','g','r')
fig, ax = plt.subplots(figsize=(4,3))
for i,col in enumerate(color):
hist = cv2.calcHist([image_bgr],[i],None,[256],[0,256])
ax.plot(hist, color=col)
ax.set_xlim([0,256])
ax.set_title("Color Channel Histogram")
ax.set_xlabel("Pixel Intensity")
ax.set_ylabel("Frequency")
fig.tight_layout()
return fig
# -----------------------------------------------------------------
# Streamlit App
# -----------------------------------------------------------------
# Page config
st.set_page_config(page_title="ITC PSPD - Blue Area Detection Demo", layout="centered")
st.title("Blue Area Detection Demo for ITC PSPD")
st.markdown("""
**This assignment showcases an Industry 4.0 approach** to **color segmentation**,
demonstrating how tools like Python, OpenCV, and Streamlit can automate **quality checks**
(similar to checking the quality of paperboards, packaging prints, or other color-critical products).
---
**Why It Matters for ITC PSPD**:
- ITC Paperboards & Specialty Papers Division (PSPD) is a leader in paper, packaging, and specialty solutions.
- Precise color detection ensures **consistent brand identity**, reduces **defects**, and aligns with ITC's
**sustainability** and **innovation** ethos.
Below, you can:
1. **Generate** a synthetic image with random shades of **blue** and **near-white** regions.
2. **Analyze** the image using **Simple** thresholding.
3. **Visualize** color histograms and masks.
4. See how this **digital transformation** approach can benefit large-scale production lines.
---
""")
st.sidebar.header("Generation Controls")
p_blue = st.sidebar.slider("Fraction of Blue-ish Pixels", 0.0, 1.0, 0.5, 0.05)
img_size = st.sidebar.selectbox("Image Size (px)", [128, 192, 256, 320], index=2)
if "random_image" not in st.session_state:
st.session_state["random_image"] = None
st.sidebar.markdown("---")
if st.sidebar.button("Generate New Random Image"):
img_bgr = generate_colorful_image(height=img_size, width=img_size, p_blue=p_blue)
st.session_state["random_image"] = img_bgr
# Check if we have an image
if st.session_state["random_image"] is None:
st.warning("Use the sidebar to generate a new image.")
else:
st.subheader("1) Randomly Generated Image & Color Analysis")
# Convert BGR->RGB for display
img_bgr = st.session_state["random_image"]
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
col1, col2 = st.columns(2)
with col1:
st.image(img_rgb, caption="Synthesized Image (RGB)", width=250)
with col2:
# Show color histogram
hist_fig = plot_color_histogram(img_bgr)
st.pyplot(hist_fig)
st.markdown("---")
# Simple Threshold
st.subheader("2) Simple Threshold Approach")
simple_perc_blue, simple_mask = simple_threshold_blue(img_bgr)
col3, col4 = st.columns(2)
with col3:
st.write(f"**Blue Percentage (Simple):** {simple_perc_blue:.2f}%")
st.progress(min(simple_perc_blue/100, 1.0))
with col4:
st.image(simple_mask, caption="Simple Mask (white=detected blue)", width=250)
st.markdown("---")
st.markdown("""
---
## Relevance to ITC PSPD:
- **Digital Quality Control**: A color detection system like this can **validate printed matter** (cartons, labels) in real time.
- **Big Data Integration**: Results could be uploaded to a **data lake**, enabling historical trend analysis and continuous improvement.
- **Sustainability**: **Accurate color checks** reduce material wastage, aligning with ITC’s triple bottom line (economic, social, environmental) philosophy.
- **Industry 4.0**: Coupling this solution with **IoT** sensors, real-time dashboards, and advanced analytics ensures **agile and data-driven** paperboard manufacturing.
## Made By:
- Name: Kaustubh Raykar
- PRN: 21070126048
- Btech AIML 2021-25
- Contact: +91 7020524609
- Symbiosis Institute Of Technology, Pune
- [email protected]
- [email protected]
---
**Thank you for exploring this demonstration!**
""")