Skip to content

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

can refresh view (changes res) now

roryclear5e413668f650
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index 771d074..d37bdbf 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -111,6 +111,155 @@ NSMutableDictionary *classColorMap;
UIDeviceOrientation initialOrientation = [self getCurrentOrientation];
}
+- (void)refreshView {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ // Preserve recording state
+ BOOL wasRecording = self.recordPressed;
+
+ // Stop and clean up the current capture session
+ if (self.captureSession.isRunning) {
+ [self.captureSession stopRunning];
+ }
+ [self.captureSession.inputs enumerateObjectsUsingBlock:^(AVCaptureInput *input, NSUInteger idx, BOOL *stop) {
+ [self.captureSession removeInput:input];
+ }];
+ [self.captureSession.outputs enumerateObjectsUsingBlock:^(AVCaptureOutput *output, NSUInteger idx, BOOL *stop) {
+ [self.captureSession removeOutput:output];
+ }];
+ self.captureSession = nil;
+
+ // Finish any ongoing recording
+ if (self.isRecording) {
+ [self finishRecording];
+ }
+
+ // Remove notification observers
+ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
+ [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
+
+ // Remove KVO observers
+ SettingsManager *settings = [SettingsManager sharedManager];
+ [settings removeObserver:self forKeyPath:@"width"];
+ [settings removeObserver:self forKeyPath:@"height"];
+ [settings removeObserver:self forKeyPath:@"text_size"];
+ [settings removeObserver:self forKeyPath:@"preset"];
+
+ // Reset UI and remove all sublayers
+ [self resetUI];
+ [self.previewLayer removeFromSuperlayer];
+ self.previewLayer = nil;
+
+ // Reset all properties to initial state
+ self.fpsLabel = nil;
+ self.recordButton = nil;
+ self.settingsButton = nil;
+ self.galleryButton = nil;
+ self.lastFrameTime = 0;
+ self.frameCount = 0;
+ self.yolo = nil;
+ self.ciContext = nil;
+ self.isProcessing = NO;
+ self.isProcessingCoreData = NO;
+ self.recordPressed = wasRecording; // Restore recording state
+ self.isRecording = NO;
+ self.last_check_time = [[NSDate date] timeIntervalSince1970];
+ self.startTime = kCMTimeInvalid;
+ self.currentTime = kCMTimeZero;
+ self.assetWriter = nil;
+ self.videoWriterInput = nil;
+ self.adaptor = nil;
+ self.fileServer = nil;
+ self.seg_number = 0;
+ self.current_file_timestamp = nil;
+ self.digits = nil;
+ self.current_segment_squares = nil;
+ self.segmentLock = nil;
+ self.backgroundContext = nil;
+ self.dayFolderName = nil;
+ self.segmentQueue = nil;
+ self.finishRecordingQueue = nil;
+ self.scene = nil;
+
+ // Reinitialize all components as in viewDidLoad
+ self.scene = [[SceneState alloc] init];
+ self.segmentQueue = dispatch_queue_create("com.example.segmentQueue", DISPATCH_QUEUE_SERIAL);
+ self.finishRecordingQueue = dispatch_queue_create("com.example.finishRecordingQueue", DISPATCH_QUEUE_SERIAL);
+
+ self.current_segment_squares = [[NSMutableArray alloc] init];
+ self.digits = [NSMutableDictionary dictionary];
+ self.digits[@"0"] = @[@[ @0, @0, @3, @1], @[ @0, @1, @1 , @3], @[ @2, @1, @1 , @3], @[ @0, @4, @3 , @1]];
+ self.digits[@"1"] = @[@[ @2, @0, @1, @5 ]];
+ self.digits[@"2"] = @[@[ @0, @0, @3, @1 ], @[ @2, @1, @1, @1 ], @[ @0, @2, @3, @1 ], @[ @0, @3, @1, @1 ], @[ @0, @4, @3, @1 ]];
+ self.digits[@"3"] = @[@[ @0, @0, @3, @1 ], @[ @2, @1, @1, @3 ], @[ @0, @2, @3, @1 ], @[ @0, @4, @3, @1 ]];
+ self.digits[@"4"] = @[@[ @2, @0, @1, @5 ], @[ @0, @0, @1, @2 ], @[ @0, @2, @3, @1 ]];
+ self.digits[@"5"] = @[@[ @0, @0, @3, @1 ], @[ @0, @1, @1, @1 ], @[ @0, @2, @3, @1 ], @[ @2, @3, @1, @1 ], @[ @0, @4, @3, @1 ]];
+ self.digits[@"6"] = @[@[ @0, @0, @3, @1 ],@[ @0, @0, @1, @5 ], @[ @1, @2, @2, @1 ], @[ @1, @4, @2, @1 ], @[ @2, @3, @1, @1 ]];
+ self.digits[@"7"] = @[@[ @0, @0, @3, @1 ], @[ @2, @1, @1, @4 ]];
+ self.digits[@"8"] = @[@[ @0, @0, @1, @5 ], @[ @2, @0, @1, @5 ], @[ @1, @0, @1, @1 ], @[ @1, @2, @1, @1 ], @[ @1, @4, @1, @1 ]];
+ self.digits[@"9"] = @[@[ @2, @0, @1, @5 ], @[ @1, @0, @1, @1 ], @[ @0, @0, @1, @2 ], @[ @0, @2, @3, @1 ],@[ @0, @4, @3, @1 ]];
+ self.digits[@"-"] = @[@[ @0, @2, @3, @1 ]];
+ self.digits[@":"] = @[@[ @1, @1, @1, @1 ], @[ @1, @3, @1, @1 ]];
+ self.segmentLock = [[NSLock alloc] init];
+
+ self.ciContext = [CIContext context];
+ self.yolo = [[Yolo alloc] init];
+ self.seg_number = 0;
+ self.fileServer = [FileServer sharedInstance];
+ [self.fileServer start];
+ self.backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
+ self.backgroundContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
+ self.backgroundContext.parentContext = self.fileServer.context;
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(handleDeviceOrientationChange)
+ name:UIDeviceOrientationDidChangeNotification
+ object:nil];
+
+ [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
+
+ [settings addObserver:self forKeyPath:@"width" options:NSKeyValueObservingOptionNew context:nil];
+ [settings addObserver:self forKeyPath:@"height" options:NSKeyValueObservingOptionNew context:nil];
+ [settings addObserver:self forKeyPath:@"text_size" options:NSKeyValueObservingOptionNew context:nil];
+ [settings addObserver:self forKeyPath:@"preset" options:NSKeyValueObservingOptionNew context:nil];
+
+ // Re-setup camera with current settings
+ [self setupCameraWithWidth:settings.width height:settings.height];
+
+ // Re-setup UI
+ [self setupUI];
+
+ // Update record button state if was recording
+ if (self.recordPressed) {
+ [self.recordButton setTitle:@"Stop" forState:UIControlStateNormal];
+ for (CALayer *layer in self.recordButton.layer.sublayers) {
+ if ([layer.name isEqualToString:@"redShape"]) {
+ CAShapeLayer *redShape = (CAShapeLayer *)layer;
+ [CATransaction begin];
+ [CATransaction setAnimationDuration:0.2];
+ redShape.path = [UIBezierPath bezierPathWithRect:CGRectMake(25, 25, 30, 30)].CGPath;
+ [CATransaction commit];
+ break;
+ }
+ }
+ }
+
+ // Set initial orientation
+ [self setInitialOrientation];
+
+ // Start the capture session
+ [self.captureSession startRunning];
+
+ // Start a new recording if was recording
+ if (self.recordPressed) {
+ [self startNewRecording];
+ }
+
+ // Mimic viewWillAppear setup
+ [UIApplication sharedApplication].idleTimerDisabled = YES;
+ });
+}
+
+
- (void)setInitialOrientation {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
AVCaptureVideoOrientation videoOrientation;
@@ -944,15 +1093,15 @@ NSMutableDictionary *classColorMap;
}
NSTimeInterval elapsedTime = CMTimeGetSeconds(self.currentTime);
- //todo add back
if (self.fileServer.segment_length == 1 && [[NSDate now] timeIntervalSinceDate:self.fileServer.last_req_time] > 60) {
self.fileServer.segment_length = 60;
}
- if ([[NSDate date] timeIntervalSince1970] - self.last_check_time > 10.0) { //todo, check for flag in settings!
+ if ([[NSDate date] timeIntervalSince1970] - self.last_check_time > 10.0) {
NSLog(@"Making request at %.2f %.2f seconds", [[NSDate date] timeIntervalSince1970],self.last_check_time);
self.last_check_time = [[NSDate date] timeIntervalSince1970];
NSString *deviceName = [[NSUserDefaults standardUserDefaults] stringForKey:@"device_name"];
NSString *sessionToken = [[StoreManager sharedInstance] retrieveSessionTokenFromKeychain];
+
NSLog(@"session token = %@", sessionToken);
NSString *encodedDeviceName = [deviceName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
@@ -971,8 +1120,26 @@ NSMutableDictionary *classColorMap;
if (error) {
NSLog(@"❌ Error: %@", error.localizedDescription);
} else {
- NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"✅ Response: %@", responseString);
+ NSError *jsonError;
+ NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
+
+ if (jsonError) {
+ NSLog(@"⚠️ JSON Parsing Error: %@", jsonError.localizedDescription);
+ } else {
+ NSString *uploadLink = json[@"upload_link"];
+ if (uploadLink && ![uploadLink isKindOfClass:[NSNull class]]) {
+ NSLog(@"📤 Upload link received: %@", uploadLink);
+
+ //[FileServer sharedInstance].segment_length = 2;
+ //[[SettingsManager sharedManager] updateResolutionWithWidth:@"640" height:@"480" textSize:@"2" preset:@"AVCaptureSessionPreset640x480"];
+
+ //dispatch_async(dispatch_get_main_queue(), ^{
+ // [self refreshView];
+ //});
+ } else {
+ NSLog(@"no link");
+ }
+ }
}
}];
[task resume];
@@ -1023,7 +1190,7 @@ NSMutableDictionary *classColorMap;
CGImageRef cgImage = [self.ciContext createCGImage:croppedImage fromRect:cropRect];
NSArray *output = [self.yolo yolo_infer:cgImage withOrientation:videoOrientation];
- if(self.recordPressed) [self.scene processOutput:output withImage:ciImage orientation:self.previewLayer.connection.videoOrientation]; //todo, should event detection without recording be a thing?
+ if(self.recordPressed) [self.scene processOutput:output withImage:ciImage orientation:self.previewLayer.connection.videoOrientation];
CGImageRelease(cgImage);
__weak typeof(self) weak_self = self;
@@ -1074,7 +1241,6 @@ NSMutableDictionary *classColorMap;
}
}
-
- (CVPixelBufferRef)addColoredRectangleToPixelBuffer:(CVPixelBufferRef)pixelBuffer withColor:(UIColor *)color originX:(CGFloat)originX originY:(CGFloat)originY width:(CGFloat)width height:(CGFloat)height opacity:(CGFloat)opacity {
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
@@ -1221,4 +1387,3 @@ NSMutableDictionary *classColorMap;
}
}
@end
-