Skip to content

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

rotate thumbnail

roryclearecb0c31d4ea5
diff --git a/clearcam/SceneState.h b/clearcam/SceneState.h
index ba5e6fa..9d60fa0 100644
--- a/clearcam/SceneState.h
+++ b/clearcam/SceneState.h
@@ -10,7 +10,7 @@
@property (nonatomic, strong) NSDictionary *alerts;
@property (strong, nonatomic) NSManagedObjectContext *backgroundContext;
@property (strong, nonatomic) NSDate *last_notif_time;
-- (void)processOutput:(NSArray *)array withImage:(CIImage *)image;
+- (void)processOutput:(NSArray *)array withImage:(CIImage *)image orientation:(AVCaptureVideoOrientation)orientation;
- (void)sendnotification; //todo, remove
@end
diff --git a/clearcam/SceneState.m b/clearcam/SceneState.m
index 99c9005..e708cdf 100644
--- a/clearcam/SceneState.m
+++ b/clearcam/SceneState.m
@@ -28,7 +28,7 @@
return self;
}
-- (void)processOutput:(NSArray *)array withImage:(CIImage *)image {
+- (void)processOutput:(NSArray *)array withImage:(CIImage *)image orientation:(AVCaptureVideoOrientation)orientation{
NSArray *events = [[NSUserDefaults standardUserDefaults] objectForKey:@"yolo_presets"][[[NSUserDefaults standardUserDefaults] objectForKey:@"yolo_preset_idx"]];
if(!events) return;
NSMutableDictionary *frame = [[NSMutableDictionary alloc] init];
@@ -67,18 +67,12 @@
CGImageRef cgImage = [ciContext createCGImage:image fromRect:image.extent];
if (cgImage) {
- // Convert CGImage to UIImage
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
-
- // Release CGImage since it was manually created
CGImageRelease(cgImage);
-
- // Get the app's Documents directory
+ uiImage = [self rotatedImage:uiImage forOrientation:orientation];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *imagesDirectory = [documentsDirectory stringByAppendingPathComponent:@"images"];
-
- // Create the images directory if it doesn't exist
if (![[NSFileManager defaultManager] fileExistsAtPath:imagesDirectory]) {
[[NSFileManager defaultManager] createDirectoryAtPath:imagesDirectory
withIntermediateDirectories:YES
@@ -88,17 +82,20 @@
NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0); // 1.0 for maximum quality
NSString *filePathSmall = [imagesDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%lld_small.jpg", roundedUnixTimestamp]];
+
// Write the image data to file
if ([imageData writeToFile:filePathSmall atomically:YES]) {
- //thumbnail
+ // Thumbnail generation
UIGraphicsBeginImageContextWithOptions(CGSizeMake(uiImage.size.width / 2, uiImage.size.height / 2), YES, 1.0);
[uiImage drawInRect:CGRectMake(0, 0, uiImage.size.width / 2, uiImage.size.height / 2)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *lowResImageData = UIImageJPEGRepresentation(resizedImage, 0.7);
[lowResImageData writeToFile:filePathSmall atomically:YES];
-
- if(![[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] || ![[NSDate date] compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"expiry"]] || [[NSDate date] compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"expiry"]] == NSOrderedDescending){
+
+ if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] ||
+ ![[NSDate date] compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"expiry"]] ||
+ [[NSDate date] compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"expiry"]] == NSOrderedDescending) {
NSLog(@"verifying subscription");
[[StoreManager sharedInstance] verifySubscriptionWithCompletion:^(BOOL isActive, NSDate *expiryDate) {
[self sendnotification];
@@ -108,6 +105,7 @@
}
}
}
+
[self.backgroundContext performBlockAndWait:^{
NSManagedObject *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"EventEntity"
inManagedObjectContext:self.backgroundContext];
@@ -129,6 +127,27 @@
}
}
+- (UIImage *)rotatedImage:(UIImage *)image forOrientation:(AVCaptureVideoOrientation)orientation {
+ UIImageOrientation uiOrientation;
+ switch (orientation) {
+ case AVCaptureVideoOrientationPortrait:
+ uiOrientation = UIImageOrientationRight;
+ break;
+ case AVCaptureVideoOrientationPortraitUpsideDown:
+ uiOrientation = UIImageOrientationLeft;
+ break;
+ case AVCaptureVideoOrientationLandscapeLeft:
+ uiOrientation = UIImageOrientationDown;
+ break;
+ case AVCaptureVideoOrientationLandscapeRight:
+ default:
+ uiOrientation = UIImageOrientationUp;
+ break;
+ }
+ return [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:uiOrientation];
+}
+
+
- (void)sendnotification {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"send_notif_enabled"] || !([[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] || [[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"])){ //todo add back other stuff
NSDate *now = [NSDate date];
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index bf48058..1c920ae 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -991,7 +991,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]; //todo, should event detection without recording be a thing?
+ if(self.recordPressed) [self.scene processOutput:output withImage:ciImage orientation:self.previewLayer.connection.videoOrientation]; //todo, should event detection without recording be a thing?
CGImageRelease(cgImage);
__weak typeof(self) weak_self = self;