Add object detection, tracking, mobile notifications, and search to any security camera.
can use other segment sizes
roryclearfa91568be412
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.mindex 2975219..ae59df6 100644--- a/clearcam/FileServer.m+++ b/clearcam/FileServer.m@@ -10,6 +10,7 @@@interface FileServer ()@property (nonatomic, strong) NSString *basePath;+@property (nonatomic, strong) NSMutableDictionary *durationCache;@end@implementation FileServer@@ -17,6 +18,7 @@- (void)start {dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{self.basePath = [self getDocumentsDirectory];+ self.durationCache = [[NSMutableDictionary alloc] init];[self startHTTPServerWithBasePath:self.basePath];});}@@ -40,7 +42,7 @@memset(&serverAddr, 0, sizeof(serverAddr));serverAddr.sin_family = AF_INET;serverAddr.sin_addr.s_addr = INADDR_ANY;- serverAddr.sin_port = htons(8080);+ serverAddr.sin_port = htons(8081);if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {NSLog(@"Failed to bind socket: %s", strerror(errno));@@ -54,7 +56,7 @@return;}- NSLog(@"Serving files at http://localhost:8080/");+ NSLog(@"Serving files at http://localhost:8081/");while (1) {int clientSocket = accept(serverSocket, NULL, NULL);@@ -123,16 +125,24 @@NSMutableArray *responseArray = [NSMutableArray array];for (NSString *file in sortedFiles) {NSString *filePath = [documentsPath stringByAppendingPathComponent:file];-- // Get duration using AVAsset- AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filePath]];- CMTime time = asset.duration;- Float64 duration = CMTimeGetSeconds(time);-- // Round the duration to 3 decimal places++ NSNumber *cachedDuration = self.durationCache[file];+ Float64 duration;+ if (cachedDuration) {+ duration = [cachedDuration doubleValue];+ } else {+ // Get duration using AVAsset+ AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filePath]];+ CMTime time = asset.duration;+ duration = CMTimeGetSeconds(time);++ if (isfinite(duration)) {+ duration = round(duration * 1000) / 1000.0; // Round to 3 decimal places+ self.durationCache[file] = @(duration);+ }+ }+if (isfinite(duration)) {- duration = round(duration * 1000) / 1000.0; // Round to 3 decimal places-[responseArray addObject:@{@"url": file,@"duration": @(duration)diff --git a/clearcam/ViewController.m b/clearcam/ViewController.mindex bf2e83b..b749c0c 100644--- a/clearcam/ViewController.m+++ b/clearcam/ViewController.m@@ -279,80 +279,84 @@ NSMutableDictionary *classColorMap;}- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {- if (self.isRecording && self.assetWriter.status == AVAssetWriterStatusWriting) {- CMTime timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);- if (CMTIME_IS_INVALID(self.startTime)) {- self.startTime = timestamp;- self.currentTime = kCMTimeZero;- } else {- self.currentTime = CMTimeSubtract(timestamp, self.startTime);- }+ @try {+ if (self.isRecording && self.assetWriter.status == AVAssetWriterStatusWriting) {+ CMTime timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);+ if (CMTIME_IS_INVALID(self.startTime)) {+ self.startTime = timestamp;+ self.currentTime = kCMTimeZero;+ } else {+ self.currentTime = CMTimeSubtract(timestamp, self.startTime);+ }- if (self.videoWriterInput.readyForMoreMediaData) {- CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);-- BOOL success = NO;- int retryCount = 3;-- while (!success && retryCount > 0) {- success = [self.adaptor appendPixelBuffer:pixelBuffer withPresentationTime:self.currentTime];- if (!success) {- retryCount--;- [NSThread sleepForTimeInterval:0.01];+ if (self.videoWriterInput.readyForMoreMediaData) {+ CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);++ BOOL success = NO;+ int retryCount = 3;++ while (!success && retryCount > 0) {+ success = [self.adaptor appendPixelBuffer:pixelBuffer withPresentationTime:self.currentTime];+ if (!success) {+ retryCount--;+ [NSThread sleepForTimeInterval:0.01];+ }}+ if (!success) [self finishRecording];+ }+ NSTimeInterval elapsedTime = CMTimeGetSeconds(self.currentTime);+ if (elapsedTime >= 10.0) { // todo, takes a little time to change+ [self finishRecording];}- if (!success) [self finishRecording];}- NSTimeInterval elapsedTime = CMTimeGetSeconds(self.currentTime);- if (elapsedTime >= 10.0) { // todo, takes a little time to change- [self finishRecording];++ AVCaptureVideoOrientation videoOrientation = self.previewLayer.connection.videoOrientation;+ CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);+ CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];++ if (self.isProcessing) {+ return;}- }- AVCaptureVideoOrientation videoOrientation = self.previewLayer.connection.videoOrientation;- CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);- CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];+ self.isProcessing = YES;- if (self.isProcessing) {- return;- }+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{+ size_t width = CVPixelBufferGetWidth(imageBuffer);+ size_t height = CVPixelBufferGetHeight(imageBuffer);+ CGFloat aspect_ratio = (CGFloat)width / (CGFloat)height;+ CGFloat targetWidth = self.yolo.yolo_res;+ CGSize targetSize = CGSizeMake(targetWidth, targetWidth / aspect_ratio);- self.isProcessing = YES;-- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{- size_t width = CVPixelBufferGetWidth(imageBuffer);- size_t height = CVPixelBufferGetHeight(imageBuffer);- CGFloat aspect_ratio = (CGFloat)width / (CGFloat)height;- CGFloat targetWidth = self.yolo.yolo_res;- CGSize targetSize = CGSizeMake(targetWidth, targetWidth / aspect_ratio);-- CGFloat scaleX = targetSize.width / width;- CGFloat scaleY = targetSize.height / height;- CIImage *resizedImage = [ciImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];-- CGRect cropRect = CGRectMake(0, 0, targetSize.width, targetSize.height);- CIImage *croppedImage = [resizedImage imageByCroppingToRect:cropRect];-- CGImageRef cgImage = [self.ciContext createCGImage:croppedImage fromRect:cropRect];-- NSArray *output = [self.yolo yolo_infer:cgImage withOrientation:videoOrientation];- CGImageRelease(cgImage);-- __weak typeof(self) weak_self = self;- dispatch_async(dispatch_get_main_queue(), ^{- [weak_self resetSquares];- for (int i = 0; i < output.count; i++) {- [weak_self drawSquareWithTopLeftX:[output[i][0] floatValue]- topLeftY:[output[i][1] floatValue]- bottomRightX:[output[i][2] floatValue]- bottomRightY:[output[i][3] floatValue]- classIndex:[output[i][4] intValue]- aspectRatio:aspect_ratio];- }- [weak_self updateFPS];- weak_self.isProcessing = NO;+ CGFloat scaleX = targetSize.width / width;+ CGFloat scaleY = targetSize.height / height;+ CIImage *resizedImage = [ciImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];++ CGRect cropRect = CGRectMake(0, 0, targetSize.width, targetSize.height);+ CIImage *croppedImage = [resizedImage imageByCroppingToRect:cropRect];++ CGImageRef cgImage = [self.ciContext createCGImage:croppedImage fromRect:cropRect];++ NSArray *output = [self.yolo yolo_infer:cgImage withOrientation:videoOrientation];+ CGImageRelease(cgImage);++ __weak typeof(self) weak_self = self;+ dispatch_async(dispatch_get_main_queue(), ^{+ [weak_self resetSquares];+ for (int i = 0; i < output.count; i++) {+ [weak_self drawSquareWithTopLeftX:[output[i][0] floatValue]+ topLeftY:[output[i][1] floatValue]+ bottomRightX:[output[i][2] floatValue]+ bottomRightY:[output[i][3] floatValue]+ classIndex:[output[i][4] intValue]+ aspectRatio:aspect_ratio];+ }+ [weak_self updateFPS];+ weak_self.isProcessing = NO;+ });});- });+ } @catch (NSException *exception) {+ NSLog(@"Exception occurred: %@, %@", exception, [exception callStackSymbols]);+ }}- (void)updateFPS {