Phaneendrabayi commited on
Commit
e9ecd65
·
verified ·
1 Parent(s): 0c537e6

Update pages/Life_cycle_of_ML.py

Browse files
Files changed (1) hide show
  1. pages/Life_cycle_of_ML.py +86 -105
pages/Life_cycle_of_ML.py CHANGED
@@ -807,113 +807,94 @@ def affine_transformation_demo():
807
 
808
 
809
 
810
- # Initialize global variables
811
- drawing = False # True if mouse is pressed
812
- start_point = (-1, -1) # Starting point of shape
813
- end_point = (-1, -1) # Ending point of shape
814
- canvas = None # Canvas to draw on
815
- current_shape = None # Shape selected
816
- polygon_points = [] # List to store points for polygon drawing
817
- shapes_list = [] # List to store all drawn shapes
818
-
819
- # Shape classes to represent different shapes
820
- class Shape:
821
- def __init__(self, shape_type, points, color, thickness):
822
- self.shape_type = shape_type
823
- self.points = points
824
- self.color = color
825
- self.thickness = thickness
826
-
827
- # Main Streamlit function to handle canvas and shapes
828
- def interactive_drawing_canvas():
829
- global canvas, current_shape, height, width, polygon_points, shapes_list
830
-
831
- st.title('Interactive Drawing Canvas')
832
-
833
- # Slider for canvas size
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
834
  width = st.slider('Canvas Width', min_value=100, max_value=1000, value=500)
835
  height = st.slider('Canvas Height', min_value=100, max_value=1000, value=500)
836
 
837
- # Shape selection dropdown
838
- current_shape = st.selectbox("Select Shape to Draw", ["None", "Line", "Rectangle", "Circle", "Polygon"])
839
-
840
- # Initialize canvas
841
- canvas = np.full((height, width, 3), 255, dtype=np.uint8) # Blank white canvas
842
-
843
- # Placeholder to show canvas
844
- st.image(canvas, caption='Canvas', channels="BGR", use_column_width=True)
845
-
846
- # Add interactivity for mouse events in Streamlit using buttons
847
- if current_shape != "None":
848
- if st.button('Clear Canvas'):
849
- canvas.fill(255) # Reset the canvas
850
- shapes_list.clear() # Clear the list of shapes
851
-
852
- # Draw the selected shape
853
- if current_shape == "Line":
854
- st.write("Drawing Line: Left-click to start, Right-click to finish")
855
-
856
- elif current_shape == "Rectangle":
857
- st.write("Drawing Rectangle: Left-click to start, Right-click to finish")
858
-
859
- elif current_shape == "Circle":
860
- st.write("Drawing Circle: Left-click to start, Right-click to finish")
861
-
862
- elif current_shape == "Polygon":
863
- st.write("Drawing Polygon: Left-click to add points, Right-click to close")
864
-
865
- # Process the drawing of shapes
866
- if st.button('Draw'):
867
- if current_shape == "Line":
868
- # Example: Drawing a line by selecting start and end points
869
- start_point = (50, 50)
870
- end_point = (200, 200)
871
- line_color = (255, 0, 0)
872
- thickness = 2
873
- shape = Shape("Line", [start_point, end_point], line_color, thickness)
874
- shapes_list.append(shape)
875
-
876
- elif current_shape == "Rectangle":
877
- # Example: Drawing a rectangle with start and end points
878
- start_point = (100, 100)
879
- end_point = (300, 200)
880
- rectangle_color = (0, 255, 0)
881
- thickness = 3
882
- shape = Shape("Rectangle", [start_point, end_point], rectangle_color, thickness)
883
- shapes_list.append(shape)
884
-
885
- elif current_shape == "Circle":
886
- # Example: Drawing a circle with center and radius
887
- center = (250, 250)
888
- radius = 100
889
- circle_color = (0, 0, 255)
890
- thickness = 3
891
- shape = Shape("Circle", [center, radius], circle_color, thickness)
892
- shapes_list.append(shape)
893
-
894
- elif current_shape == "Polygon":
895
- # Example: Drawing a polygon with multiple points
896
- polygon_points = [(100, 100), (200, 100), (250, 200), (150, 250)]
897
- polygon_color = (255, 165, 0)
898
- thickness = 2
899
- shape = Shape("Polygon", polygon_points, polygon_color, thickness)
900
- shapes_list.append(shape)
901
-
902
- # Redraw all shapes
903
- for shape in shapes_list:
904
- if shape.shape_type == "Line":
905
- cv2.line(canvas, tuple(shape.points[0]), tuple(shape.points[1]), shape.color, shape.thickness)
906
- elif shape.shape_type == "Rectangle":
907
- cv2.rectangle(canvas, tuple(shape.points[0]), tuple(shape.points[1]), shape.color, shape.thickness)
908
- elif shape.shape_type == "Circle":
909
- cv2.circle(canvas, tuple(shape.points[0]), shape.points[1], shape.color, shape.thickness)
910
- elif shape.shape_type == "Polygon":
911
- pts = np.array(shape.points, np.int32)
912
- pts = pts.reshape((-1, 1, 2))
913
- cv2.polylines(canvas, [pts], True, shape.color, shape.thickness)
914
-
915
- # Display the updated canvas
916
- st.image(canvas, caption=f'Drawn {current_shape}', channels="BGR", use_column_width=True)
917
 
918
 
919
 
@@ -932,7 +913,7 @@ elif st.session_state.page == 'Table':
932
  elif st.session_state.page == 'Affine':
933
  affine_transformation_demo()
934
  elif st.session_state.page == 'Animation':
935
- interactive_drawing_canvas()
936
 
937
  st.markdown("---")
938
 
 
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
870
+ current_shape = st.selectbox("Select Shape", ["Line", "Rectangle", "Circle", "Polygon"])
871
+
872
+ # Display canvas size options
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
+
888
+ # Keep drawing until the user stops
889
+ while True:
890
+ cv2.imshow(window_name, canvas)
891
+
892
+ # Break the loop if the user presses 'q' or closes the window
893
+ if cv2.waitKey(1) & 0xFF == ord('q'):
894
+ break
895
+
896
+ # Cleanup and destroy OpenCV window
897
+ cv2.destroyAllWindows()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
898
 
899
 
900
 
 
913
  elif st.session_state.page == 'Affine':
914
  affine_transformation_demo()
915
  elif st.session_state.page == 'Animation':
916
+ interactive_canvas()
917
 
918
  st.markdown("---")
919