-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77ac62e
commit 6a73e15
Showing
4 changed files
with
125 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
// import { createCanvas, loadImage } from 'canvas'; | ||
// import { endpoint, port } from "./enums"; | ||
|
||
// export class LineDetector { | ||
// private lastDetectedLine: number[][] = []; | ||
// private isProcessing = false; | ||
// private canvas: any; | ||
// private ctx: any; | ||
|
||
// constructor(private raspberryPiIp: string, private width = 640, private height = 480) { | ||
// this.canvas = createCanvas(this.width, this.height); | ||
// this.ctx = this.canvas.getContext('2d'); | ||
// } | ||
|
||
// async detectLine(): Promise<number[][]> { | ||
// if (this.isProcessing) return this.lastDetectedLine; | ||
// this.isProcessing = true; | ||
|
||
// try { | ||
// const image = await loadImage(`http://${this.raspberryPiIp}:${port.camera}/${endpoint.video}`); | ||
// this.ctx.drawImage(image, 0, 0, this.width, this.height); | ||
// const imageData = this.ctx.getImageData(0, 0, this.width, this.height); | ||
// const lineCoordinates = this.processImageData(imageData); | ||
|
||
// if (lineCoordinates.length > 0) { | ||
// this.lastDetectedLine = lineCoordinates; | ||
// } | ||
|
||
// return this.lastDetectedLine; | ||
// } catch (error) { | ||
// console.error('Error detecting line:', error); | ||
// return this.lastDetectedLine; | ||
// } finally { | ||
// this.isProcessing = false; | ||
// } | ||
// } | ||
|
||
// private processImageData(imageData: ImageData): number[][] { | ||
// const lineCoordinates: number[][] = []; | ||
// const threshold = 50; | ||
|
||
// for (let y = 0; y < this.height; y++) { | ||
// for (let x = 0; x < this.width; x++) { | ||
// const index = (y * this.width + x) * 4; | ||
// const r = imageData.data[index]; | ||
// const g = imageData.data[index + 1]; | ||
// const b = imageData.data[index + 2]; | ||
// if (r < threshold && g < threshold && b < threshold) { | ||
// lineCoordinates.push([x, y]); | ||
// } | ||
// } | ||
// } | ||
// return lineCoordinates.sort((a, b) => a[1] - b[1]); | ||
// } | ||
// } | ||
|
||
// export function createLineDetector(raspberryPiIp: string): () => Promise<number[][]> { | ||
// const detector = new LineDetector(raspberryPiIp); | ||
// return () => detector.detectLine(); | ||
// } | ||
import { endpoint, port } from "./enums"; | ||
import cv from '@u4/opencv4nodejs'; | ||
import axios from 'axios'; | ||
|
||
export class LineDetector { | ||
private lastDetectedLine: number[][] = []; | ||
private isProcessing = false; | ||
|
||
constructor(private raspberryPiIp: string, private width = 640, private height = 480) {} | ||
|
||
async detectLine(): Promise<number[][]> { | ||
if (this.isProcessing) return this.lastDetectedLine; | ||
this.isProcessing = true; | ||
|
||
try { | ||
// Get image from endpoint | ||
const response = await axios.get( | ||
`http://${this.raspberryPiIp}:${port.camera}/${endpoint.video}`, | ||
{ responseType: 'arraybuffer' } | ||
); | ||
|
||
// Convert response to cv Mat | ||
const buffer = Buffer.from(response.data); | ||
let mat = cv.imdecode(buffer); | ||
|
||
// Resize if needed | ||
if (mat.cols !== this.width || mat.rows !== this.height) { | ||
mat = mat.resize(this.height, this.width); | ||
} | ||
|
||
// Convert to grayscale and apply threshold | ||
const gray = mat.cvtColor(cv.COLOR_BGR2GRAY); | ||
const blurred = gray.gaussianBlur(new cv.Size(5, 5), 0); | ||
const thresh = blurred.threshold(0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU); | ||
|
||
// Find contours | ||
const contours = thresh.findContours(cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE); | ||
// Get the largest contour (assuming it's the line) | ||
const sortedContours = contours.sort((c1, c2) => c2.area - c1.area); | ||
if (sortedContours.length > 0) { | ||
// Convert contour points to coordinate array | ||
const coordinates = sortedContours[0].getPoints().map(point => [point.x, point.y]); | ||
this.lastDetectedLine = coordinates; | ||
} | ||
|
||
return this.lastDetectedLine; | ||
} catch (error) { | ||
console.error('Error detecting line:', error); | ||
return this.lastDetectedLine; | ||
} finally { | ||
this.isProcessing = false; | ||
} | ||
} | ||
} | ||
|
||
export function createLineDetector(raspberryPiIp: string): () => Promise<number[][]> { | ||
const detector = new LineDetector(raspberryPiIp); | ||
return () => detector.detectLine(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters