Add object detection, tracking, mobile notifications, and search to any security camera.
dont use segments.txt for now
rorycleare81448ac6359
diff --git a/clearcam/FileServer.h b/clearcam/FileServer.hindex 0fb14f0..384b177 100644--- a/clearcam/FileServer.h+++ b/clearcam/FileServer.h@@ -3,5 +3,6 @@@interface FileServer : NSObject- (void)start;+@property (nonatomic, strong) NSMutableArray *segmentsArray;@enddiff --git a/clearcam/FileServer.m b/clearcam/FileServer.mindex 8785f41..6057793 100644--- a/clearcam/FileServer.m+++ b/clearcam/FileServer.m@@ -84,7 +84,6 @@filePath = [filePath stringByRemovingPercentEncoding];if ([filePath isEqualToString:@"/"]) filePath = @"";- // Handle /get-segments API endpointif ([filePath hasPrefix:@"get-segments"]) {// Extract the query string if presentNSString *startParam = nil;@@ -105,51 +104,28 @@NSInteger start = startParam ? [startParam integerValue] : 0;NSLog(@"START PARAM = %ld", (long)start);-- NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];- NSString *segmentsFilePath = [documentsPath stringByAppendingPathComponent:@"segments.txt"];- if ([[NSFileManager defaultManager] fileExistsAtPath:segmentsFilePath]) {- // Read contents of segments.txt- NSData *jsonData = [NSData dataWithContentsOfFile:segmentsFilePath];- NSError *jsonError = nil;- NSArray *segmentsArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];- if (jsonError) {- NSString *httpHeader = @"HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\n\r\n";- NSString *errorMessage = [NSString stringWithFormat:@"{\"error\": \"Failed to parse segments.txt: %@\"}", jsonError.localizedDescription];- send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);- send(clientSocket, [errorMessage UTF8String], errorMessage.length, 0);- return;- }-- // Slice the array starting from the 'start' index- if (start >= segmentsArray.count) {- NSString *httpHeader = @"HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n";- NSString *errorMessage = @"{\"error\": \"Start index is out of range\"}";- send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);- send(clientSocket, [errorMessage UTF8String], errorMessage.length, 0);- return;- }-- NSArray *slicedSegments = [segmentsArray subarrayWithRange:NSMakeRange(start, segmentsArray.count - start)];-- // Convert the sliced array back to JSON- NSData *slicedJsonData = [NSJSONSerialization dataWithJSONObject:slicedSegments options:0 error:nil];-- NSString *httpHeader = @"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n";- send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);- send(clientSocket, slicedJsonData.bytes, slicedJsonData.length, 0);- return;- } else {- // If file does not exist, send an empty response+ // Use self.segmentsArray instead of reading from a file+ if (start >= self.segmentsArray.count) {NSString *httpHeader = @"HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n";- NSString *errorMessage = @"{\"error\": \"segments.txt not found\"}";+ NSString *errorMessage = @"{\"error\": \"Start index is out of range\"}";send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);send(clientSocket, [errorMessage UTF8String], errorMessage.length, 0);return;}++ NSArray *slicedSegments = [self.segmentsArray subarrayWithRange:NSMakeRange(start, self.segmentsArray.count - start)];++ // Convert the sliced array back to JSON+ NSData *slicedJsonData = [NSJSONSerialization dataWithJSONObject:slicedSegments options:0 error:nil];++ NSString *httpHeader = @"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n";+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);+ send(clientSocket, slicedJsonData.bytes, slicedJsonData.length, 0);+ return;}+NSString *fullPath = [basePath stringByAppendingPathComponent:filePath];BOOL isDirectory = NO;if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory]) {diff --git a/clearcam/ViewController.m b/clearcam/ViewController.mindex 51582ac..025ca17 100644--- a/clearcam/ViewController.m+++ b/clearcam/ViewController.m@@ -37,6 +37,7 @@ NSMutableDictionary *classColorMap;- (void)viewDidLoad {[super viewDidLoad];+ [[NSFileManager defaultManager] removeItemAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"segments.txt"] error:nil]; //TODO removeself.ciContext = [CIContext context];self.yolo = [[Yolo alloc] init];self.seg_number = 0;@@ -44,6 +45,7 @@ NSMutableDictionary *classColorMap;[self setupFPSLabel];self.fileServer = [[FileServer alloc] init];[self.fileServer start];+ self.fileServer.segmentsArray = [[NSMutableArray alloc] init];[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(handleDeviceOrientationChange)@@ -288,11 +290,6 @@ NSMutableDictionary *classColorMap;NSString *segmentsFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingPathComponent:@"segments.txt"];- static NSMutableArray *segmentsArray = nil;- if (!segmentsArray) {- segmentsArray = [NSMutableArray array];- }-if (CMTIME_IS_INVALID(self.last_file_duration)) {self.last_file_duration = time;self.last_file_url = [self.assetWriter.outputURL copy];@@ -304,15 +301,12 @@ NSMutableDictionary *classColorMap;// Create a dictionary entry with numeric valuesNSDictionary *segmentEntry = @{@"url": self.last_file_url.lastPathComponent,- @"duration": @(CMTimeGetSeconds(self.last_file_duration)), // Now stored as NSNumber- @"timeDifference": @(CMTimeGetSeconds(timeDifference)) // Now stored as NSNumber+ @"duration": @(CMTimeGetSeconds(self.last_file_duration)),+ @"timeDifference": @(CMTimeGetSeconds(timeDifference))};- // Add the entry to the array- [segmentsArray addObject:segmentEntry];-- // Write array to file as JSON- [self saveSegmentsArray:segmentsArray toFile:segmentsFilePath];+ [self saveSegmentEntry:segmentEntry toFile:segmentsFilePath]; //todo, not using this yet+ [self.fileServer.segmentsArray addObject:segmentEntry];// Update last file detailsself.last_file_duration = time;@@ -328,16 +322,42 @@ NSMutableDictionary *classColorMap;}}-- (void)saveSegmentsArray:(NSMutableArray *)segmentsArray toFile:(NSString *)filePath {+- (void)saveSegmentEntry:(NSDictionary *)segmentEntry toFile:(NSString *)filePath {+ NSFileManager *fileManager = [NSFileManager defaultManager];+ if (![fileManager fileExistsAtPath:filePath]) {+ NSError *error;+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@[segmentEntry] options:NSJSONWritingPrettyPrinted error:&error];+ if (!error) {+ [jsonData writeToFile:filePath atomically:YES];+ } else {+ NSLog(@"Error writing JSON to file: %@", error.localizedDescription);+ }+ } else {+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];+ if (fileHandle) {+ [fileHandle seekToEndOfFile];+ [fileHandle truncateFileAtOffset:[fileHandle offsetInFile] - 1];+ NSString *entryString = [NSString stringWithFormat:@", %@]", [self jsonStringFromDictionary:segmentEntry]];+ [fileHandle writeData:[entryString dataUsingEncoding:NSUTF8StringEncoding]];+ [fileHandle closeFile];+ } else {+ NSLog(@"Error opening file for writing.");+ }+ }+}++- (NSString *)jsonStringFromDictionary:(NSDictionary *)dictionary {NSError *error;- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:segmentsArray options:NSJSONWritingPrettyPrinted error:&error];+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];if (!error) {- [jsonData writeToFile:filePath atomically:YES];+ return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];} else {- NSLog(@"Error writing JSON to file: %@", error.localizedDescription);+ NSLog(@"Error converting dictionary to JSON string: %@", error.localizedDescription);+ return nil;}}+- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {@try {if (self.isRecording && self.assetWriter.status == AVAssetWriterStatusWriting) {