Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mayarajan3 committed Nov 21, 2024
2 parents ba883cd + 9588714 commit 0f606dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
2 changes: 1 addition & 1 deletion extensions/src/doodlebot/Doodlebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export default class Doodlebot {
while (true) {
try {
console.log("before 1");
let lineData = await this.detector.detectLine(); // Await works here
let lineData = await this.detector.detectLine(this); // Await works here
console.log("after 1");

// Process line data
Expand Down
77 changes: 21 additions & 56 deletions extensions/src/doodlebot/LineDetection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { endpoint, port } from "./enums";
import Doodlebot from "./Doodlebot";

const debug = {
info: (msg: string, ...args: any[]) => console.log(`[LineDetector] ${msg}`, ...args),
Expand All @@ -9,7 +10,6 @@ const debug = {
};

export class LineDetector {
private raspberryPiIp: string;
private width: number;
private height: number;
private canvas: HTMLCanvasElement;
Expand All @@ -22,13 +22,10 @@ export class LineDetector {
private isProcessing: boolean = false;
private lastProcessTime: number = 0;
private readonly MIN_PROCESS_INTERVAL = 100;
private imageElement: HTMLImageElement | null = null;
private imageUrl: string | null = null;

constructor(raspberryPiIp: string, width = 640, height = 480) {
constructor(private raspberryPiIp: string, width = 640, height = 480) {
debug.info('Initializing LineDetector', { raspberryPiIp, width, height });

this.raspberryPiIp = raspberryPiIp;
this.width = width;
this.height = height;
this.canvas = document.createElement('canvas');
Expand Down Expand Up @@ -77,56 +74,25 @@ export class LineDetector {
: [];
}

private loadImage(url: string, timeoutMs: number = 5000): Promise<HTMLImageElement> {
if (this.imageElement && this.imageUrl === url) {
debug.info('Reusing existing image');
return Promise.resolve(this.imageElement);
}
private async getImage(doodlebot: Doodlebot): Promise<HTMLImageElement> {
debug.time('getImage');
debug.info('Getting image stream from Doodlebot');

debug.time('loadImage');
debug.info('Loading image from URL:', url);

return new Promise((resolve, reject) => {
if (!this.imageElement) {
this.imageElement = new Image();
this.imageElement.crossOrigin = 'anonymous';
}

const timeoutId = setTimeout(() => {
debug.error('Image load timeout after', timeoutMs, 'ms');
cleanup();
reject(new Error('Image load timeout'));
}, timeoutMs);

const cleanup = () => {
this.imageElement!.onload = null;
this.imageElement!.onerror = null;
clearTimeout(timeoutId);
};

this.imageElement.onload = () => {
debug.info('Image loaded successfully');
debug.timeEnd('loadImage');
cleanup();
this.imageUrl = url;
resolve(this.imageElement!);
};

this.imageElement.onerror = (error) => {
debug.error('Failed to load image:', error);
cleanup();
this.imageUrl = null;
reject(new Error('Failed to load image'));
};

if (this.imageUrl !== url) {
this.imageUrl = url;
this.imageElement.src = url;
}
});
try {
const image = await doodlebot.getImageStream();
if (!image) {
throw new Error('Failed to get image stream from Doodlebot');
}
debug.info('Successfully got image from Doodlebot');
debug.timeEnd('getImage');
return image;
} catch (error) {
debug.error('Error getting image stream:', error);
throw error;
}
}

async detectLine(retries: number = 3): Promise<number[][]> {
async detectLine(doodlebot: Doodlebot, retries: number = 3): Promise<number[][]> {
const now = Date.now();

if (this.isProcessing) {
Expand All @@ -147,8 +113,7 @@ export class LineDetector {
try {
debug.info(`Detection attempt ${attempt + 1}/${retries}`);

const imageUrl = `http://${this.raspberryPiIp}:${port.camera}/${endpoint.video}`;
const image = await this.loadImage(imageUrl);
const image = await this.getImage(doodlebot);

debug.info('Clearing canvas and drawing new image');
this.ctx.clearRect(0, 0, this.width, this.height);
Expand Down Expand Up @@ -240,8 +205,8 @@ export class LineDetector {
}
}

export function createLineDetector(raspberryPiIp: string): () => Promise<number[][]> {
export function createLineDetector(raspberryPiIp: string): (doodlebot: Doodlebot) => Promise<number[][]> {
debug.info('Creating new LineDetector instance');
const detector = new LineDetector(raspberryPiIp);
return () => detector.detectLine();
return (doodlebot: Doodlebot) => detector.detectLine(doodlebot);
}

0 comments on commit 0f606dd

Please sign in to comment.