Skip to content

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

clean and add SE2 to if

rorycleare5833d4c3381
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index 8cb3b6e..9948015 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -286,15 +286,16 @@ NSMutableDictionary *classColorMap;
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
BOOL success = NO;
- int retryCount = 3; //TODO, will this stop the crash?
+ int retryCount = 3;
+
while (!success && retryCount > 0) {
success = [self.adaptor appendPixelBuffer:pixelBuffer withPresentationTime:self.currentTime];
if (!success) {
- NSLog(@"Error appending pixel buffer (remaining retries: %d): %@", retryCount - 1, self.assetWriter.error.localizedDescription);
retryCount--;
[NSThread sleepForTimeInterval:0.01];
}
}
+ if (!success) [self finishRecording];
}
NSTimeInterval elapsedTime = CMTimeGetSeconds(self.currentTime);
if (elapsedTime >= 60.0) {
@@ -317,8 +318,7 @@ NSMutableDictionary *classColorMap;
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGFloat aspect_ratio = (CGFloat)width / (CGFloat)height;
CGFloat targetWidth = self.yolo.yolo_res;
- CGFloat aspectRatio = (CGFloat)width / (CGFloat)height;
- CGSize targetSize = CGSizeMake(targetWidth, targetWidth / aspectRatio);
+ CGSize targetSize = CGSizeMake(targetWidth, targetWidth / aspect_ratio);
CGFloat scaleX = targetSize.width / width;
CGFloat scaleY = targetSize.height / height;
diff --git a/clearcam/Yolo.h b/clearcam/Yolo.h
index 36afde9..621ac37 100644
--- a/clearcam/Yolo.h
+++ b/clearcam/Yolo.h
@@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) NSMutableArray *_q;
@property (nonatomic, strong) NSString *input_buffer;
@property (nonatomic, strong) NSString *output_buffer;
+@property (nonatomic, assign) UInt8 *rgbData;
- (NSArray *)yolo_infer:(CGImageRef)cgImage withOrientation:(AVCaptureVideoOrientation)orientation;
// Initializer
diff --git a/clearcam/Yolo.m b/clearcam/Yolo.m
index aaffdfd..ad5f8eb 100644
--- a/clearcam/Yolo.m
+++ b/clearcam/Yolo.m
@@ -17,6 +17,7 @@ NSMutableDictionary *_h;
NSMutableArray *_q;
NSString *input_buffer;
NSString *output_buffer;
+UInt8 *rgbData;
- (instancetype)init {
self = [super init];
@@ -26,6 +27,7 @@ NSString *output_buffer;
self.mtl_queue = [self.device newCommandQueueWithMaxCommandBufferCount:1024];
self.mtl_buffers_in_flight = [[NSMutableArray alloc] init];
self.yolo_res = 640;
+ self.rgbData = (UInt8 *)malloc(self.yolo_res * self.yolo_res * 3);
self.yolo_classes = @[
@[@"person", [UIColor redColor]],@[@"bicycle", [UIColor greenColor]],@[@"car", [UIColor blueColor]],
@[@"motorcycle", [UIColor cyanColor]],@[@"airplane", [UIColor magentaColor]],@[@"bus", [UIColor yellowColor]],
@@ -76,7 +78,7 @@ NSString *output_buffer;
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSString *urlPath = @"https://raw.githubusercontent.com/roryclear/yolov8-tinygrad-ios/main/batch_req_%dx%d";
- if ([deviceModel isEqualToString:@"iPhone8,4"]) { //IPHONE SE1
+ if ([deviceModel isEqualToString:@"iPhone8,4"] || [deviceModel isEqualToString:@"iPhone12,8"]) { //IPHONE SE1 and SE2, TODO finish
path = @"batch_req_se1_%dx%d";
urlPath = @"https://raw.githubusercontent.com/roryclear/yolov8-tinygrad-ios/main/batch_req_se1_%dx%d";
}
@@ -271,33 +273,26 @@ NSString *output_buffer;
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
size_t rgbLength = (length / 4) * 3;
-
- UInt8 *rgbData = (UInt8 *)malloc(width * width * 3);
- if (!rgbData) {
- CFRelease(rawData);
- NSLog(@"Memory allocation failed for rgbData");
- return nil;
- }
-
+
if (orientation == AVCaptureVideoOrientationLandscapeRight) {
for (size_t i = 0, j = 0; i < length; i += 4, j += 3) {
- rgbData[j] = rawBytes[i];
- rgbData[j + 1] = rawBytes[i + 1];
- rgbData[j + 2] = rawBytes[i + 2];
+ self.rgbData[j] = rawBytes[i];
+ self.rgbData[j + 1] = rawBytes[i + 1];
+ self.rgbData[j + 2] = rawBytes[i + 2];
}
} else if (orientation == AVCaptureVideoOrientationLandscapeLeft) {
for (size_t i = 0, j = 0; i < length; i += 4, j += 3) {
- rgbData[rgbLength - 1 - j - 2] = rawBytes[i];
- rgbData[rgbLength - 1 - j - 1] = rawBytes[i + 1];
- rgbData[rgbLength - 1 - j] = rawBytes[i + 2];
+ self.rgbData[rgbLength - 1 - j - 2] = rawBytes[i];
+ self.rgbData[rgbLength - 1 - j - 1] = rawBytes[i + 1];
+ self.rgbData[rgbLength - 1 - j] = rawBytes[i + 2];
}
} else if (orientation == AVCaptureVideoOrientationPortrait) {
for (size_t i = 0; i < length; i += 4) {
int row = i / (width * 4);
int col = (i % (width * 4)) / 4;
- rgbData[col * (width * 3) + ((height - 1 - row) * 3)] = rawBytes[i];
- rgbData[col * (width * 3) + ((height - 1 - row) * 3) + 1] = rawBytes[i + 1];
- rgbData[col * (width * 3) + ((height - 1 - row) * 3) + 2] = rawBytes[i + 2];
+ self.rgbData[col * (width * 3) + ((height - 1 - row) * 3)] = rawBytes[i];
+ self.rgbData[col * (width * 3) + ((height - 1 - row) * 3) + 1] = rawBytes[i + 1];
+ self.rgbData[col * (width * 3) + ((height - 1 - row) * 3) + 2] = rawBytes[i + 2];
}
}
@@ -305,14 +300,12 @@ NSString *output_buffer;
id<MTLBuffer> buffer = self.buffers[self.input_buffer];
if (!buffer || !buffer.contents) {
- free(rgbData);
NSLog(@"Metal buffer is not initialized or invalid");
return nil;
}
memset(buffer.contents, 0, buffer.length);
- memcpy(buffer.contents, rgbData, MIN(buffer.length, width * width * 3));
- free(rgbData);
+ memcpy(buffer.contents, self.rgbData, MIN(buffer.length, width * width * 3));
for (NSMutableDictionary *values in self._q) {
id<MTLCommandBuffer> commandBuffer = [self.mtl_queue commandBuffer];
@@ -391,4 +384,3 @@ NSString *output_buffer;
}
@end
-