Severian commited on
Commit
4dba69f
·
verified ·
1 Parent(s): 669cf4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -24
app.py CHANGED
@@ -13,6 +13,8 @@ import numpy as np
13
  import cv2
14
  from pyzxing import BarCodeReader
15
  import tempfile
 
 
16
 
17
  from diffusers import (
18
  StableDiffusionPipeline,
@@ -135,6 +137,32 @@ def scan_qr_code(image, sensitivity=0.5):
135
 
136
  return successful_scans
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  @spaces.GPU()
139
  def inference(
140
  qr_code_content: str,
@@ -149,6 +177,12 @@ def inference(
149
  use_qr_code_as_init_image = True,
150
  sampler = "DPM++ Karras SDE",
151
  verify_sensitivity: float = 0.5,
 
 
 
 
 
 
152
  ):
153
  try:
154
  if prompt is None or prompt == "":
@@ -163,33 +197,39 @@ def inference(
163
 
164
  if qr_code_content != "" or qrcode_image.size == (1, 1):
165
  print("Generating QR Code from content")
166
- qr = qrcode.QRCode(
167
- version=1,
168
- error_correction=qrcode.constants.ERROR_CORRECT_H,
169
- box_size=10,
170
- border=4,
 
 
 
 
 
 
 
 
 
171
  )
172
- qr.add_data(qr_code_content)
173
- qr.make(fit=True)
174
-
175
- qrcode_image = qr.make_image(fill_color="black", back_color="white")
176
- qrcode_image = resize_for_condition_image(qrcode_image, 768)
177
- else:
178
- print("Using QR Code Image")
179
  qrcode_image = resize_for_condition_image(qrcode_image, 768)
180
 
181
- # hack due to gradio examples
182
- init_image = qrcode_image
 
 
 
 
183
 
184
  out = pipe(
185
  prompt=prompt,
186
  negative_prompt=negative_prompt,
187
- image=qrcode_image,
188
- control_image=qrcode_image, # type: ignore
189
- width=768, # type: ignore
190
- height=768, # type: ignore
191
  guidance_scale=float(guidance_scale),
192
- controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
193
  generator=generator,
194
  strength=float(strength),
195
  num_inference_steps=50,
@@ -198,11 +238,11 @@ def inference(
198
  # Verify the generated QR code
199
  verify_result = scan_qr_code(out.images[0], sensitivity=verify_sensitivity)
200
 
201
- return out.images[0], verify_result
202
  except Exception as e:
203
  print(f"Error in inference: {str(e)}")
204
  # Return a blank image in case of an error
205
- return Image.new('RGB', (768, 768), color='white'), []
206
 
207
 
208
 
@@ -374,7 +414,6 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
374
  maximum=1.0,
375
  step=0.05,
376
  value=0.5,
377
- visible=True,
378
  label="Verify Sensitivity",
379
  )
380
  gr.Markdown(
@@ -389,6 +428,8 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
389
 
390
  with gr.Column():
391
  result_image = gr.Image(label="Your Artistic QR Code")
 
 
392
  scan_button = gr.Button("Scan QR Code")
393
  scan_result = gr.Textbox(label="Scan Result", interactive=False)
394
 
@@ -405,6 +446,14 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
405
  """
406
  )
407
 
 
 
 
 
 
 
 
 
408
  def scan_and_display(image):
409
  if image is None:
410
  return "No image to scan"
@@ -424,7 +473,9 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
424
  outputs=[scan_result]
425
  )
426
 
427
-
 
 
428
  run_btn.click(
429
  inference,
430
  inputs=[
@@ -440,10 +491,19 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
440
  use_qr_code_as_init_image,
441
  sampler,
442
  verify_sensitivity,
 
 
 
 
 
 
443
  ],
444
- outputs=[result_image, scan_result],
445
  concurrency_limit=20
446
  )
447
 
 
 
 
448
  blocks.queue(max_size=20,api_open=False)
449
  blocks.launch(share=bool(os.environ.get("SHARE", False)), show_api=True)
 
13
  import cv2
14
  from pyzxing import BarCodeReader
15
  import tempfile
16
+ from io import BytesIO
17
+ import base64
18
 
19
  from diffusers import (
20
  StableDiffusionPipeline,
 
137
 
138
  return successful_scans
139
 
140
+ def generate_qr_code(
141
+ data,
142
+ version=None,
143
+ error_correction=qrcode.constants.ERROR_CORRECT_H,
144
+ box_size=10,
145
+ border=4,
146
+ fill_color="black",
147
+ back_color="white"
148
+ ):
149
+ qr = qrcode.QRCode(
150
+ version=version,
151
+ error_correction=error_correction,
152
+ box_size=box_size,
153
+ border=border,
154
+ )
155
+ qr.add_data(data)
156
+ qr.make(fit=True)
157
+
158
+ img = qr.make_image(fill_color=fill_color, back_color=back_color)
159
+ return img
160
+
161
+ def image_to_base64(img):
162
+ buffered = BytesIO()
163
+ img.save(buffered, format="PNG")
164
+ return base64.b64encode(buffered.getvalue()).decode()
165
+
166
  @spaces.GPU()
167
  def inference(
168
  qr_code_content: str,
 
177
  use_qr_code_as_init_image = True,
178
  sampler = "DPM++ Karras SDE",
179
  verify_sensitivity: float = 0.5,
180
+ qr_version: int = 1,
181
+ qr_error_correction: str = "H",
182
+ qr_box_size: int = 10,
183
+ qr_border: int = 4,
184
+ qr_fill_color: str = "black",
185
+ qr_back_color: str = "white",
186
  ):
187
  try:
188
  if prompt is None or prompt == "":
 
197
 
198
  if qr_code_content != "" or qrcode_image.size == (1, 1):
199
  print("Generating QR Code from content")
200
+ error_correction_map = {
201
+ "L": qrcode.constants.ERROR_CORRECT_L,
202
+ "M": qrcode.constants.ERROR_CORRECT_M,
203
+ "Q": qrcode.constants.ERROR_CORRECT_Q,
204
+ "H": qrcode.constants.ERROR_CORRECT_H,
205
+ }
206
+ qrcode_image = generate_qr_code(
207
+ qr_code_content,
208
+ version=qr_version,
209
+ error_correction=error_correction_map[qr_error_correction],
210
+ box_size=qr_box_size,
211
+ border=qr_border,
212
+ fill_color=qr_fill_color,
213
+ back_color=qr_back_color,
214
  )
 
 
 
 
 
 
 
215
  qrcode_image = resize_for_condition_image(qrcode_image, 768)
216
 
217
+ # Generate base64 string of the initial QR code
218
+ initial_qr_base64 = image_to_base64(qrcode_image)
219
+
220
+ # Use QR code as init image if specified
221
+ if use_qr_code_as_init_image:
222
+ init_image = qrcode_image
223
 
224
  out = pipe(
225
  prompt=prompt,
226
  negative_prompt=negative_prompt,
227
+ image=init_image,
228
+ control_image=qrcode_image,
229
+ width=768,
230
+ height=768,
231
  guidance_scale=float(guidance_scale),
232
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
233
  generator=generator,
234
  strength=float(strength),
235
  num_inference_steps=50,
 
238
  # Verify the generated QR code
239
  verify_result = scan_qr_code(out.images[0], sensitivity=verify_sensitivity)
240
 
241
+ return out.images[0], verify_result, initial_qr_base64
242
  except Exception as e:
243
  print(f"Error in inference: {str(e)}")
244
  # Return a blank image in case of an error
245
+ return Image.new('RGB', (768, 768), color='white'), [], ""
246
 
247
 
248
 
 
414
  maximum=1.0,
415
  step=0.05,
416
  value=0.5,
 
417
  label="Verify Sensitivity",
418
  )
419
  gr.Markdown(
 
428
 
429
  with gr.Column():
430
  result_image = gr.Image(label="Your Artistic QR Code")
431
+ initial_qr_image = gr.Image(label="Initial QR Code", visible=False)
432
+ download_initial_qr = gr.Button("Download Initial QR Code")
433
  scan_button = gr.Button("Scan QR Code")
434
  scan_result = gr.Textbox(label="Scan Result", interactive=False)
435
 
 
446
  """
447
  )
448
 
449
+ with gr.Accordion("QR Code Customization", open=False):
450
+ qr_version = gr.Slider(minimum=1, maximum=40, step=1, value=1, label="QR Code Version")
451
+ qr_error_correction = gr.Dropdown(choices=["L", "M", "Q", "H"], value="H", label="Error Correction Level")
452
+ qr_box_size = gr.Slider(minimum=1, maximum=50, step=1, value=10, label="Box Size")
453
+ qr_border = gr.Slider(minimum=0, maximum=10, step=1, value=4, label="Border Size")
454
+ qr_fill_color = gr.ColorPicker(label="QR Code Color", value="#000000")
455
+ qr_back_color = gr.ColorPicker(label="Background Color", value="#FFFFFF")
456
+
457
  def scan_and_display(image):
458
  if image is None:
459
  return "No image to scan"
 
473
  outputs=[scan_result]
474
  )
475
 
476
+ def update_initial_qr(base64_string):
477
+ return gr.Image.update(value=base64_string, visible=True)
478
+
479
  run_btn.click(
480
  inference,
481
  inputs=[
 
491
  use_qr_code_as_init_image,
492
  sampler,
493
  verify_sensitivity,
494
+ qr_version,
495
+ qr_error_correction,
496
+ qr_box_size,
497
+ qr_border,
498
+ qr_fill_color,
499
+ qr_back_color,
500
  ],
501
+ outputs=[result_image, scan_result, initial_qr_image],
502
  concurrency_limit=20
503
  )
504
 
505
+ download_initial_qr.click(lambda x: x, inputs=[initial_qr_image], outputs=[initial_qr_image])
506
+
507
+
508
  blocks.queue(max_size=20,api_open=False)
509
  blocks.launch(share=bool(os.environ.get("SHARE", False)), show_api=True)