Sebbe33 commited on
Commit
6c79114
·
verified ·
1 Parent(s): 40a87e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -27
app.py CHANGED
@@ -1,49 +1,92 @@
1
  import os
2
- import streamlit as st
3
- from PIL import Image
4
  import io
5
-
6
- from google import genai
 
7
  from google.genai import types
8
 
 
 
 
 
 
 
9
 
10
- # API-Schlüssel laden
11
- #genai.configure(api_key=os.get("KEY"))
12
-
 
 
 
 
 
 
 
 
 
 
13
 
 
14
  st.title("Bildanalyse mit Gemini")
15
  col1, col2 = st.columns(2)
16
 
17
  with col1:
18
-
19
  uploaded_file = st.file_uploader("Bild hochladen", type=["jpg", "png", "jpeg"])
20
-
21
- if uploaded_file is not None:
 
22
  image = Image.open(uploaded_file)
23
  st.image(image, caption="Hochgeladenes Bild", use_container_width=True)
24
-
25
  if st.button("Analysieren"):
26
  with st.spinner("Analysiere Bild..."):
27
  try:
28
- # Bild in Bytes umwandeln
29
  image_bytes = io.BytesIO()
30
  image.save(image_bytes, format=image.format)
31
- image_bytes = image_bytes.getvalue()
32
-
33
-
34
- # Anfrage an Gemini senden
35
- client = genai.Client(api_key=os.getenv("KEY")) # Client innerhalb der Funktion erstellen
36
- response = client.models.generate_content(
37
- model="gemini-2.0-flash-exp", # Oder "gemini-2.0-flash-exp", je nach Verfügbarkeit
38
- contents=["Beschreibe dieses Bild und identifiziere das Hauptobjekt.", types.Part.from_bytes(data=image_bytes, mime_type=f"image/{image.format.lower()}")
39
- ]
40
  )
41
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  with col2:
43
- # Antwort anzeigen
44
- st.write("## Analyseergebnis:")
45
- st.write(response.text)
 
 
 
46
 
47
-
48
  except Exception as e:
49
- st.error(f"Ein Fehler ist aufgetreten: {e}")
 
1
  import os
2
+ import re
 
3
  import io
4
+ import streamlit as st
5
+ from PIL import Image, ImageDraw
6
+ from google import genai
7
  from google.genai import types
8
 
9
+ # Hilfsfunktionen
10
+ def parse_list_boxes(text):
11
+ """Extrahiert Bounding Boxes aus dem Antworttext"""
12
+ pattern = r'\[([\d\.]+),\s*([\d\.]+),\s*([\d\.]+),\s*([\d\.]+)\]'
13
+ matches = re.findall(pattern, text)
14
+ return [[float(m) for m in match] for match in matches]
15
 
16
+ def draw_bounding_boxes(image, boxes):
17
+ """Zeichnet Bounding Boxes auf das Bild"""
18
+ draw = ImageDraw.Draw(image)
19
+ width, height = image.size
20
+ for box in boxes:
21
+ ymin, xmin, ymax, xmax = box
22
+ draw.rectangle([
23
+ xmin * width,
24
+ ymin * height,
25
+ xmax * width,
26
+ ymax * height
27
+ ], outline="red", width=3)
28
+ return image
29
 
30
+ # Streamlit UI
31
  st.title("Bildanalyse mit Gemini")
32
  col1, col2 = st.columns(2)
33
 
34
  with col1:
 
35
  uploaded_file = st.file_uploader("Bild hochladen", type=["jpg", "png", "jpeg"])
36
+ object_name = st.text_input("Objekt zur Erkennung", placeholder="z.B. 'Auto', 'Person'")
37
+
38
+ if uploaded_file and object_name:
39
  image = Image.open(uploaded_file)
40
  st.image(image, caption="Hochgeladenes Bild", use_container_width=True)
41
+
42
  if st.button("Analysieren"):
43
  with st.spinner("Analysiere Bild..."):
44
  try:
45
+ # Bildvorbereitung
46
  image_bytes = io.BytesIO()
47
  image.save(image_bytes, format=image.format)
48
+ image_part = types.Part.from_bytes(
49
+ data=image_bytes.getvalue(),
50
+ mime_type=f"image/{image.format.lower()}"
 
 
 
 
 
 
51
  )
52
+
53
+ # API-Client
54
+ client = genai.Client(api_key=os.getenv("KEY"))
55
+
56
+ # Bildbeschreibung
57
+ desc_response = client.models.generate_content(
58
+ model="gemini-1.0-pro-vision",
59
+ contents=["Beschreibe dieses Bild detailliert.", image_part]
60
+ )
61
+
62
+ # Objekterkennung
63
+ detection_prompt = (
64
+ f"Gib alle Bounding Boxes für {object_name} im Format "
65
+ "[ymin, xmin, ymax, xmax] als Liste. Nur die Liste zurückgeben!"
66
+ )
67
+ box_response = client.models.generate_content(
68
+ model="gemini-1.0-pro-vision",
69
+ contents=[detection_prompt, image_part]
70
+ )
71
+
72
+ # Verarbeitung
73
+ boxes = parse_list_boxes(box_response.text)
74
+ annotated_image = image.copy()
75
+
76
+ if boxes:
77
+ annotated_image = draw_bounding_boxes(annotated_image, boxes)
78
+ result_text = f"{len(boxes)} {object_name} erkannt"
79
+ else:
80
+ result_text = "Keine Objekte gefunden"
81
+
82
+ # Ergebnisse anzeigen
83
  with col2:
84
+ st.write("## Beschreibung:")
85
+ st.write(desc_response.text)
86
+
87
+ st.write("## Objekterkennung:")
88
+ st.write(result_text)
89
+ st.image(annotated_image, caption="Erkannte Objekte", use_column_width=True)
90
 
 
91
  except Exception as e:
92
+ st.error(f"Fehler: {str(e)}")