diff --git a/imageProcessor.js b/imageProcessor.js new file mode 100644 index 0000000..6bca9bf --- /dev/null +++ b/imageProcessor.js @@ -0,0 +1,7 @@ +module.exports = function(image){ + //greyscale + + //blur to remove noise + //canny edges + //dilate +} \ No newline at end of file diff --git a/index.js b/index.js index 53c439e..c4b905b 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ const path = require('path') const cliProgress = require('cli-progress'); const multer = require('multer'); const express = require('express'); - +const tesseract = require('node-tesseract'); const app = express(); const upload = multer({ @@ -19,8 +19,28 @@ app.post('/classify', upload.single("file"), (req, res) => { const tempPath = req.file.path; console.log(tempPath); classifier.predict(tempPath).then(async (result) => { - await fs.unlink(tempPath) - res.status(200).json(result); + const readable = ['VIN', 'Data Plates', 'Dash Number', 'Door Tag']; + if(readable.includes(result.label)){ + tesseract.process(path.resolve(__dirname, tempPath),{}, async (err, text) => { + result.processedText = text; + if(err) { + console.error('Error:', err); + } + + await fs.unlink(tempPath, (err) =>{ if(err) console.error('Unlink Error:', err)}); + res.status(200).json(result); + }) + } else { + console.log('Not readable, continuing'); + await fs.unlink(tempPath, (err) =>{ if(err) console.error('Unlink Error:', err)}); + res.status(200).json(result); + } + + // tesseract.process(tempPath, function(err, text) => { + // console.log() + // }) + + }) // res.status(200).json({message: 'Success'}) }) @@ -56,6 +76,7 @@ async function runAsync() { classifier = await library.create(); const matches = await pGlob('training images/**/*'); + const startTime = new Date().getTime(); console.log(`Building model with ${matches.length} training items...`) const rebuildBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_grey); rebuildBar.start(matches.length); @@ -73,6 +94,10 @@ async function runAsync() { console.log('all examples loaded...'); console.log('saving dataset...') await classifier.save('./export.json'); + const now = new Date().getTime(); + + const elapsed = now - startTime; + console.log(`Time to build: ${elapsed / 1000}s`) } else { classifier = await library.load('./export.json'); } diff --git a/ocrData/eng.traineddata b/ocrData/eng.traineddata new file mode 100644 index 0000000..f4744c2 Binary files /dev/null and b/ocrData/eng.traineddata differ diff --git a/package-lock.json b/package-lock.json index bfd8ad8..b0fe3a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -322,6 +322,11 @@ "get-intrinsic": "^1.0.2" } }, + "canny-edge-detector": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/canny-edge-detector/-/canny-edge-detector-1.0.0.tgz", + "integrity": "sha512-SpewmkHDE1PbJ1/AVAcpvZKOufYpUXT0euMvhb5C4Q83Q9XEOmSXC+yR7jl3F4Ae1Ev6OtQKbFgdcPrOdHjzQg==" + }, "chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -818,6 +823,11 @@ "tensorset": "^1.2.6" } }, + "image-dilate": { + "version": "0.0.114", + "resolved": "https://registry.npmjs.org/image-dilate/-/image-dilate-0.0.114.tgz", + "integrity": "sha512-tsTcbWBmOWkbAHSDgIMb7CmT34+9OwoBA3rGJ+j+XTcEeZIpqOHU9Lgb2N4d5bskGRo9f5ApNvcVb9uJcTksQQ==" + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -982,6 +992,34 @@ "tar": "^4.4.2" } }, + "node-tesseract": { + "version": "0.2.7", + "resolved": "https://registry.npmjs.org/node-tesseract/-/node-tesseract-0.2.7.tgz", + "integrity": "sha512-RKdVdISi78iSiGSmWWF5UpeC59gjO38vLmR5J5akLrnD4AOnoji+UnaLgRP0qXng8FIEiIkW2zIfl4Lmo0Rv0Q==", + "requires": { + "glob": "^5.0.10", + "node-uuid": "^1.4.1" + }, + "dependencies": { + "glob": { + "version": "5.0.15", + "resolved": "https://registry.npmjs.org/glob/-/glob-5.0.15.tgz", + "integrity": "sha512-c9IPMazfRITpmAAKi22dK1VKxGDX9ehhqfABDriL/lzO92xcUKEJPQHrVA/2YHSNFB4iFlykVmWvwo48nr3OxA==", + "requires": { + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "2 || 3", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } + } + }, + "node-uuid": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/node-uuid/-/node-uuid-1.4.8.tgz", + "integrity": "sha512-TkCET/3rr9mUuRp+CpO7qfgT++aAxfDRaalQhwPFzI9BY/2rCDn6OfpZOVggi1AXfTPpfkTrg5f5WQx5G1uLxA==" + }, "nopt": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.3.tgz", diff --git a/package.json b/package.json index 9a42070..32977fa 100644 --- a/package.json +++ b/package.json @@ -9,10 +9,13 @@ "author": "", "license": "ISC", "dependencies": { + "canny-edge-detector": "^1.0.0", "cli-progress": "^3.11.2", "express": "^4.18.2", "glob": "^8.0.3", "image-classifier": "^1.1.0", - "multer": "^1.4.5-lts.1" + "image-dilate": "0.0.114", + "multer": "^1.4.5-lts.1", + "node-tesseract": "^0.2.7" } } diff --git a/test.py b/test.py new file mode 100644 index 0000000..19e9933 --- /dev/null +++ b/test.py @@ -0,0 +1,58 @@ +import cv2 +import numpy as np +import imutils +import pytesseract + +# read image from disk +image = cv2.imread('test13.jpg') +# make it gray +img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +# blur it to remove noise +img = cv2.GaussianBlur(img, (7,7), 0) + +# perform edge detection, then perform a dilation + erosion to +# close gaps in between object edges +edged = cv2.Canny(img, 40, 90) +dilate = cv2.dilate(edged, None, iterations=2) +# perform erosion if necessay, it completely depends on the image +# erode = cv2.erode(dilate, None, iterations=1) + +# create an empty masks +mask = np.ones(img.shape[:2], dtype="uint8") * 255 + +# find contours +cnts = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] +cnts = cnts[0] if imutils.is_cv2() else cnts[1] + +orig = img.copy() +for c in cnts: + # if the contour is not sufficiently large, ignore it + print('contouring...') + if cv2.contourArea(c) < 300: + cv2.drawContours(mask, [c], -1, 0, -1) + + x,y,w,h = cv2.boundingRect(c) + + # filter more contours if nessesary + if(w>h): + cv2.drawContours(mask, [c], -1, 0, -1) +print('contourint done'); +newimage = cv2.bitwise_and(dilate.copy(), dilate.copy(), mask=mask) +img2 = cv2.dilate(newimage, None, iterations=3) +ret2,th1 = cv2.threshold(img2 ,0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) + +# Tesseract OCR on the image +temp = pytesseract.image_to_string(th1) +print(temp) +# Write results on the image +cv2.putText(image, temp, (100,100), cv2.FONT_HERSHEY_SIMPLEX, 1.8, (0,255,255), 3) + +# show the outputs +cv2.imshow('Original image', cv2.resize(image,(640,480))) +cv2.imshow('Dilated', cv2.resize(dilate,(640,480))) +cv2.imshow('New Image', cv2.resize(newimage,(640,480))) +cv2.imshow('Inverted Threshold', cv2.resize(th1,(640,480))) + +cv2.waitKey(0) +cv2.destroyAllWindows() +