|
- #coding=utf-8
- from fastapi import FastAPI
- app = FastAPI()
-
- import gradio as gr
-
- import numpy as np
- import cv2
- import common
- import os
-
- def predictor():
- args = common.get_args()
-
- # method of preprocessing input data, details in `common/preprocessing.py`
- # Generally, preprocessing contains transferring images to numpy
- # files, which may take a little long for the first time.
- pre_processor = common.PreprocessImagenet()
- # method of postprocessing output result, details in `common/postprocessing.py`
- post_processor = common.PostProcessCommon(offset=-1,label_file=args.label_file)
-
- #
- images_preprocessed = []
- image_names = args.data_path.split(',')
- for image_name in image_names:
- img_orig = cv2.imread(image_name)
- if img_orig is not None:
- img_preprocessed = pre_processor(img_orig,[224,224,3])
- images_preprocessed.append(img_preprocessed)
-
- if len(images_preprocessed) == 0:
- print("No images need to be inferenced.")
- exit(-1)
-
- images_preprocessed = np.concatenate(images_preprocessed,axis=0)#
- images_preprocessed = images_preprocessed.astype(dtype=np.float32,order='C')
-
- #
- infer = common.BaseInfer(args)
- infer.build()
- output = infer.run_inference(images_preprocessed)
-
- # result statistics according to 'labels' and 'output' of inference
- if args.label:
- gt_label = [int(label) for label in args.label.split(',')]
- else:
- gt_label = None
-
- post_processor(results=output, expected=gt_label)
- print("output is:",output)
- return output
-
-
- examples=[['./data/figures/hornbill.JPEG'],['./data/figures/nematode.JPEG']]
- io = gr.Interface(fn=predictor, inputs=gr.ImagePaint(), outputs="text",examples=examples)
- app = gr.mount_gradio_app(app, io, path=os.getenv('OPENI_GRADIO_URL'))
-
|