Spaces:
Sleeping
Sleeping
import gradio as gr | |
import zxingcpp | |
from PIL import Image | |
import cv2 | |
def decode(im,col): | |
img = cv2.imread(f'{im}') | |
try: | |
results = zxingcpp.read_barcodes(img) | |
return results[0].text | |
except Exception: | |
#qr_img=Image.open(im).convert("RGBA") | |
qr_img=Image.open(im) | |
datas = qr_img.getdata() | |
newData = [] | |
h1 = col.strip("#") | |
rgb_tup = tuple(int(h1[i:i+2], 16) for i in (0, 2, 4)) | |
for item in datas: | |
if item[0] == rgb_tup[0] and item[1] == rgb_tup[1] and item[2] == rgb_tup[2]: | |
newData.append(0,0,0) | |
else: | |
newData.append(255,255,255) | |
qr_img.putdata(newData) | |
qr_img.save('conv_im.png') | |
img = cv2.imread('conv_im.png') | |
try: | |
results = zxingcpp.read_barcodes(img) | |
return results[0].text | |
except Exception: | |
return "QR Code not Detected \nTry choosing the QR code color using the eyedropper in the Color Picker" | |
with gr.Blocks() as app: | |
in_im = gr.Image(label="QR Image to Decode") | |
dec_btn = gr.Button("Decode QR") | |
text_out = gr.Textbox(label="Decoded Text") | |
choose_color = gr.ColorPicker() | |
dec_btn.click(decode,[in_im,choose_color],text_out) | |
app.queue(concurrency_count=10).launch() |