Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Upload app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -4,14 +4,36 @@ from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input 
     | 
|
| 4 | 
         
             
            from tensorflow.keras.preprocessing import image
         
     | 
| 5 | 
         
             
            import numpy as np
         
     | 
| 6 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 7 | 
         
             
            model = tf.keras.models.load_model("inceptionv3_model_with_custom_layer.h5")
         
     | 
| 8 | 
         | 
| 9 | 
         
            -
            #  
     | 
| 10 | 
         
             
            def predict(img):
         
     | 
| 11 | 
         
            -
                img = img.resize((224, 224))  #  
     | 
| 12 | 
         
            -
                img_array = image.img_to_array(img)  #  
     | 
| 13 | 
         
            -
                img_array = np.expand_dims(img_array, axis=0)  #  
     | 
| 14 | 
         
            -
                img_array = preprocess_input(img_array)  #  
     | 
| 15 | 
         | 
| 16 | 
         
             
                predictions = model.predict(img_array)
         
     | 
| 17 | 
         
             
                class_idx = np.argmax(predictions, axis=1)[0]
         
     | 
| 
         @@ -20,7 +42,7 @@ def predict(img): 
     | 
|
| 20 | 
         | 
| 21 | 
         
             
                return {class_label: confidence}
         
     | 
| 22 | 
         | 
| 23 | 
         
            -
            #  
     | 
| 24 | 
         
             
            interface = gr.Interface(
         
     | 
| 25 | 
         
             
                fn=predict,
         
     | 
| 26 | 
         
             
                inputs=gr.Image(type="pil", label="Upload an Image"),
         
     | 
| 
         @@ -29,5 +51,5 @@ interface = gr.Interface( 
     | 
|
| 29 | 
         
             
                description="Upload an image to classify it into one of the classes."
         
     | 
| 30 | 
         
             
            )
         
     | 
| 31 | 
         | 
| 32 | 
         
            -
            #  
     | 
| 33 | 
         
             
            interface.launch()
         
     | 
| 
         | 
|
| 4 | 
         
             
            from tensorflow.keras.preprocessing import image
         
     | 
| 5 | 
         
             
            import numpy as np
         
     | 
| 6 | 
         | 
| 7 | 
         
            +
            # กำหนดเลเยอร์แบบกำหนดเอง
         
     | 
| 8 | 
         
            +
            class CustomScaleLayer(tf.keras.layers.Layer):
         
     | 
| 9 | 
         
            +
                def __init__(self, scale=1.0, **kwargs):
         
     | 
| 10 | 
         
            +
                    super(CustomScaleLayer, self).__init__(**kwargs)
         
     | 
| 11 | 
         
            +
                    self.scale = tf.Variable(initial_value=scale, trainable=True)
         
     | 
| 12 | 
         
            +
             
     | 
| 13 | 
         
            +
                def call(self, inputs):
         
     | 
| 14 | 
         
            +
                    return inputs * self.scale
         
     | 
| 15 | 
         
            +
                
         
     | 
| 16 | 
         
            +
                def get_config(self):
         
     | 
| 17 | 
         
            +
                    config = super(CustomScaleLayer, self).get_config()
         
     | 
| 18 | 
         
            +
                    config.update({'scale': self.scale.numpy()})
         
     | 
| 19 | 
         
            +
                    return config
         
     | 
| 20 | 
         
            +
             
     | 
| 21 | 
         
            +
                @classmethod
         
     | 
| 22 | 
         
            +
                def from_config(cls, config):
         
     | 
| 23 | 
         
            +
                    return cls(**config)
         
     | 
| 24 | 
         
            +
             
     | 
| 25 | 
         
            +
            # ลงทะเบียนเลเยอร์แบบกำหนดเอง
         
     | 
| 26 | 
         
            +
            tf.keras.utils.get_custom_objects().update({'CustomScaleLayer': CustomScaleLayer})
         
     | 
| 27 | 
         
            +
             
     | 
| 28 | 
         
            +
            # โหลดโมเดล
         
     | 
| 29 | 
         
             
            model = tf.keras.models.load_model("inceptionv3_model_with_custom_layer.h5")
         
     | 
| 30 | 
         | 
| 31 | 
         
            +
            # ฟังก์ชันสำหรับการพยากรณ์
         
     | 
| 32 | 
         
             
            def predict(img):
         
     | 
| 33 | 
         
            +
                img = img.resize((224, 224))  # ปรับขนาดรูปภาพ
         
     | 
| 34 | 
         
            +
                img_array = image.img_to_array(img)  # แปลงรูปภาพเป็นอาร์เรย์
         
     | 
| 35 | 
         
            +
                img_array = np.expand_dims(img_array, axis=0)  # เพิ่มมิติแบทช์
         
     | 
| 36 | 
         
            +
                img_array = preprocess_input(img_array)  # เตรียมรูปภาพให้สอดคล้องกับความต้องการของโมเดล
         
     | 
| 37 | 
         | 
| 38 | 
         
             
                predictions = model.predict(img_array)
         
     | 
| 39 | 
         
             
                class_idx = np.argmax(predictions, axis=1)[0]
         
     | 
| 
         | 
|
| 42 | 
         | 
| 43 | 
         
             
                return {class_label: confidence}
         
     | 
| 44 | 
         | 
| 45 | 
         
            +
            # สร้าง Gradio Interface
         
     | 
| 46 | 
         
             
            interface = gr.Interface(
         
     | 
| 47 | 
         
             
                fn=predict,
         
     | 
| 48 | 
         
             
                inputs=gr.Image(type="pil", label="Upload an Image"),
         
     | 
| 
         | 
|
| 51 | 
         
             
                description="Upload an image to classify it into one of the classes."
         
     | 
| 52 | 
         
             
            )
         
     | 
| 53 | 
         | 
| 54 | 
         
            +
            # เปิดใช้งานอินเตอร์เฟส
         
     | 
| 55 | 
         
             
            interface.launch()
         
     |