Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import pytesseract
|
| 4 |
+
import numpy as np
|
| 5 |
+
from skimage.restoration import estimate_sigma
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
def rate_image_clarity(image):
|
| 9 |
+
# Convert PIL image to OpenCV
|
| 10 |
+
image = np.array(image)
|
| 11 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 12 |
+
|
| 13 |
+
# OCR Config
|
| 14 |
+
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
| 15 |
+
details = pytesseract.image_to_data(gray, config=custom_config, output_type=pytesseract.Output.DICT)
|
| 16 |
+
|
| 17 |
+
# OCR Confidence
|
| 18 |
+
confidences = [int(conf) for conf in details['conf'] if conf.isdigit()]
|
| 19 |
+
avg_conf = sum(confidences) / len(confidences) if confidences else 0
|
| 20 |
+
|
| 21 |
+
# Noise Estimation
|
| 22 |
+
gray_float = gray.astype(np.float32) / 255.0
|
| 23 |
+
sigma_est = estimate_sigma(gray_float, multichannel=False, average_sigmas=True)
|
| 24 |
+
|
| 25 |
+
# Clarity Score
|
| 26 |
+
clarity_score = max(0, min(100, (avg_conf * (1 - sigma_est))))
|
| 27 |
+
|
| 28 |
+
# Rating
|
| 29 |
+
if clarity_score >= 80:
|
| 30 |
+
rating = "🌟 Excellent"
|
| 31 |
+
elif clarity_score >= 60:
|
| 32 |
+
rating = "✅ Good"
|
| 33 |
+
elif clarity_score >= 40:
|
| 34 |
+
rating = "⚠️ Moderate"
|
| 35 |
+
else:
|
| 36 |
+
rating = "❌ Poor"
|
| 37 |
+
|
| 38 |
+
return {
|
| 39 |
+
"Clarity Score (0-100)": round(clarity_score, 2),
|
| 40 |
+
"OCR Confidence": round(avg_conf, 2),
|
| 41 |
+
"Noise Estimate": round(sigma_est, 4),
|
| 42 |
+
"Rating": rating
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Gradio UI
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=rate_image_clarity,
|
| 48 |
+
inputs=gr.Image(type="pil"),
|
| 49 |
+
outputs=[
|
| 50 |
+
gr.Number(label="Clarity Score (0-100)"),
|
| 51 |
+
gr.Number(label="OCR Confidence"),
|
| 52 |
+
gr.Number(label="Noise Estimate"),
|
| 53 |
+
gr.Textbox(label="Clarity Rating")
|
| 54 |
+
],
|
| 55 |
+
title="🧠 OCR-Based Image Clarity Rater",
|
| 56 |
+
description="Upload an image to rate its clarity using OCR confidence and noise estimation."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
iface.launch()
|