TheoBH commited on
Commit
436a31d
·
verified ·
1 Parent(s): 15dffd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -28
app.py CHANGED
@@ -29,6 +29,7 @@ from PIL import Image
29
  import torch
30
  import requests
31
  import tensorflow as tf
 
32
 
33
 
34
  def _normalized_to_pixel_coordinates(
@@ -50,7 +51,6 @@ def _normalized_to_pixel_coordinates(
50
  return x_px, y_px
51
 
52
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
53
-
54
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
55
  "stabilityai/stable-diffusion-2-inpainting",
56
  torch_dtype=torch.float16,
@@ -68,34 +68,8 @@ base_options = python.BaseOptions(model_asset_path='model.tflite')
68
  options = vision.ImageSegmenterOptions(base_options=base_options,
69
  output_category_mask=True)
70
 
71
- def get_bounding_box(mask):
72
- """Generate bounding box coordinates from a binary mask."""
73
- contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
74
- if contours:
75
- x, y, w, h = cv2.boundingRect(contours[0])
76
- return x, y, x + w, y + h
77
- return 0, 0, mask.shape[1], mask.shape[0]
78
-
79
-
80
- def example_segmentation_function(image_file_path, x, y):
81
- OVERLAY_COLOR = (255, 105, 180) # Rose
82
- base_options = python.BaseOptions(model_asset_path='model.tflite')
83
- options = vision.ImageSegmenterOptions(base_options=base_options, output_category_mask=True)
84
-
85
- with python.vision.InteractiveSegmenter.create_from_options(options) as segmenter:
86
- image = mp.Image.create_from_file(image_file_path)
87
- roi = vision.InteractiveSegmenterRegionOfInterest(
88
- format=vision.InteractiveSegmenterRegionOfInterest.Format.KEYPOINT,
89
- keypoint=containers.keypoint.NormalizedKeypoint(x, y)
90
- )
91
- segmentation_result = segmenter.segment(image, roi)
92
- category_mask = segmentation_result.category_mask
93
- segmentation_mask = category_mask.numpy_view().astype(np.uint8)
94
-
95
- return segmentation_mask, image
96
-
97
-
98
 
 
99
  def segment(image_file_name, x, y, prompt):
100
  OVERLAY_COLOR = (255, 105, 180) # Rose
101
 
@@ -175,6 +149,7 @@ def segment(image_file_name, x, y, prompt):
175
  return output_image, bbox_image
176
 
177
 
 
178
  def generate(image_file_path, x, y, prompt):
179
  output_image, bbox_image = segment(image_file_path, x, y, prompt)
180
 
 
29
  import torch
30
  import requests
31
  import tensorflow as tf
32
+ import spaces
33
 
34
 
35
  def _normalized_to_pixel_coordinates(
 
51
  return x_px, y_px
52
 
53
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
54
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
55
  "stabilityai/stable-diffusion-2-inpainting",
56
  torch_dtype=torch.float16,
 
68
  options = vision.ImageSegmenterOptions(base_options=base_options,
69
  output_category_mask=True)
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ @spaces.GPU
73
  def segment(image_file_name, x, y, prompt):
74
  OVERLAY_COLOR = (255, 105, 180) # Rose
75
 
 
149
  return output_image, bbox_image
150
 
151
 
152
+ @spaces.GPU
153
  def generate(image_file_path, x, y, prompt):
154
  output_image, bbox_image = segment(image_file_path, x, y, prompt)
155