Phaneendrabayi commited on
Commit
6deba7c
·
verified ·
1 Parent(s): 5835e49

Update pages/Life_cycle_of_ML.py

Browse files
Files changed (1) hide show
  1. pages/Life_cycle_of_ML.py +74 -79
pages/Life_cycle_of_ML.py CHANGED
@@ -813,19 +813,12 @@ def affine_transformation_demo():
813
 
814
 
815
 
816
- # Function to handle the drawing
817
  def draw_shapes_on_canvas():
818
- # Global variables for drawing state
819
- drawing = False
820
- start_point = (-1, -1)
821
- end_point = (-1, -1)
822
- polygon_points = []
823
-
824
  # Streamlit UI for shape selection and canvas size
825
- st.title('Interactive Shape Drawing with Mouse')
826
 
827
  # Shape selection dropdown
828
- current_shape = st.selectbox("Select Shape", ["Line", "Rectangle", "Circle", "Polygon"])
829
 
830
  # Display canvas size options
831
  width = st.slider('Canvas Width', min_value=100, max_value=1000, value=500)
@@ -834,76 +827,78 @@ def draw_shapes_on_canvas():
834
  # Create canvas
835
  canvas = np.ones((height, width, 3), dtype=np.uint8) * 255 # White canvas
836
 
837
- # Store the current image as a session state so it can be updated
838
- if 'canvas_image' not in st.session_state:
839
- st.session_state.canvas_image = canvas
840
-
841
- # Mouse callback function
842
- def mouse_callback(event, x, y, flags, param):
843
- nonlocal drawing, start_point, end_point, current_shape, polygon_points, canvas
844
-
845
- if event == cv2.EVENT_LBUTTONDOWN: # Left mouse button press
846
- if current_shape == 'Polygon':
847
- polygon_points.append((x, y)) # Add points for the polygon
848
- else:
849
- start_point = (x, y)
850
- drawing = True # Start drawing
851
-
852
- elif event == cv2.EVENT_MOUSEMOVE: # Mouse move while drawing
853
- if drawing and current_shape != 'Polygon':
854
- canvas = np.ones((height, width, 3), dtype=np.uint8) * 255 # Clear canvas
855
- if current_shape == 'Line':
856
- cv2.line(canvas, start_point, (x, y), (0, 0, 255), 2) # Draw line
857
- elif current_shape == 'Rectangle':
858
- cv2.rectangle(canvas, start_point, (x, y), (0, 255, 0), 2) # Draw rectangle
859
- elif current_shape == 'Circle':
860
- radius = int(np.linalg.norm(np.array(start_point) - np.array((x, y)))) # Circle radius
861
- cv2.circle(canvas, start_point, radius, (255, 0, 0), 2) # Draw circle
862
- elif current_shape == 'Polygon':
863
- if len(polygon_points) > 1:
864
- pts = np.array(polygon_points, np.int32)
865
- pts = pts.reshape((-1, 1, 2))
866
- cv2.polylines(canvas, [pts], False, (0, 255, 0), 2) # Draw polygon
867
-
868
- elif event == cv2.EVENT_LBUTTONUP: # Left mouse button release
869
- if current_shape != 'Polygon': # Complete shape drawing
870
- end_point = (x, y)
871
- drawing = False # Stop drawing
872
- if current_shape == 'Line':
873
- cv2.line(canvas, start_point, end_point, (0, 0, 255), 2) # Final line
874
- elif current_shape == 'Rectangle':
875
- cv2.rectangle(canvas, start_point, end_point, (0, 255, 0), 2) # Final rectangle
876
- elif current_shape == 'Circle':
877
- radius = int(np.linalg.norm(np.array(start_point) - np.array(end_point))) # Calculate radius
878
- cv2.circle(canvas, start_point, radius, (255, 0, 0), 2) # Final circle
879
- else:
880
- if len(polygon_points) > 2:
881
- pts = np.array(polygon_points, np.int32)
882
- pts = pts.reshape((-1, 1, 2))
883
- cv2.polylines(canvas, [pts], True, (0, 255, 0), 2) # Complete polygon
884
- polygon_points = [] # Reset polygon points after closing
885
-
886
- # Create a window to display the canvas
887
- window_name = "Drawing Canvas"
888
- cv2.namedWindow(window_name)
889
-
890
- # Set mouse callback function to capture mouse events
891
- cv2.setMouseCallback(window_name, mouse_callback)
892
-
893
- st.write("Click on the canvas to draw shapes. Left-click for the start, Right-click for the end.")
894
-
895
- # Display the current canvas in Streamlit
896
- while True:
897
- # Show the current canvas
898
- st.image(canvas, channels="BGR", use_column_width=True)
899
-
900
- # Break the loop if the user presses 'q' or closes the window
901
- if cv2.waitKey(1) & 0xFF == ord('q'):
902
- break
903
-
904
- # Cleanup and destroy OpenCV window
905
- cv2.destroyAllWindows()
906
-
 
 
907
 
908
 
909
  if 'page' not in st.session_state:
 
813
 
814
 
815
 
 
816
  def draw_shapes_on_canvas():
 
 
 
 
 
 
817
  # Streamlit UI for shape selection and canvas size
818
+ st.title('Interactive Shape Drawing & Animation')
819
 
820
  # Shape selection dropdown
821
+ current_shape = st.selectbox("Select Shape", ["Line", "Rectangle", "Circle", "Polygon", "Text"])
822
 
823
  # Display canvas size options
824
  width = st.slider('Canvas Width', min_value=100, max_value=1000, value=500)
 
827
  # Create canvas
828
  canvas = np.ones((height, width, 3), dtype=np.uint8) * 255 # White canvas
829
 
830
+ # Parameters for drawing shapes
831
+ if current_shape == "Line":
832
+ start_point = (st.slider("Line Start X", 0, width, 0), st.slider("Line Start Y", 0, height, 0))
833
+ end_point = (st.slider("Line End X", 0, width, 200), st.slider("Line End Y", 0, height, 0))
834
+ line_color = st.color_picker("Line Color", "#b4b4b4")
835
+ thickness = st.slider("Line Thickness", 1, 10, 2)
836
+
837
+ # Convert color from Hex to BGR
838
+ line_color_bgr = tuple(int(line_color[i:i+2], 16) for i in (5, 3, 1))
839
+
840
+ # Draw Line
841
+ cv2.line(canvas, start_point, end_point, line_color_bgr, thickness)
842
+
843
+ elif current_shape == "Rectangle":
844
+ top_left = (st.slider("Top-left X", 0, width, 200), st.slider("Top-left Y", 0, height, 200))
845
+ bottom_right = (st.slider("Bottom-right X", 0, width, 500), st.slider("Bottom-right Y", 0, height, 400))
846
+ rect_color = st.color_picker("Rectangle Color", "#ff0000")
847
+ rect_thickness = st.slider("Rectangle Thickness", 1, 10, 2)
848
+
849
+ # Convert color from Hex to BGR
850
+ rect_color_bgr = tuple(int(rect_color[i:i+2], 16) for i in (5, 3, 1))
851
+
852
+ # Draw Rectangle
853
+ cv2.rectangle(canvas, top_left, bottom_right, rect_color_bgr, rect_thickness)
854
+
855
+ elif current_shape == "Circle":
856
+ center = (st.slider("Center X", 0, width, 250), st.slider("Center Y", 0, height, 250))
857
+ radius = st.slider("Circle Radius", 10, min(width, height)//2, 150)
858
+ circle_color = st.color_picker("Circle Color", "#00ff00")
859
+ circle_thickness = st.slider("Circle Thickness", 1, 10, 2)
860
+
861
+ # Convert color from Hex to BGR
862
+ circle_color_bgr = tuple(int(circle_color[i:i+2], 16) for i in (5, 3, 1))
863
+
864
+ # Draw Circle
865
+ cv2.circle(canvas, center, radius, circle_color_bgr, circle_thickness)
866
+
867
+ elif current_shape == "Polygon":
868
+ # Define points for the polygon
869
+ polygon_points = []
870
+ for i in range(5):
871
+ x = st.slider(f"Polygon Point {i+1} X", 0, width, 100 + i*50)
872
+ y = st.slider(f"Polygon Point {i+1} Y", 0, height, 100 + i*50)
873
+ polygon_points.append((x, y))
874
+
875
+ polygon_color = st.color_picker("Polygon Color", "#00ff00")
876
+ polygon_thickness = st.slider("Polygon Thickness", 1, 10, 2)
877
+
878
+ # Convert color from Hex to BGR
879
+ polygon_color_bgr = tuple(int(polygon_color[i:i+2], 16) for i in (5, 3, 1))
880
+
881
+ # Draw Polygon
882
+ pts = np.array(polygon_points, np.int32)
883
+ pts = pts.reshape((-1, 1, 2))
884
+ cv2.polylines(canvas, [pts], True, polygon_color_bgr, polygon_thickness)
885
+
886
+ elif current_shape == "Text":
887
+ text = st.text_input("Text to display", "SaniyaMastan")
888
+ font = cv2.FONT_ITALIC
889
+ font_size = st.slider("Font Size", 1, 5, 2)
890
+ text_color = st.color_picker("Text Color", "#ff0000")
891
+ text_thickness = st.slider("Text Thickness", 1, 10, 2)
892
+ text_position = (st.slider("Text Position X", 0, width, 100), st.slider("Text Position Y", 0, height, 100))
893
+
894
+ # Convert color from Hex to BGR
895
+ text_color_bgr = tuple(int(text_color[i:i+2], 16) for i in (5, 3, 1))
896
+
897
+ # Add Text to Canvas
898
+ cv2.putText(canvas, text, text_position, font, font_size, text_color_bgr, text_thickness, lineType=cv2.LINE_AA)
899
+
900
+ # Display the updated canvas
901
+ st.image(canvas, channels="BGR", use_column_width=True)
902
 
903
 
904
  if 'page' not in st.session_state: