batuergun commited on
Commit
c57f54f
1 Parent(s): 032e6b6

image shape transform

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -147,20 +147,26 @@ def encrypt(user_id, input_image):
147
  if input_image is None:
148
  raise gr.Error("Please choose an image first.")
149
 
150
- if input_image.shape[-1] != 3:
151
- raise ValueError(f"Input image must have 3 channels (RGB). Current shape: {input_image.shape}")
152
-
153
  # Resize the image if it hasn't the shape (224, 224, 3)
154
  if input_image.shape != (224, 224, 3):
155
  input_image_pil = Image.fromarray(input_image)
156
  input_image_pil = input_image_pil.resize((224, 224))
157
  input_image = numpy.array(input_image_pil)
158
 
 
 
 
 
 
 
 
 
 
159
  # Retrieve the client API
160
  client = get_client(user_id)
161
 
162
  # Pre-process, encrypt and serialize the image
163
- encrypted_image = client.encrypt_serialize(input_image)
164
 
165
  # Save encrypted_image to bytes in a file, since too large to pass through regular Gradio
166
  # buttons, https://github.com/gradio-app/gradio/issues/1877
 
147
  if input_image is None:
148
  raise gr.Error("Please choose an image first.")
149
 
 
 
 
150
  # Resize the image if it hasn't the shape (224, 224, 3)
151
  if input_image.shape != (224, 224, 3):
152
  input_image_pil = Image.fromarray(input_image)
153
  input_image_pil = input_image_pil.resize((224, 224))
154
  input_image = numpy.array(input_image_pil)
155
 
156
+ # Convert RGB to grayscale
157
+ input_image_gray = numpy.mean(input_image, axis=2).astype(numpy.uint8)
158
+
159
+ # Reshape to (1, 1, 224, 224)
160
+ input_image_reshaped = input_image_gray.reshape(1, 1, 224, 224)
161
+
162
+ # Convert to int12 (assuming the range is 0-255, we can simply cast to int16)
163
+ input_image_int12 = input_image_reshaped.astype(numpy.int16)
164
+
165
  # Retrieve the client API
166
  client = get_client(user_id)
167
 
168
  # Pre-process, encrypt and serialize the image
169
+ encrypted_image = client.encrypt_serialize(input_image_int12)
170
 
171
  # Save encrypted_image to bytes in a file, since too large to pass through regular Gradio
172
  # buttons, https://github.com/gradio-app/gradio/issues/1877