diff --git a/db/dataSource.ts b/db/dataSource.ts new file mode 100644 index 0000000..ed0c76f --- /dev/null +++ b/db/dataSource.ts @@ -0,0 +1,22 @@ +import { DataSource } from "typeorm"; +import { Logger } from "./entities/logger.js"; + +const dataSource = new DataSource({ + type: 'mysql', + host: 'geeky-scripters.ckxcq2pvrc9s.eu-west-2.rds.amazonaws.com', + port: 3306, + username: 'root', + password: '123456789', + database: 'Hackathon', + entities: [Logger], + synchronize: true, + logging: true +}); + +dataSource.initialize().then(() => { + console.log("Connected to DB!"); +}).catch(err => { + console.error('Failed to connect to DB: ' + err); +}); + +export default dataSource; \ No newline at end of file diff --git a/db/entities/logger.ts b/db/entities/logger.ts new file mode 100644 index 0000000..9ee0b51 --- /dev/null +++ b/db/entities/logger.ts @@ -0,0 +1,19 @@ +import { BaseEntity, Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; + +@Entity() +export class Logger extends BaseEntity { + @PrimaryGeneratedColumn('uuid') + id: string; + + @CreateDateColumn({ + type: 'timestamp', + default: () => "CURRENT_TIMESTAMP(6)" + }) + createdAt: Date; + + @Column({length:5000, nullable:false}) + result:string + + @Column({ nullable:false}) + imgPath:string +} \ No newline at end of file diff --git a/images/meme.jpg b/images/meme.jpg deleted file mode 100644 index bd6062b..0000000 Binary files a/images/meme.jpg and /dev/null differ diff --git a/index.ts b/index.ts index e3190c9..787f6cb 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,11 @@ +import express from 'express' import aws from 'aws-sdk' +import multer from 'multer'; +import fs from 'fs' +import ObjectRouter from './routes/ObjectLable.js' +import celebrityRouter from './routes/celebrityFaces.js' +import textRouter from './routes/text.js' +import db from './db/dataSource.js' aws.config.update({ region: 'us-west-2', @@ -6,12 +13,95 @@ aws.config.update({ secretAccessKey: 't0XfxHED8l4G2nLfyzgNrcZgq1uSFAQKk7FV4Hef' }) -const s3 = new aws.S3(); -s3.putObject({Bucket:"sarah-test-medium",Key:"images/meme.jpg"},(err,sucsess)=>{ - if (err){ - console.log(err) - }else{ - console.log(sucsess) +const PORT = 3000; +const app = express() + + +app.use(express.json()) +app.use('/ObjectLable',ObjectRouter) +app.use('/celebrityFaces',celebrityRouter) +app.use('/textDetection',textRouter) + +const storage = multer.diskStorage({ + destination: (req, file, callback) => { + callback(null, 'uploads/'); + }, + filename: (req, file, callback) => { + callback(null, file.originalname) } - }) \ No newline at end of file +}); +const upload = multer({ storage }); + + + +app.listen(PORT, () => { + console.log(`App is running and Listening on port ${PORT}`); + db.initialize() +}); + + +// app.post('/upload', upload.single('file'), async (req, res) => { +// if (!req.file) { +// return res.status(500).send("Failed to upload file!"); +// } + +// const params = { +// Image: { +// Bytes: req.file.buffer, // Use the buffer from the uploaded file +// }, +// }; + +// try { +// const data = await rekognition.detectLabels(params).promise(); +// console.log('Detected labels:', data.Labels); +// const dataLabels = data.Labels.map(label => label.Name); // Extract label names +// res.status(201).json({ labels: dataLabels }); +// } catch (err) { +// console.error('Error detecting labels:', err); +// res.status(500).send("Failed to detect labels"); +// } +// }); + + + + + + + + +// app.get('/file', (req:any, res) => { +// const fileName = req.query.name?.toString() || ''; +// try { +// const data = fs.readFileSync('uploads/' + fileName, 'utf-8'); +// const JSONData = JSON.parse(data) as any[]; +// console.log("-----------------------"); +// console.log(JSONData[0].author); +// console.log("-----------------------"); +// res.send(JSONData); +// } catch (error) { +// console.error(error); +// res.status(500).send("Something went wrong"); +// } +// }); + + + + +// const s3 = new aws.S3(); + +// const rekognition = new aws.Rekognition(); + +// const params = { +// Image: { +// S3Object: { +// Bucket: 'sarah-test-medium', +// Name: 'images/meme.jpg', +// }, +// }, +// }; + +// rekognition.detectFaces(params, (err, data) => { +// if (err) console.error(err); +// else console.log(data); +// }); \ No newline at end of file diff --git a/routes/ObjectLable.ts b/routes/ObjectLable.ts new file mode 100644 index 0000000..80f047f --- /dev/null +++ b/routes/ObjectLable.ts @@ -0,0 +1,54 @@ +import aws from 'aws-sdk' +import express from 'express' +import multer from 'multer'; +import fs from 'fs' +import rekognition from 'aws-sdk/clients/rekognition.js'; +import { Any } from 'typeorm'; +import { Logger } from '../db/entities/logger.js'; + +const storage = multer.diskStorage({ + destination: (req, file, callback) => { + callback(null, 'uploads/'); + }, + filename: (req, file, callback) => { + callback(null, Date.now() + '-' + file.originalname) + } +}); +const upload = multer({ storage }); + +const router = express.Router() +router.post('/', upload.single('file'), async (req, res) => { + if (!req.file) { + res.status(500).send("Failed Upload File!"); + return; + } + const imagePath = 'uploads/dad.jpeg'; + const imageBuffer = fs.readFileSync(imagePath); + const params = { + Image: { + Bytes: imageBuffer, + }, + }; + let dataLable; + const rekognition = new aws.Rekognition(); + + try { + const data = await rekognition.detectLabels(params).promise(); + console.log('Detected labels:', data.Labels); + const dataLabels = data.Labels?.map(label => label.Name); // Extract label names + + const newLogger = new Logger(); + newLogger.result = await JSON.stringify(dataLabels); + newLogger.imgPath = imagePath; + await newLogger.save() + + res.status(201).json({ labels: dataLabels }); + } catch (err) { + console.error('Error detecting labels:', err); + res.status(500).send("Failed to detect labels"); + } + +}); + + +export default router diff --git a/routes/celebrityFaces.ts b/routes/celebrityFaces.ts new file mode 100644 index 0000000..683c9e0 --- /dev/null +++ b/routes/celebrityFaces.ts @@ -0,0 +1,57 @@ +import aws from 'aws-sdk'; +import express from 'express'; +import multer from 'multer'; +import fs from 'fs'; +import { Logger } from '../db/entities/logger.js'; + +const storage = multer.diskStorage({ + destination: (req, file, callback) => { + callback(null, 'uploads/'); + }, + filename: (req, file, callback) => { + callback(null,file.originalname); + }, +}); +const upload = multer({ storage }); + +const router = express.Router(); + +router.post('/', upload.single('file'), async (req, res) => { + if (!req.file) { + res.status(500).send("Failed Upload File!"); + return; + } + + const imagePath = req.file.path; // Use the uploaded image path + const imageBuffer = fs.readFileSync(imagePath); + + const rekognition = new aws.Rekognition(); + + const params = { + Image: { + Bytes: imageBuffer, + }, + }; + + try { + const data = await rekognition.recognizeCelebrities(params).promise(); + console.log('Detected celebrities:', data.CelebrityFaces); + + const celebrities = data.CelebrityFaces?.map((celebrity) => ({ + name: celebrity.Name, + confidence: celebrity.MatchConfidence, + })); + + const newLogger= new Logger(); + newLogger.result= await JSON.stringify(celebrities); + newLogger.imgPath=imagePath; + await newLogger.save() + + res.status(201).json({ celebrities }); + } catch (err) { + console.error('Error recognizing celebrities:', err); + res.status(500).send("Failed to recognize celebrities"); + } +}); + +export default router; diff --git a/routes/text.ts b/routes/text.ts new file mode 100644 index 0000000..86803c7 --- /dev/null +++ b/routes/text.ts @@ -0,0 +1,58 @@ +import aws from 'aws-sdk'; +import express from 'express'; +import multer from 'multer'; +import fs from 'fs'; +import { Logger } from '../db/entities/logger.js'; + +const storage = multer.diskStorage({ + destination: (req, file, callback) => { + callback(null, 'uploads/'); + }, + filename: (req, file, callback) => { + callback(null, Date.now() + '-' + file.originalname); + }, +}); +const upload = multer({ storage }); + +const router = express.Router(); + +router.post('/', upload.single('file'), async (req, res) => { + if (!req.file) { + res.status(500).send("Failed Upload File!"); + return; + } + + const imagePath = req.file.path; // Use the uploaded image path + const imageBuffer = fs.readFileSync(imagePath); + + const rekognition = new aws.Rekognition(); + + const params = { + Image: { + Bytes: imageBuffer, + }, + }; + + try { + const data = await rekognition.detectText(params).promise(); + console.log('Detected text:', data.TextDetections); + + const detectedText = data.TextDetections?.map((textDetection) => ({ + detectedText: textDetection.DetectedText, + confidence: textDetection.Confidence, + boundingBox: textDetection.Geometry?.BoundingBox, + })); + + const newLogger = new Logger(); + newLogger.result = await JSON.stringify(detectedText); + newLogger.imgPath = imagePath; + await newLogger.save() + + res.status(201).json({ detectedText }); + } catch (err) { + console.error('Error detecting text:', err); + res.status(500).send("Failed to detect text"); + } +}); + +export default router; diff --git a/tsconfig.json b/tsconfig.json index 8214235..2802c0b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "ES6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2018", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ diff --git a/uploads/1693647646663-text.jpeg b/uploads/1693647646663-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693647646663-text.jpeg differ diff --git a/uploads/1693647819657-text.jpeg b/uploads/1693647819657-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693647819657-text.jpeg differ diff --git a/uploads/1693649144587-dad.jpeg b/uploads/1693649144587-dad.jpeg new file mode 100644 index 0000000..a119873 Binary files /dev/null and b/uploads/1693649144587-dad.jpeg differ diff --git a/uploads/1693649201460-text.jpeg b/uploads/1693649201460-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693649201460-text.jpeg differ diff --git a/uploads/1693649280286-text.jpeg b/uploads/1693649280286-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693649280286-text.jpeg differ diff --git a/uploads/1693649345091-text.jpeg b/uploads/1693649345091-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693649345091-text.jpeg differ diff --git a/uploads/1693649391315-text.jpeg b/uploads/1693649391315-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693649391315-text.jpeg differ diff --git a/uploads/1693650428947-text.jpeg b/uploads/1693650428947-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693650428947-text.jpeg differ diff --git a/uploads/1693650447851-text.jpeg b/uploads/1693650447851-text.jpeg new file mode 100644 index 0000000..8c6b340 Binary files /dev/null and b/uploads/1693650447851-text.jpeg differ diff --git a/uploads/Harry_Potter.jpg b/uploads/Harry_Potter.jpg new file mode 100644 index 0000000..e93aba9 Binary files /dev/null and b/uploads/Harry_Potter.jpg differ diff --git a/uploads/dad.jpeg b/uploads/dad.jpeg new file mode 100644 index 0000000..a119873 Binary files /dev/null and b/uploads/dad.jpeg differ