Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

faster get-segments, still too slow

rorycleare31614420949
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index 0ea465d..9209a5b 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -43,7 +43,7 @@
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
- serverAddr.sin_port = htons(8081);
+ serverAddr.sin_port = htons(8080);
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {
NSLog(@"Failed to bind socket: %s", strerror(errno));
@@ -57,7 +57,7 @@
return;
}
- NSLog(@"Serving files at http://localhost:8081/");
+ NSLog(@"Serving files at http://localhost:8080/");
while (1) {
int clientSocket = accept(serverSocket, NULL, NULL);
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index 425b39e..51582ac 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -23,6 +23,11 @@
@property (nonatomic, strong) AVAssetWriterInputPixelBufferAdaptor *adaptor;
@property (nonatomic, strong) FileServer *fileServer;
@property (nonatomic, assign) int seg_number;
+@property (nonatomic, strong) NSURL *last_file_url;
+@property (nonatomic, assign) CMTime last_file_duration;
+@property (nonatomic, assign) NSDate *last_file_timestamp;
+@property (nonatomic, assign) NSDate *current_file_timestamp;
+
@end
@@ -107,7 +112,8 @@ NSMutableDictionary *classColorMap;
- (void)startNewRecording {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd_HH:mm:ss:SSS"];
- NSString *timestamp = [formatter stringFromDate:[NSDate date]];
+ self.current_file_timestamp = [NSDate date];
+ NSString *timestamp = [formatter stringFromDate:self.current_file_timestamp];
NSString *segNumberString = [NSString stringWithFormat:@"_%05ld_", (long)self.seg_number];
self.seg_number += 1;
NSString *finalTimestamp = [NSString stringWithFormat:@"%@%@%@",
@@ -271,6 +277,50 @@ NSMutableDictionary *classColorMap;
[self.videoWriterInput markAsFinished];
[self.assetWriter finishWritingWithCompletionHandler:^{
NSLog(@"Finished recording and saving file");
+
+ NSString *fileName = [self.assetWriter.outputURL lastPathComponent];
+ NSLog(@"File saved is %@", fileName);
+
+ AVAsset *asset = [AVAsset assetWithURL:self.assetWriter.outputURL];
+ CMTime time = asset.duration;
+
+ // Path to segments file
+ 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];
+ self.last_file_timestamp = self.current_file_timestamp;
+ } else {
+ // Calculate the time difference
+ CMTime timeDifference = CMTimeMakeWithSeconds([self.current_file_timestamp timeIntervalSinceDate:self.last_file_timestamp], NSEC_PER_SEC);
+
+ // Create a dictionary entry with numeric values
+ NSDictionary *segmentEntry = @{
+ @"url": self.last_file_url.lastPathComponent,
+ @"duration": @(CMTimeGetSeconds(self.last_file_duration)), // Now stored as NSNumber
+ @"timeDifference": @(CMTimeGetSeconds(timeDifference)) // Now stored as NSNumber
+ };
+
+ // Add the entry to the array
+ [segmentsArray addObject:segmentEntry];
+
+ // Write array to file as JSON
+ [self saveSegmentsArray:segmentsArray toFile:segmentsFilePath];
+
+ // Update last file details
+ self.last_file_duration = time;
+ self.last_file_url = [self.assetWriter.outputURL copy];
+ self.last_file_timestamp = self.current_file_timestamp;
+ }
+
+ NSLog(@"Duration is %f seconds", CMTimeGetSeconds(time));
[self startNewRecording];
}];
} else {
@@ -278,6 +328,16 @@ NSMutableDictionary *classColorMap;
}
}
+- (void)saveSegmentsArray:(NSMutableArray *)segmentsArray toFile:(NSString *)filePath {
+ NSError *error;
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:segmentsArray options:NSJSONWritingPrettyPrinted error:&error];
+ if (!error) {
+ [jsonData writeToFile:filePath atomically:YES];
+ } else {
+ NSLog(@"Error writing JSON to file: %@", error.localizedDescription);
+ }
+}
+
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
@try {
if (self.isRecording && self.assetWriter.status == AVAssetWriterStatusWriting) {
@@ -376,6 +436,3 @@ NSMutableDictionary *classColorMap;
}
@end
-
-
-