fireedman commited on
Commit
88e9c63
1 Parent(s): 28924f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -27,22 +27,31 @@ sam_pipeline = pipeline(
27
  model="facebook/sam-vit-large",
28
  device=DEVICE
29
  )
 
 
 
 
 
30
 
31
  mask_generator = SamAutomaticMaskGenerator(sam_pipeline)
32
 
33
  # Función para procesar y anotar la imagen
34
  def process_image(image_pil):
 
35
  image_rgb = np.array(image_pil)
36
  image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
37
 
 
38
  sam_result = mask_generator.generate(image_rgb)
39
  mask_annotator = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
40
  detections = sv.Detections.from_sam(sam_result=sam_result)
41
-
42
  annotated_image = mask_annotator.annotate(scene=image_bgr.copy(), detections=detections)
 
 
43
  annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
 
44
 
45
- return Image.fromarray(image_rgb), Image.fromarray(annotated_image_rgb)
46
 
47
  # Construcción de la interfaz Gradio
48
  with gr.Blocks() as demo:
@@ -60,5 +69,14 @@ with gr.Blocks() as demo:
60
  inputs=input_image,
61
  outputs=[original_image, segmented_image]
62
  )
 
 
 
 
 
 
 
 
 
63
 
64
  demo.launch(debug=True)
 
27
  model="facebook/sam-vit-large",
28
  device=DEVICE
29
  )
30
+ EXAMPLES = [
31
+ ["https://media.roboflow.com/notebooks/examples/dog.jpeg"],
32
+ ["https://media.roboflow.com/notebooks/examples/dog-3.jpeg"]
33
+ ]
34
+
35
 
36
  mask_generator = SamAutomaticMaskGenerator(sam_pipeline)
37
 
38
  # Función para procesar y anotar la imagen
39
  def process_image(image_pil):
40
+ # Convertir PIL Image a numpy array para procesamiento
41
  image_rgb = np.array(image_pil)
42
  image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
43
 
44
+ # Generar la máscara y anotar la imagen
45
  sam_result = mask_generator.generate(image_rgb)
46
  mask_annotator = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
47
  detections = sv.Detections.from_sam(sam_result=sam_result)
 
48
  annotated_image = mask_annotator.annotate(scene=image_bgr.copy(), detections=detections)
49
+
50
+ # Convertir de nuevo a formato RGB y luego a PIL Image para Gradio
51
  annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
52
+ annotated_image_pil = Image.fromarray(annotated_image_rgb)
53
 
54
+ return image_pil, annotated_image_pil
55
 
56
  # Construcción de la interfaz Gradio
57
  with gr.Blocks() as demo:
 
69
  inputs=input_image,
70
  outputs=[original_image, segmented_image]
71
  )
72
+ with gr.Row():
73
+ gr.Examples(
74
+ examples=EXAMPLES,
75
+ fn=inference,
76
+ inputs=[input_image],
77
+ outputs=[gallery],
78
+ cache_examples=False,
79
+ run_on_click=True
80
+ )
81
 
82
  demo.launch(debug=True)