Severian commited on
Commit
ebd6751
·
verified ·
1 Parent(s): d954ee3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -10,6 +10,8 @@ import os
10
  from PIL import Image
11
  import spaces
12
  import numpy as np
 
 
13
 
14
  from diffusers import (
15
  StableDiffusionPipeline,
@@ -73,19 +75,39 @@ def scan_qr_code(image):
73
  image = Image.fromarray(image)
74
 
75
  # Convert to grayscale
76
- image = image.convert('L')
77
 
78
- # Increase contrast
79
- image = Image.eval(image, lambda x: 255 if x > 128 else 0)
80
 
81
- # Try to decode the QR code
82
  try:
83
  qr = qrcode.QRCode()
84
  qr.add_data('')
85
- qr.decode(image)
86
  return qr.data.decode('utf-8')
87
- except Exception as e:
88
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  @spaces.GPU()
91
  def inference(
@@ -238,7 +260,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
238
 
239
  - **High settings (1.6-5.0)**: If you need to make sure that the QR code is super easy to scan, even if it means the image looks less like art and more like a regular QR code, then choose a higher value. This is ideal when functionality is the main goal, and the artistic side can take a backseat.
240
 
241
- Start with **1.3** if youre unsure, and adjust up or down depending on whether you want the QR code to be more artistic or more functional.
242
  """
243
  )
244
 
@@ -251,13 +273,13 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
251
  )
252
  gr.Markdown(
253
  """
254
- **Artistic Freedom** controls how much the AI is allowed to change the QR code's look to match your description. Its like telling the AI how creative it can get with your QR code:
255
 
256
  - **Low settings (0.0-0.3)**: If you set this low, the AI will make small changes and your QR code will look more like a regular, plain QR code. This is useful if you want something that is still creative but not too wild, keeping it simple and easy to scan.
257
 
258
  - **Medium settings (0.4-0.7)**: Here, the AI will add more artistic touches but keep the QR code recognizable. You get the best of both worlds—your QR code will have some creative flair, but it will still be easy to scan. For most cases, setting it to **0.6** is a great way to keep the code functional and artistic.
259
 
260
- - **High settings (0.8-1.0)**: If you set this high, the AI will go all-out creative. The QR code will look amazing, but it might be difficult to scan because the art can start to take over the code. This setting is perfect if youre aiming for a highly creative piece of art and dont mind if its a bit harder to scan. Start at **0.9** to explore creative but functional designs.
261
  """
262
  )
263
 
@@ -276,7 +298,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
276
 
277
  - **Medium settings (5-15)**: This is a good balance where the AI will mostly follow your prompt but will also add some of its own creative touches. If you want to see some surprises but still want the design to look like what you described, start at around **7.5**.
278
 
279
- - **High settings (15+)**: If you choose a higher value, the AI will stick very closely to what you wrote in the description. This is good if you have a very specific idea and dont want the AI to change much. Just keep in mind that this might limit the AIs creativity.
280
 
281
  Start at **7.5** for a balanced approach where the AI follows your ideas but still adds some artistic flair.
282
  """
@@ -291,13 +313,13 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as blocks:
291
  """
292
  **Art Style** changes how the AI creates the image, using different methods (or "samplers"). Each method has a different effect on how detailed or artistic the final QR code looks:
293
 
294
- - **DPM++ Karras SDE**: This is a great all-around option for creating high-quality, detailed images. Its a good place to start if you want a balance of sharpness and creativity.
295
 
296
  - **Euler**: This method creates very sharp, detailed images, making the QR code look crisp and clear. Choose this if you want a precise, well-defined design.
297
 
298
- - **DDIM**: This method is better if you want the QR code to have a more artistic, abstract style. Its great for when you want the QR code to look like a piece of modern art.
299
 
300
- Feel free to experiment with different samplers to see what works best for the look youre going for!
301
  """
302
  )
303
 
 
10
  from PIL import Image
11
  import spaces
12
  import numpy as np
13
+ import cv2
14
+ from pyzxing import BarCodeReader
15
 
16
  from diffusers import (
17
  StableDiffusionPipeline,
 
75
  image = Image.fromarray(image)
76
 
77
  # Convert to grayscale
78
+ gray_image = image.convert('L')
79
 
80
+ # Convert to numpy array
81
+ np_image = np.array(gray_image)
82
 
83
+ # Method 1: Using qrcode library
84
  try:
85
  qr = qrcode.QRCode()
86
  qr.add_data('')
87
+ qr.decode(gray_image)
88
  return qr.data.decode('utf-8')
89
+ except Exception:
90
+ pass
91
+
92
+ # Method 2: Using OpenCV
93
+ try:
94
+ qr_detector = cv2.QRCodeDetector()
95
+ retval, decoded_info, points, straight_qrcode = qr_detector.detectAndDecodeMulti(np_image)
96
+ if retval:
97
+ return decoded_info[0]
98
+ except Exception:
99
+ pass
100
+
101
+ # Method 3: Fallback to zxing-cpp
102
+ try:
103
+ reader = BarCodeReader()
104
+ results = reader.decode(np_image)
105
+ if results:
106
+ return results[0].parsed
107
+ except Exception:
108
+ pass
109
+
110
+ return None
111
 
112
  @spaces.GPU()
113
  def inference(
 
260
 
261
  - **High settings (1.6-5.0)**: If you need to make sure that the QR code is super easy to scan, even if it means the image looks less like art and more like a regular QR code, then choose a higher value. This is ideal when functionality is the main goal, and the artistic side can take a backseat.
262
 
263
+ Start with **1.3** if you're unsure, and adjust up or down depending on whether you want the QR code to be more artistic or more functional.
264
  """
265
  )
266
 
 
273
  )
274
  gr.Markdown(
275
  """
276
+ **Artistic Freedom** controls how much the AI is allowed to change the QR code's look to match your description. It's like telling the AI how creative it can get with your QR code:
277
 
278
  - **Low settings (0.0-0.3)**: If you set this low, the AI will make small changes and your QR code will look more like a regular, plain QR code. This is useful if you want something that is still creative but not too wild, keeping it simple and easy to scan.
279
 
280
  - **Medium settings (0.4-0.7)**: Here, the AI will add more artistic touches but keep the QR code recognizable. You get the best of both worlds—your QR code will have some creative flair, but it will still be easy to scan. For most cases, setting it to **0.6** is a great way to keep the code functional and artistic.
281
 
282
+ - **High settings (0.8-1.0)**: If you set this high, the AI will go all-out creative. The QR code will look amazing, but it might be difficult to scan because the art can start to take over the code. This setting is perfect if you're aiming for a highly creative piece of art and don't mind if it's a bit harder to scan. Start at **0.9** to explore creative but functional designs.
283
  """
284
  )
285
 
 
298
 
299
  - **Medium settings (5-15)**: This is a good balance where the AI will mostly follow your prompt but will also add some of its own creative touches. If you want to see some surprises but still want the design to look like what you described, start at around **7.5**.
300
 
301
+ - **High settings (15+)**: If you choose a higher value, the AI will stick very closely to what you wrote in the description. This is good if you have a very specific idea and don't want the AI to change much. Just keep in mind that this might limit the AI's creativity.
302
 
303
  Start at **7.5** for a balanced approach where the AI follows your ideas but still adds some artistic flair.
304
  """
 
313
  """
314
  **Art Style** changes how the AI creates the image, using different methods (or "samplers"). Each method has a different effect on how detailed or artistic the final QR code looks:
315
 
316
+ - **DPM++ Karras SDE**: This is a great all-around option for creating high-quality, detailed images. It's a good place to start if you want a balance of sharpness and creativity.
317
 
318
  - **Euler**: This method creates very sharp, detailed images, making the QR code look crisp and clear. Choose this if you want a precise, well-defined design.
319
 
320
+ - **DDIM**: This method is better if you want the QR code to have a more artistic, abstract style. It's great for when you want the QR code to look like a piece of modern art.
321
 
322
+ Feel free to experiment with different samplers to see what works best for the look you're going for!
323
  """
324
  )
325