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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -28
app.py CHANGED
@@ -12,10 +12,6 @@ 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]
@@ -27,41 +23,22 @@ def resize_image(image, target_dimension):
27
  return resized_image
28
 
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)
51
- output = show_image_with_annotation_scene(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), prediction, show_results=False)
52
- output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
53
  return [output, prediction.overview]
54
 
55
 
56
  interface = gr.Interface(fn=infer,
57
- inputs=['image', 'text'],
58
  outputs=['image', 'text'],
59
  allow_flagging='manual',
60
  flagging_dir='flagged',
61
- examples=[["no_bird.jpg", ""],
62
- ["bird_example.jpg",""],
63
- [None,"https://upload.wikimedia.org/wikipedia/commons/9/9a/Pinz%C3%B3n_azul_de_Gran_Canaria_%28macho%29%2C_M._A._Pe%C3%B1a.jpg"],
64
- [None, "https://upload.wikimedia.org/wikipedia/commons/2/2f/Phaethon_lepturus_%28Warwick%2C_Bermuda%29_%28cropped%29.jpg"]
65
- ])
66
 
67
  interface.launch()
 
12
  deployment.load_inference_models(device="CPU")
13
 
14
 
 
 
 
 
15
 
16
  def resize_image(image, target_dimension):
17
  height, width = image.shape[:2]
 
23
  return resized_image
24
 
25
 
26
+ def infer(image=None):
 
 
 
27
  if image is None:
28
+ return [None,'Error: No image provided']
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  image = resize_image(image, 1200)
31
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
32
  prediction = deployment.infer(image)
33
+ output = show_image_with_annotation_scene(image, prediction, show_results=False)
 
34
  return [output, prediction.overview]
35
 
36
 
37
  interface = gr.Interface(fn=infer,
38
+ inputs=['image'],
39
  outputs=['image', 'text'],
40
  allow_flagging='manual',
41
  flagging_dir='flagged',
42
+ examples=["no_bird.jpg", "bird_example.jpg"])
 
 
 
 
43
 
44
  interface.launch()