File size: 2,084 Bytes
d3f340f
 
f443ef8
704d03b
 
731bcbb
d3f340f
f443ef8
d3f340f
0a9a758
 
 
 
 
 
 
704d03b
0a9a758
 
704d03b
ea00ccb
 
 
 
 
 
 
e064287
ea00ccb
e064287
ca8e0ee
ea00ccb
ca8e0ee
ea00ccb
e0d446b
ea00ccb
 
 
1152606
95cf6cc
ea00ccb
95cf6cc
704d03b
d3f340f
 
 
ca8e0ee
 
 
95cf6cc
ca8e0ee
f443ef8
 
 
ca8e0ee
 
d3f340f
f443ef8
d3f340f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import zxingcpp
import numpy as np
from PIL import Image

import cv2

def decode(im,col,tol):
    img = cv2.imread(f'{im}')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.medianBlur(gray, 5)
    sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
    sharpen = cv2.filter2D(blur, -1, sharpen_kernel)
    
    

    try:
        results = zxingcpp.read_barcodes(sharpen)
        return results[0].text,sharpen
    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))
        tol=100
        for item in datas:
            if item[0] in range(rgb_tup[0]-tol,rgb_tup[0]+tol) and item[1] in range(rgb_tup[1]-tol,rgb_tup[1]+tol) and item[2] in range(rgb_tup[2]-tol,rgb_tup[2]+tol):
                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)
            print (results[0].text)
            return results[0].text,'conv_im.png'
        except Exception:
            return "QR Code not Detected \nTry choosing the QR code color using the eyedropper in the Color Picker",'conv_im.png'
    


with gr.Blocks() as app:
    with gr.Row():
        with gr.Column():
            in_im = gr.Image(label="QR Image to Decode",type='filepath')
            out_im = gr.Image(type='filepath')
        with gr.Column():
            with gr.Row():
                choose_color = gr.ColorPicker(label="Choose QR color (eyedropper)")
                color_tol = gr.Slider(1,255, step=1,value=50,label="Color Detect Tolerance")
            dec_btn = gr.Button("Decode QR")
            text_out = gr.Textbox(label="Decoded Text")    
    
    dec_btn.click(decode,[in_im,choose_color,color_tol],[text_out,out_im])
app.queue(concurrency_count=10).launch()