TuringsSolutions commited on
Commit
74de75a
·
verified ·
1 Parent(s): 220a711

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -9
app.py CHANGED
@@ -21,11 +21,11 @@ class SwarmAgent:
21
  self.v = np.zeros_like(position)
22
 
23
  class SwarmNeuralNetwork:
24
- def __init__(self, num_agents, image_shape, target_image):
25
  self.image_shape = image_shape
26
  self.resized_shape = (256, 256, 3) # High resolution
27
  self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)]
28
- self.target_image = self.load_target_image(target_image)
29
  self.generated_image = np.random.randn(*image_shape) # Start with noise
30
  self.mobilenet = self.load_mobilenet_model()
31
  self.current_epoch = 0
@@ -166,9 +166,20 @@ class SwarmNeuralNetwork:
166
  self.generate_image()
167
  return self.generated_image
168
 
 
 
 
 
 
 
 
 
 
 
 
169
  # Gradio Interface
170
  def train_snn(image_path, num_agents, epochs, arm_position, leg_position, brightness, contrast, color):
171
- snn = SwarmNeuralNetwork(num_agents=num_agents, image_shape=(256, 256, 3), target_image=image_path) # High resolution
172
 
173
  # Apply user-specified adjustments to the target image
174
  image = Image.open(image_path)
@@ -176,18 +187,18 @@ def train_snn(image_path, num_agents, epochs, arm_position, leg_position, bright
176
  image = ImageEnhance.Contrast(image).enhance(contrast)
177
  image = ImageEnhance.Color(image).enhance(color)
178
 
179
- # Mock adjustment for arm and leg positions (to be implemented with actual logic)
180
- # For now, we just log the values
181
- print(f"Adjusting arm position: {arm_position}, leg position: {leg_position}")
 
182
 
183
- snn.target_image = snn.load_target_image(image)
184
  snn.train(epochs=epochs)
185
  snn.save_model('snn_model.npy')
186
  generated_image = snn.generated_image
187
  return generated_image
188
 
189
  def generate_new_image():
190
- snn = SwarmNeuralNetwork(num_agents=2000, image_shape=(256, 256, 3), target_image=None) # High resolution and optimized number of agents
191
  snn.load_model('snn_model.npy')
192
  new_image = snn.generate_new_image()
193
  return new_image
@@ -195,4 +206,18 @@ def generate_new_image():
195
  interface = gr.Interface(
196
  fn=train_snn,
197
  inputs=[
198
- gr.Image(type="filepath", label
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  self.v = np.zeros_like(position)
22
 
23
  class SwarmNeuralNetwork:
24
+ def __init__(self, num_agents, image_shape, target_image_path):
25
  self.image_shape = image_shape
26
  self.resized_shape = (256, 256, 3) # High resolution
27
  self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)]
28
+ self.target_image = self.load_target_image(target_image_path)
29
  self.generated_image = np.random.randn(*image_shape) # Start with noise
30
  self.mobilenet = self.load_mobilenet_model()
31
  self.current_epoch = 0
 
166
  self.generate_image()
167
  return self.generated_image
168
 
169
+ def adjust_limbs(self, arm_position, leg_position):
170
+ # Logic to adjust arm and leg positions in the target image
171
+ # For simplicity, let's assume arm_position and leg_position range from -100 to 100
172
+ arm_shift = arm_position / 100.0 * 0.2 # Scale to a reasonable range
173
+ leg_shift = leg_position / 100.0 * 0.2 # Scale to a reasonable range
174
+
175
+ # Translate the positions of the arms and legs in the image
176
+ for agent in self.agents:
177
+ agent.position[50:100, 50:200, :] += arm_shift # Example adjustment
178
+ agent.position[150:200, 50:200, :] += leg_shift # Example adjustment
179
+
180
  # Gradio Interface
181
  def train_snn(image_path, num_agents, epochs, arm_position, leg_position, brightness, contrast, color):
182
+ snn = SwarmNeuralNetwork(num_agents=num_agents, image_shape=(256, 256, 3), target_image_path=image_path) # High resolution
183
 
184
  # Apply user-specified adjustments to the target image
185
  image = Image.open(image_path)
 
187
  image = ImageEnhance.Contrast(image).enhance(contrast)
188
  image = ImageEnhance.Color(image).enhance(color)
189
 
190
+ snn.target_image = snn.load_target_image(image_path)
191
+
192
+ # Adjust limb positions based on slider values
193
+ snn.adjust_limbs(arm_position, leg_position)
194
 
 
195
  snn.train(epochs=epochs)
196
  snn.save_model('snn_model.npy')
197
  generated_image = snn.generated_image
198
  return generated_image
199
 
200
  def generate_new_image():
201
+ snn = SwarmNeuralNetwork(num_agents=2000, image_shape=(256, 256, 3), target_image_path=None) # High resolution and optimized number of agents
202
  snn.load_model('snn_model.npy')
203
  new_image = snn.generate_new_image()
204
  return new_image
 
206
  interface = gr.Interface(
207
  fn=train_snn,
208
  inputs=[
209
+ gr.Image(type="filepath", label="Upload Target Image"),
210
+ gr.Slider(minimum=500, maximum=2000, value=1000, label="Number of Agents"), # Adjusted range for number of agents
211
+ gr.Slider(minimum=10, maximum=100, value=50, label="Number of Epochs"), # Adjusted range for number of epochs
212
+ gr.Slider(minimum=-100, maximum=100, value=0, label="Arm Position"),
213
+ gr.Slider(minimum=-100, maximum=100, value=0, label="Leg Position"),
214
+ gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Brightness"),
215
+ gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Contrast"),
216
+ gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Color Balance")
217
+ ],
218
+ outputs=gr.Image(type="numpy", label="Generated Image"),
219
+ title="Swarm Neural Network Image Generation",
220
+ description="Upload an image and set the number of agents and epochs to train the Swarm Neural Network to generate a new image. Adjust arm and leg positions, brightness, contrast, and color balance for personalization."
221
+ )
222
+
223
+ interface.launch()