oliver9523 commited on
Commit
d7953e2
·
1 Parent(s): bfca396

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -4,12 +4,19 @@ 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
 
8
  # Step 1: Load the deployment
9
  deployment = Deployment.from_folder("deployment")
10
  deployment.load_inference_models(device="CPU")
11
 
12
 
 
 
 
 
 
13
  def resize_image(image, target_dimension):
14
  height, width = image.shape[:2]
15
  max_dimension = max(height, width)
@@ -22,17 +29,22 @@ def resize_image(image, target_dimension):
22
 
23
  def infer(image=None, url:str=None):
24
  if image is None and url is None:
25
- return [None,'no image or URL provided']
26
 
27
  if image is None:
28
  if isinstance(url, str):
 
 
 
 
29
  req = urlopen(url)
30
  arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
31
  image = cv2.imdecode(arr, -1)
32
  if image is None:
33
- return [None, f'Unable to fetch image from {url}']
34
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
35
 
 
36
  image = resize_image(image, 1200)
37
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
38
  prediction = deployment.infer(image)
 
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
 
10
  # Step 1: Load the deployment
11
  deployment = Deployment.from_folder("deployment")
12
  deployment.load_inference_models(device="CPU")
13
 
14
 
15
+ def is_valid_url(url):
16
+ pattern = r'^(https?|ftp)://[^\s/$.?#].[^\s]*$'
17
+ return re.match(pattern, url) is not None
18
+
19
+
20
  def resize_image(image, target_dimension):
21
  height, width = image.shape[:2]
22
  max_dimension = max(height, width)
 
29
 
30
  def infer(image=None, url:str=None):
31
  if image is None and url is None:
32
+ return [None,'Error: No image or URL provided']
33
 
34
  if image is None:
35
  if isinstance(url, str):
36
+
37
+ if not is_valid_url(url):
38
+ return [None,'Error: URL appears to be invalid']
39
+
40
  req = urlopen(url)
41
  arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
42
  image = cv2.imdecode(arr, -1)
43
  if image is None:
44
+ return [None, f'Error: Unable to fetch image from {url}']
45
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
46
 
47
+
48
  image = resize_image(image, 1200)
49
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
50
  prediction = deployment.infer(image)