datafreak commited on
Commit
5d8c095
·
verified ·
1 Parent(s): ccf9092

main files

Browse files
Files changed (2) hide show
  1. app.py +53 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+
5
+ # Function to process the image and extract contours
6
+ def extract_contours(image, min_contour_area=100):
7
+ # Convert the uploaded image from RGB to BGR format for OpenCV processing
8
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
9
+
10
+ # Step 1: Convert to grayscale
11
+ gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
12
+
13
+ # Step 2: Apply Gaussian blur to reduce noise
14
+ blurred = cv2.GaussianBlur(gray, (5, 5), 0)
15
+
16
+ # Step 3: Apply Canny edge detection with low thresholds for finer edges
17
+ edges = cv2.Canny(blurred, 30, 100) # Adjust thresholds as needed
18
+
19
+ # Step 4: Apply morphological operations to refine edges
20
+ kernel = np.ones((3, 3), np.uint8)
21
+ edges_dilated = cv2.dilate(edges, kernel, iterations=1) # Dilation to emphasize edges
22
+ edges_eroded = cv2.erode(edges_dilated, kernel, iterations=1) # Erosion to refine
23
+
24
+ # Step 5: Find contours
25
+ contours, _ = cv2.findContours(edges_eroded, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
26
+
27
+ # Step 6: Create a blank white background
28
+ white_background = np.ones_like(image_bgr) * 255 # White background
29
+
30
+ # Step 7: Draw contours on the white background, excluding small contours
31
+ for contour in contours:
32
+ if cv2.contourArea(contour) > min_contour_area:
33
+ cv2.drawContours(white_background, [contour], -1, (0, 0, 0), thickness=1) # Thinner lines
34
+
35
+ # Convert the result back to RGB for displaying
36
+ result_rgb = cv2.cvtColor(white_background, cv2.COLOR_BGR2RGB)
37
+
38
+ return result_rgb
39
+
40
+ # Gradio interface
41
+ interface = gr.Interface(
42
+ fn=extract_contours,
43
+ inputs=[
44
+ gr.Image(type="numpy", label="Upload Image"),
45
+ gr.Slider(50, 500, step=10, value=100, label="Minimum Contour Area") # Use 'value' instead of 'default'
46
+ ],
47
+ outputs=gr.Image(type="numpy", label="Processed Image"),
48
+ title="Edge Detection and Contour Extraction",
49
+ description="Upload an image to extract contours, excluding small areas like text labels. Adjust the minimum contour area using the slider."
50
+ )
51
+
52
+ # Launch the Gradio app
53
+ interface.launch()
requirements.txt ADDED
Binary file (1.97 kB). View file