Sebbe33 commited on
Commit
b908919
·
verified ·
1 Parent(s): cdb1e78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -24
app.py CHANGED
@@ -13,20 +13,6 @@ def parse_list_boxes(text):
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
  def draw_bounding_boxes(image, boxes):
31
  """Zeichnet Bounding Boxes auf das Bild"""
32
  draw = ImageDraw.Draw(image)
@@ -39,15 +25,15 @@ def draw_bounding_boxes(image, boxes):
39
  ymax = max(0.0, min(1.0, box[2]))
40
  xmax = max(0.0, min(1.0, box[3]))
41
 
 
42
  draw.rectangle([
43
  xmin * width,
44
  ymin * height,
45
  xmax * width,
46
  ymax * height
47
- ], outline="red", width=5) # Dickere Linien
48
  return image
49
 
50
-
51
  # Streamlit UI
52
  st.title("Bildanalyse mit Gemini")
53
  col1, col2 = st.columns(2)
@@ -58,6 +44,7 @@ with col1:
58
 
59
  if uploaded_file and object_name:
60
  image = Image.open(uploaded_file)
 
61
  st.image(image, caption="Hochgeladenes Bild", use_container_width=True)
62
 
63
  if st.button("Analysieren"):
@@ -82,34 +69,62 @@ with col1:
82
 
83
  # Objekterkennung
84
  detection_prompt = (
85
- f"Gib alle Bounding Boxes für {object_name} im Format "
86
- "[ymin, xmin, ymax, xmax] als Liste. Nur die Liste zurückgeben!"
 
87
  )
88
  box_response = client.models.generate_content(
89
  model="gemini-2.0-flash-exp",
90
  contents=[detection_prompt, image_part]
91
  )
92
- st.write("Raw API Response:", box_response.text)
93
 
 
 
 
94
  # Verarbeitung
95
- boxes = parse_list_boxes(box_response.text)
 
 
 
 
 
 
96
  annotated_image = image.copy()
97
 
98
  if boxes:
99
  annotated_image = draw_bounding_boxes(annotated_image, boxes)
100
  result_text = f"{len(boxes)} {object_name} erkannt"
 
 
 
 
 
 
 
 
 
 
 
101
  else:
102
  result_text = "Keine Objekte gefunden"
 
103
 
104
  # Ergebnisse anzeigen
105
  with col2:
106
- st.write("## Objekterkennung:")
107
- st.write(result_text)
108
- st.image(annotated_image, caption="Erkannte Objekte", use_container_width=True)
109
-
110
  st.write("## Beschreibung:")
111
  st.write(desc_response.text)
112
 
 
 
113
 
 
 
 
 
 
 
 
 
 
114
  except Exception as e:
115
  st.error(f"Fehler: {str(e)}")
 
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)
 
25
  ymax = max(0.0, min(1.0, box[2]))
26
  xmax = max(0.0, min(1.0, box[3]))
27
 
28
+ # Zeichne den Rahmen
29
  draw.rectangle([
30
  xmin * width,
31
  ymin * height,
32
  xmax * width,
33
  ymax * height
34
+ ], outline="#00FF00", width=7) # Neon green mit dicken Linien
35
  return image
36
 
 
37
  # Streamlit UI
38
  st.title("Bildanalyse mit Gemini")
39
  col1, col2 = st.columns(2)
 
44
 
45
  if uploaded_file and object_name:
46
  image = Image.open(uploaded_file)
47
+ width, height = image.size
48
  st.image(image, caption="Hochgeladenes Bild", use_container_width=True)
49
 
50
  if st.button("Analysieren"):
 
69
 
70
  # Objekterkennung
71
  detection_prompt = (
72
+ f"Gib exakt 4 Dezimalzahlen pro Box für alle {object_name} im Format "
73
+ "[ymin, xmin, ymax, xmax] als reine Python-Liste ohne weiteren Text. "
74
+ "Beispiel: [[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8]]"
75
  )
76
  box_response = client.models.generate_content(
77
  model="gemini-2.0-flash-exp",
78
  contents=[detection_prompt, image_part]
79
  )
 
80
 
81
+ # Debug-Ausgaben
82
+ st.write("**Raw API Response:**", box_response.text)
83
+
84
  # Verarbeitung
85
+ try:
86
+ boxes = parse_list_boxes(box_response.text)
87
+ st.write("**Parsed Boxes:**", boxes)
88
+ except Exception as e:
89
+ st.error(f"Parsing Error: {str(e)}")
90
+ boxes = []
91
+
92
  annotated_image = image.copy()
93
 
94
  if boxes:
95
  annotated_image = draw_bounding_boxes(annotated_image, boxes)
96
  result_text = f"{len(boxes)} {object_name} erkannt"
97
+
98
+ # Zoom auf erste Box
99
+ ymin, xmin, ymax, xmax = boxes[0]
100
+ zoom_area = (
101
+ max(0, int(xmin * width - 50)),
102
+ max(0, int(ymin * height - 50)),
103
+ min(width, int(xmax * width + 50)),
104
+ min(height, int(ymax * height + 50))
105
+ )
106
+ zoomed_image = annotated_image.crop(zoom_area)
107
+
108
  else:
109
  result_text = "Keine Objekte gefunden"
110
+ zoomed_image = None
111
 
112
  # Ergebnisse anzeigen
113
  with col2:
 
 
 
 
114
  st.write("## Beschreibung:")
115
  st.write(desc_response.text)
116
 
117
+ st.write("## Objekterkennung:")
118
+ st.write(result_text)
119
 
120
+ if boxes:
121
+ st.image(
122
+ [annotated_image, zoomed_image],
123
+ caption=["Gesamtbild", "Zoom auf Erkennung"],
124
+ width=400
125
+ )
126
+ else:
127
+ st.image(annotated_image, caption="Keine Objekte erkannt", width=400)
128
+
129
  except Exception as e:
130
  st.error(f"Fehler: {str(e)}")