Spaces:
Running
Running
pangyuteng commited on
Commit ·
b746796
1
Parent(s): cae4464
updated logging
Browse files
app.py
CHANGED
|
@@ -1,5 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import uuid
|
| 5 |
import traceback
|
|
@@ -33,7 +41,9 @@ example_nifti_path = "img.nii.gz"
|
|
| 33 |
|
| 34 |
def main_func(input_file_list):
|
| 35 |
with tempfile.TemporaryDirectory() as tempdir:
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
image_file = os.path.join(tempdir,f"image.nii.gz")
|
| 38 |
first_file = input_file_list[0].name
|
| 39 |
if first_file.endswith(".dcm"):
|
|
@@ -43,15 +53,20 @@ def main_func(input_file_list):
|
|
| 43 |
img_obj = sitk.ReadImage(first_file)
|
| 44 |
else:
|
| 45 |
raise ValueError("only accept .nii.gz file or .dcm files!")
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
sitk.WriteImage(img_obj,image_file)
|
| 49 |
|
|
|
|
| 50 |
inference(image_file,tempdir,weight_file)
|
| 51 |
|
| 52 |
mask_file = os.path.join(tempdir,"lungqia-0.nii.gz")
|
| 53 |
csv_file = os.path.join(tempdir,"export-0.csv")
|
| 54 |
orientation_file = os.path.join(tempdir,"orientation.json")
|
|
|
|
| 55 |
my_uid = uuid.uuid4().hex
|
| 56 |
output_pdf_file = os.path.join('/tmp',f'{my_uid}.pdf')
|
| 57 |
output_csv_file = os.path.join('/tmp',f'{my_uid}.csv')
|
|
@@ -59,11 +74,12 @@ def main_func(input_file_list):
|
|
| 59 |
try:
|
| 60 |
assert(os.path.exists(mask_file))
|
| 61 |
assert(os.path.exists(csv_file))
|
| 62 |
-
assert(os.path.exists(orientation_file))
|
| 63 |
except:
|
| 64 |
traceback.print_exc()
|
| 65 |
raise ValueError("inference failed to generate output files")
|
| 66 |
|
|
|
|
| 67 |
inst = NiftiVisualizer("NA",image_file,mask_file,csv_file,tempdir,orientation_file=orientation_file)
|
| 68 |
tmp_pdf_file = inst.gen_pdf()
|
| 69 |
assert(os.path.exists(tmp_pdf_file))
|
|
@@ -73,8 +89,9 @@ def main_func(input_file_list):
|
|
| 73 |
shutil.copy(tmp_pdf_file,output_pdf_file)
|
| 74 |
shutil.copy(csv_file,output_csv_file)
|
| 75 |
|
| 76 |
-
print('uid:'
|
| 77 |
-
|
|
|
|
| 78 |
return output_pdf_file, output_df, output_pdf_file, output_csv_file
|
| 79 |
|
| 80 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import logging
|
| 4 |
+
logger = logging.getLogger(__file__)
|
| 5 |
+
handler = logging.StreamHandler(sys.stdout)
|
| 6 |
+
handler.setLevel(logging.INFO)
|
| 7 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 8 |
+
handler.setFormatter(formatter)
|
| 9 |
+
logger.addHandler(handler)
|
| 10 |
+
|
| 11 |
import json
|
| 12 |
import uuid
|
| 13 |
import traceback
|
|
|
|
| 41 |
|
| 42 |
def main_func(input_file_list):
|
| 43 |
with tempfile.TemporaryDirectory() as tempdir:
|
| 44 |
+
tstamp = datetime.datetime.now(timezone.utc).strftime('%Y-%m-%d-%H-%M-%S-%Z')
|
| 45 |
+
print(f'start time: {tstamp}')
|
| 46 |
+
print('image reading...')
|
| 47 |
image_file = os.path.join(tempdir,f"image.nii.gz")
|
| 48 |
first_file = input_file_list[0].name
|
| 49 |
if first_file.endswith(".dcm"):
|
|
|
|
| 53 |
img_obj = sitk.ReadImage(first_file)
|
| 54 |
else:
|
| 55 |
raise ValueError("only accept .nii.gz file or .dcm files!")
|
| 56 |
+
|
| 57 |
+
print(f'size {img_obj.GetSize()}')
|
| 58 |
+
print(f'spacing {img_obj.GetSpacing()}')
|
| 59 |
+
print(f'direction {img_obj.GetDirection()}',)
|
| 60 |
|
| 61 |
sitk.WriteImage(img_obj,image_file)
|
| 62 |
|
| 63 |
+
print('inference starting...')
|
| 64 |
inference(image_file,tempdir,weight_file)
|
| 65 |
|
| 66 |
mask_file = os.path.join(tempdir,"lungqia-0.nii.gz")
|
| 67 |
csv_file = os.path.join(tempdir,"export-0.csv")
|
| 68 |
orientation_file = os.path.join(tempdir,"orientation.json")
|
| 69 |
+
orientation_file = None
|
| 70 |
my_uid = uuid.uuid4().hex
|
| 71 |
output_pdf_file = os.path.join('/tmp',f'{my_uid}.pdf')
|
| 72 |
output_csv_file = os.path.join('/tmp',f'{my_uid}.csv')
|
|
|
|
| 74 |
try:
|
| 75 |
assert(os.path.exists(mask_file))
|
| 76 |
assert(os.path.exists(csv_file))
|
| 77 |
+
#assert(os.path.exists(orientation_file))
|
| 78 |
except:
|
| 79 |
traceback.print_exc()
|
| 80 |
raise ValueError("inference failed to generate output files")
|
| 81 |
|
| 82 |
+
print('report generating...')
|
| 83 |
inst = NiftiVisualizer("NA",image_file,mask_file,csv_file,tempdir,orientation_file=orientation_file)
|
| 84 |
tmp_pdf_file = inst.gen_pdf()
|
| 85 |
assert(os.path.exists(tmp_pdf_file))
|
|
|
|
| 89 |
shutil.copy(tmp_pdf_file,output_pdf_file)
|
| 90 |
shutil.copy(csv_file,output_csv_file)
|
| 91 |
|
| 92 |
+
print(f'uid: {my_uid}')
|
| 93 |
+
tstamp = datetime.datetime.now(timezone.utc).strftime('%Y-%m-%d-%H-%M-%S-%Z')
|
| 94 |
+
print(f'end time: {tstamp}')
|
| 95 |
return output_pdf_file, output_df, output_pdf_file, output_csv_file
|
| 96 |
|
| 97 |
with gr.Blocks() as demo:
|