Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

AI Camera - IMX500 • Re: Colors in the ai Camera

$
0
0
i use a python script:

import cv2
import numpy as np
import os
import time
from functools import lru_cache

from picamera2 import MappedArray, Picamera2
from picamera2.devices import IMX500
from picamera2.devices.imx500 import NetworkIntrinsics

# Globale Variablen initialisieren
last_detections = []
last_results = None
detection_counter = 0 # Zaehler fuer die Erkennungen
frame_counter = 0 # Zaehler fuer die Frames

# Initialisiere IMX500 und Picamera2
imx500 = IMX500('network.rpk')
picam2 = Picamera2(imx500.camera_num)

class Detection:
def __init__(self, coords, category, conf, metadata):
"""Erstellt ein Detection-Objekt mit korrekt berechneter Bounding Box, behandelt coords als [x_min, y_min, x_max, y_max]."""
x_min, y_min, x_max, y_max = coords # Annahme: coords sind [x_min, y_min, x_max, y_max]

# Debug-Ausgaben
print(f"Urspruengliche Koordinaten: x_min={x_min}, y_min={y_min}, x_max={x_max}, y_max={y_max}")

# Eingabebildgroesse des Modells
input_w, input_h = imx500.get_input_size()
print(f"Eingabebildgroesse des Modells: width={input_w}, height={input_h}")

# Ausgabebildgroesse (ISP)
isp_size = imx500.get_isp_output_size(picam2)
isp_w = isp_size.width
isp_h = isp_size.height
print(f"Ausgabebildgroesse (ISP): width={isp_w}, height={isp_h}")

# Skalierungsfaktoren
scale_x = isp_w / input_w
scale_y = isp_h / input_h
print(f"Skalierungsfaktoren: scale_x={scale_x}, scale_y={scale_y}")

# Skalieren der Koordinaten
x_min_scaled = x_min * scale_x
y_min_scaled = y_min * scale_y
x_max_scaled = x_max * scale_x
y_max_scaled = y_max * scale_y

# Berechnung der Bounding Box
x1 = int(x_min_scaled)
y1 = int(y_min_scaled)
width = int(x_max_scaled - x_min_scaled)
height = int(y_max_scaled - y_min_scaled)

# Sicherstellen, dass die Werte innerhalb der Bildgrenzen liegen
x1 = max(0, min(x1, isp_w - 1))
y1 = max(0, min(y1, isp_h - 1))
width = max(0, min(width, isp_w - x1))
height = max(0, min(height, isp_h - y1))

print(f"Berechnete Bounding Box: x1={x1}, y1={y1}, width={width}, height={height}")

self.category = category
self.conf = conf
self.box = (x1, y1, width, height)

def parse_detections(metadata: dict):
"""Parst den Ausgabe-Tensor in erkannte Objekte."""
global last_detections
threshold = intrinsics.threshold

np_outputs = imx500.get_outputs(metadata, add_batch=True)
if np_outputs is None:
print("Keine Ausgaben vom Netzwerk erhalten.")
return last_detections

# Extrahieren der Ausgaben aus dem Tensor
# Annahme: np_outputs[0][0] = boxes, np_outputs[1][0] = scores, np_outputs[2][0] = classes
boxes = np_outputs[0][0]
scores = np_outputs[1][0]
classes = np_outputs[2][0]

last_detections = []
for box, score, category in zip(boxes, scores, classes):
if score > threshold:
detection = Detection(box, category, score, metadata)
last_detections.append(detection)
print(f"Erkennung: Kategorie={category}, Konfidenz={score}, Box={detection.box}")

return last_detections

@lru_cache
def get_labels():
with open('labels.txt', 'r') as f:
labels = f.read().splitlines()
return labels

def draw_detections(image, detections):
"""Zeichnet die Erkennungen auf das gegebene Bild."""
labels = get_labels()
global detection_counter # Zugriff auf den globalen Zaehler
for detection in detections:
x, y, w, h = detection.box
label = f"{labels[int(detection.category)]} ({detection.conf:.2f})"

print(f"Zeichne Bounding Box: ({x}, {y}, {w}, {h}) mit Label: {label}")

# Textgroesse und Position berechnen
(text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1.0, 2)
text_x = x + 5
text_y = y + 25

# Hintergrundrechteck fuer Text zeichnen
cv2.rectangle(image,
(text_x, text_y - text_height - baseline),
(text_x + text_width, text_y + baseline),
(255, 255, 255), # Hintergrundfarbe (weiss)
cv2.FILLED)

# Text auf das Bild zeichnen
cv2.putText(image, label, (text_x, text_y),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)

# Erkennungsbox zeichnen
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), thickness=3)

# Zaehler erhoehen und ausgeben
detection_counter += 1
print(f"Anzahl der Erkennungen bisher: {detection_counter}")

def process_request(request):
"""Verarbeitet eine einzelne Anfrage: Zeichnet Detections und speichert das Bild bei Bedarf."""
global last_results, frame_counter
frame_counter += 1 # Frame-Zaehler erhoehen
metadata = request.get_metadata()
last_results = parse_detections(metadata)

with MappedArray(request, "main") as m:
image = m.array.copy() # Sicherstellen, dass wir eine Kopie haben

if last_results:
print(f"{len(last_results)} Objekte erkannt.")
draw_detections(image, last_results)

# Bild speichern
if not os.path.exists('detect'):
os.makedirs('detect')

# Verwenden des Frame-Zaehlers im Dateinamen
filename = os.path.join('detect', f'detection_{frame_counter}.jpg')
cv2.imwrite(filename, image)
print(f"Bild mit Erkennungen gespeichert unter: {filename}")
else:
print("Keine Objekte erkannt.")

# Keine Live-Anzeige

return True

if __name__ == "__main__":

print("Hello World")

# Netzwerk-Parameter einstellen
intrinsics = imx500.network_intrinsics
if not intrinsics:
intrinsics = NetworkIntrinsics()
intrinsics.task = "object detection"
elif intrinsics.task != "object detection":
print("Das Netzwerk ist nicht fuer Objekterkennung konfiguriert.")
exit()

# Standardwerte fuer Intrinsics setzen
intrinsics.threshold = 0.55 # Erkennungsschwelle
intrinsics.iou = 0.65 # IOU-Schwelle
intrinsics.max_detections = 10 # Maximale Anzahl von Erkennungen pro Frame
intrinsics.ignore_dash_labels = False
intrinsics.postprocess = "" # Anpassen, falls Ihr Modell dies erfordert
intrinsics.preserve_aspect_ratio = False # Bei Bedarf auf True setzen

intrinsics.labels = get_labels()
intrinsics.update_with_defaults()

# Kamera-Konfiguration mit Aufloesung 1280x1024 und 5 fps erstellen
config = picam2.create_still_configuration(
main={"size": (1280, 1024)},
controls={"FrameRate": 5},
buffer_count=12
)
imx500.show_network_fw_progress_bar()
picam2.start(config, show_preview=False) # Keine Live-Anzeige

if intrinsics.preserve_aspect_ratio:
imx500.set_auto_aspect_ratio()

try:
while True:
# Naechste Anfrage abrufen
request = picam2.capture_request()
continue_loop = process_request(request)
request.release()
if not continue_loop:
break
except KeyboardInterrupt:
print("Programm beendet durch Benutzer.")
finally:
picam2.stop()

Statistics: Posted by JochenSchmitz — Mon Dec 02, 2024 3:53 pm



Viewing all articles
Browse latest Browse all 4151

Trending Articles