NitishBorthakur commited on
Commit
0e64b9a
·
verified ·
1 Parent(s): c3af435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -3,23 +3,22 @@ import requests
3
  from PIL import Image
4
  from io import BytesIO
5
  import numpy as np
6
- import re
7
  from landingai.common import decode_bitmap_rle
8
- import os # For accessing secrets
9
 
10
  ENDPOINT_ID = "ba678fa4-65d1-4b87-8c85-cebd15224783"
11
- API_KEY = os.environ.get("land_sk_ikq7WEKGtaKI7pXIcKt2x7RoyYE6FBReqGOmKtEhjcmFbLbQsK")
12
  API_URL = f"https://predict.app.landing.ai/inference/v1/predict?endpoint_id={ENDPOINT_ID}"
13
 
14
  def predict_from_landinglens(image_path):
15
  # Load and keep original image
16
  original_img = Image.open(image_path).convert("RGB")
17
  img_array = np.array(original_img)
18
-
19
  # Get image dimensions
20
  height, width = img_array.shape[:2]
21
  total_pixels = height * width
22
-
23
  # Prepare for API
24
  buffered = BytesIO()
25
  original_img.save(buffered, format="JPEG")
@@ -49,24 +48,27 @@ def predict_from_landinglens(image_path):
49
  mask = decode_bitmap_rle(bitmap_data["bitmap"])
50
  if isinstance(mask, list):
51
  mask = np.array(mask)
52
-
53
  # Reshape mask to match image dimensions
54
- mask = mask.reshape(prediction["predictions"]["imageHeight"],
55
  prediction["predictions"]["imageWidth"])
56
-
57
  # Calculate area coverage
58
  mask_area = np.sum(mask > 0)
59
  coverage_percentage = (mask_area / total_pixels) * 100
60
  label_name = bitmap_data.get("label_name", f"Mask {i}")
61
  coverage_info.append(f"{label_name}: {coverage_percentage:.2f}%")
62
 
63
-
64
-
65
- # Create colored overlay using Pillow
66
- mask_image = Image.fromarray((mask > 0).astype(np.uint8) * 255)
67
- overlay = Image.new("RGB", mask_image.size, (255, 0, 0)) # Red overlay
68
- mask_image = mask_image.convert("RGB")
69
- masked_image = Image.blend(original_img, overlay, alpha=0.5 * mask_image.convert("L").point(lambda x: x > 0 and 1))
 
 
 
70
  masked_images.append(masked_image)
71
 
72
  except Exception as e:
@@ -88,4 +90,6 @@ iface = gr.Interface(
88
  ],
89
  title="Crosswalk detection model",
90
  )
91
- iface.launch()
 
 
 
3
  from PIL import Image
4
  from io import BytesIO
5
  import numpy as np
 
6
  from landingai.common import decode_bitmap_rle
7
+ import cv2
8
 
9
  ENDPOINT_ID = "ba678fa4-65d1-4b87-8c85-cebd15224783"
10
+ API_KEY = "land_sk_ikq7WEKGtaKI7pXIcKt2x7RoyYE6FBReqGOmKtEhjcmFbLbQsK"
11
  API_URL = f"https://predict.app.landing.ai/inference/v1/predict?endpoint_id={ENDPOINT_ID}"
12
 
13
  def predict_from_landinglens(image_path):
14
  # Load and keep original image
15
  original_img = Image.open(image_path).convert("RGB")
16
  img_array = np.array(original_img)
17
+
18
  # Get image dimensions
19
  height, width = img_array.shape[:2]
20
  total_pixels = height * width
21
+
22
  # Prepare for API
23
  buffered = BytesIO()
24
  original_img.save(buffered, format="JPEG")
 
48
  mask = decode_bitmap_rle(bitmap_data["bitmap"])
49
  if isinstance(mask, list):
50
  mask = np.array(mask)
51
+
52
  # Reshape mask to match image dimensions
53
+ mask = mask.reshape(prediction["predictions"]["imageHeight"],
54
  prediction["predictions"]["imageWidth"])
55
+
56
  # Calculate area coverage
57
  mask_area = np.sum(mask > 0)
58
  coverage_percentage = (mask_area / total_pixels) * 100
59
  label_name = bitmap_data.get("label_name", f"Mask {i}")
60
  coverage_info.append(f"{label_name}: {coverage_percentage:.2f}%")
61
 
62
+ # Create colored overlay
63
+ colored_mask = np.zeros_like(img_array)
64
+ colored_mask[mask > 0] = [255, 0, 0] # Red overlay for mask
65
+
66
+ # Combine original image with colored mask
67
+ alpha = 0.5 # Transparency of the overlay
68
+ combined = cv2.addWeighted(img_array, 1, colored_mask, alpha, 0)
69
+
70
+ # Convert to PIL Image
71
+ masked_image = Image.fromarray(combined)
72
  masked_images.append(masked_image)
73
 
74
  except Exception as e:
 
90
  ],
91
  title="Crosswalk detection model",
92
  )
93
+
94
+ if __name__ == "__main__":
95
+ iface.launch()