kbennett2000 / rc-lap-timer

Free, easy to use lap timing system for casual RC racing in your basement, parking lot, or backyard!
0 stars 0 forks source link

Visual detection of cars #3

Open kbennett2000 opened 2 days ago

kbennett2000 commented 2 days ago

Right now the app just checks for motion to record laps. Can the app be extended to recognize a car based on an ArUco Marker?

https://claude.ai/chat/0f1b2052-089e-4834-86ed-25df68f60b65

To implement this:

import React, { useEffect, useRef, useState } from 'react';
import cv from '@techstark/opencv-js';

const ArucoDetector = () => {
  const videoRef = useRef(null);
  const canvasRef = useRef(null);
  const [detectedIds, setDetectedIds] = useState([]);

  useEffect(() => {
    const setupCamera = async () => {
      const stream = await navigator.mediaDevices.getUserMedia({ video: true });
      videoRef.current.srcObject = stream;
    };

    setupCamera();
    return () => {
      if (videoRef.current?.srcObject) {
        videoRef.current.srcObject.getTracks().forEach(track => track.stop());
      }
    };
  }, []);

  const detectMarkers = () => {
    const video = videoRef.current;
    const canvas = canvasRef.current;
    const context = canvas.getContext('2d');

    // Match canvas size to video
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;

    // Draw current frame
    context.drawImage(video, 0, 0, canvas.width, canvas.height);

    // Convert to OpenCV format
    const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
    const mat = cv.matFromImageData(imageData);

    // Setup ArUco detector
    const dictionary = new cv.aruco.Dictionary(cv.aruco.DICT_4X4_50);
    const parameters = new cv.aruco.DetectorParameters();

    // Detect markers
    const corners = new cv.Mat();
    const ids = new cv.Mat();
    cv.aruco.detectMarkers(mat, dictionary, corners, ids, parameters);

    if (ids.rows > 0) {
      // Draw detected markers
      cv.aruco.drawDetectedMarkers(mat, corners, ids);

      // Update detected IDs state
      const detectedArray = Array.from(ids.data32S);
      setDetectedIds(detectedArray);
    }

    // Display result
    cv.imshow(canvas, mat);

    // Cleanup
    mat.delete();
    corners.delete();
    ids.delete();
  };

  return (
    <div className="w-full max-w-2xl mx-auto p-4">
      <video 
        ref={videoRef}
        className="w-full h-auto mb-4"
        autoPlay
        playsInline
        onPlay={() => {
          setInterval(detectMarkers, 50); // 20fps detection
        }}
      />
      <canvas 
        ref={canvasRef}
        className="w-full h-auto"
      />
      <div className="mt-4">
        <h3 className="text-lg font-semibold">Detected Cars:</h3>
        <ul>
          {detectedIds.map(id => (
            <li key={id} className="py-1">Car #{id}</li>
          ))}
        </ul>
      </div>
    </div>
  );
};

export default ArucoDetector;
kbennett2000 commented 1 day ago

Added ArUco Marker detection to code, it doesn't work so well if something is moving fast. Scrapping this idea for now...

kbennett2000 commented 12 hours ago

Let's try this with multicolor LEDs!! -- https://claude.ai/chat/96a49907-20ac-420c-b3da-bc18f3570831