ktllc commited on
Commit
c0b02e4
·
1 Parent(s): 28d01e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
- import cv2
3
  import numpy as np
4
  from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
5
  import base64
6
- from huggingface_hub import InferenceClient
7
 
8
  # Load the segmentation model
9
  sam_checkpoint = "sam_vit_h_4b8939.pth"
@@ -12,8 +11,13 @@ sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
12
 
13
  # Define a function for image segmentation
14
  def segment_image(input_image):
15
- image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
 
 
 
16
  mask_generator = SamAutomaticMaskGenerator(sam)
 
 
17
  masks = mask_generator.generate(image)
18
 
19
  highest_cosine_value = -1
@@ -21,7 +25,7 @@ def segment_image(input_image):
21
 
22
  for i, mask_dict in enumerate(masks):
23
  mask_data = (mask_dict['segmentation'] * 255).astype(np.uint8)
24
- segmented_region = cv2.bitwise_and(input_image, input_image, mask=mask_data)
25
 
26
  x, y, w, h = map(int, mask_dict['bbox'])
27
  cropped_region = segmented_region[y:y+h, x:x+w]
@@ -31,8 +35,12 @@ def segment_image(input_image):
31
  segmented_image_base64 = base64.b64encode(buffer).decode()
32
 
33
  # Call the API to get the cosine similarity
34
- client = InferenceClient()
35
- result = client.post(json={"inputs": segmented_image_base64}, model="https://ktllc-clip-model-inputbase64.hf.space/--replicas/mmz7z/")
 
 
 
 
36
 
37
  cosine_similarity = result[0].get("score", 0.0)
38
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
  from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
4
  import base64
5
+ from gradio_client import Client
6
 
7
  # Load the segmentation model
8
  sam_checkpoint = "sam_vit_h_4b8939.pth"
 
11
 
12
  # Define a function for image segmentation
13
  def segment_image(input_image):
14
+ # Convert Gradio input image to a NumPy array
15
+ image = input_image.astype(np.uint8)
16
+
17
+ # Initialize the mask generator
18
  mask_generator = SamAutomaticMaskGenerator(sam)
19
+
20
+ # Generate masks
21
  masks = mask_generator.generate(image)
22
 
23
  highest_cosine_value = -1
 
25
 
26
  for i, mask_dict in enumerate(masks):
27
  mask_data = (mask_dict['segmentation'] * 255).astype(np.uint8)
28
+ segmented_region = cv2.bitwise_and(image, image, mask=mask_data)
29
 
30
  x, y, w, h = map(int, mask_dict['bbox'])
31
  cropped_region = segmented_region[y:y+h, x:x+w]
 
35
  segmented_image_base64 = base64.b64encode(buffer).decode()
36
 
37
  # Call the API to get the cosine similarity
38
+ client = Client("https://ktllc-clip-model-inputbase64.hf.space/--replicas/mmz7z/")
39
+ result = client.predict(
40
+ segmented_image_base64, # Base64 Image
41
+ "Text input", # Text input
42
+ api_name="/predict"
43
+ )
44
 
45
  cosine_similarity = result[0].get("score", 0.0)
46