Spaces:
Configuration error
Configuration error
File size: 3,284 Bytes
e50e42a |
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 102 103 104 105 106 107 |
import matplotlib.pyplot as plt
import cv2
import os
from skimage.filters import gabor
from skimage import data, io,color
from skimage.color import rgb2gray
import numpy as np
import plotly.express as px
from scipy import ndimage
from sklearn.cluster import KMeans
import PIL
from PIL import Image
import streamlit as st
st.sidebar.title('Textile Detection')
st.title('Detect Defects on clothing items')
def f(a):
# Read image.
img = a.copy()
# Convert to grayscale.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Blur using 3 * 3 kernel.
gray_blurred = cv2.blur(gray, (3, 3))
# Apply Hough transform on the blurred image.
detected_circles = cv2.HoughCircles(gray_blurred, cv2.HOUGH_GRADIENT, 1, 20, param1 = 50,
param2 = 30, minRadius = 1, maxRadius = 40)
# Draw circles that are detected.
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
for pt in detected_circles[0, :]:
a, b, r = pt[0], pt[1], pt[2]
# Draw the circumference of the circle.
cv2.circle(img, (a, b), r, (0, 255, 0), 2)
# Draw a small circle (of radius 1) to show the center.
cv2.circle(img, (a, b), 1, (0, 0, 255), 3)
break
return img
image = st.sidebar.file_uploader("Upload an image", type = ['jpeg', 'jpg', 'png'])
if image is not None:
option = st.selectbox('Choose an option',\
('Original Image', 'Hough Transformation', 'Gray Scale Transformation','OpenCV'))
image = Image.open(image)
image = np.array(image.convert('RGB'))
if option == "Original Image":
st.subheader("Original Image")
st.image(image, use_column_width = True)
if option == "Hough Transformation":
st.subheader("Hough Transformation")
b = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filt_real, filt_imag = gabor(b, frequency=0.05)
gray = rgb2gray(image)
gray_r = gray.reshape(gray.shape[0]*gray.shape[1])
for i in range(gray_r.shape[0]):
if gray_r[i] > gray_r.mean():
gray_r[i] = 3
elif gray_r[i] > 0.5:
gray_r[i] = 2
elif gray_r[i] > 0.25:
gray_r[i] = 1
else:
gray_r[i] = 0
gray = gray_r.reshape(gray.shape[0],gray.shape[1])
fig = px.imshow(gray)
st.plotly_chart(fig)
if option == "Gray Scale Transformation":
b = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
st.subheader("Gray Scale Transformation")
st.image(b, use_column_width = True)
if option == "OpenCV":
st.subheader("OpenCV Algo")
img = f(image)
st.image(img, use_column_width = True)
else:
st.write("please upload an image in the formats shown above") |