-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (73 loc) · 2.91 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import qrcodegen
import pyqrcode
import segno
import torch
from PIL import Image
import qrcode
from keras.preprocessing import image
from keras.models import load_model
import numpy as np
# Function to generate QR code from text input and save as PNG file
def generate_qr_from_text(text, file_path):
qr = qrcode.QRCode(version=1, box_size=10, border=4)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(file_path)
# Function to generate QR code from image input and save as PDF file
def generate_qr_from_image(image_path, file_path):
img = Image.open(image_path)
qr = pyqrcode.create(img)
qr.png(file_path, scale=10)
# Function to generate QR code from camera input and save as SVG file
def generate_qr_from_camera(file_path):
# Use OpenCV or other library to capture image from camera
# Convert image to grayscale
# Apply edge detection algorithm
# Use segno library to generate QR code from edges
qr = segno.make("QR Code")
qr.save(file_path, kind="svg")
# Function to generate QR code from file input and save as PNG file
def generate_qr_from_file(file_path, save_path):
# Use deep learning model to detect QR code in image
# Use qrcodegen library to generate QR code from detected data
# Save QR code as PNG file
model = load_model("qr_detector.h5")
img = image.load_img(file_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
if preds[0][0] > 0.5:
qr_data = decode_qr_code(file_path)
qr = qrcodegen.QrCode.encode_text(qr_data)
qr_svg = qr.to_svg_str(4)
with open(save_path, "w") as f:
f.write(qr_svg)
# Main function to prompt user for input and generate QR code
def main():
print("Select input type:")
print("1. Text input")
print("2. Image from gallery")
print("3. Camera input")
print("4. File input")
input_type = int(input())
if input_type == 1:
text = input("Enter text to encode: ")
file_path = input("Enter file path to save QR code (e.g. qr_code.png): ")
generate_qr_from_text(text, file_path)
elif input_type == 2:
image_path = input("Enter image path to encode: ")
file_path = input("Enter file path to save QR code (e.g. qr_code.pdf): ")
generate_qr_from_image(image_path, file_path)
elif input_type == 3:
file_path = input("Enter file path to save QR code (e.g. qr_code.svg): ")
generate_qr_from_camera(file_path)
elif input_type == 4:
file_path = input("Enter file path to encode: ")
save_path = input("Enter file path to save QR code (e.g. qr_code.png): ")
generate_qr_from_file(file_path, save_path)
else:
print("Invalid input type")
if name == "main":
main()