This repository has been archived by the owner on Aug 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.js
97 lines (88 loc) · 2.81 KB
/
params.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs')
const cv = require('opencv4nodejs')
const path = require('path')
// GLOBAL VARS
const debugFolder = 'debug'
// CLASSES
class HOGSVM4detection {
constructor (
hogFile = 'trained_models/hog_detector.yaml',
svmFile = 'trained_models/svm_detector.yaml',
w = 64, h = 32,
hog = new cv.HOGDescriptor(),
svm = new cv.SVM()) {
this.hogFile = hogFile
this.svmFile = svmFile
this.size = {
width: w,
height: h
}
this.hog = hog
this.svm = svm
}
loadFiles () {
this.hog.load(this.hogFile)
this.svm.load(this.svmFile)
}
}
class HOGSVM {
constructor (
hogFile = 'trained_models/hog_classifier.yaml',
svmFile = 'trained_models/svm_classifier.yaml',
classesFile = 'trained_models/classes.txt',
size = 32,
hog = new cv.HOGDescriptor(),
svm = new cv.SVM()) {
this.hogFile = hogFile
this.svmFile = svmFile
this.classesFile = classesFile
this.classes = fs.readFileSync(classesFile).toString().split('\n')
this.size = size
this.hog = hog
this.svm = svm
}
loadFiles () {
this.hog.load(this.hogFile)
this.svm.load(this.svmFile)
}
}
// FUNCTIONS
function showResult (src = new cv.Mat(), tilesArr = [], debug = false) {
tilesArr.forEach(row => {
row.forEach(tile => {
const x = tile.rect.x
const y = tile.rect.y
const color = new cv.Vec3(255 * Math.random(), 255 * Math.random(), 255 * Math.random())
src.drawRectangle(tile.rect, color, 6)
src.putText(tile.tactode.piece, new cv.Point2(x, y), 2, 1.2, color, cv.LINE_4, 6)
})
})
cv.imshow('result', src.rescale(0.3))
cv.waitKey(5000) // Wait 5000 ms or click any key to close the window
cv.destroyAllWindows()
if (debug) {
cv.imwrite(path.join(debugFolder, 'result.jpg'), src)
}
}
function printResult (tiles = [], numberOfTiles = -1) {
const numTiles = numberOfTiles
let resultStr = ` Number of tiles found: ${numTiles}\n\n `
tiles.forEach(line => {
line.forEach(piece => {
resultStr += `| ${piece.tactode.piece} `
})
resultStr += '|\n '
})
console.log(resultStr)
}
function getCurrentTime () {
const date = new Date()
let dayOfMonth = date.getDate(); dayOfMonth = (dayOfMonth < 10) ? `0${dayOfMonth}` : dayOfMonth
let month = date.getMonth(); month = (month < 10) ? `0${month + 1}` : month + 1
let year = date.getFullYear(); year = (year < 10) ? `0${year}` : year
let hours = date.getHours(); hours = (hours < 10) ? `0${hours}` : hours
let minutes = date.getMinutes(); minutes = (minutes < 10) ? `0${minutes}` : minutes
let seconds = date.getSeconds(); seconds = (seconds < 10) ? `0${seconds}` : seconds
return `${dayOfMonth}/${month}/${year} ${hours}:${minutes}:${seconds}`
}
module.exports = { debugFolder, HOGSVM4detection, HOGSVM, printResult, showResult, getCurrentTime }