Untitled
unknown
plain_text
10 months ago
3.3 kB
5
Indexable
import cv2
from ultralytics import YOLO
import numpy as np
import board
import busio
from adafruit_mlx90640 import MLX90640
import time
# Initialize I2C for MLX90640
i2c = busio.I2C(board.SCL, board.SDA)
mlx = MLX90640(i2c)
#mlx.refresh_rate = MLX90640.RefreshRate.REFRESH_8_HZ
mlx.refresh_rate = 8 # 8 Hz refresh rate
# Initialize the USB camera
camera = cv2.VideoCapture(0) # Use 0 for the default camera, or adjust if needed
if not camera.isOpened():
print("Camera not detected. Exiting...")
exit()
#camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
#camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Load YOLOv8 model
#model = YOLO("yolov8n_ncnn_model") # Use the correct path to your YOLOv8 model
model = YOLO("yolov8n.pt") # Use the correct path to your YOLOv8 model
# MLX90640 configuration
mlx_shape = (24, 32) # MLX90640 thermal resolution
thermal_data = np.zeros(mlx_shape[0] * mlx_shape[1])
prev_time = time.time() # To store the previous time for FPS calculation
while True:
# Capture a frame from the USB camera
ret, frame = camera.read()
if not ret:
print("Failed to capture frame. Exiting...")
break
# Run YOLO model on the captured frame
results = model.predict(frame, stream=False)
# Read temperature data from MLX90640
try:
mlx.getFrame(thermal_data)
thermal_array = np.reshape(thermal_data, mlx_shape)
except ValueError:
print("MLX90640 data error. Skipping this frame...")
continue
annotated_frame = frame.copy()
for detection in results:
# Get bounding box coordinates
x1, y1, x2, y2 = map(int, detection.boxes.xyxy[0].numpy())
# Crop the thermal data to the bounding box region
thermal_x1 = int(x1 * mlx_shape[1] / frame.shape[1])
thermal_y1 = int(y1 * mlx_shape[0] / frame.shape[0])
thermal_x2 = int(x2 * mlx_shape[1] / frame.shape[1])
thermal_y2 = int(y2 * mlx_shape[0] / frame.shape[0])
# Ensure indices are within bounds
thermal_x1 = max(0, min(thermal_x1, mlx_shape[1] - 1))
thermal_y1 = max(0, min(thermal_y1, mlx_shape[0] - 1))
thermal_x2 = max(0, min(thermal_x2, mlx_shape[1] - 1))
thermal_y2 = max(0, min(thermal_y2, mlx_shape[0] - 1))
# Get average temperature in the bounding box region
bbox_thermal_data = thermal_array[thermal_y1:thermal_y2, thermal_x1:thermal_x2]
avg_temperature = np.mean(bbox_thermal_data) if bbox_thermal_data.size > 0 else 0
# Prepare temperature text
temp_text = f"{avg_temperature:.1f} C"
# Draw the bounding box
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Display temperature
cv2.putText(annotated_frame, temp_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Calculate FPS
curr_time = time.time()
fps = 1 / (curr_time - prev_time) # FPS calculation
prev_time = curr_time
# Display FPS in the upper right corner with decimal precision
cv2.putText(annotated_frame, f"FPS: {fps:.2f}", (frame.shape[1] - 120, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
# Display the annotated frame
cv2.imshow("Object Detection With MLX", annotated_frame)
# Exit the program if 'q' is pressed
if cv2.waitKey(1) == ord("q"):
break
# Release the camera and close all windows
camera.release()
cv2.destroyAllWindows()
Editor is loading...
Leave a Comment