Spaces:
Sleeping
Sleeping
File size: 2,335 Bytes
3114cc4 0d798b8 a658163 3114cc4 0d798b8 3114cc4 e65263a a658163 e65263a 0d798b8 e65263a 0d798b8 e65263a a658163 0d798b8 a658163 e65263a 0d798b8 e65263a 0d798b8 e65263a a658163 e65263a a658163 e65263a |
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 60 61 62 63 64 65 66 67 |
import gradio as gr
import cv2
from geti_sdk.deployment import Deployment
from geti_sdk.utils import show_image_with_annotation_scene
import numpy as np
from urllib.request import urlopen
import re
# Step 1: Load the deployment
deployment = Deployment.from_folder("deployment")
deployment.load_inference_models(device="CPU")
def is_valid_url(url):
pattern = r'^(https?|ftp)://[^\s/$.?#].[^\s]*$'
return re.match(pattern, url) is not None
def resize_image(image, target_dimension):
height, width = image.shape[:2]
max_dimension = max(height, width)
scale_factor = target_dimension / max_dimension
new_width = int(width * scale_factor)
new_height = int(height * scale_factor)
resized_image = cv2.resize(image, (new_width, new_height))
return resized_image
def infer(image=None, url:str=None):
if image is None and url is None:
return [None,'Error: No image or URL provided']
if image is None:
if isinstance(url, str):
if not is_valid_url(url):
return [None,'Error: URL appears to be invalid']
req = urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
image = cv2.imdecode(arr, -1)
if image is None:
return [None, f'Error: Unable to fetch image from {url}']
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = resize_image(image, 1200)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
prediction = deployment.infer(image)
output = show_image_with_annotation_scene(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), prediction, show_results=False)
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
return [output, prediction.overview]
interface = gr.Interface(fn=infer,
inputs=['image', 'text'],
outputs=['image','text'],
allow_flagging='manual',
flagging_dir='flagged',
examples=[["eggsample1.jpg", ""],
["eggsample2.jpg",""],
[None,"https://upload.wikimedia.org/wikipedia/commons/f/f9/Eastern_Phoebe-nest-Brown-headed-Cowbird-egg.jpg"],
[None,"https://upload.wikimedia.org/wikipedia/commons/7/72/White-breasted_Woodswallow_chicks_in_nest.jpg"],
])
interface.launch()
|