Add object detection, tracking, mobile notifications, and search to any security camera.
move video settings to settings manager
roryclearc69768614be4
diff --git a/clearcam/SettingsManager.m b/clearcam/SettingsManager.mindex 3195d47..12f080d 100644--- a/clearcam/SettingsManager.m+++ b/clearcam/SettingsManager.m@@ -108,7 +108,7 @@- (void)loadDeleteOnLaunch {if ([[NSUserDefaults standardUserDefaults] objectForKey:@"delete_on_launch"] == nil) {NSLog(@"no delete_on_launch value found");- self.delete_on_launch = NO;+ self.delete_on_launch = YES;} else {self.delete_on_launch = [[NSUserDefaults standardUserDefaults] boolForKey:@"delete_on_launch"];}diff --git a/clearcam/ViewController.m b/clearcam/ViewController.mindex dd2556e..0ada975 100644--- a/clearcam/ViewController.m+++ b/clearcam/ViewController.m@@ -3,6 +3,7 @@#import <Metal/Metal.h>#import "Yolo.h"#import "FileServer.h"+#import "SettingsManager.h"@interface ViewController ()@@ -29,11 +30,7 @@@property (nonatomic, strong) NSLock *segmentLock;@property (nonatomic, strong) NSManagedObjectContext *backgroundContext;@property (nonatomic, strong) NSString *dayFolderName;-@property (nonatomic, assign) int width;-@property (nonatomic, assign) int height;-@property (nonatomic, assign) int text_size;-@property (nonatomic, strong) NSString *preset; //todo, can just make this-//@property (nonatomic, strong) Resolution *res;+#define MIN_FREE_SPACE_MB 500 //threshold to start deleting@@ -50,10 +47,6 @@ NSMutableDictionary *classColorMap;//self.res = [[Resolution alloc] initWithWidth:3840 height:2160 text_size:5 preset:AVCaptureSessionPreset3840x2160];//self.res = [[Resolution alloc] initWithWidth:1920 height:1080 text_size:3 preset:AVCaptureSessionPreset1920x1080];//self.res = [[Resolution alloc] initWithWidth:1280 height:720 text_size:2 preset:AVCaptureSessionPreset1280x720];- self.width = 1920;- self.height = 1080;- self.text_size = 3;- self.preset = AVCaptureSessionPreset1920x1080;self.current_segment_squares = [[NSMutableArray alloc] init];self.digits = [NSMutableDictionary dictionary];@@ -124,7 +117,11 @@ NSMutableDictionary *classColorMap;- (void)setupCamera {self.captureSession = [[AVCaptureSession alloc] init];- self.captureSession.sessionPreset = self.preset;++ // Get preset from SettingsManager+ SettingsManager *settings = [SettingsManager sharedManager];+ self.captureSession.sessionPreset = settings.preset;+AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];NSError *error = nil;AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];@@ -133,7 +130,6 @@ NSMutableDictionary *classColorMap;return;}[self.captureSession addInput:input];-AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];output.videoSettings = @{(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};output.alwaysDiscardsLateVideoFrames = YES;@@ -177,6 +173,11 @@ NSMutableDictionary *classColorMap;segNumberString,[[timestamp componentsSeparatedByString:@"_"] lastObject]];+ // Get resolution settings from SettingsManager+ SettingsManager *settings = [SettingsManager sharedManager];+ int videoWidth = [settings.width intValue];+ int videoHeight = [settings.height intValue];+// Create a folder for the day within the documents directoryNSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];NSURL *dayFolderURL = [documentsDirectory URLByAppendingPathComponent:self.dayFolderName];@@ -199,12 +200,11 @@ NSMutableDictionary *classColorMap;}// Check if the device supports HEVC encoding-NSDictionary *videoSettings;videoSettings = @{AVVideoCodecKey: AVVideoCodecTypeH264, // Fallback to H.264- AVVideoWidthKey: @(self.width),- AVVideoHeightKey: @(self.height),+ AVVideoWidthKey: @(videoWidth),+ AVVideoHeightKey: @(videoHeight),AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill};@@ -213,12 +213,12 @@ NSMutableDictionary *classColorMap;// HEVC is supportedvideoSettings = @{AVVideoCodecKey: AVVideoCodecTypeHEVC, // Use HEVC (H.265)- AVVideoWidthKey: @(self.width),- AVVideoHeightKey: @(self.height),+ AVVideoWidthKey: @(videoWidth),+ AVVideoHeightKey: @(videoHeight),AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill};} else {- NSLog(@"device doesn't support H265");+ NSLog(@"Device doesn't support H265");}self.videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];@@ -232,8 +232,8 @@ NSMutableDictionary *classColorMap;NSDictionary *sourcePixelBufferAttributes = @{(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA),- (NSString *)kCVPixelBufferWidthKey: @(self.width),- (NSString *)kCVPixelBufferHeightKey: @(self.height)+ (NSString *)kCVPixelBufferWidthKey: @(videoWidth),+ (NSString *)kCVPixelBufferHeightKey: @(videoHeight)};self.adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:self.videoWriterInput sourcePixelBufferAttributes:sourcePixelBufferAttributes];@@ -253,6 +253,7 @@ NSMutableDictionary *classColorMap;[self ensureFreeDiskSpace];}+- (void)drawSquareWithTopLeftX:(CGFloat)xOrigin topLeftY:(CGFloat)yOrigin bottomRightX:(CGFloat)bottomRightX bottomRightY:(CGFloat)bottomRightY classIndex:(int)classIndex aspectRatio:(float)aspectRatio {CGFloat leftEdgeX = (self.view.bounds.size.width - (self.view.bounds.size.height * aspectRatio)) / 2;CGFloat scaledXOrigin, scaledYOrigin, scaledWidth, scaledHeight;@@ -797,78 +798,98 @@ NSMutableDictionary *classColorMap;return pixelBuffer;}-- (CVPixelBufferRef)addTimeStampToPixelBuffer:(CVPixelBufferRef)pixelBuffer{- NSInteger pixelSize = self.text_size*2;- NSInteger spaceSize = self.text_size;+- (CVPixelBufferRef)addTimeStampToPixelBuffer:(CVPixelBufferRef)pixelBuffer {+ // Get text_size from SettingsManager+ SettingsManager *settings = [SettingsManager sharedManager];+ NSInteger textSize = [settings.text_size intValue];++ NSInteger pixelSize = textSize * 2;+ NSInteger spaceSize = textSize;NSInteger digitOriginX = spaceSize;NSInteger digitOriginY = spaceSize;- NSInteger height = spaceSize*2 + pixelSize*5;+ NSInteger height = spaceSize * 2 + pixelSize * 5;NSDate *currentDate = [NSDate date];NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSString *timestamp = [dateFormatter stringFromDate:currentDate];--- //check roatation++ // Check rotationCGAffineTransform transform = self.videoWriterInput.transform;CGFloat angle = atan2(transform.b, transform.a);// Convert radians to degreesCGFloat degrees = angle * (180.0 / M_PI);-+size_t width_res = CVPixelBufferGetWidth(pixelBuffer);size_t height_res = CVPixelBufferGetHeight(pixelBuffer);+if (degrees == 90 || degrees == -90) {- pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer withColor:[UIColor blackColor] originX:0 originY:height_res-(pixelSize*3+spaceSize)*timestamp.length width:height height:(pixelSize*3+spaceSize)*timestamp.length opacity:0.4];+ pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer+ withColor:[UIColor blackColor]+ originX:0+ originY:height_res - (pixelSize * 3 + spaceSize) * timestamp.length+ width:height+ height:(pixelSize * 3 + spaceSize) * timestamp.length+ opacity:0.4];} else if (degrees == 180 || degrees == -180) {- pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer withColor:[UIColor blackColor] originX:width_res-(pixelSize*3+spaceSize)*timestamp.length originY:height_res-height width:(pixelSize*3+spaceSize)*timestamp.length height:height opacity:0.4];+ pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer+ withColor:[UIColor blackColor]+ originX:width_res - (pixelSize * 3 + spaceSize) * timestamp.length+ originY:height_res - height+ width:(pixelSize * 3 + spaceSize) * timestamp.length+ height:height+ opacity:0.4];} else {- //no rotation- pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer withColor:[UIColor blackColor] originX:0 originY:0 width:(pixelSize*3+spaceSize)*timestamp.length height:height opacity:0.4];+ // No rotation+ pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer+ withColor:[UIColor blackColor]+ originX:0+ originY:0+ width:(pixelSize * 3 + spaceSize) * timestamp.length+ height:height+ opacity:0.4];}--+for (NSUInteger k = 0; k < [timestamp length]; k++) {unichar character = [timestamp characterAtIndex:k];- if(character == ' '){- digitOriginX += pixelSize*3;+ if (character == ' ') {+ digitOriginX += pixelSize * 3;continue;}NSString *key = [NSString stringWithFormat:@"%C", character];for (int i = 0; i < [self.digits[key] count]; i++) {if (degrees == 90 || degrees == -90) {pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer- withColor:[UIColor whiteColor]+ withColor:[UIColor whiteColor]originX:digitOriginY + [self.digits[key][i][1] doubleValue] * pixelSize- originY:height_res - (digitOriginX + [self.digits[key][i][0] doubleValue] * pixelSize) - ([self.digits[key][i][2] doubleValue] * pixelSize)- width:[self.digits[key][i][3] doubleValue] * pixelSize+ originY:height_res - (digitOriginX + [self.digits[key][i][0] doubleValue] * pixelSize) - ([self.digits[key][i][2] doubleValue] * pixelSize)+ width:[self.digits[key][i][3] doubleValue] * pixelSizeheight:[self.digits[key][i][2] doubleValue] * pixelSize- opacity:1];+ opacity:1];} else if (degrees == 180 || degrees == -180) {pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer- withColor:[UIColor whiteColor]- originX: width_res - (digitOriginX + [self.digits[key][i][0] doubleValue] * pixelSize) - ([self.digits[key][i][2] doubleValue] * pixelSize)- originY: height_res - (digitOriginY + [self.digits[key][i][1] doubleValue] * pixelSize) - ([self.digits[key][i][3] doubleValue] * pixelSize)- width:[self.digits[key][i][2] doubleValue] * pixelSize+ withColor:[UIColor whiteColor]+ originX:width_res - (digitOriginX + [self.digits[key][i][0] doubleValue] * pixelSize) - ([self.digits[key][i][2] doubleValue] * pixelSize)+ originY:height_res - (digitOriginY + [self.digits[key][i][1] doubleValue] * pixelSize) - ([self.digits[key][i][3] doubleValue] * pixelSize)+ width:[self.digits[key][i][2] doubleValue] * pixelSizeheight:[self.digits[key][i][3] doubleValue] * pixelSize- opacity:1];+ opacity:1];} else {pixelBuffer = [self addColoredRectangleToPixelBuffer:pixelBuffer- withColor:[UIColor whiteColor]- originX:digitOriginX + [self.digits[key][i][0] doubleValue] * pixelSize- originY:digitOriginY + [self.digits[key][i][1] doubleValue] * pixelSize- width:[self.digits[key][i][2] doubleValue] * pixelSize- height:[self.digits[key][i][3] doubleValue] * pixelSize- opacity:1];+ withColor:[UIColor whiteColor]+ originX:digitOriginX + [self.digits[key][i][0] doubleValue] * pixelSize+ originY:digitOriginY + [self.digits[key][i][1] doubleValue] * pixelSize+ width:[self.digits[key][i][2] doubleValue] * pixelSize+ height:[self.digits[key][i][3] doubleValue] * pixelSize+ opacity:1];}}- digitOriginX += pixelSize*3 + spaceSize;+ digitOriginX += pixelSize * 3 + spaceSize;}return pixelBuffer;}-// Helper method to create a CGContext for a CVPixelBufferRef- (CGContextRef)createContextForPixelBuffer:(CVPixelBufferRef)pixelBuffer {size_t width = CVPixelBufferGetWidth(pixelBuffer);