Manu
		
	commited on
		
		
					Commit 
							
							路
						
						2843e59
	
1
								Parent(s):
							
							743b00f
								
updated segmentation utils and app
Browse files- app.py +2 -0
 - segmentation_utils.py +7 -7
 
    	
        app.py
    CHANGED
    
    | 
         @@ -16,6 +16,7 @@ def segment_gradio_image(api_token, model, image): 
     | 
|
| 16 | 
         
             
                        'red'
         
     | 
| 17 | 
         
             
                    )
         
     | 
| 18 | 
         
             
                    segments_list = "No segments available."
         
     | 
| 
         | 
|
| 19 | 
         
             
                else:
         
     | 
| 20 | 
         
             
                    if image is None:
         
     | 
| 21 | 
         
             
                        text_image = print_text_on_image_centered(
         
     | 
| 
         @@ -24,6 +25,7 @@ def segment_gradio_image(api_token, model, image): 
     | 
|
| 24 | 
         
             
                            'orange'
         
     | 
| 25 | 
         
             
                        )
         
     | 
| 26 | 
         
             
                        segments_list = "No segments available."
         
     | 
| 
         | 
|
| 27 | 
         
             
                    else:
         
     | 
| 28 | 
         
             
                        text_image = print_text_on_image_centered(
         
     | 
| 29 | 
         
             
                            create_background_image(500, 500, "white"),
         
     | 
| 
         | 
|
| 16 | 
         
             
                        'red'
         
     | 
| 17 | 
         
             
                    )
         
     | 
| 18 | 
         
             
                    segments_list = "No segments available."
         
     | 
| 19 | 
         
            +
                    return api_token_message, text_image, segments_list
         
     | 
| 20 | 
         
             
                else:
         
     | 
| 21 | 
         
             
                    if image is None:
         
     | 
| 22 | 
         
             
                        text_image = print_text_on_image_centered(
         
     | 
| 
         | 
|
| 25 | 
         
             
                            'orange'
         
     | 
| 26 | 
         
             
                        )
         
     | 
| 27 | 
         
             
                        segments_list = "No segments available."
         
     | 
| 28 | 
         
            +
                        return api_token_message, text_image, segments_list
         
     | 
| 29 | 
         
             
                    else:
         
     | 
| 30 | 
         
             
                        text_image = print_text_on_image_centered(
         
     | 
| 31 | 
         
             
                            create_background_image(500, 500, "white"),
         
     | 
    	
        segmentation_utils.py
    CHANGED
    
    | 
         @@ -2,7 +2,6 @@ import requests 
     | 
|
| 2 | 
         
             
            from pycocotools import mask
         
     | 
| 3 | 
         
             
            import matplotlib.pyplot as plt
         
     | 
| 4 | 
         
             
            from PIL import Image, ImageDraw, ImageOps, ImageFont
         
     | 
| 5 | 
         
            -
            from dotenv import find_dotenv, load_dotenv
         
     | 
| 6 | 
         
             
            import os
         
     | 
| 7 | 
         
             
            import base64
         
     | 
| 8 | 
         
             
            import io
         
     | 
| 
         @@ -15,11 +14,7 @@ import traceback 
     | 
|
| 15 | 
         
             
            from pprint import pprint
         
     | 
| 16 | 
         | 
| 17 | 
         | 
| 18 | 
         
            -
            load_dotenv(find_dotenv())
         
     | 
| 19 | 
         
            -
            HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
         
     | 
| 20 | 
         | 
| 21 | 
         
            -
            API_URL = "https://api-inference.huggingface.co/models/facebook/mask2former-swin-tiny-coco-panoptic"
         
     | 
| 22 | 
         
            -
            headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
         
     | 
| 23 | 
         | 
| 24 | 
         
             
            # Funci贸n para transformar la entrada en un array de numpy
         
     | 
| 25 | 
         
             
            # Si la entrada es una URL, descarga la imagen y la convierte en un array de numpy
         
     | 
| 
         @@ -89,7 +84,12 @@ def transform_image_to_numpy_array2(input): 
     | 
|
| 89 | 
         
             
                else:
         
     | 
| 90 | 
         
             
                    raise ValueError("La entrada no es un array de numpy, una URL ni una ruta de archivo.")
         
     | 
| 91 | 
         | 
| 92 | 
         
            -
            def segment_image_from_numpy(image_array):
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 93 | 
         
             
                # Convert the image to bytes
         
     | 
| 94 | 
         
             
                is_success, im_buf_arr = cv2.imencode(".jpg", image_array)
         
     | 
| 95 | 
         
             
                data = im_buf_arr.tobytes()
         
     | 
| 
         @@ -340,7 +340,7 @@ def segment_and_overlay_results(image_path, api_token, model): 
     | 
|
| 340 | 
         
             
                   #     return None, []
         
     | 
| 341 | 
         | 
| 342 | 
         
             
                    ic("--- calling segment_image_from_path ---")    
         
     | 
| 343 | 
         
            -
                    segments = segment_image_from_numpy(image)
         
     | 
| 344 | 
         
             
                    #if image_type == 'FILE':
         
     | 
| 345 | 
         
             
                    #    segments = segment_image_from_path(image_path)
         
     | 
| 346 | 
         
             
                    #if image_type == 'NUMPY ARRAY':
         
     | 
| 
         | 
|
| 2 | 
         
             
            from pycocotools import mask
         
     | 
| 3 | 
         
             
            import matplotlib.pyplot as plt
         
     | 
| 4 | 
         
             
            from PIL import Image, ImageDraw, ImageOps, ImageFont
         
     | 
| 
         | 
|
| 5 | 
         
             
            import os
         
     | 
| 6 | 
         
             
            import base64
         
     | 
| 7 | 
         
             
            import io
         
     | 
| 
         | 
|
| 14 | 
         
             
            from pprint import pprint
         
     | 
| 15 | 
         | 
| 16 | 
         | 
| 
         | 
|
| 
         | 
|
| 17 | 
         | 
| 
         | 
|
| 
         | 
|
| 18 | 
         | 
| 19 | 
         
             
            # Funci贸n para transformar la entrada en un array de numpy
         
     | 
| 20 | 
         
             
            # Si la entrada es una URL, descarga la imagen y la convierte en un array de numpy
         
     | 
| 
         | 
|
| 84 | 
         
             
                else:
         
     | 
| 85 | 
         
             
                    raise ValueError("La entrada no es un array de numpy, una URL ni una ruta de archivo.")
         
     | 
| 86 | 
         | 
| 87 | 
         
            +
            def segment_image_from_numpy(image_array, api_token, model):
         
     | 
| 88 | 
         
            +
                
         
     | 
| 89 | 
         
            +
                #API_URL = "https://api-inference.huggingface.co/models/facebook/mask2former-swin-tiny-coco-panoptic"
         
     | 
| 90 | 
         
            +
                API_URL = f"https://api-inference.huggingface.co/models/facebook/{model}"
         
     | 
| 91 | 
         
            +
                headers = {"Authorization": f"Bearer {api_token}"}
         
     | 
| 92 | 
         
            +
             
     | 
| 93 | 
         
             
                # Convert the image to bytes
         
     | 
| 94 | 
         
             
                is_success, im_buf_arr = cv2.imencode(".jpg", image_array)
         
     | 
| 95 | 
         
             
                data = im_buf_arr.tobytes()
         
     | 
| 
         | 
|
| 340 | 
         
             
                   #     return None, []
         
     | 
| 341 | 
         | 
| 342 | 
         
             
                    ic("--- calling segment_image_from_path ---")    
         
     | 
| 343 | 
         
            +
                    segments = segment_image_from_numpy(image, api_token, model)
         
     | 
| 344 | 
         
             
                    #if image_type == 'FILE':
         
     | 
| 345 | 
         
             
                    #    segments = segment_image_from_path(image_path)
         
     | 
| 346 | 
         
             
                    #if image_type == 'NUMPY ARRAY':
         
     |