Martin Tomov commited on
Commit
a0de963
·
verified ·
1 Parent(s): 014d0b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -50,16 +50,30 @@ def annotate(image: Union[Image.Image, np.ndarray], detection_results: List[Dete
50
  image_cv2 = np.array(image) if isinstance(image, Image.Image) else image
51
  image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
52
 
53
- # Make the entire background yellow
54
  yellow_background = np.full(image_cv2.shape, (0, 255, 255), dtype=np.uint8)
55
 
56
  for detection in detection_results:
57
  mask = detection.mask
58
 
59
  if mask is not None:
60
- mask_expanded = np.stack([mask]*3, axis=-1) # Expand mask dimensions for color channels
61
- insect_region = np.where(mask_expanded, image_cv2, yellow_background)
62
- yellow_background = np.where(mask_expanded, insect_region, yellow_background)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  return cv2.cvtColor(yellow_background, cv2.COLOR_BGR2RGB)
65
 
 
50
  image_cv2 = np.array(image) if isinstance(image, Image.Image) else image
51
  image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_RGB2BGR)
52
 
53
+ # Create an empty yellow background
54
  yellow_background = np.full(image_cv2.shape, (0, 255, 255), dtype=np.uint8)
55
 
56
  for detection in detection_results:
57
  mask = detection.mask
58
 
59
  if mask is not None:
60
+ mask_uint8 = (mask * 255).astype(np.uint8)
61
+ mask_3_channel = cv2.merge([mask_uint8, mask_uint8, mask_uint8])
62
+
63
+ # Invert the mask to get the background part
64
+ inverted_mask = cv2.bitwise_not(mask_uint8)
65
+ inverted_mask_3_channel = cv2.merge([inverted_mask, inverted_mask, inverted_mask])
66
+
67
+ # Extract insect region using mask
68
+ insect_region = cv2.bitwise_and(image_cv2, mask_3_channel)
69
+ yellow_background_region = cv2.bitwise_and(yellow_background, inverted_mask_3_channel)
70
+
71
+ # Combine the insect region and the yellow background region
72
+ combined_region = cv2.add(insect_region, yellow_background_region)
73
+
74
+ # Apply combined region onto the yellow background
75
+ yellow_background = cv2.bitwise_and(yellow_background, inverted_mask_3_channel)
76
+ yellow_background = cv2.add(yellow_background, combined_region)
77
 
78
  return cv2.cvtColor(yellow_background, cv2.COLOR_BGR2RGB)
79