Spaces:
Sleeping
Sleeping
add application file
Browse files- app.py +44 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
|
| 6 |
+
# Function to read and display RGB at a predefined point and plot the point
|
| 7 |
+
def get_rgb_and_plot_point(img, x, y, point_size):
|
| 8 |
+
img = np.array(img) # Convert the image to a numpy array
|
| 9 |
+
if x < 0 or y < 0 or x >= img.shape[1] or y >= img.shape[0]:
|
| 10 |
+
return "Point is outside the image bounds", img
|
| 11 |
+
|
| 12 |
+
rgb_value = img[y, x] # Get the RGB value
|
| 13 |
+
|
| 14 |
+
# Create a PIL Image to draw the point
|
| 15 |
+
pil_img = Image.fromarray(img)
|
| 16 |
+
draw = ImageDraw.Draw(pil_img)
|
| 17 |
+
# Draw a circle at the specified point
|
| 18 |
+
draw.ellipse((x - point_size // 2, y - point_size // 2, x + point_size // 2, y + point_size // 2), outline="red", width=2)
|
| 19 |
+
|
| 20 |
+
return f'RGB value at ({x}, {y}): {rgb_value}', pil_img
|
| 21 |
+
|
| 22 |
+
# Gradio interface function
|
| 23 |
+
def image_rgb_extractor(img, x, y, point_size):
|
| 24 |
+
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB) # Convert to RGB format
|
| 25 |
+
return get_rgb_and_plot_point(img, int(x), int(y), int(point_size))
|
| 26 |
+
|
| 27 |
+
# Set up the Gradio interface
|
| 28 |
+
input_image = gr.Image(type="pil", label="Upload an Image")
|
| 29 |
+
input_x = gr.Number(label="X-coordinate")
|
| 30 |
+
input_y = gr.Number(label="Y-coordinate")
|
| 31 |
+
input_point_size = gr.Number(label="Point Size", value=10) # Removed default argument
|
| 32 |
+
output_rgb = gr.Textbox(label="RGB Value at Point")
|
| 33 |
+
output_image = gr.Image(label="Image with Point")
|
| 34 |
+
|
| 35 |
+
# Launch the Gradio app
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
+
fn=image_rgb_extractor,
|
| 38 |
+
inputs=[input_image, input_x, input_y, input_point_size],
|
| 39 |
+
outputs=[output_rgb, output_image],
|
| 40 |
+
title="Predefined Point RGB Inspector",
|
| 41 |
+
description="Upload an image and input coordinates (x, y) and point size to get the RGB value at that point and see it plotted on the image."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
pillow
|