eagle0504 commited on
Commit
10d3738
·
verified ·
1 Parent(s): 7c05bc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -2,6 +2,14 @@ import streamlit as st
2
  from PIL import Image, ImageDraw
3
  import numpy as np
4
 
 
 
 
 
 
 
 
 
5
  # Function to handle the form
6
  def show_form(area_label):
7
  with st.form(f"form_{area_label}"):
@@ -15,7 +23,7 @@ image = Image.open("test.png")
15
 
16
  # Define clickable areas (x, y, width, height)
17
  clickable_areas = [
18
- {"label": "Area 1", "coords": (100, 100, 50, 50)},
19
  {"label": "Area 2", "coords": (200, 200, 50, 50)},
20
  {"label": "Area 3", "coords": (300, 300, 50, 50)}
21
  ]
@@ -27,8 +35,17 @@ for area in clickable_areas:
27
  draw.text((x, y), "*", fill="red")
28
 
29
  # Display the image and get mouse click coordinates
30
- st.image(image, caption='Uploaded Image with Clickable Areas', use_container_width=True)
31
- click_coords = st.experimental_data_editor("Click on an area:", {"Click X": 0, "Click Y": 0}, num_cols=2)
 
 
 
 
 
 
32
 
33
- if click_coords:
34
- clicked_x, clicked_y = click_coords["Click X"], click_coords
 
 
 
 
2
  from PIL import Image, ImageDraw
3
  import numpy as np
4
 
5
+ # Function to check if a click is within a clickable area
6
+ def check_click(click_x, click_y, clickable_areas):
7
+ for area in clickable_areas:
8
+ x, y, w, h = area["coords"]
9
+ if x <= click_x <= x + w and y <= click_y <= y + h:
10
+ return area["label"]
11
+ return None
12
+
13
  # Function to handle the form
14
  def show_form(area_label):
15
  with st.form(f"form_{area_label}"):
 
23
 
24
  # Define clickable areas (x, y, width, height)
25
  clickable_areas = [
26
+ {"label": "Area 1", "coords": (100, 100, 50, 50)}, # Example coordinates
27
  {"label": "Area 2", "coords": (200, 200, 50, 50)},
28
  {"label": "Area 3", "coords": (300, 300, 50, 50)}
29
  ]
 
35
  draw.text((x, y), "*", fill="red")
36
 
37
  # Display the image and get mouse click coordinates
38
+ st.image(image, caption="Uploaded Image with Clickable Areas", use_container_width=True)
39
+
40
+ # Simulate a click using Streamlit's interactive input
41
+ click_x = st.number_input("Click X coordinate:", min_value=0, max_value=image.width, value=0)
42
+ click_y = st.number_input("Click Y coordinate:", min_value=0, max_value=image.height, value=0)
43
+
44
+ # Check if the click is inside any defined area
45
+ clicked_area = check_click(click_x, click_y, clickable_areas)
46
 
47
+ if clicked_area:
48
+ st.write(f"You clicked on: {clicked_area}")
49
+ show_form(clicked_area)
50
+ else:
51
+ st.write("Click on a valid area to trigger an action.")