-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (83 loc) · 3.08 KB
/
server.js
File metadata and controls
103 lines (83 loc) · 3.08 KB
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
98
99
100
101
102
103
import express from "express";
import asyncHandler from "express-async-handler";
import cors from "cors";
import morgan from "morgan";
import { IndexFacesCommand, SearchFacesByImageCommand, DetectFacesCommand } from "@aws-sdk/client-rekognition";
import { rekognitionClient, COLLECTION_ID } from "./utils/api/rekognition/rekognition.js";
import { firestoreDB } from "./utils/api/firebase/fire.js";
const { PORT = 1729 } = process.env;
const app = express();
app.use(cors());
app.use(morgan("dev"));
app.use(express.json());
app.get("/", (req, res) => {
res.json({ msg: "Hello!" });
});
app.post(
"/saveImage",
asyncHandler(async (req, res) => {
const imageDocRef = firestoreDB.collection("images").doc();
const { image, name } = req.body;
const imageBytes = Buffer.from(image, "base64");
const indexFacesCommand = new IndexFacesCommand({
CollectionId: COLLECTION_ID,
ExternalImageId: imageDocRef.id,
Image: { Bytes: imageBytes },
});
await rekognitionClient.send(indexFacesCommand);
await imageDocRef.set({
collectionId: COLLECTION_ID,
externalImageId: imageDocRef.id,
name: name,
});
res.json({ externalImageId: imageDocRef.id });
})
);
app.post(
"/processImage",
asyncHandler(async (req, res) => {
const imageBytes = Buffer.from(req.body.image, "base64");
const imageCollentionRef = firestoreDB.collection("images");
const searchFacesByImageCommand = new SearchFacesByImageCommand({
CollectionId: COLLECTION_ID,
MaxFaces: 1,
Image: { Bytes: imageBytes },
});
const detectFacesCommand = new DetectFacesCommand({ Image: { Bytes: imageBytes }, Attributes: ["ALL"] });
try {
const faceMatches = await rekognitionClient.send(searchFacesByImageCommand);
const detectFaces = await rekognitionClient.send(detectFacesCommand);
const faceDetailsDocSnapShot = await imageCollentionRef
.doc(faceMatches.FaceMatches[0].Face.ExternalImageId)
.get();
res.json({
match: true,
name: faceDetailsDocSnapShot.data(),
faceMatchData: faceMatches,
faceDetectData: detectFaces,
});
} catch {
res.json({ match: false, msg: "no face found" });
}
})
);
app.post(
"/detectFaces",
asyncHandler(async (req, res) => {
const imageBytes = Buffer.from(req.body.image, "base64");
const detectFacesCommand = new DetectFacesCommand({ Image: { Bytes: imageBytes }, Attributes: ["ALL"] });
try {
const faces = await rekognitionClient.send(detectFacesCommand);
res.json({ ...faces });
} catch (err) {
res.json({ msg: "no faces found in the image" });
}
})
);
app.use((err, req, res, next) => {
console.log(err);
res.json({ msg: err.name });
});
app.listen(PORT, async () => {
console.log(`server is running on port: ${PORT}`);
});