File size: 1,605 Bytes
6e54fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3954480
6e54fe1
 
 
3954480
6e54fe1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from face_deid_ct import drown_volume

import gradio as gr
import gradio as gr
import os
import zipfile
import shutil


def process_file(input_file):
    cache_dir = "cache"
    cache_out_dir = "cache_out"
    output_zip_file = "output.zip"

    # Check if input file is a zip file
    if zipfile.is_zipfile(input_file.name):
        with zipfile.ZipFile(input_file.name, 'r') as zip_ref:
            # Unzip the file in 'cache' directory
            zip_ref.extractall(cache_dir)
        
        # Run deid function
        drown_volume(cache_dir, cache_out_dir, replacer='face')
        
        # Create a Zip file for 'cache_out' directory
        with zipfile.ZipFile(output_zip_file, 'w') as zipf:
            for root, dirs, files in os.walk(cache_out_dir):
                for file in files:
                    zipf.write(os.path.join(root, file), 
                               os.path.relpath(os.path.join(root, file), 
                                               os.path.join(cache_out_dir, '..')))
        
        # Cleanup cache directories
        shutil.rmtree(cache_dir)
        shutil.rmtree(cache_out_dir)

        return output_zip_file

    else:
        raise ValueError("The provided file is not a zip file.")

description = "Upload a ZIP file containing a folder with a head CT's DICOM files. The ZIP file might also contain subfolders, each one containing a head CT."

inputs = gr.components.File(label="Input File")
outputs = gr.components.File(label="Output File")
demo = gr.Interface(fn=process_file, description=description, inputs=inputs, outputs=outputs)
demo.launch()