mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
Support HEIC/HEIF (#16)
* Support HEIC/HEIF backup * Storing backup directly from original file from the phone * Directly read and backup video file - Improve performance on video backup
This commit is contained in:
@@ -7,7 +7,6 @@ import { AssetService } from '../../api-v1/asset/asset.service';
|
||||
import { AssetEntity } from '../../api-v1/asset/entities/asset.entity';
|
||||
import { ImageOptimizeProcessor } from './image-optimize.processor';
|
||||
import { AssetOptimizeService } from './image-optimize.service';
|
||||
import { MachineLearningProcessor } from './machine-learning.processor';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -19,18 +18,10 @@ import { MachineLearningProcessor } from './machine-learning.processor';
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: 'machine-learning',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
|
||||
TypeOrmModule.forFeature([AssetEntity]),
|
||||
],
|
||||
providers: [AssetOptimizeService, ImageOptimizeProcessor, MachineLearningProcessor],
|
||||
providers: [AssetOptimizeService, ImageOptimizeProcessor],
|
||||
exports: [AssetOptimizeService],
|
||||
})
|
||||
export class ImageOptimizeModule {}
|
||||
|
||||
@@ -4,16 +4,16 @@ import { Job, Queue } from 'bull';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AssetEntity } from '../../api-v1/asset/entities/asset.entity';
|
||||
import sharp from 'sharp';
|
||||
import fs, { existsSync, mkdirSync } from 'fs';
|
||||
import { existsSync, mkdirSync, readFile } from 'fs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import ffmpeg from 'fluent-ffmpeg';
|
||||
import { Logger } from '@nestjs/common';
|
||||
|
||||
@Processor('optimize')
|
||||
export class ImageOptimizeProcessor {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity) private assetRepository: Repository<AssetEntity>,
|
||||
@InjectQueue('machine-learning') private machineLearningQueue: Queue,
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
|
||||
private configService: ConfigService,
|
||||
) {}
|
||||
|
||||
@@ -32,30 +32,42 @@ export class ImageOptimizeProcessor {
|
||||
mkdirSync(resizeDir, { recursive: true });
|
||||
}
|
||||
|
||||
fs.readFile(savedAsset.originalPath, (err, data) => {
|
||||
readFile(savedAsset.originalPath, async (err, data) => {
|
||||
if (err) {
|
||||
console.error('Error Reading File');
|
||||
}
|
||||
|
||||
sharp(data)
|
||||
.resize(512, 512, { fit: 'outside' })
|
||||
.toFile(resizePath, async (err, info) => {
|
||||
if (err) {
|
||||
console.error('Error resizing file ', err);
|
||||
return;
|
||||
}
|
||||
if (savedAsset.mimeType == 'image/heic' || savedAsset.mimeType == 'image/heif') {
|
||||
let desitnation = '';
|
||||
if (savedAsset.mimeType == 'image/heic') {
|
||||
desitnation = resizePath.replace('.HEIC', '.jpeg');
|
||||
} else {
|
||||
desitnation = resizePath.replace('.HEIF', '.jpeg');
|
||||
}
|
||||
|
||||
await this.assetRepository.update(savedAsset, { resizePath: resizePath });
|
||||
sharp(data)
|
||||
.toFormat('jpeg')
|
||||
.resize(512, 512, { fit: 'outside' })
|
||||
.toFile(desitnation, async (err, info) => {
|
||||
if (err) {
|
||||
console.error('Error resizing file ', err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Send file to object detection after resizing
|
||||
// const detectionJob = await this.machineLearningQueue.add(
|
||||
// 'object-detection',
|
||||
// {
|
||||
// resizePath,
|
||||
// },
|
||||
// { jobId: randomUUID() },
|
||||
// );
|
||||
});
|
||||
await this.assetRepository.update(savedAsset, { resizePath: desitnation });
|
||||
});
|
||||
} else {
|
||||
sharp(data)
|
||||
.resize(512, 512, { fit: 'outside' })
|
||||
.toFile(resizePath, async (err, info) => {
|
||||
if (err) {
|
||||
console.error('Error resizing file ', err);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.assetRepository.update(savedAsset, { resizePath: resizePath });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return 'ok';
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
import { Process, Processor } from '@nestjs/bull';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Job } from 'bull';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AssetEntity } from '../../api-v1/asset/entities/asset.entity';
|
||||
import fs from 'fs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as tfnode from '@tensorflow/tfjs-node';
|
||||
import * as cocoSsd from '@tensorflow-models/coco-ssd';
|
||||
|
||||
@Processor('machine-learning')
|
||||
export class MachineLearningProcessor {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity) private assetRepository: Repository<AssetEntity>,
|
||||
private configService: ConfigService,
|
||||
) {}
|
||||
|
||||
@Process('object-detection')
|
||||
async handleOptimization(job: Job) {
|
||||
try {
|
||||
const { resizePath }: { resizePath: string } = job.data;
|
||||
|
||||
const image = fs.readFileSync(resizePath);
|
||||
const decodedImage = tfnode.node.decodeImage(image, 3) as tfnode.Tensor3D;
|
||||
const model = await cocoSsd.load();
|
||||
const predictions = await model.detect(decodedImage);
|
||||
console.log('\n\nstart predictions ------------------ ');
|
||||
for (var result of predictions) {
|
||||
console.log(`Found ${result.class} with score ${result.score}`);
|
||||
}
|
||||
console.log('end predictions ------------------\n\n');
|
||||
|
||||
return 'ok';
|
||||
} catch (e) {
|
||||
console.log('Error object detection ', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user