sachin commited on
Commit
7ba3751
·
1 Parent(s): 2b9ad07

add-auto generated mask

Browse files
Files changed (4) hide show
  1. mask_sqaure.py +4 -4
  2. response.md +34 -0
  3. runway.py +45 -22
  4. square_mask_tank.png +0 -0
mask_sqaure.py CHANGED
@@ -1,9 +1,9 @@
1
  from PIL import Image, ImageDraw
2
 
3
  def create_square_mask(
4
- width: int = 1536,
5
- height: int = 1024,
6
- square_size: int = 512,
7
  center_x: int = None,
8
  center_y: int = None
9
  ) -> Image:
@@ -60,5 +60,5 @@ if __name__ == "__main__":
60
  center_x=768, # Center of 1536
61
  center_y=512 # Center of 1024
62
  )
63
- mask.save("square_mask.png", "PNG")
64
  print("Square mask image saved as 'square_mask.png'")
 
1
  from PIL import Image, ImageDraw
2
 
3
  def create_square_mask(
4
+ width: int = 1200,
5
+ height: int = 692,
6
+ square_size: int = 300,
7
  center_x: int = None,
8
  center_y: int = None
9
  ) -> Image:
 
60
  center_x=768, # Center of 1536
61
  center_y=512 # Center of 1024
62
  )
63
+ mask.save("square_mask_tank.png", "PNG")
64
  print("Square mask image saved as 'square_mask.png'")
response.md ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The image features a large tank positioned in the middle of a field, surrounded by trees. The tank appears to be a military vehicle, possibly a tank or a tank destroyer, and it is facing towards the right side of the image. The field is mostly brown, with some patches of green grass, and the sky above is filled with clouds, giving the scene a somewhat gloomy atmosphere.
2
+
3
+
4
+ The image depicts a vast, open field with a cloudy sky overhead. The field is covered in tall, dry grass, giving it a somewhat barren appearance. In the distance, a line of trees can be seen, providing a sense of depth to the scene. The overall atmosphere of the image is serene and somewhat melancholic, with the cloudy sky and the dry grass adding a touch of desolation to the landscape.
5
+
6
+
7
+
8
+ --
9
+
10
+
11
+ Based on the two descriptions you provided of images from a battlefield, let’s identify the most important military-related items present in each scene and then consider their significance. Since you’ve described two slightly different scenes, I’ll analyze them separately and then tie them together.
12
+ First Description:
13
+
14
+ Large Tank: The standout military item here is the tank, described as a "large tank" or possibly a "tank destroyer," positioned in the middle of a field and facing right. Tanks are critical in military contexts, serving as heavily armored, mobile firepower capable of engaging enemy forces, breaking through defenses, or holding ground. The specific type (e.g., a main battle tank like an M1 Abrams or a tank destroyer like an M10) isn’t confirmed, but its presence dominates the scene as the primary military asset.
15
+ Field Setting: While not a "military item" itself, the field’s condition (brown with patches of green) could imply a battlefield after combat—perhaps churned by vehicle tracks or affected by weather, which is tactically relevant for military operations like maneuverability or concealment.
16
+ Surrounding Trees: These could serve a military purpose, such as providing cover or camouflage for troops or vehicles, though they’re peripheral compared to the tank.
17
+
18
+ Key Military Item: The tank is unequivocally the most important military element here. It’s the central focus, a symbol of armored warfare, and likely the linchpin of any military activity in this scene.
19
+ Second Description:
20
+
21
+ Vast Open Field: This scene lacks an explicit military vehicle like the tank from the first description. However, the "vast, open field" with tall, dry grass could still be a battlefield or staging area. Open terrain is militarily significant for visibility, artillery range, or tank maneuvers, though no specific equipment is mentioned.
22
+ Line of Trees in the Distance: Similar to the first description, the trees could offer strategic value—cover for infantry, observation posts, or a natural barrier—but they’re distant and secondary.
23
+ Cloudy Sky: While atmospheric, this isn’t a military item, though it could affect military operations (e.g., air support visibility or troop morale).
24
+
25
+ Key Military Item: This description lacks a clear, tangible military object like the tank. The field itself, as a potential battlefield, is the most relevant military-related element, but it’s more abstract without equipment or personnel explicitly present.
26
+ Synthesis and Battlefield Context:
27
+ Since you mentioned these are outputs from a battlefield, I’ll assume they’re two perspectives of the same or related scenes. The first image introduces a concrete military asset (the tank), while the second might depict the broader environment around it, perhaps before or after the tank’s presence. Here’s what stands out as most important militarily across both:
28
+
29
+ The Tank: From the first description, this is the most significant military item. Tanks are pivotal in modern warfare, capable of offense, defense, and psychological impact. Its placement in the field suggests active engagement or a strategic hold.
30
+ The Field: Combining both descriptions, the open field (with dry grass, patches of green, and surrounding trees) is the battlefield itself. It’s the stage for military action, influencing how tanks, infantry, or artillery would operate. The barren, desolate vibe could hint at prior combat or abandonment.
31
+
32
+ Conclusion:
33
+ The tank is the most important military item when considering both descriptions together, as it’s the only explicit piece of military hardware mentioned. The field, as the battlefield context, is a close second, shaping the tactical environment. If these images are meant to represent a cohesive battlefield narrative, the tank likely defines the military presence, while the field sets the scene’s scope and tone.
34
+ If you’ve got more details—like whether these are literal images you want analyzed further or just text outputs to interpret—let me know, and I can refine this further! What’s your take on these scenes?
runway.py CHANGED
@@ -1,6 +1,6 @@
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from fastapi.responses import StreamingResponse
3
- from PIL import Image
4
  import io
5
  import torch
6
  import numpy as np
@@ -68,32 +68,53 @@ def soften_mask(mask_image: Image, softness: int = 5) -> Image:
68
  from PIL import ImageFilter
69
  return mask_image.filter(ImageFilter.GaussianBlur(radius=softness))
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  @app.post("/inpaint/")
72
  async def inpaint_image(
73
  image: UploadFile = File(...),
74
- mask: UploadFile = File(...),
75
- prompt: str = "Fill the masked area with appropriate content."
 
 
 
76
  ):
77
  """
78
- Endpoint for image inpainting using a text prompt.
79
  - `image`: Original image file (PNG/JPG).
80
- - `mask`: Mask file indicating areas to inpaint (black for masked areas, white for unmasked).
81
  - `prompt`: Text prompt describing the desired output.
 
82
 
83
  Returns:
84
  - The inpainted image as a PNG file.
85
  """
86
  try:
87
- # Load the uploaded image and mask
88
  image_bytes = await image.read()
89
- mask_bytes = await mask.read()
90
-
91
  original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
92
- mask_image = Image.open(io.BytesIO(mask_bytes)).convert("L")
93
 
94
- # Ensure dimensions match between image and mask
95
- if original_image.size != mask_image.size:
96
- raise HTTPException(status_code=400, detail="Image and mask dimensions must match.")
97
 
98
  # Perform inpainting using the pipeline
99
  result = pipe(prompt=prompt, image=original_image, mask_image=mask_image).images[0]
@@ -116,36 +137,38 @@ async def inpaint_image(
116
  @app.post("/inpaint-with-reference/")
117
  async def inpaint_with_reference(
118
  image: UploadFile = File(...),
119
- mask: UploadFile = File(...),
120
  reference_image: UploadFile = File(...),
121
- prompt: str = "Integrate the reference content naturally into the masked area, matching style and lighting."
 
 
 
 
122
  ):
123
  """
124
- Endpoint for replacing masked areas with reference image content, refined to look natural.
125
  - `image`: Original image file (PNG/JPG).
126
- - `mask`: Mask file (black for areas to replace, white for areas to keep).
127
  - `reference_image`: Reference image to guide the replacement (PNG/JPG).
128
  - `prompt`: Text prompt for inpainting refinement.
 
129
 
130
  Returns:
131
  - The resulting image as a PNG file.
132
  """
133
  try:
134
- # Load the uploaded image, mask, and reference image
135
  image_bytes = await image.read()
136
- mask_bytes = await mask.read()
137
  reference_bytes = await reference_image.read()
138
 
139
  original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
140
- mask_image = Image.open(io.BytesIO(mask_bytes)).convert("L")
141
  reference_image = Image.open(io.BytesIO(reference_bytes)).convert("RGB")
142
 
143
- # Ensure dimensions match between image, mask, and reference image
144
- if original_image.size != mask_image.size:
145
- raise HTTPException(status_code=400, detail="Image and mask dimensions must match.")
146
  if original_image.size != reference_image.size:
147
  reference_image = reference_image.resize(original_image.size, Image.Resampling.LANCZOS)
148
 
 
 
 
149
  # Soften the mask for smoother transitions
150
  softened_mask = soften_mask(mask_image, softness=5)
151
 
 
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from fastapi.responses import StreamingResponse
3
+ from PIL import Image, ImageDraw
4
  import io
5
  import torch
6
  import numpy as np
 
68
  from PIL import ImageFilter
69
  return mask_image.filter(ImageFilter.GaussianBlur(radius=softness))
70
 
71
+ def generate_rectangular_mask(image_size: tuple, x1: int = 100, y1: int = 100, x2: int = 200, y2: int = 200) -> Image:
72
+ """
73
+ Generate a rectangular mask matching the image dimensions.
74
+ - White (255) for unmasked areas, black (0) for masked areas.
75
+
76
+ Args:
77
+ image_size (tuple): Tuple of (width, height) of the original image.
78
+ x1, y1 (int): Top-left corner coordinates of the rectangle.
79
+ x2, y2 (int): Bottom-right corner coordinates of the rectangle.
80
+
81
+ Returns:
82
+ Image: The generated mask in grayscale (L mode).
83
+ """
84
+ # Create a blank white mask (255 = unmasked)
85
+ mask = Image.new("L", image_size, 255)
86
+ draw = ImageDraw.Draw(mask)
87
+
88
+ # Draw a black rectangle (0 = masked)
89
+ draw.rectangle([x1, y1, x2, y2], fill=0)
90
+
91
+ return mask
92
+
93
  @app.post("/inpaint/")
94
  async def inpaint_image(
95
  image: UploadFile = File(...),
96
+ prompt: str = "Fill the masked area with appropriate content.",
97
+ mask_x1: int = 100,
98
+ mask_y1: int = 100,
99
+ mask_x2: int = 200,
100
+ mask_y2: int = 200
101
  ):
102
  """
103
+ Endpoint for image inpainting using a text prompt and autogenerated mask.
104
  - `image`: Original image file (PNG/JPG).
 
105
  - `prompt`: Text prompt describing the desired output.
106
+ - `mask_x1, mask_y1, mask_x2, mask_y2`: Coordinates for the rectangular mask (default: 100,100 to 200,200).
107
 
108
  Returns:
109
  - The inpainted image as a PNG file.
110
  """
111
  try:
112
+ # Load the uploaded image
113
  image_bytes = await image.read()
 
 
114
  original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
 
115
 
116
+ # Generate the mask based on image dimensions and provided coordinates
117
+ mask_image = generate_rectangular_mask(original_image.size, mask_x1, mask_y1, mask_x2, mask_y2)
 
118
 
119
  # Perform inpainting using the pipeline
120
  result = pipe(prompt=prompt, image=original_image, mask_image=mask_image).images[0]
 
137
  @app.post("/inpaint-with-reference/")
138
  async def inpaint_with_reference(
139
  image: UploadFile = File(...),
 
140
  reference_image: UploadFile = File(...),
141
+ prompt: str = "Integrate the reference content naturally into the masked area, matching style and lighting.",
142
+ mask_x1: int = 100,
143
+ mask_y1: int = 100,
144
+ mask_x2: int = 200,
145
+ mask_y2: int = 200
146
  ):
147
  """
148
+ Endpoint for replacing masked areas with reference image content, refined to look natural, using an autogenerated mask.
149
  - `image`: Original image file (PNG/JPG).
 
150
  - `reference_image`: Reference image to guide the replacement (PNG/JPG).
151
  - `prompt`: Text prompt for inpainting refinement.
152
+ - `mask_x1, mask_y1, mask_x2, mask_y2`: Coordinates for the rectangular mask (default: 100,100 to 200,200).
153
 
154
  Returns:
155
  - The resulting image as a PNG file.
156
  """
157
  try:
158
+ # Load the uploaded image and reference image
159
  image_bytes = await image.read()
 
160
  reference_bytes = await reference_image.read()
161
 
162
  original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
 
163
  reference_image = Image.open(io.BytesIO(reference_bytes)).convert("RGB")
164
 
165
+ # Ensure reference image matches original image dimensions
 
 
166
  if original_image.size != reference_image.size:
167
  reference_image = reference_image.resize(original_image.size, Image.Resampling.LANCZOS)
168
 
169
+ # Generate the mask based on image dimensions and provided coordinates
170
+ mask_image = generate_rectangular_mask(original_image.size, mask_x1, mask_y1, mask_x2, mask_y2)
171
+
172
  # Soften the mask for smoother transitions
173
  softened_mask = soften_mask(mask_image, softness=5)
174
 
square_mask_tank.png ADDED