Create refine.py
Browse files
refine.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Function to apply image enhancement filters
|
| 7 |
+
def apply_filters(image, noise, sharpen, grayscale, threshold, edges, invert, auto, blur, contrast, brightness, scale, denoise, hist_eq, gamma, clahe):
|
| 8 |
+
img = np.array(image)
|
| 9 |
+
|
| 10 |
+
# Auto Enhancement
|
| 11 |
+
if auto:
|
| 12 |
+
lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
|
| 13 |
+
l, a, b = cv2.split(lab)
|
| 14 |
+
clahe_filter = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
| 15 |
+
l = clahe_filter.apply(l)
|
| 16 |
+
img = cv2.merge((l, a, b))
|
| 17 |
+
img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
|
| 18 |
+
|
| 19 |
+
# Scaling
|
| 20 |
+
if scale != 1.0:
|
| 21 |
+
height, width = img.shape[:2]
|
| 22 |
+
img = cv2.resize(img, (int(width * scale), int(height * scale)))
|
| 23 |
+
|
| 24 |
+
# Noise Reduction (Bilateral Filtering)
|
| 25 |
+
if noise:
|
| 26 |
+
img = cv2.bilateralFilter(img, 9, 75, 75)
|
| 27 |
+
|
| 28 |
+
# Denoising (Non-Local Means)
|
| 29 |
+
if denoise:
|
| 30 |
+
img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
|
| 31 |
+
|
| 32 |
+
# Sharpening
|
| 33 |
+
if sharpen:
|
| 34 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
| 35 |
+
img = cv2.filter2D(img, -1, kernel)
|
| 36 |
+
|
| 37 |
+
# Grayscale Conversion
|
| 38 |
+
if grayscale or threshold or hist_eq or clahe:
|
| 39 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 40 |
+
|
| 41 |
+
# Histogram Equalization
|
| 42 |
+
if hist_eq:
|
| 43 |
+
img = cv2.equalizeHist(img)
|
| 44 |
+
|
| 45 |
+
# CLAHE (Contrast Limited Adaptive Histogram Equalization)
|
| 46 |
+
if clahe:
|
| 47 |
+
clahe_filter = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
| 48 |
+
img = clahe_filter.apply(img)
|
| 49 |
+
|
| 50 |
+
# Adaptive Thresholding
|
| 51 |
+
if threshold:
|
| 52 |
+
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
|
| 53 |
+
|
| 54 |
+
# Edge Detection
|
| 55 |
+
if edges:
|
| 56 |
+
img = cv2.Canny(img, 100, 200)
|
| 57 |
+
|
| 58 |
+
# Invert Colors
|
| 59 |
+
if invert:
|
| 60 |
+
img = cv2.bitwise_not(img)
|
| 61 |
+
|
| 62 |
+
# Gamma Correction
|
| 63 |
+
if gamma != 1.0:
|
| 64 |
+
inv_gamma = 1.0 / gamma
|
| 65 |
+
table = np.array([(i / 255.0) ** inv_gamma * 255 for i in np.arange(0, 256)]).astype("uint8")
|
| 66 |
+
img = cv2.LUT(img, table)
|
| 67 |
+
|
| 68 |
+
# Blur
|
| 69 |
+
if blur:
|
| 70 |
+
img = cv2.GaussianBlur(img, (2*blur + 1, 2*blur + 1), 0)
|
| 71 |
+
|
| 72 |
+
# Contrast & Brightness Adjustment
|
| 73 |
+
if contrast != 1.0 or brightness != 0:
|
| 74 |
+
img = cv2.convertScaleAbs(img, alpha=contrast, beta=brightness)
|
| 75 |
+
|
| 76 |
+
return img
|
| 77 |
+
|
| 78 |
+
# Gradio interface to upload an image and display results
|
| 79 |
+
def enhance_image(image, noise, sharpen, grayscale, threshold, edges, invert, auto, blur, contrast, brightness, scale, denoise, hist_eq, gamma, clahe):
|
| 80 |
+
refined_img = apply_filters(image, noise, sharpen, grayscale, threshold, edges, invert, auto, blur, contrast, brightness, scale, denoise, hist_eq, gamma, clahe)
|
| 81 |
+
return Image.fromarray(refined_img)
|
| 82 |
+
|
| 83 |
+
# Setup Gradio app
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# 🖼️ Refine Image for License Plate Detection")
|
| 86 |
+
gr.Markdown("Enhance the license plate image for better recognition.")
|
| 87 |
+
|
| 88 |
+
# Image upload
|
| 89 |
+
uploaded_image = gr.Image(type="pil", label="Upload Image for License Plate Detection")
|
| 90 |
+
|
| 91 |
+
# Enhancement options sidebar
|
| 92 |
+
with gr.Column():
|
| 93 |
+
blur = gr.Slider(0, 10, label="🔹 Blur", step=1)
|
| 94 |
+
contrast = gr.Slider(0.5, 2.0, label="🔹 Contrast", step=0.1)
|
| 95 |
+
brightness = gr.Slider(0.5, 2.0, label="🔹 Brightness", step=0.1)
|
| 96 |
+
gamma = gr.Slider(0.1, 3.0, label="Gamma Correction", step=0.1)
|
| 97 |
+
scale = gr.Slider(1.0, 10.0, label="🔹 Scale", step=0.1)
|
| 98 |
+
noise = gr.Checkbox(label="Noise Reduction (Bilateral)")
|
| 99 |
+
denoise = gr.Checkbox(label="Denoise (Non-Local Means)")
|
| 100 |
+
sharpen = gr.Checkbox(label="Sharpening")
|
| 101 |
+
hist_eq = gr.Checkbox(label="Histogram Equalization")
|
| 102 |
+
clahe = gr.Checkbox(label="CLAHE (Advanced Contrast)")
|
| 103 |
+
grayscale = gr.Checkbox(label="Grayscale Conversion")
|
| 104 |
+
threshold = gr.Checkbox(label="Adaptive Thresholding")
|
| 105 |
+
edges = gr.Checkbox(label="Edge Detection")
|
| 106 |
+
invert = gr.Checkbox(label="Invert Colors")
|
| 107 |
+
auto = gr.Checkbox(label="Auto Enhancement")
|
| 108 |
+
|
| 109 |
+
# Output fields
|
| 110 |
+
image_output = gr.Image(label="Refined License Plate Image")
|
| 111 |
+
|
| 112 |
+
# Button to submit the image for enhancement
|
| 113 |
+
submit_button = gr.Button("Submit Image")
|
| 114 |
+
|
| 115 |
+
# Submit button action
|
| 116 |
+
submit_button.click(enhance_image, inputs=[uploaded_image, noise, sharpen, grayscale, threshold, edges, invert, auto, blur, contrast, brightness, scale, denoise, hist_eq, gamma, clahe], outputs=image_output)
|
| 117 |
+
|
| 118 |
+
demo.launch()
|