CoolFace
Apppublic

mayukhdeb/devolearn-instance-segmentation-demo

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py243 linesDownload Raw Back to root
1import gradio as gr2from demo import automask_image_app, automask_video_app, sahi_autoseg_app3 4 5def image_app():6    with gr.Blocks():7        with gr.Row():8            with gr.Column():9                seg_automask_image_file = gr.Image(type="filepath").style(height=260)10                with gr.Row():11                    with gr.Column():12                        seg_automask_image_model_type = gr.Dropdown(13                            choices=[14                                "vit_h",15                                "vit_l",16                                "vit_b",17                            ],18                            value="vit_l",19                            label="Model Type",20                        )21 22                        seg_automask_image_min_area = gr.Number(23                            value=0,24                            label="Min Area",25                        )26                    with gr.Row():27                        with gr.Column():28                            seg_automask_image_points_per_side = gr.Slider(29                                minimum=0,30                                maximum=32,31                                step=2,32                                value=16,33                                label="Points per Side",34                            )35 36                            seg_automask_image_points_per_batch = gr.Slider(37                                minimum=0,38                                maximum=64,39                                step=2,40                                value=64,41                                label="Points per Batch",42                            )43 44                seg_automask_image_predict = gr.Button(value="Generator")45 46            with gr.Column():47                output_image = gr.Image()48 49        seg_automask_image_predict.click(50            fn=automask_image_app,51            inputs=[52                seg_automask_image_file,53                seg_automask_image_model_type,54                seg_automask_image_points_per_side,55                seg_automask_image_points_per_batch,56                seg_automask_image_min_area,57            ],58            outputs=[output_image],59        )60 61 62def video_app():63    with gr.Blocks():64        with gr.Row():65            with gr.Column():66                seg_automask_video_file = gr.Video().style(height=260)67                with gr.Row():68                    with gr.Column():69                        seg_automask_video_model_type = gr.Dropdown(70                            choices=[71                                "vit_h",72                                "vit_l",73                                "vit_b",74                            ],75                            value="vit_l",76                            label="Model Type",77                        )78                        seg_automask_video_min_area = gr.Number(79                            value=1000,80                            label="Min Area",81                        )82 83                    with gr.Row():84                        with gr.Column():85                            seg_automask_video_points_per_side = gr.Slider(86                                minimum=0,87                                maximum=32,88                                step=2,89                                value=16,90                                label="Points per Side",91                            )92                            93                            seg_automask_video_points_per_batch = gr.Slider(94                                minimum=0,95                                maximum=64,96                                step=2,97                                value=64,98                                label="Points per Batch",99                            )100 101                seg_automask_video_predict = gr.Button(value="Generator")102            with gr.Column():103                output_video = gr.Video()104 105        seg_automask_video_predict.click(106            fn=automask_video_app,107            inputs=[108                seg_automask_video_file,109                seg_automask_video_model_type,110                seg_automask_video_points_per_side,111                seg_automask_video_points_per_batch,112                seg_automask_video_min_area,113            ],114            outputs=[output_video],115        )116 117 118def sahi_app():119    with gr.Blocks():120        with gr.Row():121            with gr.Column():122                sahi_image_file = gr.Image(type="filepath").style(height=260)123                sahi_autoseg_model_type = gr.Dropdown(124                        choices=[125                            "vit_h",126                            "vit_l",127                            "vit_b",128                        ],129                        value="vit_l",130                        label="Sam Model Type",131                    )132 133                with gr.Row():134                    with gr.Column():135                        sahi_model_type = gr.Dropdown(136                                choices=[137                                    "yolov5",138                                    "yolov8",139                                ],140                                value="yolov5",141                                label="Detector Model Type",142                            )143                        sahi_image_size = gr.Slider(144                            minimum=0,145                            maximum=1600,146                            step=32,147                            value=640,148                            label="Image Size",149                        ) 150                            151                        sahi_overlap_width = gr.Slider(152                            minimum=0,153                            maximum=1,154                            step=0.1,155                            value=0.2,156                            label="Overlap Width",157                        )158                        159                        sahi_slice_width = gr.Slider(160                            minimum=0,161                            maximum=640,162                            step=32,163                            value=256,164                            label="Slice Width",165                        )166 167                    with gr.Row():168                        with gr.Column():                          169                            sahi_model_path = gr.Dropdown(170                                choices=[171                                    "yolov5l.pt",172                                    "yolov5l6.pt",173                                    "yolov8l.pt",174                                    "yolov8x.pt"175                                ],176                                value="yolov5l6.pt",177                                label="Detector Model Path",178                            )179 180                            sahi_conf_th = gr.Slider(181                                minimum=0,182                                maximum=1,183                                step=0.1,184                                value=0.2,185                                label="Confidence Threshold",186                            )   187                            sahi_overlap_height = gr.Slider(188                                minimum=0,189                                maximum=1,190                                step=0.1,191                                value=0.2,192                                label="Overlap Height",193                            )194                            sahi_slice_height = gr.Slider(195                                minimum=0,196                                maximum=640,197                                step=32,198                                value=256,199                                label="Slice Height",200                            )201                sahi_image_predict = gr.Button(value="Generator")202 203            with gr.Column():204                output_image = gr.Image()205 206        sahi_image_predict.click(207            fn=sahi_autoseg_app,208            inputs=[209                sahi_image_file,210                sahi_autoseg_model_type,211                sahi_model_type,212                sahi_model_path,213                sahi_conf_th,214                sahi_image_size,215                sahi_slice_height,216                sahi_slice_width,217                sahi_overlap_height,218                sahi_overlap_width,219 220            ],221            outputs=[output_image],222        )223 224def metaseg_app():225    app = gr.Blocks()226    with app:227        with gr.Row():228            with gr.Column():229                with gr.Tab("Image"):230                    image_app()231                with gr.Tab("Video"):232                    video_app()233                with gr.Tab("SAHI"):234                    sahi_app()235                236 237    app.queue(concurrency_count=1)238    app.launch(debug=True, enable_queue=True, share=True)239 240 241if __name__ == "__main__":242    metaseg_app()243