Spaces:
Runtime error
Runtime error
File size: 1,647 Bytes
a258e87 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import os
import json
import numpy as np
import gradio as gr
import cv2
from drexel_metadata.gen_metadata import gen_metadata
from PIL import Image
def create_temp_file_path(prefix, suffix):
with tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix, delete=False) as tmpfile:
return tmpfile.name
def run_inference(input_img):
# input_mg: NumPy array with the shape (width, height, 3)
# Save input_mg as a temporary file
tmpfile = create_temp_file_path(prefix="input_", suffix, ".png")
im = Image.fromarray(input_img)
im.save(tmpfile)
# Create temp filenames for output images
visfname = create_temp_file_path(prefix="vis_", suffix=".png")
maskfname = create_temp_file_path(prefix="mask_", suffix=".png")
# Run inference
result = gen_metadata(tmpfile, device='cpu', maskfname=maskfname, visfname=visfname)
json_metadata = json.dumps(result)
# Cleanup
os.remove(tempfile)
return visfname, maskfname, json_metadata
def read_app_header_markdown():
with open('app_header.md') as infile:
return infile.read()
dm_app = gr.Interface(
fn=run_inference,
# Input shows markdown explaining and app and a single image upload panel
inputs=[
gr.Markdown(read_app_header_markdown()),
gr.Image()
],
# Output consists of a visualization image, a masked image, and JSON metadata
outputs=[
gr.Image(label='visualization'),
gr.Image(label='mask'),
gr.JSON(label="JSON metadata")
],
allow_flagging="never" # Do not save user's results or prompt for users to save the results
)
dm_app.launch()
|