Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner Endpoint, minimal error checking #54

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added backend/eng.traineddata
Binary file not shown.
110 changes: 109 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@prisma/client": "^5.6.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2"
"express": "^4.18.2",
"tesseract.js": "^5.0.4"
},
"devDependencies": {
"@types/chai": "^4.3.10",
Expand Down
30 changes: 30 additions & 0 deletions backend/src/controllers/scanner.controller.ts
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;
5 changes: 4 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import errorHandler from "./middlewares/error.middleware.js";
import { PrismaClient } from "@prisma/client";
import algorithmRouter from "./routes/algorithm.routes.js";
import productRouter from "./routes/product.routes.js";
import scannerRouter from "./routes/scanner.routes.js";

export const app: Express = express();
export const prisma = new PrismaClient();

app.use(express.json());
app.use(express.json({ limit: '50mb' }));

app.use(cors());

app.use("/api/v1/scanner", scannerRouter);

app.use("/api/v1/algorithm", algorithmRouter);

app.use("/api/v1/products", productRouter);
Expand Down
11 changes: 11 additions & 0 deletions backend/src/routes/scanner.routes.ts
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;
51 changes: 51 additions & 0 deletions backend/src/services/scanner.service.ts
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;
Loading