| from PIL import Image, ExifTags |
| import streamlit as st |
| from transformers import pipeline |
|
|
| import torch |
| |
| from models import vgg19 |
| from torchvision import transforms |
|
|
|
|
| |
| caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large") |
| emotion_pipeline = pipeline("image-classification", model="RickyIG/emotion_face_image_classification_v3") |
|
|
| |
| model_path = "model_sh_B.pth" |
| device = torch.device('cpu') |
|
|
| |
| model = vgg19() |
| model.to(device) |
| model.load_state_dict(torch.load(model_path, map_location=device)) |
| model.eval() |
|
|
|
|
| def predict_count(image): |
| |
| trans = transforms.Compose([ |
| transforms.ToTensor(), |
| transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) |
| ]) |
| img_tensor = trans(image) |
| inp = img_tensor.unsqueeze(0).to(device) |
|
|
| with torch.no_grad(): |
| outputs, _ = model(inp) |
|
|
| count = torch.sum(outputs).item() |
| return int(count) |
|
|
| def generate_caption_emotion_and_people_count(image): |
| |
| caption_result = caption_pipeline(image) |
| caption = caption_result[0]["generated_text"] |
|
|
| |
| emotion_result = emotion_pipeline(image) |
| emotions = ", ".join([f"{res['label']}: {res['score']:.2f}" for res in emotion_result]) |
|
|
| |
| count = predict_count(image) |
|
|
| |
| |
| |
|
|
| return caption, emotions, count |
| def main(): |
| |
| st.title("Analyse d'Affluence Événementielle via la Détection d'Objets") |
|
|
| |
| st.write("Ce projet vise à :") |
| st.markdown(""" |
| - Compter le nombre de participants. |
| - Analyser leurs émotions. |
| - Générer une description contextuelle de l'événement. |
| """) |
|
|
| |
| uploaded_image = st.file_uploader("Choisissez une image...", type=["jpg", "jpeg", "png"]) |
| if uploaded_image is not None: |
|
|
|
|
|
|
|
|
| image = Image.open(uploaded_image) |
|
|
| |
| width, height = image.size |
| if width>1200 or height > 1200: |
| try: |
| exif = image._getexif() |
| if exif is not None: |
| orientation_key = next((key for key, value in ExifTags.TAGS.items() if value == 'Orientation'), None) |
| if orientation_key is not None and orientation_key in exif: |
| orientation = exif[orientation_key] |
| if orientation == 3: |
| image = image.rotate(180, expand=True) |
| elif orientation == 6: |
| image = image.rotate(270, expand=True) |
| elif orientation == 8: |
| image = image.rotate(90, expand=True) |
| |
| width, height = image.size |
| st.write(f"Largeur: {width} pixels, Hauteur: {height} pixels") |
| st.image(image, caption='Image Uploadée', use_column_width=True) |
| except Exception as e: |
| st.write(f"Erreur lors de la correction de l'orientation: {e}") |
| pass |
| image = image.resize((224, 224)) |
| else: |
| |
| width, height = image.size |
| st.write(f"Largeur: {width} pixels, Hauteur: {height} pixels") |
| st.image(image, caption='Image Uploadée', use_column_width=True) |
| |
| |
| |
|
|
|
|
| |
| button_placeholder = st.empty() |
|
|
| |
| if button_placeholder.button('Analyser l\'image'): |
| |
| with st.spinner('En cours d\'exécution...'): |
| caption, emotions, count = generate_caption_emotion_and_people_count(image) |
| |
| |
| with st.expander("Voir les résultats !!"): |
| st.write(f"**Légende**: {caption}") |
| st.write(f"**Émotions**: {emotions}") |
| st.write(f"**Nombre de personnes**: {count}") |
| |
| |
| button_placeholder.empty() |
|
|
| if __name__ == '__main__': |
| main() |
|
|