oliver9523 commited on
Commit
a658163
·
1 Parent(s): 6a78849

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -2,12 +2,20 @@ import gradio as gr
2
  import cv2
3
  from geti_sdk.deployment import Deployment
4
  from geti_sdk.utils import show_image_with_annotation_scene
 
 
 
5
 
6
  # Step 1: Load the deployment
7
  deployment = Deployment.from_folder("deployment")
8
  deployment.load_inference_models(device="CPU")
9
 
10
 
 
 
 
 
 
11
  def resize_image(image, target_dimension):
12
  height, width = image.shape[:2]
13
  max_dimension = max(height, width)
@@ -18,9 +26,22 @@ def resize_image(image, target_dimension):
18
  return resized_image
19
 
20
 
21
- def infer(image=None):
 
 
 
22
  if image is None:
23
- return [None,'no image']
 
 
 
 
 
 
 
 
 
 
24
 
25
  image = resize_image(image, 1200)
26
 
@@ -32,10 +53,14 @@ def infer(image=None):
32
 
33
 
34
  interface = gr.Interface(fn=infer,
35
- inputs='image',
36
  outputs=['image','text'],
37
  allow_flagging='manual',
38
  flagging_dir='flagged',
39
- examples=["eggsample1.jpg", "eggsample2.jpg"])
 
 
 
 
40
 
41
  interface.launch()
 
2
  import cv2
3
  from geti_sdk.deployment import Deployment
4
  from geti_sdk.utils import show_image_with_annotation_scene
5
+ import numpy as np
6
+ from urllib.request import urlopen
7
+ import re
8
 
9
  # Step 1: Load the deployment
10
  deployment = Deployment.from_folder("deployment")
11
  deployment.load_inference_models(device="CPU")
12
 
13
 
14
+ def is_valid_url(url):
15
+ pattern = r'^(https?|ftp)://[^\s/$.?#].[^\s]*$'
16
+ return re.match(pattern, url) is not None
17
+
18
+
19
  def resize_image(image, target_dimension):
20
  height, width = image.shape[:2]
21
  max_dimension = max(height, width)
 
26
  return resized_image
27
 
28
 
29
+ def infer(image=None, url:str=None):
30
+ if image is None and url is None:
31
+ return [None,'Error: No image or URL provided']
32
+
33
  if image is None:
34
+ if isinstance(url, str):
35
+
36
+ if not is_valid_url(url):
37
+ return [None,'Error: URL appears to be invalid']
38
+
39
+ req = urlopen(url)
40
+ arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
41
+ image = cv2.imdecode(arr, -1)
42
+ if image is None:
43
+ return [None, f'Error: Unable to fetch image from {url}']
44
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
45
 
46
  image = resize_image(image, 1200)
47
 
 
53
 
54
 
55
  interface = gr.Interface(fn=infer,
56
+ inputs=['image', 'text'],
57
  outputs=['image','text'],
58
  allow_flagging='manual',
59
  flagging_dir='flagged',
60
+ examples=[["eggsample1.jpg", ""],
61
+ ["eggsample2.jpg",""],
62
+ [None,"https://upload.wikimedia.org/wikipedia/commons/f/f9/Eastern_Phoebe-nest-Brown-headed-Cowbird-egg.jpg"],
63
+ [None,"https://upload.wikimedia.org/wikipedia/commons/7/72/White-breasted_Woodswallow_chicks_in_nest.jpg"],
64
+ ])
65
 
66
  interface.launch()