Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,167 +1,167 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
import barcode
|
| 7 |
-
from barcode.writer import ImageWriter
|
| 8 |
-
import qrcode
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def image_to_bytes(img):
|
| 12 |
-
pil_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 13 |
-
buffer = BytesIO()
|
| 14 |
-
pil_image.save(buffer, format="PNG")
|
| 15 |
-
return buffer.getvalue()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def generate_barcode(link):
|
| 19 |
-
code128 = barcode.get_barcode_class('code128')
|
| 20 |
-
barcode_image = code128(link, writer=ImageWriter())
|
| 21 |
-
buffer = BytesIO()
|
| 22 |
-
barcode_image.write(buffer)
|
| 23 |
-
return Image.open(buffer)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def generate_qrcode(link):
|
| 28 |
-
qr = qrcode.QRCode(
|
| 29 |
-
version=2,
|
| 30 |
-
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 31 |
-
box_size=4,
|
| 32 |
-
border=2,
|
| 33 |
-
)
|
| 34 |
-
qr.add_data(link)
|
| 35 |
-
qr.make(fit=True)
|
| 36 |
-
qr_image = qr.make_image(fill_color="black", back_color="white")
|
| 37 |
-
|
| 38 |
-
# Réduire la taille tout en maintenant la lisibilité (ex: 100x100 pixels)
|
| 39 |
-
small_qr_image = qr_image.resize((100, 100), Image.Resampling.LANCZOS)
|
| 40 |
-
|
| 41 |
-
buffer = BytesIO()
|
| 42 |
-
small_qr_image.save(buffer, format="PNG")
|
| 43 |
-
return Image.open(buffer)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def main():
|
| 48 |
-
st.set_page_config(page_title="Application de Vision par Ordinateur", layout="wide")
|
| 49 |
-
st.title("Application de Vision par Ordinateur")
|
| 50 |
-
|
| 51 |
-
# Section pour le choix de l'image par défaut
|
| 52 |
-
st.sidebar.header("Chargement de l'image")
|
| 53 |
-
if "default_image" not in st.session_state:
|
| 54 |
-
st.session_state["default_image"] = None
|
| 55 |
-
|
| 56 |
-
uploaded_file = st.sidebar.file_uploader("Charge une image", type=["png", "jpg", "jpeg"])
|
| 57 |
-
if uploaded_file is not None:
|
| 58 |
-
image = Image.open(uploaded_file)
|
| 59 |
-
st.session_state["default_image"] = np.array(image)
|
| 60 |
-
st.sidebar.image(image, caption="Image par défaut",
|
| 61 |
-
|
| 62 |
-
if st.session_state["default_image"] is None:
|
| 63 |
-
st.sidebar.warning("Veuillez charger une image pour commencer.")
|
| 64 |
-
return
|
| 65 |
-
|
| 66 |
-
# Menu des fonctionnalités
|
| 67 |
-
st.sidebar.header("Fonctionnalités")
|
| 68 |
-
menu_option = st.sidebar.selectbox("Choisissez une fonctionnalité", [
|
| 69 |
-
"Accueil",
|
| 70 |
-
"Transformations d'image",
|
| 71 |
-
"Cropping",
|
| 72 |
-
"Rotation",
|
| 73 |
-
"Floutage",
|
| 74 |
-
"Contours",
|
| 75 |
-
"Génération de Code-barres et QR Code"
|
| 76 |
-
])
|
| 77 |
-
|
| 78 |
-
image_np = st.session_state["default_image"]
|
| 79 |
-
|
| 80 |
-
if menu_option == "Accueil":
|
| 81 |
-
st.header("Bienvenue sur l'application de Vision par Ordinateur")
|
| 82 |
-
st.write(
|
| 83 |
-
"Cette application utilise OpenCV pour effectuer diverses transformations "
|
| 84 |
-
"et manipulations d'images. Charge une image depuis la barre latérale et explore les fonctionnalités."
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
elif menu_option == "Transformations d'image":
|
| 88 |
-
st.subheader("Transformations d'image")
|
| 89 |
-
|
| 90 |
-
col1, col2, col3, col4 = st.columns(4)
|
| 91 |
-
with col1:
|
| 92 |
-
if st.button("Transformer en gris"):
|
| 93 |
-
gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
|
| 94 |
-
st.image(gray, caption="Image en Niveaux de Gris",
|
| 95 |
-
with col2:
|
| 96 |
-
if st.button("Couleur rouge"):
|
| 97 |
-
red = image_np.copy()
|
| 98 |
-
red[:, :, 1:] = 0
|
| 99 |
-
st.image(red, caption="Image Rouge",
|
| 100 |
-
with col3:
|
| 101 |
-
if st.button("Couleur verte"):
|
| 102 |
-
green = image_np.copy()
|
| 103 |
-
green[:, :, [0, 2]] = 0
|
| 104 |
-
st.image(green, caption="Image Verte",
|
| 105 |
-
with col4:
|
| 106 |
-
if st.button("Couleur jaune"):
|
| 107 |
-
yellow = image_np.copy()
|
| 108 |
-
yellow[:, :, 0] = 0
|
| 109 |
-
st.image(yellow, caption="Image Jaune",
|
| 110 |
-
|
| 111 |
-
elif menu_option == "Cropping":
|
| 112 |
-
st.subheader("Cropping")
|
| 113 |
-
|
| 114 |
-
x1 = st.number_input("x1", 0, image_np.shape[1] - 1, step=1)
|
| 115 |
-
y1 = st.number_input("y1", 0, image_np.shape[0] - 1, step=1)
|
| 116 |
-
x2 = st.number_input("x2", 1, image_np.shape[1], step=1)
|
| 117 |
-
y2 = st.number_input("y2", 1, image_np.shape[0], step=1)
|
| 118 |
-
|
| 119 |
-
if st.button("Cropper"):
|
| 120 |
-
cropped = image_np[int(y1):int(y2), int(x1):int(x2)]
|
| 121 |
-
st.image(cropped, caption="Image Croppée",
|
| 122 |
-
|
| 123 |
-
elif menu_option == "Rotation":
|
| 124 |
-
st.subheader("Rotation")
|
| 125 |
-
|
| 126 |
-
angle = st.selectbox("Angle de rotation", [45, 90, 180])
|
| 127 |
-
|
| 128 |
-
if st.button("Tourner"):
|
| 129 |
-
rows, cols, _ = image_np.shape
|
| 130 |
-
rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
| 131 |
-
rotated = cv2.warpAffine(image_np, rotation_matrix, (cols, rows))
|
| 132 |
-
st.image(rotated, caption=f"Image Rotée de {angle} degrés",
|
| 133 |
-
|
| 134 |
-
elif menu_option == "Floutage":
|
| 135 |
-
st.subheader("Floutage")
|
| 136 |
-
|
| 137 |
-
blur_level = st.slider("Niveau de flou (k)", min_value=1, max_value=51, step=2, value=15)
|
| 138 |
-
if st.button("Appliquer le flou"):
|
| 139 |
-
blurred = cv2.GaussianBlur(image_np, (blur_level, blur_level), 0)
|
| 140 |
-
st.image(blurred, caption=f"Image Floutée (k={blur_level})",
|
| 141 |
-
|
| 142 |
-
elif menu_option == "Contours":
|
| 143 |
-
st.subheader("Contours")
|
| 144 |
-
|
| 145 |
-
if st.button("Capturer les contours"):
|
| 146 |
-
gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
|
| 147 |
-
edges = cv2.Canny(gray, 100, 200)
|
| 148 |
-
st.image(edges, caption="Contours de l'Image",
|
| 149 |
-
|
| 150 |
-
elif menu_option == "Génération de Code-barres et QR Code":
|
| 151 |
-
st.subheader("Génération de Code-barres et QR Code")
|
| 152 |
-
link = st.text_input("Entre un lien ou un texte pour générer le code-barres et le QR code")
|
| 153 |
-
if st.button("Générer"):
|
| 154 |
-
if link:
|
| 155 |
-
# Génération du Code-barres
|
| 156 |
-
barcode_image = generate_barcode(link)
|
| 157 |
-
st.image(barcode_image, caption="Code-barres généré",
|
| 158 |
-
|
| 159 |
-
# Génération du QR code
|
| 160 |
-
qrcode_image = generate_qrcode(link)
|
| 161 |
-
st.image(qrcode_image, caption="QR Code généré",
|
| 162 |
-
else:
|
| 163 |
-
st.error("Veuillez entrer un lien valide pour générer les codes.")
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
if __name__ == "__main__":
|
| 167 |
-
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import barcode
|
| 7 |
+
from barcode.writer import ImageWriter
|
| 8 |
+
import qrcode
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def image_to_bytes(img):
|
| 12 |
+
pil_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 13 |
+
buffer = BytesIO()
|
| 14 |
+
pil_image.save(buffer, format="PNG")
|
| 15 |
+
return buffer.getvalue()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def generate_barcode(link):
|
| 19 |
+
code128 = barcode.get_barcode_class('code128')
|
| 20 |
+
barcode_image = code128(link, writer=ImageWriter())
|
| 21 |
+
buffer = BytesIO()
|
| 22 |
+
barcode_image.write(buffer)
|
| 23 |
+
return Image.open(buffer)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def generate_qrcode(link):
|
| 28 |
+
qr = qrcode.QRCode(
|
| 29 |
+
version=2,
|
| 30 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 31 |
+
box_size=4,
|
| 32 |
+
border=2,
|
| 33 |
+
)
|
| 34 |
+
qr.add_data(link)
|
| 35 |
+
qr.make(fit=True)
|
| 36 |
+
qr_image = qr.make_image(fill_color="black", back_color="white")
|
| 37 |
+
|
| 38 |
+
# Réduire la taille tout en maintenant la lisibilité (ex: 100x100 pixels)
|
| 39 |
+
small_qr_image = qr_image.resize((100, 100), Image.Resampling.LANCZOS)
|
| 40 |
+
|
| 41 |
+
buffer = BytesIO()
|
| 42 |
+
small_qr_image.save(buffer, format="PNG")
|
| 43 |
+
return Image.open(buffer)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
st.set_page_config(page_title="Application de Vision par Ordinateur", layout="wide")
|
| 49 |
+
st.title("Application de Vision par Ordinateur")
|
| 50 |
+
|
| 51 |
+
# Section pour le choix de l'image par défaut
|
| 52 |
+
st.sidebar.header("Chargement de l'image")
|
| 53 |
+
if "default_image" not in st.session_state:
|
| 54 |
+
st.session_state["default_image"] = None
|
| 55 |
+
|
| 56 |
+
uploaded_file = st.sidebar.file_uploader("Charge une image", type=["png", "jpg", "jpeg"])
|
| 57 |
+
if uploaded_file is not None:
|
| 58 |
+
image = Image.open(uploaded_file)
|
| 59 |
+
st.session_state["default_image"] = np.array(image)
|
| 60 |
+
st.sidebar.image(image, caption="Image par défaut", use_container_width=True)
|
| 61 |
+
|
| 62 |
+
if st.session_state["default_image"] is None:
|
| 63 |
+
st.sidebar.warning("Veuillez charger une image pour commencer.")
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
# Menu des fonctionnalités
|
| 67 |
+
st.sidebar.header("Fonctionnalités")
|
| 68 |
+
menu_option = st.sidebar.selectbox("Choisissez une fonctionnalité", [
|
| 69 |
+
"Accueil",
|
| 70 |
+
"Transformations d'image",
|
| 71 |
+
"Cropping",
|
| 72 |
+
"Rotation",
|
| 73 |
+
"Floutage",
|
| 74 |
+
"Contours",
|
| 75 |
+
"Génération de Code-barres et QR Code"
|
| 76 |
+
])
|
| 77 |
+
|
| 78 |
+
image_np = st.session_state["default_image"]
|
| 79 |
+
|
| 80 |
+
if menu_option == "Accueil":
|
| 81 |
+
st.header("Bienvenue sur l'application de Vision par Ordinateur")
|
| 82 |
+
st.write(
|
| 83 |
+
"Cette application utilise OpenCV pour effectuer diverses transformations "
|
| 84 |
+
"et manipulations d'images. Charge une image depuis la barre latérale et explore les fonctionnalités."
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
elif menu_option == "Transformations d'image":
|
| 88 |
+
st.subheader("Transformations d'image")
|
| 89 |
+
|
| 90 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 91 |
+
with col1:
|
| 92 |
+
if st.button("Transformer en gris"):
|
| 93 |
+
gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
|
| 94 |
+
st.image(gray, caption="Image en Niveaux de Gris", use_container_width=True)
|
| 95 |
+
with col2:
|
| 96 |
+
if st.button("Couleur rouge"):
|
| 97 |
+
red = image_np.copy()
|
| 98 |
+
red[:, :, 1:] = 0
|
| 99 |
+
st.image(red, caption="Image Rouge", use_container_width=True)
|
| 100 |
+
with col3:
|
| 101 |
+
if st.button("Couleur verte"):
|
| 102 |
+
green = image_np.copy()
|
| 103 |
+
green[:, :, [0, 2]] = 0
|
| 104 |
+
st.image(green, caption="Image Verte", use_container_width=True)
|
| 105 |
+
with col4:
|
| 106 |
+
if st.button("Couleur jaune"):
|
| 107 |
+
yellow = image_np.copy()
|
| 108 |
+
yellow[:, :, 0] = 0
|
| 109 |
+
st.image(yellow, caption="Image Jaune", use_container_width=True)
|
| 110 |
+
|
| 111 |
+
elif menu_option == "Cropping":
|
| 112 |
+
st.subheader("Cropping")
|
| 113 |
+
|
| 114 |
+
x1 = st.number_input("x1", 0, image_np.shape[1] - 1, step=1)
|
| 115 |
+
y1 = st.number_input("y1", 0, image_np.shape[0] - 1, step=1)
|
| 116 |
+
x2 = st.number_input("x2", 1, image_np.shape[1], step=1)
|
| 117 |
+
y2 = st.number_input("y2", 1, image_np.shape[0], step=1)
|
| 118 |
+
|
| 119 |
+
if st.button("Cropper"):
|
| 120 |
+
cropped = image_np[int(y1):int(y2), int(x1):int(x2)]
|
| 121 |
+
st.image(cropped, caption="Image Croppée", use_container_width=True)
|
| 122 |
+
|
| 123 |
+
elif menu_option == "Rotation":
|
| 124 |
+
st.subheader("Rotation")
|
| 125 |
+
|
| 126 |
+
angle = st.selectbox("Angle de rotation", [45, 90, 180])
|
| 127 |
+
|
| 128 |
+
if st.button("Tourner"):
|
| 129 |
+
rows, cols, _ = image_np.shape
|
| 130 |
+
rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
| 131 |
+
rotated = cv2.warpAffine(image_np, rotation_matrix, (cols, rows))
|
| 132 |
+
st.image(rotated, caption=f"Image Rotée de {angle} degrés", use_container_width=True)
|
| 133 |
+
|
| 134 |
+
elif menu_option == "Floutage":
|
| 135 |
+
st.subheader("Floutage")
|
| 136 |
+
|
| 137 |
+
blur_level = st.slider("Niveau de flou (k)", min_value=1, max_value=51, step=2, value=15)
|
| 138 |
+
if st.button("Appliquer le flou"):
|
| 139 |
+
blurred = cv2.GaussianBlur(image_np, (blur_level, blur_level), 0)
|
| 140 |
+
st.image(blurred, caption=f"Image Floutée (k={blur_level})", use_container_width=True)
|
| 141 |
+
|
| 142 |
+
elif menu_option == "Contours":
|
| 143 |
+
st.subheader("Contours")
|
| 144 |
+
|
| 145 |
+
if st.button("Capturer les contours"):
|
| 146 |
+
gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
|
| 147 |
+
edges = cv2.Canny(gray, 100, 200)
|
| 148 |
+
st.image(edges, caption="Contours de l'Image", use_container_width=True)
|
| 149 |
+
|
| 150 |
+
elif menu_option == "Génération de Code-barres et QR Code":
|
| 151 |
+
st.subheader("Génération de Code-barres et QR Code")
|
| 152 |
+
link = st.text_input("Entre un lien ou un texte pour générer le code-barres et le QR code")
|
| 153 |
+
if st.button("Générer"):
|
| 154 |
+
if link:
|
| 155 |
+
# Génération du Code-barres
|
| 156 |
+
barcode_image = generate_barcode(link)
|
| 157 |
+
st.image(barcode_image, caption="Code-barres généré", use_container_width=True)
|
| 158 |
+
|
| 159 |
+
# Génération du QR code
|
| 160 |
+
qrcode_image = generate_qrcode(link)
|
| 161 |
+
st.image(qrcode_image, caption="QR Code généré", use_container_width=True)
|
| 162 |
+
else:
|
| 163 |
+
st.error("Veuillez entrer un lien valide pour générer les codes.")
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
if __name__ == "__main__":
|
| 167 |
+
main()
|