/
index.js
161 lines (134 loc) · 6.15 KB
/
index.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const ImageProcessor = require('./modules/ImageProcessor');
const TensorFlow = require('./modules/TensorFlow');
const RemBg = require('./modules/RemBg');
const ImageMagick = require('./modules/ImageMagick');
const app = express();
const PORT = 3005;
const processedImagesDir = path.join(__dirname, 'processed_images');
const getServerImageUrl = (imageFileName) => `http://localhost:${PORT}/images/${imageFileName}`;
// Create the 'processed_images' folder if it doesn't exist
if (!fs.existsSync(processedImagesDir)) {
fs.mkdirSync(processedImagesDir);
}
app.use(cors()); // Use the cors middleware to enable CORS
app.use(express.json()); // Use built-in express.json() middleware to parse JSON data
app.post('/rembg', async (req, res) => {
const { imageUrl } = req.body;
console.log(imageUrl);
if (!imageUrl) {
return res.status(400).json({ error: 'Image URL not provided' });
}
const existingSkuImage = ImageProcessor.checkIfSkuImageExists(imageUrl, processedImagesDir);
if (existingSkuImage) {
return res.json({ imageUrl: getServerImageUrl(existingSkuImage) });
}
try {
const imageBuffer = await ImageProcessor.fetchImageUrl(imageUrl);
const imageWithOutBg = await RemBg.removeBackground(imageBuffer);
const imageFileName = await ImageProcessor.saveProcessedImage(imageUrl, imageWithOutBg, processedImagesDir);
const imageServeUrl = getServerImageUrl(imageFileName);
res.json({ imageUrl: imageServeUrl });
} catch (error) {
console.error('Error processing image: ', error);
res.status(500).json({ error: 'Error processing image' });
}
});
app.post('/tensorflow', async (req, res) => {
const { imageUrl } = req.body;
console.log(imageUrl);
if (!imageUrl) {
return res.status(400).json({ error: 'Image URL not provided' });
}
try {
const imageBuffer = await ImageProcessor.fetchImageUrl(imageUrl);
const predictions = await TensorFlow.detectObject(imageBuffer);
const imageRembgBuffer = await RemBg.removeBackground(imageBuffer, true);
const croppedImageBuffer = await TensorFlow.cropImage(imageRembgBuffer, predictions, false);
const imageFileName = await ImageProcessor.saveProcessedImage(imageUrl, croppedImageBuffer, processedImagesDir);
const imageServeUrl = getServerImageUrl(imageFileName);
res.json({ imageUrl: imageServeUrl });
} catch (error) {
console.error('Error processing image: ', error);
res.status(500).json({ error: 'Error processing image' });
}
});
app.post('/magick', async (req, res) => {
const { imageUrl } = req.body;
console.log(imageUrl);
if (!imageUrl) {
return res.status(400).json({ error: 'Image URL not provided' });
}
try {
const imageBuffer = await ImageProcessor.fetchImageUrl(imageUrl);
const croppedBuffer = await ImageMagick.cropBottom(imageBuffer);
const imageFileName = ImageProcessor.getNewImageFileName(imageUrl);
const imageFilePath = ImageProcessor.getNewImageFilePath(imageFileName, processedImagesDir);
// Save the cropped image to the specified path
fs.writeFileSync(imageFilePath, croppedBuffer);
const imageServeUrl = getServerImageUrl(imageFileName);
res.json({ imageUrl: imageServeUrl });
} catch (error) {
console.error('Error processing image: ', error);
res.status(500).json({ error: 'Error processing image' });
}
});
// This endpoint combines the smart cropping and the remove of background processes
app.post('/process-image', async (req, res) => {
const { imageUrl } = req.body;
console.log(imageUrl);
if (!imageUrl) {
return res.status(400).json({ error: 'Image URL not provided' });
}
const existingSkuImage = ImageProcessor.checkIfSkuImageExists(imageUrl, processedImagesDir);
if (existingSkuImage) {
return res.json({ imageUrl: getServerImageUrl(existingSkuImage) });
}
try {
const imageBuffer = await ImageProcessor.fetchImageUrl(imageUrl);
const croppedBuffer = await ImageMagick.cropBottom(imageBuffer);
const rembgBuffer = await RemBg.removeBackground(croppedBuffer);
// Save the cropped image to the specified path
const imageFileName = await ImageProcessor.saveProcessedImage(imageUrl, rembgBuffer, processedImagesDir);
const imageServeUrl = getServerImageUrl(imageFileName);
res.json({ imageUrl: imageServeUrl });
} catch (error) {
console.error('Error processing image: ', error);
res.status(500).json({ error: 'Error processing image' });
}
});
// This endpoint combines the smart cropping and the remove of background processes
app.post('/process', async (req, res) => {
const { imageUrl } = req.body;
console.log(imageUrl);
if (!imageUrl) {
return res.status(400).json({ error: 'Image URL not provided' });
}
/*
const existingSkuImage = ImageProcessor.checkIfSkuImageExists(imageUrl, processedImagesDir);
if (existingSkuImage) {
return res.json({ imageUrl: getServerImageUrl(existingSkuImage) });
}
*/
try {
const imageBuffer = await ImageProcessor.fetchImageUrl(imageUrl);
const rembgBuffer = await RemBg.removeBackground(imageBuffer, true);
const croppedBuffer = await ImageMagick.cropBottom(rembgBuffer, 'transparent');
const imageFileName = ImageProcessor.getNewImageFileName(imageUrl);
const imageFilePath = ImageProcessor.getNewImageFilePath(imageFileName, processedImagesDir);
// Save the cropped image to the specified path
fs.writeFileSync(imageFilePath, croppedBuffer);
const imageServeUrl = getServerImageUrl(imageFileName);
res.json({ imageUrl: imageServeUrl });
} catch (error) {
console.error('Error processing image: ', error);
res.status(500).json({ error: 'Error processing image' });
}
});
app.use('/images', express.static(processedImagesDir));
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});