-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scanner Endpoint, minimal error checking
- Loading branch information
1 parent
e174bcb
commit df52363
Showing
7 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Request, Response, NextFunction, response } from "express"; | ||
import ScannerService from "../services/scanner.service.js"; | ||
|
||
class ScannerController { | ||
constructor(private service: ScannerService) { | ||
this.service = service; | ||
} | ||
postMaterials = async (//must send image as tag "image" and value base64 string. Returns JSON with the percentage and material of each material found on the tag | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<Response<any, Record<string, any>> | void> => { | ||
try { | ||
console.log("postMaterials called"); | ||
let materials = await (async () => { | ||
var str; | ||
str = await this.service.getMaterials(await this.service.getTextFromImage(req.body.image)); | ||
// console.log("Fuck: ", await str); | ||
return str; | ||
})(); | ||
console.log("materials: ",materials); | ||
return res.status(201).json(JSON.parse(materials)); | ||
} catch (e) { | ||
console.log("Error: ", e); | ||
next(e); | ||
} | ||
}; | ||
} | ||
|
||
export default ScannerController; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Router } from "express"; | ||
import ScannerController from "../controllers/scanner.controller.js"; | ||
import ScannerService from "../services/scanner.service.js"; | ||
|
||
const scannerRouter = Router(); | ||
|
||
const scannerController = new ScannerController(new ScannerService()); | ||
|
||
scannerRouter.post("/", scannerController.postMaterials); | ||
|
||
export default scannerRouter; |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Tesseract from 'tesseract.js'; //Javascript OCR | ||
|
||
type Tag = { | ||
material: string; | ||
percentage: string; | ||
} | ||
|
||
class ScannerService{ | ||
|
||
getTextFromImage = async(imagePath: string): Promise<string> => { | ||
console.log("getTextFromImage called"); | ||
// return "20% Cotton 40% Shit 40% Rizz"; | ||
return new Promise<string>((resolve, reject) => { | ||
console.log("Test1"); | ||
Tesseract.recognize(imagePath,'eng',{ | ||
logger: info => console.log(info), // optional logger function | ||
}) | ||
.then(({ data: { text } }) => { | ||
console.log("Test2"); | ||
resolve(text); | ||
console.log("text: ",text); | ||
return text; | ||
}) | ||
.catch((error) => { | ||
console.log("Test2.5"); | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
|
||
getMaterials = async(txt: string): Promise<string> => { | ||
console.log("getMaterials called"); | ||
const regex = /(100|\d{1,2})% *(\b\w+\b)/g; | ||
let m; | ||
let scannedTags: Tag[] = []; | ||
do { | ||
m = regex.exec(txt); | ||
if (m){ | ||
scannedTags.push({ | ||
material: m[2], | ||
percentage: m[1] | ||
}); | ||
} | ||
}while (m); | ||
console.log(JSON.stringify(scannedTags)); | ||
return JSON.stringify(scannedTags); | ||
} | ||
} | ||
|
||
|
||
export default ScannerService; |