Schrodingers commited on
Commit
856189d
1 Parent(s): 7b8cabf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -11
app.py CHANGED
@@ -1,15 +1,55 @@
 
 
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
 
 
 
 
 
5
 
6
- def predict(image):
7
- predictions = pipeline(image)
8
- return {p["label"]: p["score"] for p in predictions}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- gr.Interface(
11
- predict,
12
- inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
13
- outputs=gr.outputs.Label(num_top_classes=2),
14
- title="Hot Dog? Or Not?",
15
- ).launch()
 
1
+ import cv2
2
+ import numpy as np
3
  import gradio as gr
 
4
 
5
+ def watershed_segmentation(input_image):
6
+ # Convert the image to grayscale
7
+ gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
8
+
9
+ # Apply adaptive thresholding
10
+ thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
11
 
12
+ # Morphological operations to remove small noise - use morphologyEx
13
+ kernel = np.ones((3, 3), np.uint8)
14
+ opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
15
+
16
+ # Identify sure background area
17
+ sure_bg = cv2.dilate(opening, kernel, iterations=3)
18
+
19
+ # Find sure foreground area using distance transform
20
+ dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
21
+ ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0)
22
+
23
+ # Find unknown region
24
+ sure_fg = np.uint8(sure_fg)
25
+ unknown = cv2.subtract(sure_bg, sure_fg)
26
+
27
+ # Marker labeling
28
+ ret, markers = cv2.connectedComponents(sure_fg)
29
+
30
+ # Add one to all labels so that sure background is not 0, but 1
31
+ markers = markers + 1
32
+
33
+ # Mark the unknown region with zero
34
+ markers[unknown == 255] = 0
35
+
36
+ # Apply watershed
37
+ cv2.watershed(input_image, markers)
38
+ input_image[markers == -1] = [0, 0, 255] # boundary region
39
+
40
+ return input_image
41
+
42
+
43
+
44
+ def ui_interface(input_image):
45
+ segmented = watershed_segmentation(input_image)
46
+ return segmented
47
+
48
+ iface = gr.Interface(
49
+ fn=ui_interface,
50
+ inputs=gr.inputs.Image(),
51
+ outputs=gr.outputs.Image()
52
+ )
53
+
54
+ iface.launch()
55