Update pages/Life_cycle_of_ML.py

#1
Files changed (1) hide show
  1. pages/Life_cycle_of_ML.py +56 -60
pages/Life_cycle_of_ML.py CHANGED
@@ -807,63 +807,15 @@ def affine_transformation_demo():
807
 
808
 
809
 
810
- # Global variables for drawing state
811
- drawing = False
812
- start_point = (-1, -1)
813
- end_point = (-1, -1)
814
- canvas = np.ones((500, 500, 3), dtype=np.uint8) * 255 # White canvas
815
- current_shape = 'Line'
816
- polygon_points = []
817
-
818
- # Shape drawing callback function
819
- def draw_shapes(event, x, y, flags, param):
820
- global drawing, start_point, end_point, current_shape, polygon_points, canvas
821
-
822
- if event == cv2.EVENT_LBUTTONDOWN: # Left mouse button press
823
- if current_shape == 'Polygon':
824
- polygon_points.append((x, y)) # Add points for the polygon
825
- else:
826
- start_point = (x, y)
827
- drawing = True # Start drawing
828
-
829
- elif event == cv2.EVENT_MOUSEMOVE: # Mouse move while drawing
830
- if drawing and current_shape != 'Polygon':
831
- canvas = np.ones((500, 500, 3), dtype=np.uint8) * 255 # Clear canvas
832
- if current_shape == 'Line':
833
- cv2.line(canvas, start_point, (x, y), (0, 0, 255), 2) # Draw line
834
- elif current_shape == 'Rectangle':
835
- cv2.rectangle(canvas, start_point, (x, y), (0, 255, 0), 2) # Draw rectangle
836
- elif current_shape == 'Circle':
837
- radius = int(np.linalg.norm(np.array(start_point) - np.array((x, y)))) # Circle radius
838
- cv2.circle(canvas, start_point, radius, (255, 0, 0), 2) # Draw circle
839
- elif current_shape == 'Polygon':
840
- if len(polygon_points) > 1:
841
- pts = np.array(polygon_points, np.int32)
842
- pts = pts.reshape((-1, 1, 2))
843
- cv2.polylines(canvas, [pts], False, (0, 255, 0), 2) # Draw polygon
844
-
845
- elif event == cv2.EVENT_LBUTTONUP: # Left mouse button release
846
- if current_shape != 'Polygon': # Complete shape drawing
847
- end_point = (x, y)
848
- drawing = False # Stop drawing
849
- if current_shape == 'Line':
850
- cv2.line(canvas, start_point, end_point, (0, 0, 255), 2) # Final line
851
- elif current_shape == 'Rectangle':
852
- cv2.rectangle(canvas, start_point, end_point, (0, 255, 0), 2) # Final rectangle
853
- elif current_shape == 'Circle':
854
- radius = int(np.linalg.norm(np.array(start_point) - np.array(end_point))) # Calculate radius
855
- cv2.circle(canvas, start_point, radius, (255, 0, 0), 2) # Final circle
856
- else:
857
- if len(polygon_points) > 2:
858
- pts = np.array(polygon_points, np.int32)
859
- pts = pts.reshape((-1, 1, 2))
860
- cv2.polylines(canvas, [pts], True, (0, 255, 0), 2) # Complete polygon
861
- polygon_points = [] # Reset polygon points after closing
862
-
863
- # Streamlit app to control shape selection and display canvas
864
- def interactive_canvas():
865
- global current_shape, canvas
866
-
867
  st.title('Interactive Shape Drawing with Mouse')
868
 
869
  # Shape selection dropdown
@@ -873,15 +825,60 @@ def interactive_canvas():
873
  width = st.slider('Canvas Width', min_value=100, max_value=1000, value=500)
874
  height = st.slider('Canvas Height', min_value=100, max_value=1000, value=500)
875
 
876
- # Resize canvas based on user selection
877
  canvas = np.ones((height, width, 3), dtype=np.uint8) * 255 # White canvas
878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
879
  # Create a window to display the canvas
880
  window_name = "Drawing Canvas"
881
  cv2.namedWindow(window_name)
882
 
883
  # Set mouse callback function to capture mouse events
884
- cv2.setMouseCallback(window_name, draw_shapes)
885
 
886
  st.write("Click on the canvas to draw shapes. Left-click for the start, Right-click for the end.")
887
 
@@ -895,7 +892,6 @@ def interactive_canvas():
895
 
896
  # Cleanup and destroy OpenCV window
897
  cv2.destroyAllWindows()
898
-
899
 
900
 
901
  if 'page' not in st.session_state:
 
807
 
808
 
809
 
810
+ # Function to handle mouse events and shape drawing
811
+ def draw_shapes_on_canvas():
812
+ # Global variables for drawing state
813
+ drawing = False
814
+ start_point = (-1, -1)
815
+ end_point = (-1, -1)
816
+ polygon_points = []
817
+
818
+ # Streamlit UI for shape selection and canvas size
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
819
  st.title('Interactive Shape Drawing with Mouse')
820
 
821
  # Shape selection dropdown
 
825
  width = st.slider('Canvas Width', min_value=100, max_value=1000, value=500)
826
  height = st.slider('Canvas Height', min_value=100, max_value=1000, value=500)
827
 
828
+ # Create canvas
829
  canvas = np.ones((height, width, 3), dtype=np.uint8) * 255 # White canvas
830
 
831
+ # Mouse callback function
832
+ def mouse_callback(event, x, y, flags, param):
833
+ nonlocal drawing, start_point, end_point, current_shape, polygon_points, canvas
834
+
835
+ if event == cv2.EVENT_LBUTTONDOWN: # Left mouse button press
836
+ if current_shape == 'Polygon':
837
+ polygon_points.append((x, y)) # Add points for the polygon
838
+ else:
839
+ start_point = (x, y)
840
+ drawing = True # Start drawing
841
+
842
+ elif event == cv2.EVENT_MOUSEMOVE: # Mouse move while drawing
843
+ if drawing and current_shape != 'Polygon':
844
+ canvas = np.ones((height, width, 3), dtype=np.uint8) * 255 # Clear canvas
845
+ if current_shape == 'Line':
846
+ cv2.line(canvas, start_point, (x, y), (0, 0, 255), 2) # Draw line
847
+ elif current_shape == 'Rectangle':
848
+ cv2.rectangle(canvas, start_point, (x, y), (0, 255, 0), 2) # Draw rectangle
849
+ elif current_shape == 'Circle':
850
+ radius = int(np.linalg.norm(np.array(start_point) - np.array((x, y)))) # Circle radius
851
+ cv2.circle(canvas, start_point, radius, (255, 0, 0), 2) # Draw circle
852
+ elif current_shape == 'Polygon':
853
+ if len(polygon_points) > 1:
854
+ pts = np.array(polygon_points, np.int32)
855
+ pts = pts.reshape((-1, 1, 2))
856
+ cv2.polylines(canvas, [pts], False, (0, 255, 0), 2) # Draw polygon
857
+
858
+ elif event == cv2.EVENT_LBUTTONUP: # Left mouse button release
859
+ if current_shape != 'Polygon': # Complete shape drawing
860
+ end_point = (x, y)
861
+ drawing = False # Stop drawing
862
+ if current_shape == 'Line':
863
+ cv2.line(canvas, start_point, end_point, (0, 0, 255), 2) # Final line
864
+ elif current_shape == 'Rectangle':
865
+ cv2.rectangle(canvas, start_point, end_point, (0, 255, 0), 2) # Final rectangle
866
+ elif current_shape == 'Circle':
867
+ radius = int(np.linalg.norm(np.array(start_point) - np.array(end_point))) # Calculate radius
868
+ cv2.circle(canvas, start_point, radius, (255, 0, 0), 2) # Final circle
869
+ else:
870
+ if len(polygon_points) > 2:
871
+ pts = np.array(polygon_points, np.int32)
872
+ pts = pts.reshape((-1, 1, 2))
873
+ cv2.polylines(canvas, [pts], True, (0, 255, 0), 2) # Complete polygon
874
+ polygon_points = [] # Reset polygon points after closing
875
+
876
  # Create a window to display the canvas
877
  window_name = "Drawing Canvas"
878
  cv2.namedWindow(window_name)
879
 
880
  # Set mouse callback function to capture mouse events
881
+ cv2.setMouseCallback(window_name, mouse_callback)
882
 
883
  st.write("Click on the canvas to draw shapes. Left-click for the start, Right-click for the end.")
884
 
 
892
 
893
  # Cleanup and destroy OpenCV window
894
  cv2.destroyAllWindows()
 
895
 
896
 
897
  if 'page' not in st.session_state: