Oguz Vuruskaner commited on
Commit
2daee64
·
1 Parent(s): d3e5083

Add application file

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+
5
+ def calculate_sharpness(image):
6
+ """
7
+ Calculate the sharpness of an image using the Laplacian variance method.
8
+ Higher variance = sharper edges = clearer image.
9
+ """
10
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11
+ # Calculate the variance of the Laplacian (standard measure for blurriness)
12
+ score = cv2.Laplacian(gray, cv2.CV_64F).var()
13
+ return score
14
+
15
+ def process_images(files):
16
+ """
17
+ Takes a list of file paths, finds the sharpest image, and returns it along with metrics.
18
+ """
19
+ if not files:
20
+ raise gr.Error("Please upload at least one image.")
21
+
22
+ best_image = None
23
+ best_score = -1
24
+ best_filename = ""
25
+
26
+ results = []
27
+
28
+ for file_path in files:
29
+ # Read image using OpenCV
30
+ # file_path is a temporary path provided by Gradio
31
+ img = cv2.imread(file_path)
32
+
33
+ if img is None:
34
+ continue
35
+
36
+ score = calculate_sharpness(img)
37
+ filename = file_path.split('/')[-1] # Simple extraction of temp filename
38
+
39
+ # Convert BGR (OpenCV standard) to RGB (Gradio standard) for display
40
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
41
+
42
+ results.append({"filename": filename, "sharpness_score": round(score, 2)})
43
+
44
+ if score > best_score:
45
+ best_score = score
46
+ best_image = img_rgb
47
+ best_filename = filename
48
+
49
+ # Sort results for the data table
50
+ results.sort(key=lambda x: x["sharpness_score"], reverse=True)
51
+
52
+ info_text = f"🏆 Best Image: **{best_filename}**\n\n**Score:** {best_score:.2f} (Laplacian Variance)"
53
+
54
+ return best_image, info_text, results
55
+
56
+ # --- Gradio UI Implementation ---
57
+ with gr.Blocks(title="Best Image Selector") as demo:
58
+ gr.Markdown("# 📸 Smart Image Selector")
59
+ gr.Markdown("Upload multiple photos. The app will analyze them for **clarity (sharpness)** and pick the best one.")
60
+
61
+ with gr.Row():
62
+ with gr.Column(scale=1):
63
+ # Input: File uploader accepting multiple files
64
+ file_input = gr.File(
65
+ label="Upload Images",
66
+ file_count="multiple",
67
+ file_types=["image"]
68
+ )
69
+ process_btn = gr.Button("Find Best Image", variant="primary")
70
+
71
+ with gr.Column(scale=1):
72
+ # Output: Best Image display
73
+ output_image = gr.Image(label="The Sharpest Image", type="numpy")
74
+ output_info = gr.Markdown()
75
+
76
+ # Output: Data table showing scores for all images
77
+ output_table = gr.JSON(label="Analysis Details")
78
+
79
+ # Wire the function
80
+ process_btn.click(
81
+ fn=process_images,
82
+ inputs=file_input,
83
+ outputs=[output_image, output_info, output_table]
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()