doinglean commited on
Commit
e265812
·
verified ·
1 Parent(s): f94189b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -27,9 +27,10 @@ def analyze_image(image, prompt):
27
  with torch.no_grad():
28
  outputs = model(**inputs)
29
 
30
- # Einfache Bildanalyse mit OpenCV (Konturen für Objekte)
31
  gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY)
32
- _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
 
33
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
34
 
35
  # Analysiere das Bild basierend auf dem Prompt
@@ -38,8 +39,8 @@ def analyze_image(image, prompt):
38
  if len(contours) == 0:
39
  description.append("Das Bild enthält keine klar erkennbaren Objekte.")
40
  else:
41
- for idx, contour in enumerate(contours[:10]): # Begrenze auf 10 Objekte
42
- if cv2.contourArea(contour) < 100: # Ignoriere kleine Konturen
43
  continue
44
  x, y, w, h = cv2.boundingRect(contour)
45
  # Extrahiere Farbe der Region
@@ -48,16 +49,22 @@ def analyze_image(image, prompt):
48
  continue
49
  mean_color = np.mean(roi, axis=(0, 1)).astype(int)
50
  color_rgb = f"RGB({mean_color[2]},{mean_color[1]},{mean_color[0]})"
 
 
51
  description.append({
52
  "object": f"Object_{idx}",
53
  "color": color_rgb,
54
- "position": f"x={x}, y={y}, width={w}, height={h}"
 
55
  })
56
 
 
 
 
57
  return {
58
  "prompt": prompt,
59
  "description": description if description else "No objects detected.",
60
- "features_shape": str(outputs.last_hidden_state.shape) if hasattr(outputs, 'last_hidden_state') else "No features extracted."
61
  }
62
 
63
  # Erstelle Gradio-Schnittstelle
 
27
  with torch.no_grad():
28
  outputs = model(**inputs)
29
 
30
+ # Verbesserte Bildanalyse mit OpenCV
31
  gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY)
32
+ # Adaptiver Schwellenwert für bessere Konturenerkennung
33
+ thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
34
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
35
 
36
  # Analysiere das Bild basierend auf dem Prompt
 
39
  if len(contours) == 0:
40
  description.append("Das Bild enthält keine klar erkennbaren Objekte.")
41
  else:
42
+ for idx, contour in enumerate(contours[:20]): # Begrenze auf 20 Objekte
43
+ if cv2.contourArea(contour) < 200 or cv2.contourArea(contour) > (image_np.shape[0] * image_np.shape[1] * 0.5): # Filtere kleine/große Konturen
44
  continue
45
  x, y, w, h = cv2.boundingRect(contour)
46
  # Extrahiere Farbe der Region
 
49
  continue
50
  mean_color = np.mean(roi, axis=(0, 1)).astype(int)
51
  color_rgb = f"RGB({mean_color[2]},{mean_color[1]},{mean_color[0]})"
52
+ # Größenkategorie
53
+ size = "small" if w * h < 1000 else "medium" if w * h < 5000 else "large"
54
  description.append({
55
  "object": f"Object_{idx}",
56
  "color": color_rgb,
57
+ "position": f"x={x}, y={y}, width={w}, height={h}",
58
+ "size": size
59
  })
60
 
61
+ # Einfache Analyse der DINOv3-Features (z. B. Anzahl der Feature-Regionen)
62
+ feature_info = str(outputs.last_hidden_state.shape) if hasattr(outputs, 'last_hidden_state') else "No features extracted."
63
+
64
  return {
65
  "prompt": prompt,
66
  "description": description if description else "No objects detected.",
67
+ "features_shape": feature_info
68
  }
69
 
70
  # Erstelle Gradio-Schnittstelle